aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaehwang Jung <tomtomjhj@gmail.com>2025-01-20 03:50:10 +0900
committerChristian Clason <ch.clason+github@icloud.com>2025-01-20 08:49:31 +0100
commit8acafab2bc25d270be88cf405cdf51bad12da33a (patch)
treebf222d524156ba38077d7b2c7e056295a6c7170d
parentfix(inc-selection): handle injections (diff)
downloadnvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar.gz
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar.bz2
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar.lz
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar.xz
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.tar.zst
nvim-treesitter-8acafab2bc25d270be88cf405cdf51bad12da33a.zip
fix: node selection at line start that ends injected region
-rw-r--r--lua/nvim-treesitter/ts_utils.lua22
1 files changed, 18 insertions, 4 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua
index f2ee7f524..ce103790c 100644
--- a/lua/nvim-treesitter/ts_utils.lua
+++ b/lua/nvim-treesitter/ts_utils.lua
@@ -202,12 +202,26 @@ function M.get_root_for_position(line, col, root_lang_tree)
local lang_tree = root_lang_tree:language_for_range { line, col, line, col }
- for _, tree in pairs(lang_tree:trees()) do
- local root = tree:root()
+ while true do
+ for _, tree in pairs(lang_tree:trees()) do
+ local root = tree:root()
- if root and ts.is_in_node_range(root, line, col) then
- return root, tree, lang_tree
+ if root and ts.is_in_node_range(root, line, col) then
+ return root, tree, lang_tree
+ end
end
+
+ if lang_tree == root_lang_tree then
+ break
+ end
+
+ -- This case can happen when the cursor is at the start of a line that ends a injected region,
+ -- e.g., the first `]` in the following lua code:
+ -- ```
+ -- vim.cmd[[
+ -- ]]
+ -- ```
+ lang_tree = lang_tree:parent() -- NOTE: parent() method is private
end
-- This isn't a likely scenario, since the position must belong to a tree somewhere.