From 623758e6be08da61291672de1ea660d700f66a0a Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Sun, 23 Jul 2023 22:40:54 +0200 Subject: [PATCH] nvim: Update formatter --- home/config/nvim/nifoc/formatting.fnl | 33 +++++-- home/config/nvim/nifoc/keymap.fnl | 1 + home/config/nvim/plugins/formatter.fnl | 122 +++++++++++++------------ home/programs/nvim/default.nix | 5 +- home/programs/nvim/plugins.nix | 31 +++++-- home/programs/nvim/plugins.yaml | 3 +- 6 files changed, 116 insertions(+), 79 deletions(-) diff --git a/home/config/nvim/nifoc/formatting.fnl b/home/config/nvim/nifoc/formatting.fnl index 08f1489..73f7eca 100644 --- a/home/config/nvim/nifoc/formatting.fnl +++ b/home/config/nvim/nifoc/formatting.fnl @@ -1,22 +1,31 @@ (let [mod {} b vim.b api vim.api - set-bufvar vim.api.nvim_buf_set_var] + set-bufvar vim.api.nvim_buf_set_var + format (require :format)] (fn mod.setup [] - (let [usercmd vim.api.nvim_create_user_command] + (let [usercmd vim.api.nvim_create_user_command + augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true}) + aucmd vim.api.nvim_create_autocmd] (usercmd :FormatDisableBuffer mod.disable-for-buffer {:desc "Disable Formatting for the current buffer"}) (usercmd :FormatEnableBuffer mod.enable-for-buffer - {:desc "Enable Formatting for the current buffer"}))) - - (fn notify [msg] - (vim.notify msg vim.log.levels.INFO {:title :Formatter})) + {:desc "Enable Formatting for the current buffer"}) + ;; (aucmd :BufWritePre + ;; {:callback mod.maybe-format-buffer + ;; :group augroup + ;; :desc "Run Formatter before saving"}) + )) (fn has-formatter-config? [ft] - (let [cfg (require :formatter.config) - fts (cfg.get :filetype)] + (let [cfg (. (require :format.static) :config) + fts (. cfg :filetypes)] (not= (. fts ft) nil))) + (fn format-with-lsp? [ft] + (let [fts [:elixir]] + (vim.list_contains fts ft))) + (fn mod.enable-for-buffer [] (set-bufvar 0 :nifoc_formatter_disabled 0)) @@ -32,5 +41,11 @@ (when (client.supports_method :textDocument/rangeFormatting) (api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))) - mod) + (fn mod.maybe-format-buffer [] + (let [ft vim.bo.filetype] + (if (= b.nifoc_formatter_disabled 1) nil + (format-with-lsp? ft) (vim.lsp.buf.format) + (has-formatter-config? ft) (format.format) + nil))) + mod) diff --git a/home/config/nvim/nifoc/keymap.fnl b/home/config/nvim/nifoc/keymap.fnl index 284d5c0..e9aaa1a 100644 --- a/home/config/nvim/nifoc/keymap.fnl +++ b/home/config/nvim/nifoc/keymap.fnl @@ -61,6 +61,7 @@ ;; 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 index 440638b..e0bda33 100644 --- a/home/config/nvim/plugins/formatter.fnl +++ b/home/config/nvim/plugins/formatter.fnl @@ -1,72 +1,78 @@ (let [M {} - formatter (require :formatter) - api vim.api + format (require :format) + format-core (require :core) toml (require :toml) treefmt-config-file vim.env.TREEFMT_CONFIG_FILE - treefmt-formatters {}] + treefmt-exts []] (fn read-file [file] (with-open [f (io.open file :rb)] (f:read :*all))) - (fn add-option [opts addable] - (table.insert opts addable) - opts) - - (fn remove-option [opts removeable] - (let [new-opts []] - (each [_ v (ipairs opts)] - (when (not= v removeable) (table.insert new-opts v))) - new-opts)) - (when (not= treefmt-config-file nil) (let [cfg (toml.parse (read-file treefmt-config-file))] - (when (not= cfg.formatter.fnlfmt nil) - (set treefmt-formatters.fnlfmt - {:exe cfg.formatter.fnlfmt.command - :args (-> cfg.formatter.fnlfmt.options (remove-option :--fix) - (add-option "-"))})) - (when (not= cfg.formatter.shfmt nil) - (set treefmt-formatters.shfmt - {:exe cfg.formatter.shfmt.command - :args (remove-option cfg.formatter.shfmt.options :-w)})) - (when (not= cfg.formatter.yamlfmt nil) - (set treefmt-formatters.yamlfmt - {:exe cfg.formatter.yamlfmt.command - :args (add-option cfg.formatter.yamlfmt.options :-in)})))) + (each [_ opts (pairs cfg.formatter)] + (vim.list_extend treefmt-exts opts.includes)))) - (fn buffer-filename [] (api.nvim_buf_get_name 0)) + (fn treefmt-or-fallback [file-path fallback] + (let [ext (format-core.file.extension file-path) + ext-glob (.. "*." ext)] + (if (vim.list_contains treefmt-exts ext-glob) + [{:cmd :treefmt + :args [file-path] + :ignore_err (fn [err data] + (and (= err nil) (not (string.find data :Error))))}] + (= (vim.fn.executable fallback.cmd) 1) + [fallback] + []))) - (fn args-prettier [parser] - {:args [:--stdin-filepath (buffer-filename) :--parser parser]}) + (fn formatter-prettier [file-path] + (treefmt-or-fallback file-path {:cmd :prettier :args [:--write file-path]})) - (fn args-shfmt [] - (let [shiftwidth (vim.opt.shiftwidth:get)] - {:args [:-i shiftwidth]})) + (fn M.treefmt-extensions [] treefmt-exts) - (fn do-format [exe opts] - (let [treefmt-formatter (. treefmt-formatters exe)] - (if (not= treefmt-formatter nil) - treefmt-formatter - (if (= (vim.fn.executable exe) 1) - (vim.tbl_extend :keep opts {: exe}) - {:exe :ls :cond #false})))) - - (fn M.treefmt-based-formatters [] treefmt-formatters) - - (formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1) - :filetype {:css #(do-format :prettier (args-prettier :css)) - :fennel #(do-format :fnlfmt {:args ["-"]}) - :fish #(do-format :fish_indent {}) - :html #(do-format :prettier - (args-prettier :html)) - :javascript #(do-format :prettier - (args-prettier :javascript)) - :json #(do-format :prettier - (args-prettier :json)) - :sh #(do-format :shfmt (args-shfmt)) - :toml #(do-format :taplo {:args [:fmt "-"]}) - :typescript #(do-format :prettier - (args-prettier :typescript)) - :yaml #(do-format :yamlfmt {:args [:-in]})} - :lsp [:elixirls :nil_ls]}) + (format.setup {:filetypes {:css formatter-prettier + :fennel (fn [file-path] + (treefmt-or-fallback file-path + {:cmd :fnlfmt + :args [:--fix + file-path]})) + :fish (fn [file-path] + {:cmd :fish_indent + :args [:--write file-path]}) + :html formatter-prettier + :javascript formatter-prettier + :json formatter-prettier + :nix (fn [file-path] + (treefmt-or-fallback file-path + {:cmd :nixpkgs-fmt + :args [file-path]})) + :sh (fn [file-path] + (treefmt-or-fallback file-path + {:cmd :shfmt + :args [:-i + :2 + :-s + :-w + file-path]})) + :typescript formatter-prettier + :yaml (fn [file-path] + (treefmt-or-fallback file-path + {:cmd :yamlfmt + :args [file-path]}))}}) + ;; (formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1) + ;; :filetype {:css #(do-format :prettier (args-prettier :css)) + ;; :fennel #(do-format :fnlfmt {:args ["-"]}) + ;; :fish #(do-format :fish_indent {}) + ;; :html #(do-format :prettier + ;; (args-prettier :html)) + ;; :javascript #(do-format :prettier + ;; (args-prettier :javascript)) + ;; :json #(do-format :prettier + ;; (args-prettier :json)) + ;; :sh #(do-format :shfmt (args-shfmt)) + ;; :toml #(do-format :taplo {:args [:fmt "-"]}) + ;; :typescript #(do-format :prettier + ;; (args-prettier :typescript)) + ;; :yaml #(do-format :yamlfmt {:args [:-in]})} + ;; :lsp [:elixirls :nil_ls]}) M) diff --git a/home/programs/nvim/default.nix b/home/programs/nvim/default.nix index f395a22..9842ddf 100644 --- a/home/programs/nvim/default.nix +++ b/home/programs/nvim/default.nix @@ -252,8 +252,11 @@ in cmp-nvim-lsp-document-symbol # Formatting + + core-nvim + { - plugin = nvim-formatter; + plugin = format-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 1e8551e..f318167 100644 --- a/home/programs/nvim/plugins.nix +++ b/home/programs/nvim/plugins.nix @@ -427,14 +427,25 @@ in fetchSubmodules = false; }; }; - nvim-formatter = buildVimPluginFrom2Nix { - pname = "nvim-formatter"; - version = "2023-07-10"; + core-nvim = buildVimPluginFrom2Nix { + pname = "core.nvim"; + version = "2023-07-18"; src = fetchFromGitHub { - owner = "seblj"; - repo = "nvim-formatter"; - rev = "a77c61eb3967668839a0ae91f4f8f43938bfae82"; - sha256 = "1d5q3xg7bf10xfipml42ji9pwp0cl9rnjs12xqdjac8hm6sq469a"; + owner = "niuiic"; + repo = "core.nvim"; + rev = "d0843388db6a6747ec1a1c2aea873da0efca2cac"; + sha256 = "0b47bsbwd1f0635r6jzsmp2d449cxgir93p50hbqlzn25kfvw43q"; + fetchSubmodules = false; + }; + }; + format-nvim = buildVimPluginFrom2Nix { + pname = "format.nvim"; + version = "2023-07-20"; + src = fetchFromGitHub { + owner = "niuiic"; + repo = "format.nvim"; + rev = "d06a60bcc2b33aace2d448279d763ed559960925"; + sha256 = "1vf28f3pxif97gsx6f6p7nlfx918jlf2hrl9as8n4szs6mm0lhig"; fetchSubmodules = false; }; }; @@ -473,12 +484,12 @@ in }; nvim-treesitter-textobjects = buildVimPluginFrom2Nix { pname = "nvim-treesitter-textobjects"; - version = "2023-06-26"; + version = "2023-07-23"; src = fetchFromGitHub { owner = "nvim-treesitter"; repo = "nvim-treesitter-textobjects"; - rev = "52f1f3280d9092bfaee5c45be5962fabee3d9654"; - sha256 = "1k0065mn4hb3ama3qxrln24rf7cqziysddvw4anxws85dan5x9sj"; + rev = "ef32a5c24b767d165ed63fd2b24ac8dc52742521"; + sha256 = "1jrg79hliagz408200vl4926a61c462lz5rv59xjfp70x5pbdjjd"; fetchSubmodules = false; }; }; diff --git a/home/programs/nvim/plugins.yaml b/home/programs/nvim/plugins.yaml index c3c849c..5898115 100644 --- a/home/programs/nvim/plugins.yaml +++ b/home/programs/nvim/plugins.yaml @@ -49,7 +49,8 @@ - src: hrsh7th/cmp-cmdline - src: hrsh7th/cmp-nvim-lsp-document-symbol # Formatting -- src: seblj/nvim-formatter +- src: niuiic/core.nvim +- src: niuiic/format.nvim # Pairs - src: windwp/nvim-autopairs - src: windwp/nvim-ts-autotag