diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-23 07:55:00 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-25 21:46:22 +0200 |
| commit | 78b40f895cb2bf789944f94abd47dd7f7ab3f8dc (patch) | |
| tree | e8af2320312a4a78ee151167eb4dcdaef8ac1a68 | |
| parent | Merge pull request #26 from kyazdani42/fix/config-isenabled (diff) | |
| download | nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar.gz nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar.bz2 nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar.lz nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar.xz nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.tar.zst nvim-treesitter-78b40f895cb2bf789944f94abd47dd7f7ab3f8dc.zip | |
textobj: add incremental node selection
| -rw-r--r-- | autoload/nvim_treesitter.vim | 16 | ||||
| -rw-r--r-- | lua/nvim-treesitter/locals.lua | 12 | ||||
| -rw-r--r-- | lua/nvim-treesitter/textobj.lua | 36 |
3 files changed, 58 insertions, 6 deletions
diff --git a/autoload/nvim_treesitter.vim b/autoload/nvim_treesitter.vim new file mode 100644 index 000000000..3e5ce40e6 --- /dev/null +++ b/autoload/nvim_treesitter.vim @@ -0,0 +1,16 @@ +function! s:visual_node(node_range) + let [l:cursor_start, l:cursor_end] = a:node_range + if !empty(l:cursor_start) && !empty(l:cursor_end) + call cursor(l:cursor_start[0]+1, l:cursor_start[1]+1) + normal v + call cursor(l:cursor_end[0]+1, l:cursor_end[1]) + endif +endfunction + +function! nvim_treesitter#select_node_incr() + call s:visual_node(luaeval('require"nvim-treesitter.textobj".node_incremental()')) +endfunction + +function! nvim_treesitter#select_context_incr() + call s:visual_node(luaeval('require"nvim-treesitter.textobj".context_incremental()')) +endfunction diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 388c7e489..1e19fcd43 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -68,8 +68,8 @@ function M.get_definitions(bufnr) local defs = {} for _, loc in ipairs(locals) do - if loc.definition then - table.insert(defs, {definition=loc.definition, kind=loc.kind}) + if loc.definition and loc.definition.node then + table.insert(defs, {node=loc.definition.node, kind=loc.kind}) end end @@ -82,8 +82,8 @@ function M.get_scopes(bufnr) local scopes = {} for _, loc in ipairs(locals) do - if loc.scope then - table.insert(scopes, loc.scope) + if loc.scope and loc.scope.node then + table.insert(scopes, loc.scope.node) end end @@ -96,8 +96,8 @@ function M.get_references(bufnr) local refs = {} for _, loc in ipairs(locals) do - if loc.reference then - table.insert(refs, loc.reference) + if loc.reference and loc.reference.node then + table.insert(refs, loc.reference.node) end end diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua new file mode 100644 index 000000000..9460e193a --- /dev/null +++ b/lua/nvim-treesitter/textobj.lua @@ -0,0 +1,36 @@ +local api = vim.api +local utils = require'nvim-treesitter.utils' +local parsers = require'nvim-treesitter.parsers' +local M = {} + +local function node_range_to_vim(node) + if node then + local start_row, start_col, end_row, end_col = node:range() + + return {{start_row, start_col}, {end_row, end_col}} + else + return {{}, {}} + end +end + +function M.node_incremental() + local buf, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) + local buf, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + local node = root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col) + local node_start_row, node_start_col, node_end_row, node_end_col = node:range() + + if (sel_start_line-1) == node_start_row and (sel_start_col-1) == node_start_col + and (sel_end_line-1) == node_end_row and sel_end_col == node_end_col then + return node_range_to_vim(node:parent() or node) + else + return node_range_to_vim(node) + end + else + return node_range_to_vim() + end +end + +return M |
