From de84da068dd039fc70dc457103e1c8490f8fe787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 13 Oct 2023 02:15:48 +0200 Subject: [PATCH] plugin: linter: Move file type check into a dedicated function --- runtime/plugins/linter/linter.lua | 46 ++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index d5bd9541..fe09f068 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -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) @@ -145,20 +148,7 @@ function onBufferOptionChanged(buf, option, old, new) if option == "filetype" then if old ~= new then for k, v in pairs(linters) do - local ftmatch = old == v.filetype - 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 + if checkFtMatch(old, v) then buf:ClearMessages(k) end end