plugin: linter: Move file type check into a dedicated function

This commit is contained in:
Jöran Karl
2023-10-13 02:15:48 +02:00
parent 771b84141f
commit de84da068d

View File

@@ -107,26 +107,29 @@ function contains(list, element)
return false return false
end end
function checkFtMatch(ft, v)
local ftmatch = ft == v.filetype
if v.domatch then
ftmatch = string.match(ft, v.filetype)
end
local hasOS = contains(v.os, runtime.GOOS)
if not hasOS and v.whitelist then
ftmatch = false
end
if hasOS and not v.whitelist then
ftmatch = false
end
return ftmatch
end
function runLinter(buf) function runLinter(buf)
local ft = buf:FileType() local ft = buf:FileType()
local file = buf.Path local file = buf.Path
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file) local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)
for k, v in pairs(linters) do for k, v in pairs(linters) do
local ftmatch = ft == v.filetype if checkFtMatch(ft, v) then
if v.domatch then
ftmatch = string.match(ft, v.filetype)
end
local hasOS = contains(v.os, runtime.GOOS)
if not hasOS and v.whitelist then
ftmatch = false
end
if hasOS and not v.whitelist then
ftmatch = false
end
if ftmatch then
local args = {} local args = {}
for k, arg in pairs(v.args) do for k, arg in pairs(v.args) do
args[k] = arg:gsub("%%f", file):gsub("%%d", dir) args[k] = arg:gsub("%%f", file):gsub("%%d", dir)
@@ -145,20 +148,7 @@ function onBufferOptionChanged(buf, option, old, new)
if option == "filetype" then if option == "filetype" then
if old ~= new then if old ~= new then
for k, v in pairs(linters) do for k, v in pairs(linters) do
local ftmatch = old == v.filetype if checkFtMatch(old, v) then
if v.domatch then
ftmatch = string.match(old, v.filetype)
end
local hasOS = contains(v.os, runtime.GOOS)
if not hasOS and v.whitelist then
ftmatch = false
end
if hasOS and not v.whitelist then
ftmatch = false
end
if ftmatch then
buf:ClearMessages(k) buf:ClearMessages(k)
end end
end end