Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tree-sitter related functionality #330

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Neovim
name: Neovim Legacy

on:
push:
Expand All @@ -13,7 +13,6 @@ jobs:
strategy:
matrix:
neovim_version:
- 'head'
- 'v0.8.3'
- 'v0.5.0'
runs-on: ubuntu-latest
Expand All @@ -32,10 +31,6 @@ jobs:
- name: 'Show version'
run: nvim --version

- name: 'Run test'
run: |
bash -c 'VIMCMD=nvim test/vader/run'

- name: 'Run new tests'
run: |
cd ./test/new && make -j1 && make -j1 coverage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Neovim with Tree-sitter
name: Neovim Stable and Head

on:
push:
Expand All @@ -14,10 +14,8 @@ jobs:
matrix:
neovim_version:
- 'head'
- 'v0.8.3'
- 'stable'
runs-on: ubuntu-latest
env:
TESTS_ENABLE_TREESITTER: 1
steps:
- uses: 'actions/checkout@v2'

Expand All @@ -33,15 +31,11 @@ jobs:
- name: Install nvim-treesitter
run: git clone --depth=1 https://github.com/nvim-treesitter/nvim-treesitter.git test/vader/plugged/nvim-treesitter

- name: Install python treesitter module
run: nvim --headless -Nu test/vader/minvimrc -c 'TSInstallSync python' -c 'q'

- name: 'Show version'
run: nvim --version

- name: 'Run test'
run: |
bash -c 'VIMCMD=nvim test/vader/run'
- name: Install python treesitter module
run: nvim --headless -Nu test/vader/minvimrc -c 'TSInstallSync python' -c 'q'

- name: Install ruby treesitter module
run: nvim --headless -Nu test/vader/minvimrc -c 'TSInstallSync ruby' -c 'q'
Expand Down
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,9 @@ This feature requires manual opt-in in your init.vim and requires
[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) to
be installed.

```vim
Plug 'nvim-treesitter/nvim-treesitter'
lua <<EOF
require'nvim-treesitter.configs'.setup {
matchup = {
enable = true, -- mandatory, false will disable the whole extension
disable = { "c", "ruby" }, -- optional, list of language that will be disabled
-- [options]
},
}
EOF
```

Beside `enable` and `disable`, the following options are available, all
defaulting to disabled:
Configure tree-sitter module by defining `g:matchup_treesitter_config` table,
with option names as keys. The following options are available, all defaulting
to disabled:

- `disable_virtual_text`: do not use virtual text to highlight the
virtual end of a block, for languages without explicit end markers
Expand Down
14 changes: 5 additions & 9 deletions autoload/matchup/loader.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ endfunction
function! matchup#loader#init_buffer() abort " {{{1
call matchup#perf#tic('loader_init_buffer')

let l:has_ts = 0
let l:has_ts = matchup#loader#_treesitter_may_be_supported() && matchup#ts_engine#register_callbacks(bufnr('%'))
let l:has_ts_hl = 0
let [l:no_words, l:filt_words] = [0, 0]
if s:ts_may_be_supported && matchup#ts_engine#is_enabled(bufnr('%'))
let l:has_ts = 1

if 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
if s:ts_may_be_supported && matchup#ts_engine#is_hl_enabled(bufnr('%'))
let l:has_ts_hl = 1

if matchup#ts_engine#get_option(
\ bufnr('%'), 'additional_vim_regex_highlighting')
if empty(&syntax)
Expand Down Expand Up @@ -92,8 +89,7 @@ function! matchup#loader#_treesitter_may_be_supported() abort
return s:ts_may_be_supported
endfunction

let s:ts_may_be_supported = has('nvim-0.5.0') && exists('*luaeval')
\ && luaeval('pcall(require, "treesitter-matchup")')
let s:ts_may_be_supported = has('nvim-0.9.0')

" }}}1
function! matchup#loader#bufwinenter() abort " {{{1
Expand Down
2 changes: 1 addition & 1 deletion autoload/matchup/matchparen.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ if has('nvim-0.5.0')
let s:ns_id = nvim_create_namespace('vim-matchup')
endif

if has('nvim-0.5.0') && matchup#loader#_treesitter_may_be_supported()
if matchup#loader#_treesitter_may_be_supported()
function s:synID(lnum, col, trans)
return matchup#ts_syntax#synID(a:lnum, a:col, a:trans)
endfunction
Expand Down
26 changes: 5 additions & 21 deletions autoload/matchup/ts_engine.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,16 @@ function! s:forward(fn, ...)
return l:ret
endfunction

function! matchup#ts_engine#is_enabled(bufnr) abort
if !has('nvim-0.5.0')
return 0
endif
return +s:forward('is_enabled', a:bufnr)
endfunction

function! matchup#ts_engine#is_hl_enabled(bufnr) abort
if !has('nvim-0.5.0')
return 0
endif
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
let s:attached[a:bufnr] = a:lang
endfunction
function! matchup#ts_engine#register_callbacks(bufnr) abort
call matchup#perf#tic('ts.register_buffer')
let l:ret = s:forward('register_callbacks', a:bufnr)
call matchup#perf#toc('ts.register_buffer', l:ret ? 'done' : 'fail')

function! matchup#ts_engine#detach(bufnr) abort
unlet s:attached[a:bufnr]
return l:ret
endfunction

function! matchup#ts_engine#get_delim(opts) abort
Expand Down
20 changes: 5 additions & 15 deletions doc/matchup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,11 @@ Note: Currently this feature is possible in neovim only. Only the latest
version of neovim and nvim-treesitter is supported.

match-up has support for language syntax provided by tree-sitter. The list
of supported languages is available here. This feature requires manual
opt-in in your init.vim and requires nvim-treesitter to be installed. >

Plug 'nvim-treesitter/nvim-treesitter'
lua <<EOF
require'nvim-treesitter.configs'.setup {
matchup = {
enable = true, -- mandatory, false will disable the whole extension
disable = { "c", "ruby" }, -- optional, list of language that will be disabled
-- [options]
},
}
<
Beside enable and disable, the following options are available, all
defaulting to false:
of supported languages is available here.

Configure tree-sitter module by defining `g:matchup_treesitter_config` table,
with option names as keys. The following options are available, all defaulting
to disabled:

*disable_virtual_text*

Expand Down
31 changes: 17 additions & 14 deletions lua/treesitter-matchup.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
if not pcall(require, 'nvim-treesitter') then
return {init = function() end}
end

local treesitter = require 'nvim-treesitter'
local queries = require 'nvim-treesitter.query'

local M = {}

-- Follow nvim-treesitter lead and just avoid some annoying warnings for this directive
vim.treesitter.query.add_directive("make-range!", function() end, true)

function M.init()
treesitter.define_modules {
matchup = {
module_path = 'treesitter-matchup.internal',
is_supported = function(lang)
return queries.has_query_files(lang, 'matchup')
end
if vim.g.vim_matchup_skip_deprecated then
return
end

local nvim_ts_available, treesitter = pcall(require, 'nvim-treesitter')
if nvim_ts_available and treesitter and treesitter.define_modules then
treesitter.define_modules {
matchup = {
module_path = 'treesitter-matchup.internal',
is_supported = function()
return true
end
}
}
}
end
end

return M
Loading