From 76697b1381657cf7b65f275df86ea9d3be31ad10 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Fri, 25 Jul 2025 14:54:18 +0545 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(lint):=20configure=20nvim-lint?= =?UTF-8?q?=20with=20codespell,=20editorconfig-checker=20and=20file-specif?= =?UTF-8?q?ic=20configs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/plugins/lang/conform.lua | 3 +- lua/plugins/lang/nvim-lint.lua | 55 ++++++++++++++++++++++++++-------- lua/plugins/list.lua | 7 +++++ lua/plugins/ui/which-key.lua | 2 +- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/lua/plugins/lang/conform.lua b/lua/plugins/lang/conform.lua index 448d108..5659d91 100644 --- a/lua/plugins/lang/conform.lua +++ b/lua/plugins/lang/conform.lua @@ -14,7 +14,8 @@ conform.setup({ html = { 'prettier' }, json = { 'prettier' }, yaml = { 'prettier' }, - sh = { 'shfmt' }, + sh = { 'shfmt', 'shellcheck' }, + ['_'] = { 'trim_whitespace', lsp_format = 'prefer' }, }, -- Autoformat on save diff --git a/lua/plugins/lang/nvim-lint.lua b/lua/plugins/lang/nvim-lint.lua index 93b4d77..6ee9106 100644 --- a/lua/plugins/lang/nvim-lint.lua +++ b/lua/plugins/lang/nvim-lint.lua @@ -14,23 +14,52 @@ lint.linters_by_ft = { yaml = { 'actionlint' }, } --- Trigger linting on save and buffer events -vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { - callback = function() - local lint = require('lint') +table.insert( + require('lint').linters.codespell.args, + 1, + '--config=' .. vim.fs.joinpath(require('dotfyls.files').config_dir, 'codespell', '.codespellrc') +) +table.insert(require('lint').linters.vala_lint.args, 1, '--config=vala-lint.conf') +table.insert( + require('lint').linters.yamllint.args, + 1, + '--config-file=' .. vim.fs.joinpath(require('dotfyls.files').config_dir, 'yamllint', 'config.yaml') +) - -- Get the filetype-specific linters - local ft = vim.bo.filetype - local linters = lint.linters_by_ft[ft] or {} +local function lint(bufnr) + local linters = + vim.list_extend({ 'codespell', 'editorconfig-checker' }, require('lint')._resolve_linter_by_ft(vim.bo.filetype)) - -- Add cspell if it's not already included - if not vim.tbl_contains(linters, 'cspell') then - table.insert(linters, 'cspell') - end + local ctx = { filename = vim.api.nvim_buf_get_name(bufnr or 0) } + ctx.dirname = vim.fn.fnamemodify(ctx.filename, ':h') - -- Run them all - lint.try_lint(linters) + linters = vim.tbl_filter(function(name) + local linter = require('lint').linters[name] + return vim.fn.executable(linter.cmd) == 1 + ---@diagnostic disable-next-line: undefined-field + and not (type(linter) == 'table' and linter.condition and not linter.condition(ctx)) + end, linters) + + if #linters > 0 then + require('lint').try_lint(linters) + end +end + +lint() + +local timer = assert(vim.uv.new_timer()) +vim.api.nvim_create_autocmd({ 'BufReadPost', 'BufWritePost', 'InsertLeave' }, { + group = require('dotfyls.interop').group, + callback = function(args) + timer:start(100, 0, function() + timer:stop() + + vim.schedule(function() + lint(args.buf) + end) + end) end, + desc = 'Lint (nvim-lint)', }) -- Setup mason-nvim-lint to auto-install linters diff --git a/lua/plugins/list.lua b/lua/plugins/list.lua index 72c31f5..f27f1e4 100644 --- a/lua/plugins/list.lua +++ b/lua/plugins/list.lua @@ -126,6 +126,11 @@ local plugins = { { 'mfussenegger/nvim-lint', dependencies = { 'rshkarin/mason-nvim-lint' }, + init = function() + if vim.fn.executable('codespell') == 1 then + vim.o.spell = false + end + end, config = load_config('lang.nvim-lint'), event = { 'BufReadPost', 'BufNewFile' }, }, @@ -454,6 +459,8 @@ local linter_sources = { 'hadolint', 'actionlint', 'vint', + 'codespell', + 'editorconfig-checker', } local formatter_sources = { diff --git a/lua/plugins/ui/which-key.lua b/lua/plugins/ui/which-key.lua index 904c712..9963c64 100644 --- a/lua/plugins/ui/which-key.lua +++ b/lua/plugins/ui/which-key.lua @@ -63,7 +63,7 @@ local normal_mappings = { { 'cc', ':CccHighlighterToggle', desc = 'Highlight Colors' }, { 'cd', ':RootDir', desc = 'Root Directory' }, { 'ce', ':%SnipRun', desc = 'Execute File' }, - { 'cf', ':lua vim.lsp.buf.format({async = true})', desc = 'Format File' }, + -- { 'cf', ':lua vim.lsp.buf.format({async = true})', desc = 'Format File' }, { 'cl', '::g/^\\s*$/d', desc = 'Clean Empty Lines' }, { 'cm', ':MarkdownPreviewToggle', desc = 'Markdown Preview' }, { 'co', ':Dashboard', desc = 'Dashboard' },