Add tab outside of indent check

This commit is contained in:
SirLynix 2024-01-26 14:38:59 +01:00
parent 6757de1be8
commit 22a047b3b1
10 changed files with 241 additions and 175 deletions

View File

@ -889,6 +889,72 @@ on_run(function ()
end
})
-- No tab character should exist after indentation
table.insert(checks, {
Name = "tab outside of indent",
Check = function (moduleName)
local files = table.join(
os.files("include/Nazara/" .. moduleName .. "/**.hpp"),
os.files("include/Nazara/" .. moduleName .. "/**.inl"),
os.files("src/Nazara/" .. moduleName .. "/**.hpp"),
os.files("src/Nazara/" .. moduleName .. "/**.inl"),
os.files("src/Nazara/" .. moduleName .. "/**.cpp")
)
local fixes = {}
for _, filePath in pairs(files) do
local lines = GetFile(filePath)
local fileFixes = {}
for i = 1, #lines do
local start = lines[i]:match("\t*[^\t+]()\t")
if start then
table.insert(fileFixes, { line = i, start = start })
end
end
if #fileFixes > 0 then
print(filePath .. " has tab character outside of indentation")
table.insert(fixes, {
File = filePath,
Func = function (lines)
for _, fix in ipairs(fileFixes) do
-- compute indent taking tabs into account
local function ComputeIndent(str, i)
local indent = 0
for i = 1, i do
if str:sub(i, i) == "\t" then
-- round up to tabSize (4)
indent = ((indent + 4) // 4) * 4
else
indent = indent + 1
end
end
return indent
end
lines[fix.line] = lines[fix.line]:gsub("()\t", function (pos)
if pos < fix.start then
return
end
local indent = ComputeIndent(lines[fix.line], pos - 1)
local indent2 = ComputeIndent(lines[fix.line], pos)
return string.rep(" ", indent2 - indent)
end)
end
UpdateFile(filePath, lines)
end
})
end
end
return fixes
end
})
local shouldFix = option.get("fix") or false
for _, check in pairs(checks) do