1
0
Fork 0

nvim: Update formatter

This commit is contained in:
Daniel Kempkens 2023-07-23 22:40:54 +02:00
parent 97a7e45428
commit 623758e6be
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
6 changed files with 116 additions and 79 deletions

View file

@ -1,22 +1,31 @@
(let [mod {} (let [mod {}
b vim.b b vim.b
api vim.api 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 [] (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 (usercmd :FormatDisableBuffer mod.disable-for-buffer
{:desc "Disable Formatting for the current buffer"}) {:desc "Disable Formatting for the current buffer"})
(usercmd :FormatEnableBuffer mod.enable-for-buffer (usercmd :FormatEnableBuffer mod.enable-for-buffer
{:desc "Enable Formatting for the current buffer"}))) {:desc "Enable Formatting for the current buffer"})
;; (aucmd :BufWritePre
(fn notify [msg] ;; {:callback mod.maybe-format-buffer
(vim.notify msg vim.log.levels.INFO {:title :Formatter})) ;; :group augroup
;; :desc "Run Formatter before saving"})
))
(fn has-formatter-config? [ft] (fn has-formatter-config? [ft]
(let [cfg (require :formatter.config) (let [cfg (. (require :format.static) :config)
fts (cfg.get :filetype)] fts (. cfg :filetypes)]
(not= (. fts ft) nil))) (not= (. fts ft) nil)))
(fn format-with-lsp? [ft]
(let [fts [:elixir]]
(vim.list_contains fts ft)))
(fn mod.enable-for-buffer [] (fn mod.enable-for-buffer []
(set-bufvar 0 :nifoc_formatter_disabled 0)) (set-bufvar 0 :nifoc_formatter_disabled 0))
@ -32,5 +41,11 @@
(when (client.supports_method :textDocument/rangeFormatting) (when (client.supports_method :textDocument/rangeFormatting)
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()"))) (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)

View file

@ -61,6 +61,7 @@
;; Other Mappings ;; Other Mappings
(keymap.set :n :<CR> ":nohlsearch<CR><CR>" {:silent true}) (keymap.set :n :<CR> ":nohlsearch<CR><CR>" {:silent true})
(keymap.set :i :<CR> npairs.autopairs_cr {:expr true :silent true}) (keymap.set :i :<CR> npairs.autopairs_cr {:expr true :silent true})
(keymap.set :n :F formatting.maybe-format-buffer {:desc "Format Buffer"})
(keymap.set :n :<A-Left> :b) (keymap.set :n :<A-Left> :b)
(keymap.set :n :<A-Right> :w) (keymap.set :n :<A-Right> :w)
(keymap.set :n :<S-Left> "^") (keymap.set :n :<S-Left> "^")

View file

@ -1,72 +1,78 @@
(let [M {} (let [M {}
formatter (require :formatter) format (require :format)
api vim.api format-core (require :core)
toml (require :toml) toml (require :toml)
treefmt-config-file vim.env.TREEFMT_CONFIG_FILE treefmt-config-file vim.env.TREEFMT_CONFIG_FILE
treefmt-formatters {}] treefmt-exts []]
(fn read-file [file] (fn read-file [file]
(with-open [f (io.open file :rb)] (with-open [f (io.open file :rb)]
(f:read :*all))) (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) (when (not= treefmt-config-file nil)
(let [cfg (toml.parse (read-file treefmt-config-file))] (let [cfg (toml.parse (read-file treefmt-config-file))]
(when (not= cfg.formatter.fnlfmt nil) (each [_ opts (pairs cfg.formatter)]
(set treefmt-formatters.fnlfmt (vim.list_extend treefmt-exts opts.includes))))
{: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)}))))
(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] (fn formatter-prettier [file-path]
{:args [:--stdin-filepath (buffer-filename) :--parser parser]}) (treefmt-or-fallback file-path {:cmd :prettier :args [:--write file-path]}))
(fn args-shfmt [] (fn M.treefmt-extensions [] treefmt-exts)
(let [shiftwidth (vim.opt.shiftwidth:get)]
{:args [:-i shiftwidth]}))
(fn do-format [exe opts] (format.setup {:filetypes {:css formatter-prettier
(let [treefmt-formatter (. treefmt-formatters exe)] :fennel (fn [file-path]
(if (not= treefmt-formatter nil) (treefmt-or-fallback file-path
treefmt-formatter {:cmd :fnlfmt
(if (= (vim.fn.executable exe) 1) :args [:--fix
(vim.tbl_extend :keep opts {: exe}) file-path]}))
{:exe :ls :cond #false})))) :fish (fn [file-path]
{:cmd :fish_indent
(fn M.treefmt-based-formatters [] treefmt-formatters) :args [:--write file-path]})
:html formatter-prettier
(formatter.setup {:format_on_save #(not= vim.b.nifoc_formatter_disabled 1) :javascript formatter-prettier
:filetype {:css #(do-format :prettier (args-prettier :css)) :json formatter-prettier
:fennel #(do-format :fnlfmt {:args ["-"]}) :nix (fn [file-path]
:fish #(do-format :fish_indent {}) (treefmt-or-fallback file-path
:html #(do-format :prettier {:cmd :nixpkgs-fmt
(args-prettier :html)) :args [file-path]}))
:javascript #(do-format :prettier :sh (fn [file-path]
(args-prettier :javascript)) (treefmt-or-fallback file-path
:json #(do-format :prettier {:cmd :shfmt
(args-prettier :json)) :args [:-i
:sh #(do-format :shfmt (args-shfmt)) :2
:toml #(do-format :taplo {:args [:fmt "-"]}) :-s
:typescript #(do-format :prettier :-w
(args-prettier :typescript)) file-path]}))
:yaml #(do-format :yamlfmt {:args [:-in]})} :typescript formatter-prettier
:lsp [:elixirls :nil_ls]}) :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) M)

