mirror of
https://github.com/neovim/neovim
synced 2025-07-16 17:21:49 +00:00
fix(treesitter): #trim! range for nodes ending at col 0 #31488
Problem: char-wise folding for `#trim!` ranges are improperly calculated for nodes that end at column 0, due to the way `get_node_text` works. Solution: Add the blank line that `get_node_text` removes for for nodes ending at column 0. Also properly set column positions when performing linewise trims.
This commit is contained in:
@ -593,6 +593,11 @@ local directive_handlers = {
|
|||||||
local start_row, start_col, end_row, end_col = node:range()
|
local start_row, start_col, end_row, end_col = node:range()
|
||||||
|
|
||||||
local node_text = vim.split(vim.treesitter.get_node_text(node, bufnr), '\n')
|
local node_text = vim.split(vim.treesitter.get_node_text(node, bufnr), '\n')
|
||||||
|
if end_col == 0 then
|
||||||
|
-- get_node_text() will ignore the last line if the node ends at column 0
|
||||||
|
node_text[#node_text + 1] = ''
|
||||||
|
end
|
||||||
|
|
||||||
local end_idx = #node_text
|
local end_idx = #node_text
|
||||||
local start_idx = 1
|
local start_idx = 1
|
||||||
|
|
||||||
@ -600,6 +605,9 @@ local directive_handlers = {
|
|||||||
while end_idx > 0 and node_text[end_idx]:find('^%s*$') do
|
while end_idx > 0 and node_text[end_idx]:find('^%s*$') do
|
||||||
end_idx = end_idx - 1
|
end_idx = end_idx - 1
|
||||||
end_row = end_row - 1
|
end_row = end_row - 1
|
||||||
|
-- set the end position to the last column of the next line, or 0 if we just trimmed the
|
||||||
|
-- last line
|
||||||
|
end_col = end_idx > 0 and #node_text[end_idx] or 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if trim_end_cols then
|
if trim_end_cols then
|
||||||
@ -616,6 +624,7 @@ local directive_handlers = {
|
|||||||
while start_idx <= end_idx and node_text[start_idx]:find('^%s*$') do
|
while start_idx <= end_idx and node_text[start_idx]:find('^%s*$') do
|
||||||
start_idx = start_idx + 1
|
start_idx = start_idx + 1
|
||||||
start_row = start_row + 1
|
start_row = start_row + 1
|
||||||
|
start_col = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if trim_start_cols and node_text[start_idx] then
|
if trim_start_cols and node_text[start_idx] then
|
||||||
|
@ -696,14 +696,14 @@ print()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('trims only empty lines by default (backwards compatible)', function()
|
it('trims only empty lines by default (backwards compatible)', function()
|
||||||
insert [[
|
insert(dedent [[
|
||||||
## Heading
|
## Heading
|
||||||
|
|
||||||
With some text
|
With some text
|
||||||
|
|
||||||
## And another
|
## And another
|
||||||
|
|
||||||
With some more]]
|
With some more here]])
|
||||||
|
|
||||||
local query_text = [[
|
local query_text = [[
|
||||||
; query
|
; query
|
||||||
@ -716,8 +716,35 @@ print()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
eq({
|
eq({
|
||||||
{ 'fold', { 0, 0, 3, 0 } },
|
{ 'fold', { 0, 0, 2, 14 } },
|
||||||
{ 'fold', { 4, 0, 7, 0 } },
|
{ 'fold', { 4, 0, 6, 19 } },
|
||||||
|
}, run_query('markdown', query_text))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('can trim lines', function()
|
||||||
|
insert(dedent [[
|
||||||
|
- Fold list
|
||||||
|
- Fold list
|
||||||
|
- Fold list
|
||||||
|
- Fold list
|
||||||
|
- Fold list
|
||||||
|
- Fold list
|
||||||
|
]])
|
||||||
|
|
||||||
|
local query_text = [[
|
||||||
|
; query
|
||||||
|
((list_item
|
||||||
|
(list)) @fold
|
||||||
|
(#trim! @fold 1 1 1 1))
|
||||||
|
]]
|
||||||
|
|
||||||
|
exec_lua(function()
|
||||||
|
vim.treesitter.start(0, 'markdown')
|
||||||
|
end)
|
||||||
|
|
||||||
|
eq({
|
||||||
|
{ 'fold', { 0, 0, 4, 13 } },
|
||||||
|
{ 'fold', { 1, 2, 3, 15 } },
|
||||||
}, run_query('markdown', query_text))
|
}, run_query('markdown', query_text))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user