From d8ba5e254eaf21e9ee37e65b78b04feb4b446519 Mon Sep 17 00:00:00 2001 From: Will Ruggiano Date: Thu, 25 Jan 2024 10:22:40 -0700 Subject: [PATCH] feat(plug): stevearc/conform.nvim --- flake.nix | 3 + nix/sources.json | 6 ++ .../lua/bombadil/lsp/null-ls/alejandra.lua | 23 ------ .../lua/bombadil/lsp/null-ls/init.lua | 17 ---- .../lua/bombadil/lsp/null-ls/jsonnet.lua | 24 ------ .../bombadil/lua/bombadil/lsp/null-ls/man.lua | 34 -------- .../lua/bombadil/lsp/null-ls/prisma.lua | 23 ------ .../lua/bombadil/lsp/null-ls/statix.lua | 77 ------------------- plugins/conform.lua | 32 ++++++++ plugins/lsp.lua | 32 ++++---- plugins/spec.nix | 24 +++--- 11 files changed, 70 insertions(+), 225 deletions(-) delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/alejandra.lua delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/init.lua delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/jsonnet.lua delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/man.lua delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/prisma.lua delete mode 100644 plugins/bombadil/lua/bombadil/lsp/null-ls/statix.lua create mode 100644 plugins/conform.lua diff --git a/flake.nix b/flake.nix index c545f2d..4db4ed0 100644 --- a/flake.nix +++ b/flake.nix @@ -69,6 +69,9 @@ git commit -am "bump: $1" } ''; + plug-add.exec = '' + niv add git git@github.com:$1 && git add nix/ && git commit -am "feat(plug): $1" + ''; }; }; diff --git a/nix/sources.json b/nix/sources.json index a4e278d..06b7408 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -83,6 +83,12 @@ "rev": "6857f10272c3cfe930cece2afa2406e1385bfef8", "type": "git" }, + "conform.nvim": { + "branch": "master", + "repo": "git@github.com:stevearc/conform.nvim", + "rev": "d99b75b4aedf0e912f41c5740a7267de739cddac", + "type": "git" + }, "cpsm": { "branch": "master", "repo": "git@github.com:nixprime/cpsm", diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/alejandra.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/alejandra.lua deleted file mode 100644 index 6121e57..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/alejandra.lua +++ /dev/null @@ -1,23 +0,0 @@ -local h = require "null-ls.helpers" -local methods = require "null-ls.methods" - -local FORMATTING = methods.internal.FORMATTING - -local sources = {} - -sources.formatting = h.make_builtin { - name = "alejandra", - meta = { - url = "https://github.com/kamadorueda/alejandra", - description = "The Uncompromising Nix Code Formatter", - }, - method = FORMATTING, - filetypes = { "nix" }, - generator_opts = { - command = "alejandra", - to_stdin = true, - }, - factory = h.formatter_factory, -} - -return sources diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/init.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/init.lua deleted file mode 100644 index 63e7c7c..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/init.lua +++ /dev/null @@ -1,17 +0,0 @@ -local custom_sources = { "alejandra", "jsonnet", "man", "prisma", "statix" } - -local sources = { - code_actions = {}, - diagnostics = {}, - formatting = {}, - hover = {}, -} - -for _, name in ipairs(custom_sources) do - local custom_source = require("bombadil.lsp.null-ls." .. name) - for action, source in pairs(custom_source) do - sources[action][name] = source - end -end - -return sources diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/jsonnet.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/jsonnet.lua deleted file mode 100644 index 651ed32..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/jsonnet.lua +++ /dev/null @@ -1,24 +0,0 @@ -local h = require "null-ls.helpers" -local methods = require "null-ls.methods" - -local FORMATTING = methods.internal.FORMATTING - -local sources = {} - -sources.formatting = h.make_builtin { - name = "jsonnetfmt", - meta = { - url = "https://github.com/google/jsonnet", - description = "Jsonnet - The data templating language", - }, - method = FORMATTING, - filetypes = { "jsonnet" }, - generator_opts = { - command = "jsonnetfmt", - args = { "-" }, - to_stdin = true, - }, - factory = h.formatter_factory, -} - -return sources diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/man.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/man.lua deleted file mode 100644 index 416ce52..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/man.lua +++ /dev/null @@ -1,34 +0,0 @@ -local h = require "null-ls.helpers" -local methods = require "null-ls.methods" -local HOVER = methods.internal.HOVER - -local Job = require "plenary.job" - -local sources = {} - -sources.hover = h.make_builtin { - name = "man", - method = HOVER, - filetypes = { "bash", "sh", "zsh" }, - generator = { - fn = function(_, done) - local cword = vim.fn.expand "" - local job = Job:new { - command = "man", - args = { cword }, - on_exit = function(j, return_val) - if return_val ~= 0 then - done(false) - else - done(j:result()) - end - end, - } - - job:sync() - end, - async = true, - }, -} - -return sources diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/prisma.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/prisma.lua deleted file mode 100644 index e0adaa9..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/prisma.lua +++ /dev/null @@ -1,23 +0,0 @@ -local h = require "null-ls.helpers" -local methods = require "null-ls.methods" -local FORMATTING = methods.internal.FORMATTING - -local sources = {} - -sources.formatting = h.make_builtin { - name = "prisma-fmt", - meta = { - url = "https://github.com/prisma/prisma-engines", - description = "Formatter for the prisma filetype.", - }, - method = FORMATTING, - filetypes = { "prisma" }, - generator_opts = { - command = "prisma-fmt", - args = { "format" }, - to_stdin = true, - }, - factory = h.formatter_factory, -} - -return sources diff --git a/plugins/bombadil/lua/bombadil/lsp/null-ls/statix.lua b/plugins/bombadil/lua/bombadil/lsp/null-ls/statix.lua deleted file mode 100644 index 4d68210..0000000 --- a/plugins/bombadil/lua/bombadil/lsp/null-ls/statix.lua +++ /dev/null @@ -1,77 +0,0 @@ -local h = require "null-ls.helpers" -local methods = require "null-ls.methods" -local DIAGNOSTICS = methods.internal.DIAGNOSTICS -local CODE_ACTION = methods.internal.CODE_ACTION - -local sources = {} - -sources.diagnostics = h.make_builtin { - method = DIAGNOSTICS, - filetypes = { "nix" }, - generator_opts = { - command = "statix", - args = { "check", "--format=errfmt", "--", "$FILENAME" }, - format = "line", - to_temp_file = true, - from_stderr = false, - on_output = h.diagnostics.from_pattern( - [[>(%d+):(%d+):(.):(%d+):(.*)]], - { "row", "col", "severity", "code", "message" }, - { - severities = { - E = h.diagnostics.severities["error"], - W = h.diagnostics.severities["warning"], - }, - } - ), - }, - factory = h.generator_factory, -} - -sources.code_actions = h.make_builtin { - name = "statix", - method = CODE_ACTION, - filetypes = { "nix" }, - generator_opts = { - command = "statix", - args = { "check", "--format=json", "--", "$FILENAME" }, - format = "json", - to_temp_file = true, - from_stderr = false, - on_output = function(params) - local actions = {} - for _, r in ipairs(params.output.report) do - for _, d in ipairs(r.diagnostics) do - if d.suggestion ~= vim.NIL then - local from = d.suggestion.at.from - local to = d.suggestion.at.to - if params.row >= from.line and params.row <= to.line then - local mess = "Fix: " .. d.message - local fix = {} - for l in vim.gsplit(d.suggestion.fix, "\n") do - table.insert(fix, l) - end - table.insert(actions, { - title = mess, - action = function() - vim.api.nvim_buf_set_text( - params.bufnr, - from.line - 1, - from.column - 1, - to.line - 1, - to.column - 1, - fix - ) - end, - }) - end - end - end - end - return actions - end, - }, - factory = h.generator_factory, -} - -return sources diff --git a/plugins/conform.lua b/plugins/conform.lua new file mode 100644 index 0000000..4b70411 --- /dev/null +++ b/plugins/conform.lua @@ -0,0 +1,32 @@ +return function() + local js = { { "biome", "prettier" } } + local sh = { "shfmt", "shellcheck", "shellharden" } + + require("conform").setup { + formatters_by_ft = { + bash = sh, + javascript = js, + javascriptreact = js, + lua = { "stylua", "luacheck" }, + markdown = { "prettier", "injected" }, + sh = sh, + sql = { "sqlfluff" }, + typescript = js, + typescriptreact = js, + yaml = { "prettier" }, + }, + formatters = { + biome = { + command = "biome", -- not necessarily from node_modules/.bin + }, + sqlfluff = { + prepend_args = { "--dialect", "postgres" }, + }, + stylua = { + require_cwd = true, -- only when it finds the root marker + }, + }, + } + + vim.o.formatexpr = [[v:lua.require("conform").formatexpr()]] +end diff --git a/plugins/lsp.lua b/plugins/lsp.lua index f6d3133..c2ed8e6 100644 --- a/plugins/lsp.lua +++ b/plugins/lsp.lua @@ -61,15 +61,6 @@ return function() local lsp_codelens = vim.api.nvim_create_augroup("LspCodelens", {}) - local function format() - vim.lsp.buf.format { - async = true, - filter = function(client) - return client.name ~= "tsserver" - end, - } - end - -- Use an on_attach function to only map the following keys -- after the language server attaches to the current buffer local on_attach = function(client, bufnr) @@ -95,7 +86,13 @@ return function() { buffer = bufnr, desc = "Code lens" }, }, ["f"] = { - format, + function() + require("conform").format { + async = true, + bufnr = bufnr, + lsp_fallback = true, + } + end, { buffer = bufnr, desc = "Format" }, }, ["l"] = { @@ -175,7 +172,13 @@ return function() -- { buffer = bufnr, desc = "Code actions" }, -- }, ["f"] = { - format, + function() + require("conform").format { + async = true, + bufnr = bufnr, + lsp_fallback = true, + } + end, { buffer = bufnr, desc = "Format" }, }, } @@ -451,12 +454,10 @@ return function() -- TODO: Move to separate file local null_ls = require "null-ls" - local custom_sources = require "bombadil.lsp.null-ls" null_ls.setup { -- debug = true, on_attach = on_attach, sources = { - custom_sources.formatting.jsonnet, null_ls.builtins.code_actions.gitsigns, null_ls.builtins.code_actions.shellcheck.with { filetypes = { "bash", "sh" } }, null_ls.builtins.code_actions.statix, @@ -466,11 +467,6 @@ return function() null_ls.builtins.diagnostics.shellcheck.with { filetypes = { "bash", "sh" } }, null_ls.builtins.diagnostics.sqlfluff.with { extra_args = { "--dialect", "postgres" } }, null_ls.builtins.diagnostics.statix, - null_ls.builtins.formatting.shellharden.with { filetypes = { "bash", "sh" } }, - null_ls.builtins.formatting.shfmt.with { filetypes = { "bash", "sh" } }, - null_ls.builtins.formatting.sqlfluff.with { extra_args = { "--dialect", "postgres" } }, - null_ls.builtins.formatting.stylua, - null_ls.builtins.hover.dictionary, }, } diff --git a/plugins/spec.nix b/plugins/spec.nix index 89b685d..6929fe4 100644 --- a/plugins/spec.nix +++ b/plugins/spec.nix @@ -100,6 +100,19 @@ in rec { }; }; + conform = { + src = sources."conform.nvim"; + config = ./conform.lua; + paths = with pkgs; [ + nodePackages.prettier # for markdown + shellcheck + shellharden + shfmt + sqlfluff + stylua + ]; + }; + dap = { src = sources.nvim-dap; config = ./dap.lua; @@ -262,7 +275,7 @@ in rec { src = sources.nvim-lspconfig; config = ./lsp.lua; dependencies = { - inherit fun lspkind sg; + inherit conform fun lspkind sg; clangd_extensions = { src = sources."clangd_extensions.nvim"; }; @@ -287,23 +300,16 @@ in rec { }; }; paths = with pkgs; [ - (pkgs.callPackage ../pkgs/languagetool-rs {}) + # (pkgs.callPackage ../pkgs/languagetool-rs {}) # (pkgs.callPackage ../pkgs/sql-language-server {}) actionlint - alejandra - cmake-format cppcheck luajitPackages.luacheck nodePackages.jsonlint - pgformatter - rustfmt shellcheck shellharden - shfmt sqlfluff statix - stylua - yapf ]; }; rust-tools = {