mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 03:10:04 -04:00
fix(indent): indent empty lines correctly
fix(indent):
This commit is contained in:
parent
55a13862cd
commit
2d917106d1
1 changed files with 13 additions and 6 deletions
|
|
@ -17,12 +17,19 @@ local function getline(lnum)
|
||||||
return vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1] or ""
|
return vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1] or ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param lnum integer
|
||||||
|
---@return integer
|
||||||
|
local function get_indentcols_at_line(lnum)
|
||||||
|
local _, indentcols = getline(lnum):find "^%s*"
|
||||||
|
return indentcols or 0
|
||||||
|
end
|
||||||
|
|
||||||
---@param root TSNode
|
---@param root TSNode
|
||||||
---@param lnum integer
|
---@param lnum integer
|
||||||
---@param col? integer
|
---@param col? integer
|
||||||
---@return TSNode
|
---@return TSNode
|
||||||
local function get_first_node_at_line(root, lnum, col)
|
local function get_first_node_at_line(root, lnum, col)
|
||||||
col = col or vim.fn.indent(lnum)
|
col = col or get_indentcols_at_line(lnum)
|
||||||
return root:descendant_for_range(lnum - 1, col, lnum - 1, col + 1)
|
return root:descendant_for_range(lnum - 1, col, lnum - 1, col + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -152,20 +159,20 @@ function M.get_indent(lnum)
|
||||||
local node ---@type TSNode
|
local node ---@type TSNode
|
||||||
if is_empty_line then
|
if is_empty_line then
|
||||||
local prevlnum = vim.fn.prevnonblank(lnum)
|
local prevlnum = vim.fn.prevnonblank(lnum)
|
||||||
local indent = vim.fn.indent(prevlnum)
|
local indentcols = get_indentcols_at_line(prevlnum)
|
||||||
local prevline = vim.trim(getline(prevlnum))
|
local prevline = vim.trim(getline(prevlnum))
|
||||||
-- The final position can be trailing spaces, which should not affect indentation
|
-- The final position can be trailing spaces, which should not affect indentation
|
||||||
node = get_last_node_at_line(root, prevlnum, indent + #prevline - 1)
|
node = get_last_node_at_line(root, prevlnum, indentcols + #prevline - 1)
|
||||||
if node:type():match "comment" then
|
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
|
-- 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
|
-- 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)
|
local first_node = get_first_node_at_line(root, prevlnum, indentcols)
|
||||||
local _, scol, _, _ = node:range()
|
local _, scol, _, _ = node:range()
|
||||||
if first_node:id() ~= node:id() then
|
if first_node:id() ~= node:id() then
|
||||||
-- In case the last captured node is a trailing comment node, re-trim the string
|
-- In case the last captured node is a trailing comment node, re-trim the string
|
||||||
prevline = vim.trim(prevline:sub(1, scol - indent))
|
prevline = vim.trim(prevline:sub(1, scol - indentcols))
|
||||||
-- Add back indent as indent of prevline was trimmed away
|
-- Add back indent as indent of prevline was trimmed away
|
||||||
local col = indent + #prevline - 1
|
local col = indentcols + #prevline - 1
|
||||||
node = get_last_node_at_line(root, prevlnum, col)
|
node = get_last_node_at_line(root, prevlnum, col)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue