From 62786ec7c60ea29cbbd48ae658cde7042dba4bb3 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Wed, 22 Apr 2020 11:13:05 +0200 Subject: feat/refacto: improve configurations - You should now get the configs through functions - Configs for languages are now inside a local object called parsers - You can get the parser installation configurations with `get_parser_configs` - A new object has been initialized inside configs to specify module config (called config). - Provide functions to enable/disable a module on one buffer - Provide functions to enable/disable a module on all buffers, and if filetype is specified, for specific filetype - Provide function to determine if module is activated for a specified filetype --- plugin/nvim-treesitter.vim | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'plugin/nvim-treesitter.vim') diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 06c760a18..a94f56a96 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -4,14 +4,17 @@ if exists('g:loaded_nvim_treesitter') finish endif +augroup NvimTreesitter +augroup END + +let g:loaded_nvim_treesitter = 1 + lua << EOF ts_installable_parsers = function() - return table.concat(require'nvim-treesitter'.available_parsers(), '\n') + return table.concat(require'nvim-treesitter.configs'.available_parsers(), '\n') +end +ts_available_modules = function() + return table.concat(require'nvim-treesitter.configs'.available_modules(), '\n') end -require'nvim-treesitter'._root.setup() +require'nvim-treesitter'.setup() EOF - -let g:loaded_nvim_treesitter = 1 - -augroup NvimTreesitter -augroup END -- cgit v1.2.3-70-g09d2 From c62685841e52dacb4659f49d16336765e7e10f10 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Thu, 23 Apr 2020 08:43:35 +0200 Subject: textobj: add incremental scope selection --- autoload/nvim_treesitter.vim | 4 ++-- lua/nvim-treesitter/textobj.lua | 22 ++++++++++++++++++++++ lua/nvim-treesitter/utils.lua | 15 +++++++++++++++ plugin/nvim-treesitter.vim | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) (limited to 'plugin/nvim-treesitter.vim') diff --git a/autoload/nvim_treesitter.vim b/autoload/nvim_treesitter.vim index 3e5ce40e6..e050ae5b2 100644 --- a/autoload/nvim_treesitter.vim +++ b/autoload/nvim_treesitter.vim @@ -11,6 +11,6 @@ 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()')) +function! nvim_treesitter#select_scope_incr() + call s:visual_node(luaeval('require"nvim-treesitter.textobj".scope_incremental()')) endfunction diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index 9460e193a..a28440e32 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -33,4 +33,26 @@ function M.node_incremental() end end +function M.scope_incremental() + local _, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) + local _, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + local node = utils.smallest_containing_scope( + 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(utils.smallest_containing_scope(node:parent() or node)) + else + return node_range_to_vim(node) + end + else + return node_range_to_vim() + end +end + return M diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 59de5dd57..2ef2746aa 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -1,6 +1,7 @@ -- Utils collection for nvim-treesitter local api = vim.api local parsers = require'nvim-treesitter.parsers' +local locals = require'nvim-treesitter.locals' local M = {} @@ -74,6 +75,20 @@ function M.setup_commands(mod, commands) }) api.nvim_command(table.concat(parts, " ")) end +--- Gets the smallest scope which contains @param node +function M.smallest_containing_scope(node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local root = parsers.get_parser(bufnr):parse():root() + if not node then return root end + + local scopes = locals.get_scopes(bufnr) + local current = node + while current ~= nil and not vim.tbl_contains(scopes, current) do + current = current:parent() + end + + return current or root end return M diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index a94f56a96..31329c61e 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -1,4 +1,4 @@ -" Last Change: 2020 avril 19 +" Last Change: 2020 avril 23 if exists('g:loaded_nvim_treesitter') finish -- cgit v1.2.3-70-g09d2 From cadb3ee1ff0f20a8d26b91502c410e56be90fd46 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sat, 25 Apr 2020 16:35:10 +0200 Subject: refactor(textobj): use configs and don't use VimL --- autoload/nvim_treesitter.vim | 16 ------- lua/nvim-treesitter/configs.lua | 15 +++--- lua/nvim-treesitter/textobj.lua | 103 +++++++++++++++++++++++++--------------- plugin/nvim-treesitter.vim | 2 +- 4 files changed, 76 insertions(+), 60 deletions(-) delete mode 100644 autoload/nvim_treesitter.vim (limited to 'plugin/nvim-treesitter.vim') diff --git a/autoload/nvim_treesitter.vim b/autoload/nvim_treesitter.vim deleted file mode 100644 index e050ae5b2..000000000 --- a/autoload/nvim_treesitter.vim +++ /dev/null @@ -1,16 +0,0 @@ -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_scope_incr() - call s:visual_node(luaeval('require"nvim-treesitter.textobj".scope_incremental()')) -endfunction diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 64d4170e2..2761f0be8 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -157,12 +157,15 @@ local config = { return queries.get_query(ft, 'highlights') ~= nil end }, - -- selection = { - -- enable = false, - -- disable = {}, - -- keymaps = {}, - -- is_supported = function() return false end - -- }, + textobj = { + enable = false, + disable = {}, + keymaps = { + node_incremental="grn", + scope_incremental="grc" + }, + is_supported = function() return true end + }, -- folding = { -- enable = false, -- disable = {}, diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index 8ba20d793..a373e4146 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -4,54 +4,83 @@ 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() + if not node then return end - return {{start_row, start_col}, {end_row, end_col}} - else - return {{}, {}} - end + local start_row, start_col, end_row, end_col = node:range() + + local select_range = [[ + call cursor(%d, %d) + normal v + call cursor(%d, %d) + ]] + local exec_command = string.format(select_range, + start_row+1, start_col+1, + end_row+1, end_col+1) + + api.nvim_exec(exec_command, false) 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("'>")) - - local node = nil - if parsers.has_parser() then - local root = parsers.get_parser():parse():root() - 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 - -- TODO(vigoux): actually it is not really the parent here - -- it might be needed to climb more than only one parent, but a strictly growing node - node = node:parent() or node +local function select_incremental(increment_func) + return function() + local buf, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) + local buf, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + + local node = nil + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + 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 + node = increment_func(node) + end end - end - return node_range_to_vim(node) + return node_range_to_vim(node) + end end -function M.scope_incremental() - local _, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) - local _, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) +M.node_incremental = select_incremental(function(node) + if node then + return node:parent() or node + end +end) - local node = nil - if parsers.has_parser() then - local root = parsers.get_parser():parse():root() - node = utils.smallest_containing_scope( - root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col)) +M.scope_incremental = select_incremental(function(node) + if node then + return utils.smallest_containing_scope(node:parent() or node) + end +end) - local node_start_row, node_start_col, node_end_row, node_end_col = node:range() +function M.attach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() - 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 - node = utils.smallest_containing_scope(node:parent() or node) - end + local function textobj_create(mapping, funcname) + api.nvim_buf_set_keymap(buf, 'v', mapping, + string.format(":lua require'nvim-treesitter.textobj'.%s()", funcname), { silent = true }) + api.nvim_buf_set_keymap(buf, 'o', mapping, + string.format(":normal v%s", mapping), { silent = true }) + end + + local config = require'nvim-treesitter.configs'.get_config().textobj + for funcname, mapping in pairs(config.keymaps) do + textobj_create(mapping, funcname) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local function textobj_delete(mapping) + api.nvim_buf_del_keymap(buf, 'v', default_mapping) + api.nvim_buf_del_keymap(buf, 'o', default_mapping) + end + + local config = require'nvim-treesitter.configs'.get_config().textobj + for _, mapping in pairs(config.keymaps) do + textobj_delete(mapping) end - return node_range_to_vim(node) end return M diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim index 31329c61e..30ee3241d 100644 --- a/plugin/nvim-treesitter.vim +++ b/plugin/nvim-treesitter.vim @@ -1,4 +1,4 @@ -" Last Change: 2020 avril 23 +" Last Change: 2020 avril 25 if exists('g:loaded_nvim_treesitter') finish -- cgit v1.2.3-70-g09d2