diff options
| author | kiyan42 <yazdani.kiyan@protonmail.com> | 2020-05-08 11:22:59 +0200 |
|---|---|---|
| committer | kiyan42 <yazdani.kiyan@protonmail.com> | 2020-05-12 16:16:48 +0200 |
| commit | 45dcebb15f1a954eba2bcb3ae4c1a03f710a1f2a (patch) | |
| tree | 50cbca8e9615d69a8ff9aca6b33a35abb32f9363 /lua/nvim-treesitter/node_movement.lua | |
| parent | 307c78aa1e2cc5e499469fe892108b7fcf6cdb5e (diff) | |
refacto/feat: better handling of parser updates
features:
- node_movement is moving between scopes.
- add selection initialization from normal mode
- add a decremental selection
improvements:
- attach to buffer to run tree parsing on change
- run state update on CursorMoved
- the buffer state is:
```
{
cursor_pos = { row=row, col=col },
current_node = node_under_cursor,
selection = {
range = nil, -- activates when starting a selection
nodes = {} -- filling up when starting an incremental selection
},
parser = parser, -- parser for current buffer
}
```
- refacto all the modules reliant on parsing the tree, update the current nodes, get the current nodes...
fixes:
- fix has_parser to look for .so libraries
- fix should select the whole file when selection root in selection
Diffstat (limited to 'lua/nvim-treesitter/node_movement.lua')
| -rw-r--r-- | lua/nvim-treesitter/node_movement.lua | 101 |
1 files changed, 41 insertions, 60 deletions
diff --git a/lua/nvim-treesitter/node_movement.lua b/lua/nvim-treesitter/node_movement.lua index 5d4813bc..f6f15fce 100644 --- a/lua/nvim-treesitter/node_movement.lua +++ b/lua/nvim-treesitter/node_movement.lua @@ -1,88 +1,69 @@ local api = vim.api -local parsers = require'nvim-treesitter.parsers' -local utils = require'nvim-treesitter.utils' -local M = {} - -M.NodeMovementKind = { - up = 'up', - down = 'down', - left = 'left', - right = 'right', -} +local configs = require'nvim-treesitter.configs' +local state = require'nvim-treesitter.state' +local ts_utils = require'nvim-treesitter.ts_utils' -M.current_node = {} +local M = {} -local function node_start_to_vim(node) - if not node then return end +local NodeMovementKind = { + parent_scope = 'parent', + child_scope = 'child', + next_scope = 'next', + previous_scope = 'previous', +} - local row, col = node:start() - local exec_command = string.format('call cursor(%d, %d)', row+1, col+1) - api.nvim_exec(exec_command, false) -end +local get_node_fn = { + [NodeMovementKind.parent_scope] = function(node, curpos) + return ts_utils.parent_scope(node, curpos) + end, + [NodeMovementKind.child_scope] = function(node, curpos) + return ts_utils.nested_scope(node, curpos) + end, + [NodeMovementKind.next_scope] = function(node) + return ts_utils.next_scope(node) + end, + [NodeMovementKind.previous_scope] = function(node) + return ts_utils.previous_scope(node) + end, +} M.do_node_movement = function(kind) - local buf, line, col = unpack(vim.fn.getpos(".")) + local buf = api.nvim_get_current_buf() - local current_node = M.current_node[buf] + local buf_state = state.get_buf_state(buf) + if not buf_state then return end - if current_node then - local node_line, node_col = current_node:start() - if line-1 ~= node_line or col-1 ~= node_col then - current_node = nil - end - end - local destination_node + local current_node = buf_state.current_node + if not current_node then return end - if parsers.has_parser() then - local root = parsers.get_parser():parse():root() - if not current_node then - current_node = root:named_descendant_for_range(line-1, col-1, line-1, col) - end - - if kind == M.NodeMovementKind.up then - destination_node = current_node:parent() - elseif kind == M.NodeMovementKind.down then - if current_node:named_child_count() > 0 then - destination_node = current_node:named_child(0) - else - local next_node = utils.get_next_node(current_node) - if next_node and next_node:named_child_count() > 0 then - destination_node = next_node:named_child(0) - end - end - elseif kind == M.NodeMovementKind.left then - destination_node = utils.get_previous_node(current_node, true, true) - elseif kind == M.NodeMovementKind.right then - destination_node = utils.get_next_node(current_node, true, true) - end - M.current_node[buf] = destination_node or current_node - end + local destination_node = get_node_fn[kind](current_node, buf_state.cursor_pos) if destination_node then - node_start_to_vim(destination_node) + local row, col = destination_node:start() + vim.fn.setpos(".", { buf, row+1, col+1, 0 }) end end -M.move_up = function() M.do_node_movement(M.NodeMovementKind.up) end -M.move_down = function() M.do_node_movement(M.NodeMovementKind.down) end -M.move_left = function() M.do_node_movement(M.NodeMovementKind.left) end -M.move_right = function() M.do_node_movement(M.NodeMovementKind.right) end +function M.parent_scope() M.do_node_movement(NodeMovementKind.parent_scope) end +function M.child_scope() M.do_node_movement(NodeMovementKind.child_scope) end +function M.next_scope() M.do_node_movement(NodeMovementKind.next_scope) end +function M.previous_scope() M.do_node_movement(NodeMovementKind.previous_scope) end function M.attach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() + local bufnr = bufnr or api.nvim_get_current_buf() - local config = require'nvim-treesitter.configs'.get_module('node_movement') + local config = configs.get_module('node_movement') for funcname, mapping in pairs(config.keymaps) do - api.nvim_buf_set_keymap(buf, 'n', mapping, - string.format(":lua require'nvim-treesitter.node_movement'.%s()<CR>", funcname), { silent = true }) + local cmd = string.format(":lua require'nvim-treesitter.node_movement'.%s()<CR>", funcname) + api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { 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('node_movement') + local config = configs.get_module('node_movement') for _, mapping in pairs(config.keymaps) do api.nvim_buf_del_keymap(buf, 'n', mapping) end |
