Skip to content

Commit

Permalink
plugin: linter: Move file type check into a dedicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed Oct 13, 2023
1 parent 7ba2514 commit f88779e
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions runtime/plugins/linter/linter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,29 @@ function contains(list, element)
return false
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)
local ft = buf:FileType()
local file = buf.Path
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)

for k, v in pairs(linters) do
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

if ftmatch then
if checkFtMatch(ft, v) then
local args = {}
for k, arg in pairs(v.args) do
args[k] = arg:gsub("%%f", file):gsub("%%d", dir)
Expand All @@ -146,20 +149,7 @@ function onBufferOptionChanged(buf, option, old, new)
if old ~= new then
ft = old
for k, v in pairs(linters) do
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

if ftmatch then
if checkFtMatch(ft, v) then
buf:ClearMessages(k)
end
end
Expand Down

0 comments on commit f88779e

Please sign in to comment.