fix(treesitter): get_node_text() inconsistent trailing newline #39409

Problem:
`get_node_text()` returned inconsistent results between buffer and
string sources when a node's range ends at `end_col == 0` (i.e. the node
ends with a newline). The buffer path dropped the trailing newline; the
string path included it correctly.

Solution:
Append `'\n'` in `buf_range_get_text()` when `end_col == 0` and
`start_row ~= end_row`. The `start_row ~= end_row` guard excludes
zero-width nodes at column 0, which should return `""`.

Remove the workaround in the `#trim!` directive that manually
compensated for the missing newline.

Strip whitespace in `resolve_lang()` so injection language nodes ending
at `end_col == 0` (e.g. `">lua\n"`) still resolve correctly.
This commit is contained in:
David Balatero
2026-05-03 09:23:32 -04:00
committed by GitHub
parent 14819d55fb
commit 7ed5609439
5 changed files with 37 additions and 6 deletions

View File

@@ -201,6 +201,8 @@ end
---@returns string
local function buf_range_get_text(buf, range)
local start_row, start_col, end_row, end_col = M._range.unpack4(range)
local append_newline = end_col == 0 and start_row ~= end_row
if end_col == 0 then
if start_row == end_row then
start_col = -1
@@ -209,7 +211,12 @@ local function buf_range_get_text(buf, range)
end_col = -1
end_row = end_row - 1
end
local lines = api.nvim_buf_get_text(buf, start_row, start_col, end_row, end_col, {})
if append_newline then
table.insert(lines, '')
end
return table.concat(lines, '\n')
end