From 78b40f895cb2bf789944f94abd47dd7f7ab3f8dc Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Thu, 23 Apr 2020 07:55:00 +0200 Subject: textobj: add incremental node selection --- lua/nvim-treesitter/textobj.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lua/nvim-treesitter/textobj.lua (limited to 'lua/nvim-treesitter/textobj.lua') diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua new file mode 100644 index 00000000..9460e193 --- /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 -- cgit v1.2.3 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 --- lua/nvim-treesitter/textobj.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lua/nvim-treesitter/textobj.lua') diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index 9460e193..a28440e3 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 -- cgit v1.2.3 From 7c7d4e52703ba76a2aa96ddcc30c4e210addc8af Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Thu, 23 Apr 2020 12:15:03 +0200 Subject: textobj: little refactor --- lua/nvim-treesitter/textobj.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'lua/nvim-treesitter/textobj.lua') diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index a28440e3..8ba20d79 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -17,42 +17,41 @@ 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() - local node = root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col) + 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) + -- 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 end - else - return node_range_to_vim() end + + return node_range_to_vim(node) 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("'>")) + local node = nil 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)) + 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) + node = utils.smallest_containing_scope(node:parent() or node) end - else - return node_range_to_vim() end + return node_range_to_vim(node) end return M -- cgit v1.2.3 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 --- lua/nvim-treesitter/textobj.lua | 103 +++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 37 deletions(-) (limited to 'lua/nvim-treesitter/textobj.lua') diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index 8ba20d79..a373e414 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 -- cgit v1.2.3 From bb709aa8d0e241232b31ca1ae9fc042b16413ffd Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sat, 25 Apr 2020 21:49:19 +0200 Subject: refactor(textobj): remove unnecessary functions --- lua/nvim-treesitter/textobj.lua | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'lua/nvim-treesitter/textobj.lua') diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index a373e414..c2bcc30f 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -56,30 +56,22 @@ end) function M.attach(bufnr) local buf = bufnr or api.nvim_get_current_buf() - local function textobj_create(mapping, funcname) + local config = require'nvim-treesitter.configs'.get_config().textobj + for funcname, mapping in pairs(config.keymaps) do 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) + api.nvim_buf_del_keymap(buf, 'v', mapping) + api.nvim_buf_del_keymap(buf, 'o', mapping) end end -- cgit v1.2.3