diff options
Diffstat (limited to 'store/config/nvim/lua/setup/plugins/lint.lua')
-rw-r--r-- | store/config/nvim/lua/setup/plugins/lint.lua | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/store/config/nvim/lua/setup/plugins/lint.lua b/store/config/nvim/lua/setup/plugins/lint.lua new file mode 100644 index 0000000..d03f539 --- /dev/null +++ b/store/config/nvim/lua/setup/plugins/lint.lua @@ -0,0 +1,94 @@ +--- spellchecker: ignore markdownlintrc + +---@alias CruLinter { name: string, config_patterns: string[], filetypes: string[] | nil, fast: boolean } + +local cspell = { + name = "cspell", + config_patterns = { + ".cspell.json", + "cspell.json", + ".cSpell.json", + "cSpell.json", + "cspell.config.js", + "cspell.config.cjs", + "cspell.config.json", + "cspell.config.yaml", + "cspell.config.yml", + "cspell.yaml", + "cspell.yml", + }, + fast = true, +} + +local markdownlint = { + name = "markdownlint", + config_patterns = { + ".markdownlint.jsonc", + ".markdownlint.json", + ".markdownlint.yaml", + ".markdownlint.yml", + ".markdownlintrc", + }, + filetypes = { "markdown" }, + fast = true, +} + +local linters = { cspell = cspell, markdownlint = markdownlint } + +---@param linter CruLinter +---@param buf integer +---@return string | nil +local function find_config(linter, buf) + local files = vim.fs.find(linter.config_patterns, { + path = vim.api.nvim_buf_get_name(buf), upward = true }) + if #files ~= 0 then + return files[1]; + end + return nil +end + +vim.list_extend(require("lint.linters.markdownlint").args, { + "--config", + function() + return find_config(markdownlint, 0); + end +}) + +---@param linter CruLinter +---@param buf integer +function vim.crupest.lint(linter, buf) + if linter.filetypes then + local filetype = vim.api.nvim_get_option_value("filetype", { buf = buf }) + if not vim.list_contains(linter.filetypes, filetype) then + return + end + end + + if find_config(linter, buf) then + require("lint").try_lint(linter.name) + end +end + +function vim.crupest.lint_all(buf, fast) + for _, linter in pairs(linters) do + if not fast or linter.fast then + vim.crupest.lint(linter, buf) + end + end +end + +local function setup() + vim.api.nvim_create_autocmd( + { "BufReadPost", "InsertLeave", "TextChanged" }, + { + callback = function(opt) + if vim.api.nvim_get_option_value("buftype", { buf = opt.buf }) == "" then + vim.crupest.lint_all(opt.buf, true) + end + end + }) +end + +return { + setup = setup, +} |