mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-15 18:00:07 -04:00
fix: indent on new line
This commit is contained in:
parent
ca3bbdbe94
commit
a2cee8852b
1 changed files with 18 additions and 7 deletions
|
|
@ -59,20 +59,31 @@ function M.get_indent(lnum)
|
||||||
local indent = 0
|
local indent = 0
|
||||||
local indent_size = vim.fn.shiftwidth()
|
local indent_size = vim.fn.shiftwidth()
|
||||||
|
|
||||||
-- to get corret indetation when we land on an empty line (for instance by typing `o`), we try
|
-- to get correct indentation when we land on an empty line (for instance by typing `o`), we try
|
||||||
-- to use indentation of previous nonblank line, this solves the issue also for languages that
|
-- to use indentation of previous nonblank line, this solves the issue also for languages that
|
||||||
-- do not use @branch after blocks (e.g. Python)
|
-- do not use @branch after blocks (e.g. Python)
|
||||||
if not node then
|
if not node then
|
||||||
local prevnonblank = vim.fn.prevnonblank(lnum)
|
local prevnonblank = vim.fn.prevnonblank(lnum)
|
||||||
if prevnonblank ~= lnum then
|
if prevnonblank ~= lnum then
|
||||||
local prev_node = get_node_at_line(root, prevnonblank-1)
|
local prev_node = get_node_at_line(root, prevnonblank-1)
|
||||||
-- we take that node only if ends before lnum, or else we would get incorrect indent
|
-- get previous node in any case to avoid erroring
|
||||||
-- on <cr> in positions like e.g. `{|}` in C (| denotes cursor position)
|
while not prev_node do
|
||||||
local use_prev = prev_node and (prev_node:end_() < lnum-1)
|
prevnonblank = vim.fn.prevnonblank(prevnonblank-1)
|
||||||
|
prev_node = get_node_at_line(root, prevnonblank-1)
|
||||||
|
end
|
||||||
|
|
||||||
-- nodes can be marked @return to prevent using them
|
-- nodes can be marked @return to prevent using them
|
||||||
use_prev = use_prev and not q.returns[node_fmt(prev_node)]
|
if not q.returns[node_fmt(prev_node)] then
|
||||||
if use_prev then
|
local row = prev_node:start()
|
||||||
node = prev_node
|
local end_row = prev_node:end_()
|
||||||
|
|
||||||
|
-- if the previous node is being constructed (like function() `o` in lua), or line is inside the node
|
||||||
|
-- we indent one more from the start of node, else we indent default
|
||||||
|
-- NOTE: this doesn't work for python which behave strangely
|
||||||
|
if prev_node:has_error() or lnum <= end_row then
|
||||||
|
return vim.fn.indent(row + 1) + indent_size
|
||||||
|
end
|
||||||
|
return vim.fn.indent(row + 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue