Skip to content

Commit

Permalink
Add TS lang-based skips, fixes #198
Browse files Browse the repository at this point in the history
  • Loading branch information
andymass committed Nov 24, 2021
1 parent 4e684c1 commit 66d8936
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions after/queries/ruby/matchup.scm
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@
(do_block
"do" @open.do
"end" @close.do) @scope.do

(if_modifier) @skip
8 changes: 8 additions & 0 deletions autoload/matchup/ts_syntax.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ function! matchup#ts_syntax#synID(lnum, col, trans) abort
return s:forward('synID', a:lnum, a:col, a:trans)
endfunction

function! matchup#ts_syntax#lang_skip(lnum, col) abort
return s:forward('lang_skip', a:lnum, a:col)
endfunction

function! matchup#ts_syntax#skip_expr(lnum, col) abort
if matchup#ts_syntax#lang_skip(a:lnum, a:col)
return 1
endif

let l:syn = synIDattr(matchup#ts_syntax#synID(
\ a:lnum, a:col, 1), 'name')
return l:syn =~? '\%(String\|Comment\)'
Expand Down
36 changes: 36 additions & 0 deletions lua/treesitter-matchup/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ end

local api = vim.api
local hl_info = require'treesitter-matchup.third-party.hl-info'
local queries = require'treesitter-matchup.third-party.query'
local ts_utils = require'nvim-treesitter.ts_utils'

local M = {}

Expand All @@ -18,6 +20,40 @@ function M.is_active(bufnr)
and api.nvim_buf_get_option(bufnr, 'syntax') == '')
end

--- Get all nodes that are marked as skip
function M.get_skips(bufnr)
local matches = queries.get_matches(bufnr, 'matchup')

local skips = {}

for _, match in ipairs(matches) do
if match.skip then
skips[match.skip.node:id()] = 1
end
end

return skips
end

function M.lang_skip(lnum, col)
local bufnr = api.nvim_get_current_buf()
local skips = M.get_skips(bufnr)

if vim.tbl_isempty(skips) then
return false
end

local node = ts_utils.get_node_at_cursor()
if not node then
return false
end
if skips[node:id()] then
return true
end

return false
end

function M.synID(lnum, col, transparent)
if not M.is_active() then
return vim.fn.synID(lnum, col, transparent)
Expand Down

0 comments on commit 66d8936

Please sign in to comment.