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 {}
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)

View file

@ -61,6 +61,7 @@
;; Other Mappings
(keymap.set :n :<CR> ":nohlsearch<CR><CR>" {: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-Right> :w)
(keymap.set :n :<S-Left> "^")

View file

@ -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)

View file

@ -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";
}

View file

@ -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;
};
};

View file

@ -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