Change reformat algorithm
Treat blank lines inside functions the same as outside functions
Add special rules for goto labels
@@ -57,20 +57,24 @@ end
--
function C.reformat(input)
local out = {}
- local indent = 0
+ local depth = 0
local previous_is_blank = true
for line in input:gmatch("([^\n]*)") do
line = line:match("^[ \t]*(.-)[ \t]*$")
- -- Inside functions ignore all empty lines. In the toplevel, collapse
- -- groups of empty lines between declarations into a single empty line.
+ -- Collapse groups of empty lines into a single empty line.
local is_blank = (#line == 0)
- if (not is_blank) or (indent == 0 and not previous_is_blank) then
+ if not (is_blank and previous_is_blank) then
- local indent_for_this_line
+ local nspaces
if line:match("^#") then
-- Preprocessor directives are never indented
- indent_for_this_line = 0
+ nspaces = 0
+
+ elseif line:match("^[A-Za-z_][A-Za-z_0-9]*:$") then
+ -- Labels are indented halfway
+ nspaces = math.max(0, 4 * depth - 2)
+
else
-- Otherwise, count braces and parens
local without_strings = line:gsub('\\\"', "")
@@ -81,24 +85,24 @@ function C.reformat(input)
local n_close = #string.gsub(without_comments, "[^})]", "")
local unindent_this_line = string.match(line, "^[})]")
- indent_for_this_line = indent - (unindent_this_line and 1 or 0)
+ nspaces = 4 * (depth - (unindent_this_line and 1 or 0))
if n_open > n_close then
- indent = indent + 1
+ depth = depth + 1
elseif n_open < n_close then
- indent = indent - 1
+ depth = depth - 1
end
- if indent < 0 then
+ if depth < 0 then
-- Don't let the indentation level get negative. If by any
-- chance our heuristics fail to spot an open brace or
-- paren, this confines the messed up indentation to a
-- single function.
- indent = 0
+ depth = 0
end
end
- table.insert(out, string.rep(" ", indent_for_this_line))
+ table.insert(out, string.rep(" ", nspaces))
table.insert(out, line)
table.insert(out, "\n")
end
