From b33cf84310ba0466774f228cc147bb8000bbaa63 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 27 May 2023 17:43:29 -0500 Subject: feat(nvim): move null-ls linters to nvim-lint --- nvim/.config/nvim/lazy-lock.json | 1 + nvim/.config/nvim/lua/plugins/lint.lua | 89 +++++++++++++++++++++++++++++++ nvim/.config/nvim/lua/plugins/null-ls.lua | 7 --- 3 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 nvim/.config/nvim/lua/plugins/lint.lua diff --git a/nvim/.config/nvim/lazy-lock.json b/nvim/.config/nvim/lazy-lock.json index 70c2946..d6ec63c 100644 --- a/nvim/.config/nvim/lazy-lock.json +++ b/nvim/.config/nvim/lazy-lock.json @@ -42,6 +42,7 @@ "nvim-dap-repl-highlights": { "branch": "master", "commit": "97a2b322c05cf945c5aabaad5e599a20b25e77d9" }, "nvim-dap-virtual-text": { "branch": "master", "commit": "57f1dbd0458dd84a286b27768c142e1567f3ce3b" }, "nvim-jdtls": { "branch": "master", "commit": "365811ecf97a08d0e2055fba210d65017344fd15" }, + "nvim-lint": { "branch": "master", "commit": "00b9feee38586d4521c17a0a6a0e2c94ea75d44a" }, "nvim-lspconfig": { "branch": "master", "commit": "465042c0f786992212284311ebb5da1f89479774" }, "nvim-neoclip.lua": { "branch": "main", "commit": "4e406ae0f759262518731538f2585abb9d269bac" }, "nvim-notify": { "branch": "master", "commit": "f3024b912073774111202f5fa6518b0cd2a74432" }, diff --git a/nvim/.config/nvim/lua/plugins/lint.lua b/nvim/.config/nvim/lua/plugins/lint.lua new file mode 100644 index 0000000..ebb7b1c --- /dev/null +++ b/nvim/.config/nvim/lua/plugins/lint.lua @@ -0,0 +1,89 @@ +---@type LazySpec +local M = { + "mfussenegger/nvim-lint", + opts = { + markdown = { "markdownlint" }, + htmldjango = { "djlint" }, + }, +} + +function M:init() + local augroup = vim.api.nvim_create_augroup("nvim-lint", { clear = true }) + + vim.api.nvim_create_autocmd({ "BufReadPost", "TextChanged", "InsertLeave" }, { + group = augroup, + callback = function() + require("lint").try_lint({ "typos" }) + end, + }) + + vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost" }, { + group = augroup, + callback = function() + require("lint").try_lint() + end, + }) +end + +local in_spell_node = function(bufnr, row, col) + local captures = vim.treesitter.get_captures_at_pos(bufnr, row, col) + local spell_node = false + + for _, capture in pairs(captures) do + if capture.capture == "nospell" then + return false + end + + spell_node = spell_node or capture.capture == "spell" + end + return spell_node +end + +function M:config(opts) + require("lint").linters.djlint = { + cmd = "djlint", + args = { "--quiet", "--profile=django", "-" }, + stdin = true, + stream = "stdout", + ignore_exitcode = true, + parser = require("lint.parser").from_pattern( + "(%w+) (%d+):(%d+) (.*)", + { "code", "lnum", "col", "message" }, + { source = "djlint" }, + { end_col_offset = 1 } + ), + } + + require("lint").linters.typos = { + cmd = "typos", + args = { "--format", "json", "-" }, + stdin = true, + stream = "stdout", + ignore_exitcode = true, + parser = function(output, bufnr) + local diagnostics = {} + + for line in vim.gsplit(output, "\n", { trimempty = true }) do + local ok, data = pcall(vim.json.decode, line) + if ok and data ~= nil and in_spell_node(bufnr, data.line_num - 1, data.byte_offset) then + table.insert(diagnostics, { + bufnr = bufnr, + lnum = data.line_num - 1, + col = data.byte_offset, + end_col = data.byte_offset + data.typo:len(), + severity = vim.diagnostic.severity.ERROR, + message = ("`%s` -> `%s`"):format(data.typo, table.concat(data.corrections, "`, `")), + source = "typos", + user_data = { corrections = data.corrections }, + }) + end + end + + return diagnostics + end, + } + + require("lint").linters_by_ft = opts +end + +return M diff --git a/nvim/.config/nvim/lua/plugins/null-ls.lua b/nvim/.config/nvim/lua/plugins/null-ls.lua index 8493d1a..7813531 100644 --- a/nvim/.config/nvim/lua/plugins/null-ls.lua +++ b/nvim/.config/nvim/lua/plugins/null-ls.lua @@ -24,13 +24,6 @@ function M.config() extra_filetypes = { "PKGBUILD" }, }), null_ls.builtins.code_actions.typos, - null_ls.builtins.diagnostics.markdownlint.with({ - extra_args = { - ("--config=%s/markdownlint/markdownlint.yaml"):format(vim.env.XDG_CONFIG_HOME), - }, - }), - null_ls.builtins.diagnostics.djlint, - null_ls.builtins.diagnostics.typos, null_ls.builtins.formatting.prettier.with({ disabled_filetypes = { "json", "jsonc" }, extra_args = { "--prose-wrap=always" }, -- cgit v1.2.3-70-g09d2