View file

@ -252,8 +252,11 @@ in
cmp-nvim-lsp-document-symbol cmp-nvim-lsp-document-symbol
# Formatting # Formatting
core-nvim
{ {
plugin = nvim-formatter; plugin = format-nvim;
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl; config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
type = "fennel"; type = "fennel";
} }

View file

@ -427,14 +427,25 @@ in
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
nvim-formatter = buildVimPluginFrom2Nix { core-nvim = buildVimPluginFrom2Nix {
pname = "nvim-formatter"; pname = "core.nvim";
version = "2023-07-10"; version = "2023-07-18";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "seblj"; owner = "niuiic";
repo = "nvim-formatter"; repo = "core.nvim";
rev = "a77c61eb3967668839a0ae91f4f8f43938bfae82"; rev = "d0843388db6a6747ec1a1c2aea873da0efca2cac";
sha256 = "1d5q3xg7bf10xfipml42ji9pwp0cl9rnjs12xqdjac8hm6sq469a"; 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; fetchSubmodules = false;
}; };
}; };
@ -473,12 +484,12 @@ in
}; };
nvim-treesitter-textobjects = buildVimPluginFrom2Nix { nvim-treesitter-textobjects = buildVimPluginFrom2Nix {
pname = "nvim-treesitter-textobjects"; pname = "nvim-treesitter-textobjects";
version = "2023-06-26"; version = "2023-07-23";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "nvim-treesitter"; owner = "nvim-treesitter";
repo = "nvim-treesitter-textobjects"; repo = "nvim-treesitter-textobjects";
rev = "52f1f3280d9092bfaee5c45be5962fabee3d9654"; rev = "ef32a5c24b767d165ed63fd2b24ac8dc52742521";
sha256 = "1k0065mn4hb3ama3qxrln24rf7cqziysddvw4anxws85dan5x9sj"; sha256 = "1jrg79hliagz408200vl4926a61c462lz5rv59xjfp70x5pbdjjd";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };

View file

@ -49,7 +49,8 @@
- src: hrsh7th/cmp-cmdline - src: hrsh7th/cmp-cmdline
- src: hrsh7th/cmp-nvim-lsp-document-symbol - src: hrsh7th/cmp-nvim-lsp-document-symbol
# Formatting # Formatting
- src: seblj/nvim-formatter - src: niuiic/core.nvim
- src: niuiic/format.nvim
# Pairs # Pairs
- src: windwp/nvim-autopairs - src: windwp/nvim-autopairs
- src: windwp/nvim-ts-autotag - src: windwp/nvim-ts-autotag