diff options
author | Riley Bruins <ribru17@hotmail.com> | 2024-12-07 03:01:59 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-07 03:01:59 -0800 |
commit | c63e49cce2d20cee129a6312319dde8dcea6e3f6 (patch) | |
tree | 34e35eb3626fff0aa4f0d101d4f4828c531c357c /runtime/lua/vim/treesitter/query.lua | |
parent | 5c245ec3e95570e515c1665a2ec694828706ac52 (diff) | |
download | rneovim-c63e49cce2d20cee129a6312319dde8dcea6e3f6.tar.gz rneovim-c63e49cce2d20cee129a6312319dde8dcea6e3f6.tar.bz2 rneovim-c63e49cce2d20cee129a6312319dde8dcea6e3f6.zip |
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.
Diffstat (limited to 'runtime/lua/vim/treesitter/query.lua')
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 3c7bc2eb89..dbe3d54c2f 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -593,6 +593,11 @@ 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 @@ -600,6 +605,9 @@ local directive_handlers = { while end_idx > 0 and node_text[end_idx]:find('^%s*$') do end_idx = end_idx - 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 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 start_idx = start_idx + 1 start_row = start_row + 1 + start_col = 0 end end if trim_start_cols and node_text[start_idx] then |