aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorKiyan Yazdani <yazdani.kiyan@protonmail.com>2020-04-25 23:33:13 +0200
committerGitHub <noreply@github.com>2020-04-25 23:33:13 +0200
commitf8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0 (patch)
tree6ff340a7c1e243e111bd0e593444eaeca7236741 /lua
parentMerge pull request #26 from kyazdani42/fix/config-isenabled (diff)
parentrefactor(textobj): remove unnecessary functions (diff)
downloadnvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar.gz
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar.bz2
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar.lz
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar.xz
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.tar.zst
nvim-treesitter-f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0.zip
Merge pull request #23 from vigoux/feature/textobjects
Node and scope text objects
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/configs.lua15
-rw-r--r--lua/nvim-treesitter/locals.lua12
-rw-r--r--lua/nvim-treesitter/textobj.lua78
-rw-r--r--lua/nvim-treesitter/utils.lua17
4 files changed, 110 insertions, 12 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/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..c2bcc30f0
--- /dev/null
+++ b/lua/nvim-treesitter/textobj.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_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()<CR>", funcname), { silent = true })
+ api.nvim_buf_set_keymap(buf, 'o', mapping,
+ string.format(":normal v%s<CR>", 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
diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua
index 59de5dd57..391863f0d 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 = {}
@@ -76,4 +77,20 @@ function M.setup_commands(mod, commands)
end
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