diff options
| author | Jaehwang Jung <tomtomjhj@gmail.com> | 2025-01-19 17:58:09 +0900 |
|---|---|---|
| committer | Christian Clason <ch.clason+github@icloud.com> | 2025-01-20 08:49:31 +0100 |
| commit | e8c5242f5323ee8b11983f955cab21bbef7e2df0 (patch) | |
| tree | 6d97f4aaeb237c99ea208ad6b2b8fd4d83a5411f /lua | |
| parent | bot(lockfile): update hyprlang, query, sourcepawn, templ, vhdl, xresources (diff) | |
| download | nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar.gz nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar.bz2 nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar.lz nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar.xz nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.tar.zst nvim-treesitter-e8c5242f5323ee8b11983f955cab21bbef7e2df0.zip | |
fix(inc-selection): handle injections
* Parse injections before starting selection.
* Make node_incremental climb up the LanaguageTree step by step.
Previously it only considered the root parser and the bottommost
parser. Now it works well with document with deeper injections, e.g.,
```lua
vim.cmd[=[
echo 'hello' 'world!'
lua << EOF
vim.cmd[[echo 'hello' 'world!']]
EOF
]=]
```
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/incremental_selection.lua | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua index 78f0915c8..570f9eed9 100644 --- a/lua/nvim-treesitter/incremental_selection.lua +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -14,6 +14,7 @@ local selections = {} function M.init_selection() local buf = api.nvim_get_current_buf() + parsers.get_parser():parse { vim.fn.line "w0" - 1, vim.fn.line "w$" } local node = ts_utils.get_node_at_cursor() selections[buf] = { [1] = node } ts_utils.update_selection(buf, node) @@ -62,8 +63,12 @@ local function select_incremental(get_parent) local csrow, cscol, cerow, cecol = visual_selection_range() -- Initialize incremental selection with current selection if not nodes or #nodes == 0 or not range_matches(nodes[#nodes]) then - local root = parsers.get_parser():parse()[1]:root() - local node = root:named_descendant_for_range(csrow - 1, cscol - 1, cerow - 1, cecol) + local parser = parsers.get_parser() + parser:parse { vim.fn.line "w0" - 1, vim.fn.line "w$" } + local node = parser:named_node_for_range( + { csrow - 1, cscol - 1, cerow - 1, cecol }, + { ignore_injections = false } + ) ts_utils.update_selection(buf, node) if nodes and #nodes > 0 then table.insert(selections[buf], node) @@ -78,14 +83,18 @@ local function select_incremental(get_parent) while true do local parent = get_parent(node) if not parent or parent == node then - -- Keep searching in the main tree - -- TODO: we should search on the parent tree of the current node. - local root = parsers.get_parser():parse()[1]:root() - parent = root:named_descendant_for_range(csrow - 1, cscol - 1, cerow - 1, cecol) - if not parent or root == node or parent == node then + -- Keep searching in the parent tree + local root_parser = parsers.get_parser() + root_parser:parse { vim.fn.line "w0" - 1, vim.fn.line "w$" } + local current_parser = root_parser:language_for_range { csrow - 1, cscol - 1, cerow - 1, cecol } + if root_parser == current_parser then + node = root_parser:named_node_for_range { csrow - 1, cscol - 1, cerow - 1, cecol } ts_utils.update_selection(buf, node) return end + -- NOTE: parent() method is private + local parent_parser = current_parser:parent() + parent = parent_parser:named_node_for_range { csrow - 1, cscol - 1, cerow - 1, cecol } end node = parent local srow, scol, erow, ecol = ts_utils.get_vim_range { node:range() } |
