From dd0925b1664aee2d639dae76109eea8c72de5152 Mon Sep 17 00:00:00 2001 From: long Date: Sun, 30 Mar 2025 02:19:08 -0500 Subject: [PATCH] - replaced null-ls with none-ls - added deprecated formatters and diagnostics --- lazy-lock.json | 2 +- lua/plugins/config.lua | 40 ++----- lua/plugins/lazy.lua | 2 +- lua/plugins/null_ls_builtins.lua | 172 +++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 34 deletions(-) create mode 100644 lua/plugins/null_ls_builtins.lua diff --git a/lazy-lock.json b/lazy-lock.json index bd748df..ee7e58d 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -19,7 +19,7 @@ "lualine.nvim": { "branch": "master", "commit": "f4f791f67e70d378a754d02da068231d2352e5bc" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "1a31f824b9cd5bc6f342fc29e9a53b60d74af245" }, "mason.nvim": { "branch": "main", "commit": "fc98833b6da5de5a9c5b1446ac541577059555be" }, - "null-ls.nvim": { "branch": "main", "commit": "0010ea927ab7c09ef0ce9bf28c2b573fc302f5a7" }, + "none-ls.nvim": { "branch": "main", "commit": "a117163db44c256d53c3be8717f3e1a2a28e6299" }, "nvim-autopairs": { "branch": "master", "commit": "68f0e5c3dab23261a945272032ee6700af86227a" }, "nvim-cmp": { "branch": "main", "commit": "12509903a5723a876abd65953109f926f4634c30" }, "nvim-fzf": { "branch": "master", "commit": "305aa90aeb8409b4bd2af1812a4b6e157ee93953" }, diff --git a/lua/plugins/config.lua b/lua/plugins/config.lua index d7b7450..cd84c18 100644 --- a/lua/plugins/config.lua +++ b/lua/plugins/config.lua @@ -315,38 +315,18 @@ function M.null_ls() local null_ls = require("null-ls") local config = lsp_config_defaults() - local h = require("null-ls.helpers") - local methods = require("null-ls.methods") - local FORMATTING = methods.internal.FORMATTING - - local templ_fmt = h.make_builtin({ - name = "templ", - method = FORMATTING, - filetypes = { "templ" }, - generator_opts = { - command = "templ", - args = { "fmt" }, - to_stdin = true, - }, - factory = h.formatter_factory, - }) + local builtins = require("plugins.null_ls_builtins") + local formatters = builtins.formatters + local diagnostics = builtins.diagnostics -- ORDER IN TABLE DETERMINES EXECUTION ORDER local sources = { -- python - null_ls.builtins.diagnostics.ruff.with({ - args = { "check", "-n", "-e", "--stdin-filename", "$FILENAME", "-" }, - }), - null_ls.builtins.formatting.ruff.with({ - args = { "format", "--stdin-filename", "$FILENAME", "-" }, - }), - null_ls.builtins.formatting.ruff.with({ - args = { "check", "--select", "I", "--fix", "--stdin-filename", "$FILENAME", "-" }, - }), + diagnostics.ruff(), + formatters.ruff_imports(), + formatters.ruff_code(), -- js - null_ls.builtins.diagnostics.eslint, - null_ls.builtins.code_actions.eslint, null_ls.builtins.formatting.prettier.with({ extra_filetypes = { "svelte" }, extra_args = { "--tab-width", "4" }, @@ -357,8 +337,7 @@ function M.null_ls() -- bash null_ls.builtins.formatting.shfmt, - null_ls.builtins.diagnostics.shellcheck, - null_ls.builtins.code_actions.shellcheck, + diagnostics.shellcheck(), -- cpp null_ls.builtins.formatting.clang_format, @@ -366,7 +345,7 @@ function M.null_ls() -- golang --null_ls.builtins.diagnostics.golangci_lint, null_ls.builtins.formatting.gofmt, - templ_fmt, + formatters.templ(), -- sql -- null_ls.builtins.diagnostics.sqlfluff.with(sqlfluff_args), @@ -378,9 +357,6 @@ function M.null_ls() null_ls.builtins.diagnostics.credo, null_ls.builtins.formatting.mix, - -- zig - null_ls.builtins.formatting.zigfmt, - -- c_sharp null_ls.builtins.formatting.csharpier, } diff --git a/lua/plugins/lazy.lua b/lua/plugins/lazy.lua index 4cc97d5..73a3809 100644 --- a/lua/plugins/lazy.lua +++ b/lua/plugins/lazy.lua @@ -61,7 +61,7 @@ function M.install_plugins() }, "nvim-treesitter/nvim-treesitter-context", "nvim-treesitter/playground", - "jose-elias-alvarez/null-ls.nvim", + "nvimtools/none-ls.nvim", "windwp/nvim-autopairs", { diff --git a/lua/plugins/null_ls_builtins.lua b/lua/plugins/null_ls_builtins.lua new file mode 100644 index 0000000..1d518c4 --- /dev/null +++ b/lua/plugins/null_ls_builtins.lua @@ -0,0 +1,172 @@ +local M = {} + +local h = require("null-ls.helpers") +local methods = require("null-ls.methods") +local DIAGNOSTICS = methods.internal.DIAGNOSTICS +local FORMATTING = methods.internal.FORMATTING + +M.diagnostics = { + ruff = function() + local custom_end_col = { + end_col = function(entries, line) + if not line then + return + end + + local start_col = entries["col"] + local message = entries["message"] + local code = entries["code"] + local default_position = start_col + 1 + + local pattern = nil + local trimmed_line = line:sub(start_col, -1) + + if code == "F841" or code == "F823" then + pattern = [[Local variable %`(.*)%`]] + elseif code == "F821" or code == "F822" then + pattern = [[Undefined name %`(.*)%`]] + elseif code == "F401" then + pattern = [[%`(.*)%` imported but unused]] + elseif code == "F841" then + pattern = [[Local variable %`(.*)%` is assigned to but never used]] + end + if not pattern then + return default_position + end + + local results = message:match(pattern) + local _, end_col = trimmed_line:find(results, 1, true) + + if not end_col then + return default_position + end + + end_col = end_col + start_col + if end_col > tonumber(start_col) then + return end_col + end + + return default_position + end, + } + + return h.make_builtin({ + name = "ruff", + meta = { + url = "https://github.com/charliermarsh/ruff/", + description = "An extremely fast Python linter, written in Rust.", + }, + method = DIAGNOSTICS, + filetypes = { "python" }, + generator_opts = { + command = "ruff", + args = { "check", "-n", "-e", "--stdin-filename", "$FILENAME", "-" }, + format = "line", + check_exit_code = function(code) + return code == 0 + end, + to_stdin = true, + ignore_stderr = true, + on_output = h.diagnostics.from_pattern( + [[(%d+):(%d+): ((%u)%w+) (.*)]], + { "row", "col", "code", "severity", "message" }, + { + adapters = { + custom_end_col, + }, + severities = { + E = h.diagnostics.severities["error"], -- pycodestyle errors + W = h.diagnostics.severities["warning"], -- pycodestyle warnings + F = h.diagnostics.severities["information"], -- pyflakes + A = h.diagnostics.severities["information"], -- flake8-builtins + B = h.diagnostics.severities["warning"], -- flake8-bugbear + C = h.diagnostics.severities["warning"], -- flake8-comprehensions + T = h.diagnostics.severities["information"], -- flake8-print + U = h.diagnostics.severities["information"], -- pyupgrade + D = h.diagnostics.severities["information"], -- pydocstyle + M = h.diagnostics.severities["information"], -- Meta + }, + } + ), + }, + factory = h.generator_factory, + }) + end, + + shellcheck = function() + return h.make_builtin({ + name = "shellcheck", + meta = { + url = "https://www.shellcheck.net/", + description = "A shell script static analysis tool.", + }, + method = DIAGNOSTICS, + filetypes = { "sh" }, + generator_opts = { + command = "shellcheck", + args = { "--format", "json1", "--source-path=$DIRNAME", "--external-sources", "-" }, + to_stdin = true, + format = "json", + check_exit_code = function(code) + return code <= 1 + end, + on_output = function(params) + local parser = h.diagnostics.from_json({ + attributes = { code = "code" }, + severities = { + info = h.diagnostics.severities["information"], + style = h.diagnostics.severities["hint"], + }, + }) + + return parser({ output = params.output.comments }) + end, + }, + factory = h.generator_factory, + }) + end, +} + +M.formatters = { + templ = function() + return h.make_builtin({ + name = "templ", + method = FORMATTING, + filetypes = { "templ" }, + generator_opts = { + command = "templ", + args = { "fmt" }, + to_stdin = true, + }, + factory = h.formatter_factory, + }) + end, + ruff_code = function() + return h.make_builtin({ + name = "ruff", + method = FORMATTING, + filetypes = { "python" }, + generator_opts = { + command = "ruff", + args = { "format", "--stdin-filename", "$FILENAME", "-" }, + to_stdin = true, + }, + factory = h.formatter_factory, + }) + end, + ruff_imports = function() + return h.make_builtin({ + name = "ruff", + method = FORMATTING, + filetypes = { "python" }, + generator_opts = { + command = "ruff", + args = { "check", "--select", "I", "--fix", "--stdin-filename", "$FILENAME", "-" }, + to_stdin = true, + }, + factory = h.formatter_factory, + }) + end, +} + +return M