aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPham Huy Hoang <hoangtun0810@gmail.com>2023-04-01 18:31:00 +0900
committerStephan Seitz <stephan.seitz@fau.de>2023-04-08 09:56:06 -0700
commit1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70 (patch)
tree5623fa455ad9c5a823a84af8c6b2e41244f6b1c3
parentfeat(cpp): more distinction between function/method declaration and call (diff)
downloadnvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar.gz
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar.bz2
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar.lz
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar.xz
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.tar.zst
nvim-treesitter-1c7ba3ffcfe8b828b69c7100b4caf7daa837ce70.zip
fix(indent): Make indent ignore trailing spaces/comment
Signed-off-by: Pham Huy Hoang <hoangtun0810@gmail.com>
-rw-r--r--lua/nvim-treesitter/indent.lua28
1 files changed, 23 insertions, 5 deletions
diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua
index 7290b3148..7b3485341 100644
--- a/lua/nvim-treesitter/indent.lua
+++ b/lua/nvim-treesitter/indent.lua
@@ -17,17 +17,19 @@ M.comment_parsers = {
---@param root TSNode
---@param lnum integer
+---@param col? integer
---@return TSNode
-local function get_first_node_at_line(root, lnum)
- local col = vim.fn.indent(lnum)
+local function get_first_node_at_line(root, lnum, col)
+ col = col or vim.fn.indent(lnum)
return root:descendant_for_range(lnum - 1, col, lnum - 1, col)
end
---@param root TSNode
---@param lnum integer
+---@param col? integer
---@return TSNode
-local function get_last_node_at_line(root, lnum)
- local col = #vim.fn.getline(lnum) - 1
+local function get_last_node_at_line(root, lnum, col)
+ col = col or (#vim.fn.getline(lnum) - 1)
return root:descendant_for_range(lnum - 1, col, lnum - 1, col)
end
@@ -128,7 +130,23 @@ function M.get_indent(lnum)
local node ---@type TSNode
if is_empty_line then
local prevlnum = vim.fn.prevnonblank(lnum)
- node = get_last_node_at_line(root, prevlnum)
+ local indent = vim.fn.indent(prevlnum)
+ local prevline = vim.trim(vim.fn.getline(prevlnum))
+ -- The final position can be trailing spaces, which should not affect indentation
+ node = get_last_node_at_line(root, prevlnum, indent + #prevline - 1)
+ if node:type():match "comment" then
+ -- The final node we capture of the previous line can be a comment node, which should also be ignored
+ -- Unless the last line is an entire line of comment, ignore the comment range and find the last node again
+ local first_node = get_first_node_at_line(root, prevlnum, indent)
+ if first_node:id() ~= node:id() then
+ -- In case the last captured node is a trailing comment node, re-trim the string
+ prevline = vim.trim(prevline:sub(1, node:start() + 1 - indent))
+ -- Add back indent as indent of prevline was trimmed away
+ local col = indent + #prevline - 1
+ node = get_last_node_at_line(root, prevlnum, col)
+ end
+ end
+
if q.indent["end"][node:id()] then
node = get_first_node_at_line(root, lnum)
end