Add TS lang-based skips, fixes #198

This commit is contained in:
Andy K. Massimino
2021-11-24 09:41:24 -05:00
parent 4e684c1fdf
commit 66d8936fd0
3 changed files with 46 additions and 0 deletions

View File

@@ -63,3 +63,5 @@
(do_block
"do" @open.do
"end" @close.do) @scope.do
(if_modifier) @skip

View File

@@ -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\)'

View File

@@ -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 = {}
@@ -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)