From 39da51bbd12b8cda64226f905dba6556cc9d9b75 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sat, 2 May 2020 11:33:32 +0200 Subject: modules: move textobj to incremental_selection As suggested in #37, rename the textobj module to incremental_selection. Also adds a utility function to get the config of a module. --- lua/nvim-treesitter/configs.lua | 6 ++- lua/nvim-treesitter/incremental_selection.lua | 78 +++++++++++++++++++++++++++ lua/nvim-treesitter/textobj.lua | 78 --------------------------- 3 files changed, 83 insertions(+), 79 deletions(-) create mode 100644 lua/nvim-treesitter/incremental_selection.lua delete mode 100644 lua/nvim-treesitter/textobj.lua (limited to 'lua') diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 9363c9e68..10dcc9605 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -214,7 +214,7 @@ local config = { return queries.get_query(ft, 'highlights') ~= nil end }, - textobj = { + incremental_selection = { enable = false, disable = {}, keymaps = { @@ -403,4 +403,8 @@ function M.available_modules() return vim.tbl_keys(config.modules) end +function M.get_module(mod) + return config.modules[mod] +end + return M diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua new file mode 100644 index 000000000..70eefe37c --- /dev/null +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -0,0 +1,78 @@ +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 not node then 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 + +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 + + return node_range_to_vim(node) + end +end + +M.node_incremental = select_incremental(function(node) + if node then + return node:parent() or node + end +end) + +M.scope_incremental = select_incremental(function(node) + if node then + return utils.smallest_containing_scope(node:parent() or node) + end +end) + +function M.attach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('incremental_selection') + for funcname, mapping in pairs(config.keymaps) do + api.nvim_buf_set_keymap(buf, 'v', mapping, + string.format(":lua require'nvim-treesitter.incremental_selection'.%s()", funcname), { silent = true }) + api.nvim_buf_set_keymap(buf, 'o', mapping, + string.format(":normal v%s", mapping), { silent = true }) + end +end + +function M.detach(bufnr) + local buf = bufnr or api.nvim_get_current_buf() + + local config = require'nvim-treesitter.configs'.get_module('incremental_selection') + for _, mapping in pairs(config.keymaps) do + api.nvim_buf_del_keymap(buf, 'v', mapping) + api.nvim_buf_del_keymap(buf, 'o', mapping) + end +end + +return M diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua deleted file mode 100644 index c2bcc30f0..000000000 --- a/lua/nvim-treesitter/textobj.lua +++ /dev/null @@ -1,78 +0,0 @@ -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 not node then 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 - -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 - - return node_range_to_vim(node) - end -end - -M.node_incremental = select_incremental(function(node) - if node then - return node:parent() or node - end -end) - -M.scope_incremental = select_incremental(function(node) - if node then - return utils.smallest_containing_scope(node:parent() or node) - end -end) - -function M.attach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() - - 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 -end - -function M.detach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() - - local config = require'nvim-treesitter.configs'.get_config().textobj - for _, mapping in pairs(config.keymaps) do - api.nvim_buf_del_keymap(buf, 'v', mapping) - api.nvim_buf_del_keymap(buf, 'o', mapping) - end -end - -return M -- cgit v1.2.3-70-g09d2