mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 11:20:07 -04:00
Fix incremental selection (#1135)
So, there are two problems: - There was an infinite loop when inc selection was initiated from an injection. - The comparison was wrong when the whole file was selected, this is because ts matches the EOF. This is an extra line with one char (EOF). I put a workaround to try to select the node from the main tree, but we should try to select the node from the parent tree of the injection, but I wasn't able to get the parent tree from the node.
This commit is contained in:
parent
fb8762271c
commit
79040e539d
1 changed files with 35 additions and 4 deletions
|
|
@ -22,11 +22,32 @@ end
|
||||||
local function visual_selection_range()
|
local function visual_selection_range()
|
||||||
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
|
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
|
||||||
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
|
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
|
||||||
|
|
||||||
|
local start_row, start_col, end_row, end_col
|
||||||
|
|
||||||
if csrow < cerow or (csrow == cerow and cscol <= cecol) then
|
if csrow < cerow or (csrow == cerow and cscol <= cecol) then
|
||||||
return csrow - 1, cscol - 1, cerow - 1, cecol
|
start_row = csrow - 1
|
||||||
|
start_col = cscol - 1
|
||||||
|
end_row = cerow - 1
|
||||||
|
end_col = cecol
|
||||||
else
|
else
|
||||||
return cerow - 1, cecol - 1, csrow - 1, cscol
|
start_row = cerow - 1
|
||||||
|
start_col = cecol - 1
|
||||||
|
end_row = csrow - 1
|
||||||
|
end_col = cscol
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- The last char in ts is equivalent to the EOF in another line.
|
||||||
|
local last_row = vim.fn.line("$")
|
||||||
|
local last_col = vim.fn.col({last_row, "$"})
|
||||||
|
last_row = last_row - 1
|
||||||
|
last_col = last_col - 1
|
||||||
|
if end_row == last_row and end_col == last_col then
|
||||||
|
end_row = end_row + 1
|
||||||
|
end_col = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
return start_row, start_col, end_row, end_col
|
||||||
end
|
end
|
||||||
|
|
||||||
local function range_matches(node)
|
local function range_matches(node)
|
||||||
|
|
@ -57,8 +78,18 @@ local function select_incremental(get_parent)
|
||||||
-- Find a node that changes the current selection.
|
-- Find a node that changes the current selection.
|
||||||
local node = nodes[#nodes]
|
local node = nodes[#nodes]
|
||||||
while true do
|
while true do
|
||||||
node = get_parent(node)
|
local parent = get_parent(node)
|
||||||
if not node then return end
|
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, cscol, cerow, cecol)
|
||||||
|
if not parent or parent == node then
|
||||||
|
ts_utils.update_selection(buf, node)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
node = parent
|
||||||
local srow, scol, erow, ecol = node:range()
|
local srow, scol, erow, ecol = node:range()
|
||||||
local same_range = (
|
local same_range = (
|
||||||
srow == csrow
|
srow == csrow
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue