mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-06 05:20:00 -04:00
indents(c): fix indentation on block comment
This commit is contained in:
parent
346366a330
commit
616dc885fc
5 changed files with 20 additions and 12 deletions
|
|
@ -37,11 +37,9 @@ local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
|
|||
aligned_indent = {},
|
||||
}
|
||||
|
||||
highlighter.active[bufnr].tree:for_each_tree(function(tstree, tree)
|
||||
for name, node, metadata in queries.iter_captures(bufnr, "indents", tstree:root(), tree:lang()) do
|
||||
map[name][node:id()] = metadata or {}
|
||||
end
|
||||
end)
|
||||
for name, node, metadata in queries.iter_captures(bufnr, "indents", root, lang) do
|
||||
map[name][node:id()] = metadata or {}
|
||||
end
|
||||
|
||||
return map
|
||||
end, {
|
||||
|
|
@ -124,16 +122,21 @@ function M.get_indent(lnum)
|
|||
-- do not indent for nodes that starts-and-ends on same line and starts on target line (lnum)
|
||||
if q.aligned_indent[node:id()] and srow ~= erow and (srow ~= lnum - 1) then
|
||||
local metadata = q.aligned_indent[node:id()]
|
||||
local opening_delimiter = metadata.delimiter:sub(1, 1)
|
||||
local o_delim_node, is_last_in_line = find_delimiter(bufnr, node, opening_delimiter)
|
||||
local o_delim_node, is_last_in_line
|
||||
if metadata.delimiter then
|
||||
local opening_delimiter = metadata.delimiter and metadata.delimiter:sub(1, 1)
|
||||
o_delim_node, is_last_in_line = find_delimiter(bufnr, node, opening_delimiter)
|
||||
else
|
||||
o_delim_node = node
|
||||
end
|
||||
|
||||
if o_delim_node then
|
||||
if is_last_in_line then
|
||||
-- hanging indent (previous line ended with starting delimiter)
|
||||
indent = indent + indent_size * 1
|
||||
else
|
||||
local _, o_scol = o_delim_node:end_()
|
||||
o_scol = o_scol + (metadata.increment or 0)
|
||||
local _, o_scol = o_delim_node:start()
|
||||
o_scol = o_scol + (metadata.increment or 1)
|
||||
return math.max(indent, 0) + o_scol
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -38,7 +38,6 @@
|
|||
] @branch
|
||||
|
||||
[
|
||||
(comment)
|
||||
(preproc_arg)
|
||||
(string_literal)
|
||||
] @ignore
|
||||
|
|
@ -47,3 +46,8 @@
|
|||
(#set! "delimiter" "()"))
|
||||
([(argument_list) (parameter_list)] @aligned_indent
|
||||
(#set! "delimiter" "()"))
|
||||
|
||||
([ (ERROR) (comment) ] @aligned_indent
|
||||
(#lua-match? @aligned_indent "^/\*"))
|
||||
((comment) @ignore
|
||||
(#lua-match? @ignore "^//"))
|
||||
|
|
|
|||
|
|
@ -6,3 +6,6 @@
|
|||
void foo(int *x, int y) {
|
||||
*x = y;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ describe("indent C:", function()
|
|||
expected_failures = {
|
||||
"./preproc_func.c",
|
||||
"./label.c",
|
||||
"./comment.c",
|
||||
},
|
||||
})
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ describe("indent C++:", function()
|
|||
-- C
|
||||
"c/preproc_func.c",
|
||||
"c/label.c",
|
||||
"c/comment.c",
|
||||
},
|
||||
})
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue