aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorChristian Clason <c.clason@uni-graz.at>2022-04-18 18:49:14 +0200
committerChristian Clason <christian.clason@uni-due.de>2022-04-18 18:50:46 +0200
commita8bce851bf3bde7c9c25b1d504dc25c877d66713 (patch)
tree30103ee0c998b35fc5f1e5007427b318e8d161bf /lua
parentfix(statusline): adjust to the new API (query) (diff)
downloadnvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar.gz
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar.bz2
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar.lz
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar.xz
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.tar.zst
nvim-treesitter-a8bce851bf3bde7c9c25b1d504dc25c877d66713.zip
fixup: restore old implementation
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/ts_utils.lua22
1 files changed, 21 insertions, 1 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua
index 64d423166..c95ddf54e 100644
--- a/lua/nvim-treesitter/ts_utils.lua
+++ b/lua/nvim-treesitter/ts_utils.lua
@@ -16,7 +16,27 @@ function M.get_node_text(node, bufnr)
"nvim-treesitter.ts_utils.get_node_text is deprecated: use vim.treesitter.query.get_node_text",
vim.log.levels.WARN
)
- vim.treesitter.query.get_node_text(node, bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+ if not node then
+ return {}
+ end
+
+ -- We have to remember that end_col is end-exclusive
+ local start_row, start_col, end_row, end_col = M.get_node_range(node)
+
+ if start_row ~= end_row then
+ local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row + 1, false)
+ lines[1] = string.sub(lines[1], start_col + 1)
+ -- end_row might be just after the last line. In this case the last line is not truncated.
+ if #lines == end_row - start_row + 1 then
+ lines[#lines] = string.sub(lines[#lines], 1, end_col)
+ end
+ return lines
+ else
+ local line = api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
+ -- If line is nil then the line is empty
+ return line and { string.sub(line, start_col + 1, end_col) } or {}
+ end
end
--- Determines whether a node is the parent of another