indents(c): add zero_indent for #if

This commit is contained in:
Stephan Seitz 2022-01-22 18:24:38 +01:00
parent 616dc885fc
commit fd5a551d7c
7 changed files with 18 additions and 13 deletions

View file

@ -35,6 +35,7 @@ local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
branch = {}, branch = {},
ignore = {}, ignore = {},
aligned_indent = {}, aligned_indent = {},
zero_indent = {},
} }
for name, node, metadata in queries.iter_captures(bufnr, "indents", root, lang) do for name, node, metadata in queries.iter_captures(bufnr, "indents", root, lang) do
@ -88,6 +89,10 @@ function M.get_indent(lnum)
-- tracks to ensure multiple indent levels are not applied for same line -- tracks to ensure multiple indent levels are not applied for same line
local is_processed_by_row = {} local is_processed_by_row = {}
if q.zero_indent[node:id()] then
return 0
end
while node do while node do
-- do 'autoindent' if not marked as @indent -- do 'autoindent' if not marked as @indent
if not q.indent[node:id()] and q.auto[node:id()] and node:start() < lnum - 1 and lnum - 1 <= node:end_() then if not q.indent[node:id()] and q.auto[node:id()] and node:start() < lnum - 1 and lnum - 1 <= node:end_() then

View file

@ -26,16 +26,20 @@
(compound_statement "}" @indent_end) (compound_statement "}" @indent_end)
[
"else"
")"
"}"
(statement_identifier)
] @branch
[ [
"#define" "#define"
"#ifdef" "#ifdef"
"#if" "#if"
"#else" "#else"
"else"
"#endif" "#endif"
")" ] @zero_indent
"}"
] @branch
[ [
(preproc_arg) (preproc_arg)
@ -47,7 +51,7 @@
([(argument_list) (parameter_list)] @aligned_indent ([(argument_list) (parameter_list)] @aligned_indent
(#set! "delimiter" "()")) (#set! "delimiter" "()"))
([ (ERROR) (comment) ] @aligned_indent ((comment) @aligned_indent
(#lua-match? @aligned_indent "^/\*")) (#lua-match? @aligned_indent "^/\*"))
((comment) @ignore ((comment) @ignore
(#lua-match? @ignore "^//")) (#lua-match? @ignore "^//"))

View file

@ -5,6 +5,3 @@ void foo(int a,
void foo(int a, void foo(int a,
int b int b

View file

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

View file

@ -0,0 +1,2 @@
/*
*

View file

@ -13,7 +13,7 @@ describe("indent C:", function()
runner:whole_file(".", { runner:whole_file(".", {
expected_failures = { expected_failures = {
"./preproc_func.c", "./preproc_func.c",
"./label.c", "./unfinished_comment.c",
}, },
}) })
end) end)

View file

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