Skip to content

Commit de84da0

Browse files
committed
plugin: linter: Move file type check into a dedicated function
1 parent 771b841 commit de84da0

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

runtime/plugins/linter/linter.lua

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,29 @@ function contains(list, element)
107107
return false
108108
end
109109

110+
function checkFtMatch(ft, v)
111+
local ftmatch = ft == v.filetype
112+
if v.domatch then
113+
ftmatch = string.match(ft, v.filetype)
114+
end
115+
116+
local hasOS = contains(v.os, runtime.GOOS)
117+
if not hasOS and v.whitelist then
118+
ftmatch = false
119+
end
120+
if hasOS and not v.whitelist then
121+
ftmatch = false
122+
end
123+
return ftmatch
124+
end
125+
110126
function runLinter(buf)
111127
local ft = buf:FileType()
112128
local file = buf.Path
113129
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)
114130

115131
for k, v in pairs(linters) do
116-
local ftmatch = ft == v.filetype
117-
if v.domatch then
118-
ftmatch = string.match(ft, v.filetype)
119-
end
120-
121-
local hasOS = contains(v.os, runtime.GOOS)
122-
if not hasOS and v.whitelist then
123-
ftmatch = false
124-
end
125-
if hasOS and not v.whitelist then
126-
ftmatch = false
127-
end
128-
129-
if ftmatch then
132+
if checkFtMatch(ft, v) then
130133
local args = {}
131134
for k, arg in pairs(v.args) do
132135
args[k] = arg:gsub("%%f", file):gsub("%%d", dir)
@@ -145,20 +148,7 @@ function onBufferOptionChanged(buf, option, old, new)
145148
if option == "filetype" then
146149
if old ~= new then
147150
for k, v in pairs(linters) do
148-
local ftmatch = old == v.filetype
149-
if v.domatch then
150-
ftmatch = string.match(old, v.filetype)
151-
end
152-
153-
local hasOS = contains(v.os, runtime.GOOS)
154-
if not hasOS and v.whitelist then
155-
ftmatch = false
156-
end
157-
if hasOS and not v.whitelist then
158-
ftmatch = false
159-
end
160-
161-
if ftmatch then
151+
if checkFtMatch(old, v) then
162152
buf:ClearMessages(k)
163153
end
164154
end

0 commit comments

Comments
 (0)