diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-25 16:35:10 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-25 21:46:22 +0200 |
| commit | cadb3ee1ff0f20a8d26b91502c410e56be90fd46 (patch) | |
| tree | 87f3d22d8590a0ce21db90fc11400db4a3394aa5 /lua | |
| parent | textobj: little refactor (diff) | |
| download | nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar.gz nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar.bz2 nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar.lz nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar.xz nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.tar.zst nvim-treesitter-cadb3ee1ff0f20a8d26b91502c410e56be90fd46.zip | |
refactor(textobj): use configs and don't use VimL
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 15 | ||||
| -rw-r--r-- | lua/nvim-treesitter/textobj.lua | 99 |
2 files changed, 73 insertions, 41 deletions
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 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() + 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 + 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()<CR>", funcname), { silent = true }) + api.nvim_buf_set_keymap(buf, 'o', mapping, + string.format(":normal v%s<CR>", 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 |
