From 45462f676e5edf4776dbd6ee579cfb8e26c2716b Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Wed, 12 Jul 2023 20:39:50 +0200 Subject: [PATCH] nvim: Move to formatter.nvim and use treefmt --- flake.lock | 12 ++--- home/config/nvim/after/ftplugin/fennel.fnl | 2 - home/config/nvim/after/ftplugin/fish.fnl | 3 +- .../config/nvim/after/ftplugin/javascript.fnl | 5 -- home/config/nvim/after/ftplugin/sh.fnl | 3 -- home/config/nvim/nifoc/formatting.fnl | 50 ++++++++++++------- home/config/nvim/nifoc/keymap.fnl | 1 - home/config/nvim/plugins/formatter.fnl | 24 +++++++++ home/config/nvim/plugins/neoformat.fnl | 3 -- home/programs/nvim/default.nix | 4 +- home/programs/nvim/plugins.nix | 26 +++++----- home/programs/nvim/plugins.yaml | 2 +- 12 files changed, 79 insertions(+), 56 deletions(-) delete mode 100644 home/config/nvim/after/ftplugin/fennel.fnl delete mode 100644 home/config/nvim/after/ftplugin/javascript.fnl delete mode 100644 home/config/nvim/after/ftplugin/sh.fnl create mode 100644 home/config/nvim/plugins/formatter.fnl delete mode 100644 home/config/nvim/plugins/neoformat.fnl diff --git a/flake.lock b/flake.lock index 8559ff0..d3ef909 100644 --- a/flake.lock +++ b/flake.lock @@ -110,11 +110,11 @@ ] }, "locked": { - "lastModified": 1689116343, - "narHash": "sha256-eaYfwQTSEbuB7rs5/W227SbVeDP9cbcoT1TEbnmOgOk=", + "lastModified": 1689178160, + "narHash": "sha256-TVR0hn/JWo1qmtvgzqjfNerLPsIxfdFR3hf3QrnEj7U=", "owner": "lnl7", "repo": "nix-darwin", - "rev": "eb22022ba8faeeb7a9be8afe925511b88ad12ca5", + "rev": "795492c9a895762f36f6c1ff43d6e0de66fdffa8", "type": "github" }, "original": { @@ -377,11 +377,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1689078114, - "narHash": "sha256-osG8BrX5RpKJ7wH+vI6auOU+ctvNOblT4XXCgknK47c=", + "lastModified": 1689138773, + "narHash": "sha256-NEcJPQfwc1jNDI/ITvieJW2Y+IWdGIYCFHLJl5PGx3o=", "owner": "nixos", "repo": "nixpkgs", - "rev": "b6cc7ff8fee93789bc871a267ab876c3fca042cb", + "rev": "bf57c599729771cd23054a18c0f3a391ae85e193", "type": "github" }, "original": { diff --git a/home/config/nvim/after/ftplugin/fennel.fnl b/home/config/nvim/after/ftplugin/fennel.fnl deleted file mode 100644 index 90d9109..0000000 --- a/home/config/nvim/after/ftplugin/fennel.fnl +++ /dev/null @@ -1,2 +0,0 @@ -(let [lo vim.opt_local] - (set lo.formatprg "fnlfmt -")) diff --git a/home/config/nvim/after/ftplugin/fish.fnl b/home/config/nvim/after/ftplugin/fish.fnl index 41c9be5..2e7c9e6 100644 --- a/home/config/nvim/after/ftplugin/fish.fnl +++ b/home/config/nvim/after/ftplugin/fish.fnl @@ -1,6 +1,5 @@ (let [lo vim.opt_local] (set lo.tabstop 4) (set lo.shiftwidth 4) - (set lo.softtabstop 4) - (set lo.formatprg :fish_indent)) + (set lo.softtabstop 4)) diff --git a/home/config/nvim/after/ftplugin/javascript.fnl b/home/config/nvim/after/ftplugin/javascript.fnl deleted file mode 100644 index 6be1217..0000000 --- a/home/config/nvim/after/ftplugin/javascript.fnl +++ /dev/null @@ -1,5 +0,0 @@ -(let [lo vim.opt_local - b vim.b] - (set lo.formatprg "eslint_d --stdin --fix-to-stdout") - (set b.nifoc_formatter_force_formatprg 1)) - diff --git a/home/config/nvim/after/ftplugin/sh.fnl b/home/config/nvim/after/ftplugin/sh.fnl deleted file mode 100644 index d6d18ae..0000000 --- a/home/config/nvim/after/ftplugin/sh.fnl +++ /dev/null @@ -1,3 +0,0 @@ -(let [lo vim.opt_local] - (set lo.formatprg "shfmt -i 2")) - diff --git a/home/config/nvim/nifoc/formatting.fnl b/home/config/nvim/nifoc/formatting.fnl index ea19368..f378e67 100644 --- a/home/config/nvim/nifoc/formatting.fnl +++ b/home/config/nvim/nifoc/formatting.fnl @@ -2,7 +2,8 @@ b vim.b cmd vim.cmd api vim.api - set-bufvar vim.api.nvim_buf_set_var] + set-bufvar vim.api.nvim_buf_set_var + treefmt-exists (vim.fn.executable :treefmt)] (fn mod.setup [] (let [usercmd vim.api.nvim_create_user_command augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true}) @@ -11,14 +12,21 @@ {:desc "Disable Formatting for the current buffer"}) (usercmd :FormatEnableBuffer mod.enable-for-buffer {:desc "Enable Formatting for the current buffer"}) - (aucmd :BufWritePre {:callback mod.maybe-format-buffer - :group augroup - :desc "Run Formatter"}))) + (aucmd :BufWritePre + {:callback mod.maybe-format-buffer-pre + :group augroup + :desc "Run Formatter before saving"}) + (aucmd :BufWritePost + {:callback mod.maybe-format-buffer-post + :group augroup + :desc "Run Formatter after saving"}))) - (fn run-neoformat [formatprg] - (let [neoformat (.. "Neoformat " formatprg)] - (cmd (.. "try | undojoin | " neoformat " | catch /E790/ | " neoformat - " | endtry")))) + (fn has-formatter-config? [ft] + (let [formatter-fts (vim.tbl_keys (. (require :formatter.config) :values + :filetype))] + (vim.tbl_contains formatter-fts ft))) + + (fn run-formatter-exe [] (cmd :FormatWriteLock)) (fn run-lsp-format [] (if (not= b.nifoc_formatter_filter_lsp_client nil) @@ -34,10 +42,12 @@ (set-bufvar 0 :nifoc_formatter_disabled 1)) (fn mod.active? [] - (if (= b.nifoc_formatter_disabled 1) false - (= b.nifoc_lsp_formatter_enabled 1) true - (not= (vim.opt_local.formatprg:get) "") true - false)) + (let [ft vim.bo.filetype] + (if (= b.nifoc_formatter_disabled 1) false + (= treefmt-exists 1) true + (= b.nifoc_lsp_formatter_enabled 1) true + (has-formatter-config? ft) true + false))) (fn mod.maybe-enable-lsp [client bufnr] (when (client.supports_method :textDocument/rangeFormatting) @@ -45,13 +55,17 @@ (when (client.supports_method :textDocument/formatting) (set-bufvar bufnr :nifoc_lsp_formatter_enabled 1))) - (fn mod.maybe-format-buffer [] - (let [formatprg (vim.opt_local.formatprg:get) - formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))] + (fn mod.maybe-format-buffer-pre [] + (if (= b.nifoc_formatter_disabled 1) nil + (= treefmt-exists 1) nil + (= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format))) + + (fn mod.maybe-format-buffer-post [] + (let [ft vim.bo.filetype] (if (= b.nifoc_formatter_disabled 1) nil - (= b.nifoc_formatter_force_formatprg 1) (run-neoformat formatprg-exe) - (= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format) - (not= formatprg-exe nil) (run-neoformat formatprg-exe)))) + (= treefmt-exists 1) (run-formatter-exe) + (= b.nifoc_lsp_formatter_enabled 1) nil + (has-formatter-config? ft) (run-formatter-exe)))) mod) diff --git a/home/config/nvim/nifoc/keymap.fnl b/home/config/nvim/nifoc/keymap.fnl index e9aaa1a..284d5c0 100644 --- a/home/config/nvim/nifoc/keymap.fnl +++ b/home/config/nvim/nifoc/keymap.fnl @@ -61,7 +61,6 @@ ;; Other Mappings (keymap.set :n : ":nohlsearch" {:silent true}) (keymap.set :i : npairs.autopairs_cr {:expr true :silent true}) - (keymap.set :n :F formatting.maybe-format-buffer {:desc "Format Buffer"}) (keymap.set :n : :b) (keymap.set :n : :w) (keymap.set :n : "^") diff --git a/home/config/nvim/plugins/formatter.fnl b/home/config/nvim/plugins/formatter.fnl new file mode 100644 index 0000000..861a8b2 --- /dev/null +++ b/home/config/nvim/plugins/formatter.fnl @@ -0,0 +1,24 @@ +(let [formatter (require :formatter) + formatter-utils (require :formatter.util) + treefmt-exists (vim.fn.executable :treefmt) + treefmt-formatter #{:exe :treefmt + :stdin false + :tempfile_prefix :treefmt + :tempfile_postfix (.. "." + (formatter-utils.get_current_buffer_file_extension))}] + (fn builtin-formatter [ft formatter] + (let [file (.. :formatter.filetypes. ft)] + (. (require file) formatter))) + + (fn prefer-treefmt [ft formatter] + (if (= treefmt-exists 1) + (treefmt-formatter) + ((builtin-formatter ft formatter)))) + + (formatter.setup {:logging true + :log_level vim.log.levels.WARN + :filetype {:fennel [#{:exe :fnlfmt :args ["-"] :stdin true}] + :fish [(builtin-formatter :fish :fishindent)] + :sh [(prefer-treefmt :sh :shfmt)] + :typescript [treefmt-formatter]}})) + diff --git a/home/config/nvim/plugins/neoformat.fnl b/home/config/nvim/plugins/neoformat.fnl deleted file mode 100644 index efc5298..0000000 --- a/home/config/nvim/plugins/neoformat.fnl +++ /dev/null @@ -1,3 +0,0 @@ -(let [g vim.g] - (set g.neoformat_try_formatprg 1) - (set g.neoformat_basic_format_trim 1)) diff --git a/home/programs/nvim/default.nix b/home/programs/nvim/default.nix index 3e5feb2..a633ae6 100644 --- a/home/programs/nvim/default.nix +++ b/home/programs/nvim/default.nix @@ -247,8 +247,8 @@ in # Formatting { - plugin = neoformat; - config = builtins.readFile ../../config/nvim/plugins/neoformat.fnl; + plugin = formatter-nvim; + config = builtins.readFile ../../config/nvim/plugins/formatter.fnl; type = "fennel"; } diff --git a/home/programs/nvim/plugins.nix b/home/programs/nvim/plugins.nix index e5a7058..bbf1e6a 100644 --- a/home/programs/nvim/plugins.nix +++ b/home/programs/nvim/plugins.nix @@ -107,12 +107,12 @@ in }; nvim-treesitter = buildVimPluginFrom2Nix { pname = "nvim-treesitter"; - version = "2023-07-11"; + version = "2023-07-12"; src = fetchFromGitHub { owner = "nvim-treesitter"; repo = "nvim-treesitter"; - rev = "b4b94ec387525cb9df4b56eb1a8bf33231a0df47"; - sha256 = "0z3nqqwvnfvi6nqjl9cjpik7cw3xf8armmgn627v9l8jqgz3a6lv"; + rev = "575f5a4b1fcb60c1ac4f560c46a5171454d674ca"; + sha256 = "0i9rn6gz09rk49gpq305p740xza4mgpb3nwmi2zm4rg9psxamcsr"; fetchSubmodules = false; }; }; @@ -416,14 +416,14 @@ in fetchSubmodules = false; }; }; - neoformat = buildVimPluginFrom2Nix { - pname = "neoformat"; - version = "2023-07-01"; + formatter-nvim = buildVimPluginFrom2Nix { + pname = "formatter.nvim"; + version = "2023-04-30"; src = fetchFromGitHub { - owner = "sbdchd"; - repo = "neoformat"; - rev = "b35ae45f9425c817126be4cc946a950c1cffe6fa"; - sha256 = "1qcrladfl3zvci58aiv4w6bq6dc8ibfjgp95l4q10bx8gjkdipkq"; + owner = "mhartington"; + repo = "formatter.nvim"; + rev = "fa4f2729cc2909db599169f22d8e55632d4c8d59"; + sha256 = "0hv79gdg7cgqr3a8vw1ysc48f6i3b8xabbglxspm5mbpf22c8xbk"; fetchSubmodules = false; }; }; @@ -451,12 +451,12 @@ in }; vim-matchup = buildVimPluginFrom2Nix { pname = "vim-matchup"; - version = "2023-07-11"; + version = "2023-07-12"; src = fetchFromGitHub { owner = "andymass"; repo = "vim-matchup"; - rev = "3c4ccc489002fe72b5d2e2ffc4b3b5a7d3ab65d8"; - sha256 = "0mpvqlk8a7vms2j29mpfpkqa1pxsncqr7flsg6q6z3cdqcrgh1j2"; + rev = "17cc05867cb3314761e4baa20115a27120f8e82c"; + sha256 = "0hxp9akqxz7h2ly1qpiviqbgbk52mbxk88vvq6rxf2kb12sgbiag"; fetchSubmodules = false; }; }; diff --git a/home/programs/nvim/plugins.yaml b/home/programs/nvim/plugins.yaml index 383f30c..08785cd 100644 --- a/home/programs/nvim/plugins.yaml +++ b/home/programs/nvim/plugins.yaml @@ -59,7 +59,7 @@ - src: hrsh7th/cmp-nvim-lsp-document-symbol # Formatting -- src: sbdchd/neoformat +- src: mhartington/formatter.nvim # Pairs - src: windwp/nvim-autopairs