From cdf8ff7c1ec6e42d5c4bd8705eb15f85f0a5bb82 Mon Sep 17 00:00:00 2001 From: "Andy K. Massimino" Date: Tue, 28 Sep 2021 22:15:05 -0400 Subject: [PATCH] Add ts engine option include_match_words --- autoload/matchup/loader.vim | 21 +++++++++++++++++---- autoload/matchup/ts_engine.vim | 4 ++++ lua/treesitter-matchup/internal.lua | 24 +++++++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/autoload/matchup/loader.vim b/autoload/matchup/loader.vim index 7c1d5b5..8395d7c 100644 --- a/autoload/matchup/loader.vim +++ b/autoload/matchup/loader.vim @@ -22,8 +22,14 @@ function! matchup#loader#init_buffer() abort " {{{1 call matchup#perf#tic('loader_init_buffer') let l:has_ts = 0 + let [l:no_words, l:filt_words] = [0, 0] if has('nvim-0.5.0') && matchup#ts_engine#is_enabled(bufnr('%')) let l:has_ts = 1 + if matchup#ts_engine#get_option(bufnr('%'), 'include_match_words') + let l:filt_words = 1 + else + let l:no_words = 1 + endif endif let l:has_ts_hl = 0 @@ -34,7 +40,7 @@ function! matchup#loader#init_buffer() abort " {{{1 " initialize lists of delimiter pairs and regular expressions " this is the data obtained from parsing b:match_words - let b:matchup_delim_lists = s:init_delim_lists(!l:has_ts) + let b:matchup_delim_lists = s:init_delim_lists(l:no_words, l:filt_words) " this is the combined set of regular expressions used for matching " its structure is matchup_delim_re[type][open,close,both,mid,both_all] @@ -102,7 +108,7 @@ function! matchup#loader#refresh_match_words() abort " {{{1 call matchup#perf#toc('refresh', 'cache_hit') else " re-parse match words - let b:matchup_delim_lists = s:init_delim_lists(1) + let b:matchup_delim_lists = s:init_delim_lists(0, 0) let b:matchup_delim_re = s:init_delim_regexes() let s:match_word_cache[l:match_words] = { \ 'delim_lists' : b:matchup_delim_lists, @@ -117,7 +123,7 @@ let s:match_word_cache = {} " }}}1 -function! s:init_delim_lists(use_match_words) abort " {{{1 +function! s:init_delim_lists(no_words, filter_words) abort " {{{1 let l:lists = { \ 'delim_tex': { \ 'regex': [], @@ -145,7 +151,7 @@ function! s:init_delim_lists(use_match_words) abort " {{{1 endif " parse matchpairs and b:match_words - let l:match_words = a:use_match_words ? get(b:, 'match_words', '') : '' + let l:match_words = !a:no_words ? get(b:, 'match_words', '') : '' if !empty(l:match_words) && l:match_words !~# ':' execute 'let l:match_words =' b:match_words endif @@ -162,6 +168,13 @@ function! s:init_delim_lists(use_match_words) abort " {{{1 let l:sets = split(l:match_words, g:matchup#re#not_bslash.',') + if a:filter_words + call filter(l:sets, 'v:val =~ "^[^a-zA-Z]\\{3,18\\}$"') + if empty(l:sets) + return s:init_delim_lists_fast(l:match_words) + endif + endif + " do not duplicate whole groups of match words let l:seen = {} for l:s in l:sets diff --git a/autoload/matchup/ts_engine.vim b/autoload/matchup/ts_engine.vim index 5e1ad06..f68809b 100644 --- a/autoload/matchup/ts_engine.vim +++ b/autoload/matchup/ts_engine.vim @@ -28,6 +28,10 @@ function! matchup#ts_engine#is_hl_enabled(bufnr) abort return +s:forward('is_hl_enabled', a:bufnr) endfunction +function! matchup#ts_engine#get_option(bufnr, opt_name) abort + return s:forward('get_option', a:bufnr, a:opt_name) +endfunction + let s:attached = {} function! matchup#ts_engine#attach(bufnr, lang) abort diff --git a/lua/treesitter-matchup/internal.lua b/lua/treesitter-matchup/internal.lua index 3507593..ff67029 100644 --- a/lua/treesitter-matchup/internal.lua +++ b/lua/treesitter-matchup/internal.lua @@ -311,9 +311,31 @@ function M.get_matching(delim, down, bufnr) return matches end +local function opt_tbl_for_lang(opt, lang) + local is_table = type(opt) == "table" + if opt and (not is_table or vim.tbl_contains(opt, lang)) then + return true + end + return false +end + +function M.get_option(bufnr, opt_name) + local config = configs.get_module('matchup') + local lang = parsers.get_buf_lang(bufnr) + if opt_name == 'include_match_words' then + return opt_tbl_for_lang(config[opt_name], lang) + end + error('invalid option ' .. opt_name) +end + function M.attach(bufnr, lang) -- local parser = parsers.get_parser(bufnr, lang) - -- local config = configs.get_module('matchup') + local config = configs.get_module('matchup') + + if opt_tbl_for_lang(config.additional_vim_regex_highlighting, lang) + and api.nvim_buf_set_option(bufnr, 'syntax') == '' then + api.nvim_buf_set_option(bufnr, 'syntax', 'ON') + end api.nvim_call_function('matchup#ts_engine#attach', {bufnr, lang}) end