From 6d179c9de8d3964b8d7e0cc09a3ed42c695e3e21 Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Thu, 27 Jul 2023 10:44:24 -0400 Subject: [PATCH] feat(nvim): Add missing formatters/linters to efm Once I get them all working I'll contribute them upstream --- fish/functions/emptytrash.fish | 2 +- home-manager/modules/nvim.nix | 8 ++++--- nvim/lua/efmls-configs/formatters/cbfmt.lua | 9 +++++++ .../efmls-configs/formatters/fish_indent.lua | 6 +++++ nvim/lua/efmls-configs/formatters/gofmt.lua | 6 +++++ .../efmls-configs/formatters/markdownlint.lua | 8 +++++++ nvim/lua/efmls-configs/formatters/nixfmt.lua | 11 +++++++++ nvim/lua/efmls-configs/formatters/rustfmt.lua | 7 ++++++ nvim/lua/efmls-configs/linters/fish.lua | 9 +++++++ nvim/lua/efmls-configs/linters/statix.lua | 15 ++++++++++++ nvim/lua/my/configure/lspconfig.lua | 3 +++ nvim/lua/my/legendary/autocmds.lua | 2 +- nvim/lua/my/legendary/commands.lua | 2 +- nvim/lua/my/lsp/efm.lua | 24 +++++++++++++++++++ nvim/lua/my/lsp/utils/init.lua | 10 +++++++- 15 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 nvim/lua/efmls-configs/formatters/cbfmt.lua create mode 100644 nvim/lua/efmls-configs/formatters/fish_indent.lua create mode 100644 nvim/lua/efmls-configs/formatters/gofmt.lua create mode 100644 nvim/lua/efmls-configs/formatters/markdownlint.lua create mode 100644 nvim/lua/efmls-configs/formatters/nixfmt.lua create mode 100644 nvim/lua/efmls-configs/formatters/rustfmt.lua create mode 100644 nvim/lua/efmls-configs/linters/fish.lua create mode 100644 nvim/lua/efmls-configs/linters/statix.lua diff --git a/fish/functions/emptytrash.fish b/fish/functions/emptytrash.fish index c17373dd..64a86b78 100644 --- a/fish/functions/emptytrash.fish +++ b/fish/functions/emptytrash.fish @@ -7,4 +7,4 @@ function emptytrash sudo rm -rfv ~/.Trash sudo rm -rfv /private/var/log/asl/*.asl sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent' -end + # end diff --git a/home-manager/modules/nvim.nix b/home-manager/modules/nvim.nix index fe25f2af..1150c296 100644 --- a/home-manager/modules/nvim.nix +++ b/home-manager/modules/nvim.nix @@ -1,7 +1,8 @@ { config, pkgs, lib, ... }: let - inherit (pkgs) stdenv; - inherit (stdenv) isLinux; + # inherit (pkgs) stdenv; + # inherit (stdenv) isLinux; + isLinux = pkgs.stdenv.isLinux; in { home.sessionVariables = { MANPAGER = "nvim -c 'Man!' -o -"; @@ -58,7 +59,7 @@ in { viAlias = true; vimAlias = true; vimdiffAlias = true; - withNodeJs = true; + withNodeJs = false; withRuby = false; withPython3 = false; defaultEditor = true; @@ -82,6 +83,7 @@ in { nodePackages_latest.markdownlint-cli # LSP servers + efm-langserver rnix-lsp rust-analyzer taplo diff --git a/nvim/lua/efmls-configs/formatters/cbfmt.lua b/nvim/lua/efmls-configs/formatters/cbfmt.lua new file mode 100644 index 00000000..b9ddab50 --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/cbfmt.lua @@ -0,0 +1,9 @@ +-- TODO this one isn't working +local fs = require('efmls-configs.fs') +local formatter = fs.executable('cbfmt') +local command = string.format('%s --stdin-filepath ${INPUT} --best-effort', formatter) + +return { + formatCommand = command, + formatStdin = true, +} diff --git a/nvim/lua/efmls-configs/formatters/fish_indent.lua b/nvim/lua/efmls-configs/formatters/fish_indent.lua new file mode 100644 index 00000000..d66713f9 --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/fish_indent.lua @@ -0,0 +1,6 @@ +local fs = require('efmls-configs.fs') +local formatter = fs.executable('fish_indent') +return { + formatCommand = formatter, + formatStdin = true, +} diff --git a/nvim/lua/efmls-configs/formatters/gofmt.lua b/nvim/lua/efmls-configs/formatters/gofmt.lua new file mode 100644 index 00000000..2703ea1c --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/gofmt.lua @@ -0,0 +1,6 @@ +local fs = require('efmls-configs.fs') +local formatter = fs.executable('gofmt') +return { + formatCommand = formatter, + formatStdin = true, +} diff --git a/nvim/lua/efmls-configs/formatters/markdownlint.lua b/nvim/lua/efmls-configs/formatters/markdownlint.lua new file mode 100644 index 00000000..ceb35cac --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/markdownlint.lua @@ -0,0 +1,8 @@ +-- TODO file isn't reloaded after formatting +local fs = require('efmls-configs.fs') +local formatter = fs.executable('markdownlint') +local command = string.format('%s --fix ${INPUT}', formatter) +return { + formatCommand = command, + formatStdin = false, +} diff --git a/nvim/lua/efmls-configs/formatters/nixfmt.lua b/nvim/lua/efmls-configs/formatters/nixfmt.lua new file mode 100644 index 00000000..f67b9330 --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/nixfmt.lua @@ -0,0 +1,11 @@ +local fs = require('efmls-configs.fs') +local formatter = fs.executable('nixfmt') +return { + formatCommand = formatter, + formatStdin = true, + rootMarkers = { + 'flake.nix', + 'shell.nix', + 'default.nix', + }, +} diff --git a/nvim/lua/efmls-configs/formatters/rustfmt.lua b/nvim/lua/efmls-configs/formatters/rustfmt.lua new file mode 100644 index 00000000..43bd8a39 --- /dev/null +++ b/nvim/lua/efmls-configs/formatters/rustfmt.lua @@ -0,0 +1,7 @@ +local fs = require('efmls-configs.fs') +local formatter = fs.executable('rustfmt') +local command = string.format('%s --emit=stdout', formatter) +return { + formatCommand = command, + formatStdin = true, +} diff --git a/nvim/lua/efmls-configs/linters/fish.lua b/nvim/lua/efmls-configs/linters/fish.lua new file mode 100644 index 00000000..568358a8 --- /dev/null +++ b/nvim/lua/efmls-configs/linters/fish.lua @@ -0,0 +1,9 @@ +-- TODO this isn't working +local fs = require('efmls-configs.fs') +local linter = fs.executable('fish') +local command = string.format('%s --no-execute ${INPUT}', linter) + +return { + lintCommand = command, + lintFormats = { '%f (line %l): %m' }, +} diff --git a/nvim/lua/efmls-configs/linters/statix.lua b/nvim/lua/efmls-configs/linters/statix.lua new file mode 100644 index 00000000..451d2992 --- /dev/null +++ b/nvim/lua/efmls-configs/linters/statix.lua @@ -0,0 +1,15 @@ +-- TODO this one doesn't work +local fs = require('efmls-configs.fs') +local linter = fs.executable('statix') +local command = string.format('%s check --stdin --format=errfmt', linter) +return { + lintCommand = command, + lintStdin = true, + -- lintIgnoreExitCode = true, + lintFormats = { '%l:%c:%t::%m' }, + rootMarkers = { + 'flake.nix', + 'shell.nix', + 'default.nix', + }, +} diff --git a/nvim/lua/my/configure/lspconfig.lua b/nvim/lua/my/configure/lspconfig.lua index 983263fc..2e275a9e 100644 --- a/nvim/lua/my/configure/lspconfig.lua +++ b/nvim/lua/my/configure/lspconfig.lua @@ -4,6 +4,8 @@ return { 'hrsh7th/cmp-nvim-lsp', { 'creativenull/efmls-configs-nvim', + -- neoconf must be loaded before any LSP + dependencies = { 'folke/neoconf.nvim' }, enabled = false, event = 'BufReadPre', config = function() @@ -12,6 +14,7 @@ return { }, { 'jose-elias-alvarez/null-ls.nvim', + enabled = true, event = 'BufReadPre', opts = function() return require('my.lsp.null-ls') diff --git a/nvim/lua/my/legendary/autocmds.lua b/nvim/lua/my/legendary/autocmds.lua index 4925144a..600e96d7 100644 --- a/nvim/lua/my/legendary/autocmds.lua +++ b/nvim/lua/my/legendary/autocmds.lua @@ -72,7 +72,7 @@ function M.lsp_autocmds(bufnr, server_name) end if - server_name == 'null-ls' + (server_name == 'null-ls' or server_name == 'efm') and #vim.tbl_filter(function(autocmd) return autocmd.buflocal == true and autocmd.buffer == bufnr and autocmd.event == 'BufWritePost' end, autocmds) diff --git a/nvim/lua/my/legendary/commands.lua b/nvim/lua/my/legendary/commands.lua index d7c81797..c882a67a 100644 --- a/nvim/lua/my/legendary/commands.lua +++ b/nvim/lua/my/legendary/commands.lua @@ -90,7 +90,7 @@ function M.lsp_commands(bufnr, server_name) }, } - if server_name == 'null-ls' and not (vim.api.nvim_buf_get_commands(0, {}) or {}).Format then + if (server_name == 'null-ls' or server_name == 'efm') and not (vim.api.nvim_buf_get_commands(0, {}) or {}).Format then commands = TblUtils.join_lists(commands, { { ':Format', diff --git a/nvim/lua/my/lsp/efm.lua b/nvim/lua/my/lsp/efm.lua index 64fb88ab..a9cfabda 100644 --- a/nvim/lua/my/lsp/efm.lua +++ b/nvim/lua/my/lsp/efm.lua @@ -1,5 +1,9 @@ local efmls = require('efmls-configs') +local cbfmt = require('efmls-configs.formatters.cbfmt') +cbfmt.formatCommand = + string.format('%s --config %s', cbfmt.formatCommand, string.format('%s/.config/cbfmt.toml', vim.env.HOME)) + local node_config = { linter = require('efmls-configs.linters.eslint_d'), -- switch to prettier_d once I can figure out how to install it with Nix @@ -30,4 +34,24 @@ efmls.setup({ linter = require('efmls-configs.linters.luacheck'), formatter = require('efmls-configs.formatters.stylua'), }, + nix = { + linter = require('efmls-configs.linters.statix'), + formatter = require('efmls-configs.formatters.nixfmt'), + }, + markdown = { + formatter = { + require('efmls-configs.formatters.markdownlint'), + cbfmt, + }, + }, + fish = { + formatter = require('efmls-configs.formatters.fish_indent'), + linter = require('efmls-configs.linters.fish'), + }, + go = { + formatter = require('efmls-configs.formatters.gofmt'), + }, + rust = { + formatter = require('efmls-configs.formatters.rustfmt'), + }, }) diff --git a/nvim/lua/my/lsp/utils/init.lua b/nvim/lua/my/lsp/utils/init.lua index 0f4925af..8e09c065 100644 --- a/nvim/lua/my/lsp/utils/init.lua +++ b/nvim/lua/my/lsp/utils/init.lua @@ -17,7 +17,7 @@ function M.on_attach(client) end -- Disable formatting with other LSPs because we're handling formatting via null-ls - if client.name ~= 'null-ls' then + if client.name ~= 'null-ls' and client.name ~= 'efm' then client.server_capabilities.documentFormattingProvider = false end end @@ -26,6 +26,14 @@ function M.setup_async_formatting() -- format on save asynchronously, see M.format_document vim.lsp.handlers['textDocument/formatting'] = function(err, result, ctx) if err ~= nil then + -- efm uses table messages + if type(err) == 'table' then + if err.message then + err = err.message + else + err = vim.inspect(err) + end + end vim.api.nvim_err_write(err) return end