mirror of
https://github.com/neovim/neovim.git
synced 2026-05-29 08:15:36 +00:00
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:
@@ -706,10 +706,6 @@ local directive_handlers = {
|
||||
local start_row, start_col, end_row, end_col = node:range()
|
||||
|
||||
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 start_idx = 1
|
||||
|
||||
Reference in New Issue
Block a user