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 --- lua/nvim-treesitter/utils.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lua/nvim-treesitter/utils.lua') diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 8313a46a..59de5dd5 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -63,4 +63,17 @@ function M.is_parent(dest, source) return false end +function M.setup_commands(mod, commands) + for command_name, def in pairs(commands) do + local call_fn = string.format("lua require'nvim-treesitter.%s'.commands.%s.run()", mod, command_name) + local parts = vim.tbl_flatten({ + "command!", + def.args, + command_name, + call_fn, + }) + api.nvim_command(table.concat(parts, " ")) + 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/utils.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lua/nvim-treesitter/utils.lua') diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 59de5dd5..2ef2746a 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 -- 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/utils.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lua/nvim-treesitter/utils.lua') diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 2ef2746a..391863f0 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -75,6 +75,8 @@ function M.setup_commands(mod, commands) }) api.nvim_command(table.concat(parts, " ")) 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() -- cgit v1.2.3 From d0b84dd89fb1cb4fc637cd3deec427745ba3145b Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Fri, 1 May 2020 16:25:06 +0200 Subject: Add 'nvim-treesitter/node-movement' --- lua/nvim-treesitter/utils.lua | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'lua/nvim-treesitter/utils.lua') diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 391863f0..9d591eab 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -93,4 +93,63 @@ function M.smallest_containing_scope(node, bufnr) return current or root end +--- Get next node with same parent +-- @param node node +-- @param allow_switch_parents allow switching parents if last node +-- @param allow_next_parent allow next parent if last node and next parent without children +function M.get_next_node(node, allow_switch_parents, allow_next_parent) + local destination_node + local parent = node:parent() + + if parent then + local found_pos = 0 + for i = 0,parent:named_child_count()-1,1 do + if parent:named_child(i) == node then + found_pos = i + break + end + end + if parent:named_child_count() > found_pos + 1 then + destination_node = parent:named_child(found_pos + 1) + elseif allow_switch_parents then + local next_node = M.get_next_node(node:parent()) + if next_node and next_node:named_child_count() > 0 then + destination_node = next_node:named_child(0) + elseif next_node and allow_next_parent then + destination_node = next_node + end + end + end + return destination_node +end + +--- Get previous node with same parent +-- @param node node +-- @param allow_switch_parents allow switching parents if first node +-- @param allow_previous_parent allow previous parent if first node and previous parent without children +function M.get_previous_node(node, allow_switch_parents, allow_previous_parent) + local destination_node + local parent = node:parent() + if parent then + local found_pos = 0 + for i = 0,parent:named_child_count()-1,1 do + if parent:named_child(i) == node then + found_pos = i + break + end + end + if 0 < found_pos then + destination_node = parent:named_child(found_pos - 1) + elseif allow_switch_parents then + local previous_node = M.get_previous_node(node:parent()) + if previous_node and previous_node:named_child_count() > 0 then + destination_node = previous_node:named_child(previous_node:named_child_count() - 1) + elseif previous_node and allow_previous_parent then + destination_node = previous_node + end + end + end + return destination_node +end + return M -- cgit v1.2.3