indents(c): fix indentation on block comment

This commit is contained in:
Stephan Seitz 2022-01-22 17:48:44 +01:00
parent 346366a330
commit 616dc885fc
5 changed files with 20 additions and 12 deletions

View file

@ -37,11 +37,9 @@ local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
aligned_indent = {}, aligned_indent = {},
} }
highlighter.active[bufnr].tree:for_each_tree(function(tstree, tree) for name, node, metadata in queries.iter_captures(bufnr, "indents", root, lang) do
for name, node, metadata in queries.iter_captures(bufnr, "indents", tstree:root(), tree:lang()) do map[name][node:id()] = metadata or {}
map[name][node:id()] = metadata or {} end
end
end)
return map return map
end, { 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) -- 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 if q.aligned_indent[node:id()] and srow ~= erow and (srow ~= lnum - 1) then
local metadata = q.aligned_indent[node:id()] local metadata = q.aligned_indent[node:id()]
local opening_delimiter = metadata.delimiter:sub(1, 1) local o_delim_node, is_last_in_line
local o_delim_node, is_last_in_line = find_delimiter(bufnr, node, opening_delimiter) 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 o_delim_node then
if is_last_in_line then if is_last_in_line then
-- hanging indent (previous line ended with starting delimiter) -- hanging indent (previous line ended with starting delimiter)
indent = indent + indent_size * 1 indent = indent + indent_size * 1
else else
local _, o_scol = o_delim_node:end_() local _, o_scol = o_delim_node:start()
o_scol = o_scol + (metadata.increment or 0) o_scol = o_scol + (metadata.increment or 1)
return math.max(indent, 0) + o_scol return math.max(indent, 0) + o_scol
end end
end end

View file

@ -38,7 +38,6 @@
] @branch ] @branch
[ [
(comment)
(preproc_arg) (preproc_arg)
(string_literal) (string_literal)
] @ignore ] @ignore
@ -47,3 +46,8 @@
(#set! "delimiter" "()")) (#set! "delimiter" "()"))
([(argument_list) (parameter_list)] @aligned_indent ([(argument_list) (parameter_list)] @aligned_indent
(#set! "delimiter" "()")) (#set! "delimiter" "()"))
([ (ERROR) (comment) ] @aligned_indent
(#lua-match? @aligned_indent "^/\*"))
((comment) @ignore
(#lua-match? @ignore "^//"))

View file

@ -6,3 +6,6 @@
void foo(int *x, int y) { void foo(int *x, int y) {
*x = y; *x = y;
} }
/*
*

View file

@ -14,7 +14,6 @@ describe("indent C:", function()
expected_failures = { expected_failures = {
"./preproc_func.c", "./preproc_func.c",
"./label.c", "./label.c",
"./comment.c",
}, },
}) })
end) end)

View file

@ -17,7 +17,6 @@ describe("indent C++:", function()
-- C -- C
"c/preproc_func.c", "c/preproc_func.c",
"c/label.c", "c/label.c",
"c/comment.c",
}, },
}) })
end) end)