From 2363ce941d2ecedc9ac5e6e4cd478d039e799d85 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Mon, 6 Nov 2023 13:28:05 -0600 Subject: fix(nvim): fix nvim-lint conditional linting --- nvim/.config/nvim/lua/plugins/nvim-lint.lua | 107 ++++++++++++++++++---------- nvim/.config/nvim/lua/tobyvin/utils.lua | 13 ++++ 2 files changed, 82 insertions(+), 38 deletions(-) (limited to 'nvim/.config') diff --git a/nvim/.config/nvim/lua/plugins/nvim-lint.lua b/nvim/.config/nvim/lua/plugins/nvim-lint.lua index d8a8640..a6ceaa7 100644 --- a/nvim/.config/nvim/lua/plugins/nvim-lint.lua +++ b/nvim/.config/nvim/lua/plugins/nvim-lint.lua @@ -1,3 +1,38 @@ +local utils = require("tobyvin.utils") + +local function find_config(filename) + local config = unpack(vim.fs.find(filename, { + path = vim.api.nvim_buf_get_name(0), + upward = true, + })) or ("%s/%s/%s"):format(vim.env.XDG_CONFIG_HOME, vim.fs.basename(filename), filename) + if vim.fn.filereadable(config) == 1 then + return config + end +end + +local function try_lint() + local lint = require("lint") + local names = lint._resolve_linter_by_ft(vim.bo.filetype) + + if #names == 0 then + vim.list_extend(names, lint.linters_by_ft["_"] or {}) + end + + vim.list_extend(names, lint.linters_by_ft["*"] or {}) + + names = vim.tbl_filter(function(name) + local linter = lint.linters[name] + if not linter then + vim.notify("Linter not found: " .. name, vim.log.levels.WARN, { title = "nvim-lint" }) + end + return linter and not (type(linter) == "table" and linter.condition and not linter.condition()) + end, names) + + if #names > 0 then + lint.try_lint(names) + end +end + ---@type LazyPluginSpec local M = { "mfussenegger/nvim-lint", @@ -11,57 +46,53 @@ local M = { }, linters = { markdownlint = { - config = "markdownlint.yaml", + prepend_args = { + "--config", + function() + return find_config("markdownlint.yaml") + end, + }, + condition = function() + return find_config("markdownlint.yaml") + end, }, selene = { - config = "selene.toml", + prepend_args = { + "--config", + function() + return find_config("selene.toml") + end, + }, + condition = function() + return find_config("selene.toml") + end, }, }, }, } -local function prepend(tbl, items) - for i, arg in ipairs(items or {}) do - table.insert(tbl, i, arg) - end - return tbl -end - -local function with_config(args, filename) - return prepend(args, { - function() - local config = unpack(vim.fs.find(filename, { - path = vim.api.nvim_buf_get_name(0), - upward = true, - })) or ("%s/%s"):format(vim.fs.basename(filename), filename) - return vim.fn.filereadable(config) == 1 and string.format("--config=%s", config) or "" - end, - }) -end - function M:init() - local linters = self.opts.linters or {} - self.opts.linters = setmetatable({}, { - __index = function(t, k) - local ok, linter = pcall(require, "lint.linters." .. k) - if ok and linters[k] then - t[k] = vim.tbl_extend("force", { args = {} }, linter, linters[k] or {}) - prepend(with_config(t[k].args, t[k].config), t[k].prepend_args) - end - return t[k] - end, - }) - vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost", "InsertLeave" }, { - callback = function() - require("lint").try_lint() - end, + group = vim.api.nvim_create_augroup("nvim-lint", { clear = true }), + callback = utils.debounce(100, try_lint), }) end function M:config(opts) - require("lint").linters = opts.linters - require("lint").linters_by_ft = opts.linters_by_ft + local lint = require("lint") + + for name, linter in pairs(opts.linters) do + if type(linter) == "table" and type(lint.linters[name]) == "table" then + lint.linters[name] = vim.tbl_deep_extend("force", lint.linters[name], linter) + for _, arg in pairs(lint.linters[name].prepend_args) do + lint.linters[name].args = lint.linters[name].args or {} + table.insert(lint.linters[name].args, arg) + end + else + lint.linters[name] = linter + end + end + lint.linters_by_ft = opts.linters_by_ft end return M diff --git a/nvim/.config/nvim/lua/tobyvin/utils.lua b/nvim/.config/nvim/lua/tobyvin/utils.lua index af54cae..61410ce 100644 --- a/nvim/.config/nvim/lua/tobyvin/utils.lua +++ b/nvim/.config/nvim/lua/tobyvin/utils.lua @@ -5,6 +5,19 @@ function M.inspect(v) return v end +---@param ms integer +---@param fn function +function M.debounce(ms, fn) + local timer = vim.loop.new_timer() + return function(...) + local argv = { ... } + timer:start(ms, 0, function() + timer:stop() + vim.schedule_wrap(fn)(unpack(argv)) + end) + end +end + ---Register callback to run when a lsp server matching a filter attaches to a buffer ---@param on_attach fun(client: lsp.Client, buffer: integer): boolean|nil ---@param filter vim.lsp.get_clients.filter|nil -- cgit v1.2.3-70-g09d2