1
0
Fork 0

nvim: Use conform for formatting

This commit is contained in:
Daniel Kempkens 2023-10-09 15:05:57 +02:00
parent bd79289b20
commit f497dba501
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
8 changed files with 85 additions and 131 deletions

View file

@ -109,11 +109,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1696468923, "lastModified": 1696814493,
"narHash": "sha256-qSM7NKgf8LcZ5hjKHZ8ANFI8+LQivvAypbhJHBJmYFM=", "narHash": "sha256-1qArVsJGG2RHbV2iKFpAmM5os3myvwpXMOdFy5nh54M=",
"owner": "nix-community", "owner": "nix-community",
"repo": "disko", "repo": "disko",
"rev": "cde886a1c97ef2399b4f91409db045785020291f", "rev": "32ce057c183506cecb0b84950e4eaf39f37e8c75",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -492,11 +492,11 @@
"nixpkgs-stable": "nixpkgs-stable_2" "nixpkgs-stable": "nixpkgs-stable_2"
}, },
"locked": { "locked": {
"lastModified": 1696773021, "lastModified": 1696846637,
"narHash": "sha256-JzjKl1h96cM5iSrkNvZX7Fgo+e3lisv2tn+uIMnXmD4=", "narHash": "sha256-0hv4kbXxci2+pxhuXlVgftj/Jq79VSmtAyvfabCCtYk=",
"owner": "cachix", "owner": "cachix",
"repo": "pre-commit-hooks.nix", "repo": "pre-commit-hooks.nix",
"rev": "8edf336c5ca85efe20004d076747f91b85a6d087", "rev": "42e1b6095ef80a51f79595d9951eb38e91c4e6ca",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -2,24 +2,22 @@
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)] conform (require :conform)]
(fn mod.setup [] (fn mod.setup []
(let [usercmd vim.api.nvim_create_user_command (let [usercmd api.nvim_create_user_command
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true}) augroup (api.nvim_create_augroup :NifocFormatting {:clear true})
aucmd vim.api.nvim_create_autocmd] aucmd 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 (aucmd :BufWritePre
;; {:callback mod.maybe-format-buffer {:callback mod.maybe-format-buffer
;; :group augroup :group augroup
;; :desc "Run Formatter before saving"}) :desc "Run Formatter before saving"})))
))
(fn has-formatter-config? [ft] (fn has-formatter-config? [ft]
(let [cfg (. (require :format.static) :config) (let [fts conform.formatters_by_ft]
fts (. cfg :filetypes)]
(not= (. fts ft) nil))) (not= (. fts ft) nil)))
(fn format-with-lsp? [ft] (fn format-with-lsp? [ft]
@ -35,17 +33,11 @@
(fn mod.active? [] (fn mod.active? []
(let [ft vim.bo.filetype] (let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) false (if (= b.nifoc_formatter_disabled 1) false
(has-formatter-config? ft)))) (or (has-formatter-config? ft) (format-with-lsp? ft)))))
(fn mod.maybe-enable-lsp [client bufnr]
(when (client.supports_method :textDocument/rangeFormatting)
(api.nvim_buf_set_option bufnr :formatexpr "v:lua.vim.lsp.formatexpr()")))
(fn mod.maybe-format-buffer [] (fn mod.maybe-format-buffer []
(let [ft vim.bo.filetype] (let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) nil (if (= b.nifoc_formatter_disabled 1) nil
(format-with-lsp? ft) (vim.lsp.buf.format) (conform.format {:lsp_fallback (format-with-lsp? ft)}))))
(has-formatter-config? ft) (format.format)
nil)))
mod) mod)

View file

@ -52,10 +52,7 @@
{:desc "Show Diagnostics"}) {:desc "Show Diagnostics"})
(keymap.set :n :<leader>dtp :<cmd>TSPlaygroundToggle<CR> (keymap.set :n :<leader>dtp :<cmd>TSPlaygroundToggle<CR>
{:desc "Toggle Treetsitter Playground"}) {:desc "Toggle Treetsitter Playground"})
; (keymap.set :n :<leader>dn #(noice.cmd :telescope)
; {:desc "Display Notifications"})
;; Other Mappings ;; Other Mappings
; (keymap.set :n :<CR> ":nohlsearch<CR><CR>" {:silent true})
(keymap.set :n :F formatting.maybe-format-buffer {:desc "Format Buffer"}) (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)
@ -109,4 +106,3 @@
(keymap.set :t :<C-l> "<C-\\><C-n><C-W>l" map-opts))) (keymap.set :t :<C-l> "<C-\\><C-n><C-W>l" map-opts)))
mod) mod)

View file

@ -1,15 +1,9 @@
(let [M {} (let [conform (require :conform)
format (require :format) conform-util (require :conform.util)
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-exts [] treefmt-exts []
level vim.log.levels] tmp-filename "\$FILENAME"]
(var latest-used-formatter :unknown)
(fn notify [msg lvl]
(vim.notify (.. msg " [" latest-used-formatter "]") lvl {:title :Format}))
(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)))
@ -19,57 +13,43 @@
(each [_ opts (pairs cfg.formatter)] (each [_ opts (pairs cfg.formatter)]
(vim.list_extend treefmt-exts opts.includes)))) (vim.list_extend treefmt-exts opts.includes))))
(fn treefmt-or-fallback [file-path fallback] (fn treefmt-or-fallback [fallback]
(let [ext (format-core.file.extension file-path) (let [ext (vim.fn.expand "%:e")
ext-glob (.. "*." ext)] ext-glob (.. "*." ext)]
(if (vim.list_contains treefmt-exts ext-glob) (if (vim.list_contains treefmt-exts ext-glob)
(do {:command :treefmt
(set latest-used-formatter :treefmt) :args [tmp-filename]
[{:cmd :treefmt :stdin false
:args [file-path] :cwd (conform-util.root_file [:flake.nix :flake.lock])
:ignore_err (fn [err data] :require_cwd true}
(and (= err nil) (not (string.find data :Error))))}]) (= (vim.fn.executable fallback.command) 1)
(= (vim.fn.executable fallback.cmd) 1) fallback
(do
(set latest-used-formatter fallback.cmd)
[fallback])
[]))) [])))
(fn formatter-prettier [file-path] (conform.setup {:formatters_by_ft {:css [:treefmt_or_prettier]
(treefmt-or-fallback file-path {:cmd :prettier :args [:--write file-path]})) :fennel [:treefmt_or_fnlfmt]
:fish [:fish_indent]
(fn M.treefmt-extensions [] treefmt-exts) :html [:treefmt_or_prettier]
:javascript [:treefmt_or_prettier]
(format.setup {:filetypes {:css formatter-prettier :json [:treefmt_or_prettier]
:fennel (fn [file-path] :nix [:treefmt_or_nixpkgs_fmt]
(treefmt-or-fallback file-path :sh [:treefmt_or_shfmt]
{:cmd :fnlfmt :typescript [:treefmt_or_prettier]
:args [:--fix :yaml [:treefmt_or_yamlfmt]}
file-path]})) :notify_on_error true
:fish (fn [file-path] :formatters {:treefmt_or_fnlfmt #(treefmt-or-fallback {:command :fnlfmt
{:cmd :fish_indent :args ["-"]})
:args [:--write file-path]}) :treefmt_or_nixpkgs_fmt #(treefmt-or-fallback {:command :nixpkgs-fmt})
:html formatter-prettier :treefmt_or_prettier #(treefmt-or-fallback {:command :prettier
:javascript formatter-prettier :args [:--stdin-filepath
:json formatter-prettier tmp-filename]
:nix (fn [file-path] :cwd (conform-util.root_file [:.prettierrc
(treefmt-or-fallback file-path :package.json])})
{:cmd :nixpkgs-fmt :treefmt_or_shfmt #(treefmt-or-fallback {:command :shfmt
:args [file-path]})) :args [:-filename
:sh (fn [file-path] tmp-filename
(treefmt-or-fallback file-path :-i
{:cmd :shfmt :2
:args [:-i :-s]})
:2 :treefmt_or_yamlfmt #(treefmt-or-fallback {:command :yamlfmt
:-s :args ["-"]})}}))
:-w
file-path]}))
:typescript formatter-prettier
:yaml (fn [file-path]
(treefmt-or-fallback file-path
{:cmd :yamlfmt
:args [file-path]}))}
:hooks {:on_success #(notify "Formatting Succeed" level.INFO)
:on_err #(notify "Formatting Failed" level.ERROR)
:on_timeout #(notify "Formatting Timeout" level.ERROR)}})
M)

View file

@ -3,7 +3,6 @@
coq (require :coq) coq (require :coq)
navic (require :nvim-navic) navic (require :nvim-navic)
diagnostic (require :nifoc.diagnostic) diagnostic (require :nifoc.diagnostic)
formatting (require :nifoc.formatting)
augroup (vim.api.nvim_create_augroup :NifocLsp {:clear true}) augroup (vim.api.nvim_create_augroup :NifocLsp {:clear true})
aucmd vim.api.nvim_create_autocmd] aucmd vim.api.nvim_create_autocmd]
(fn setup-inlay-hint-toggle [bufnr] (fn setup-inlay-hint-toggle [bufnr]
@ -24,7 +23,6 @@
(vim.lsp.inlay_hint bufnr true) (vim.lsp.inlay_hint bufnr true)
(setup-inlay-hint-toggle bufnr)) (setup-inlay-hint-toggle bufnr))
(diagnostic.maybe-enable-lsp client bufnr) (diagnostic.maybe-enable-lsp client bufnr)
(formatting.maybe-enable-lsp client bufnr)
((. (require :lsp-notify) :setup) {}))) ((. (require :lsp-notify) :setup) {})))
:group augroup :group augroup
:desc "Automatic LSP setup"}) :desc "Automatic LSP setup"})

View file

@ -224,10 +224,8 @@ in
# Formatting # Formatting
core-nvim
{ {
plugin = format-nvim; plugin = conform-nvim;
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl; config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
type = "fennel"; type = "fennel";
} }
@ -321,7 +319,7 @@ in
for fnlfile in $nifoc_store_fnl; do for fnlfile in $nifoc_store_fnl; do
file_out_path="$(echo "$fnlfile" | sed "s|$nifoc_store_path/||" | sed "s/.fnl$/.lua/")" file_out_path="$(echo "$fnlfile" | sed "s|$nifoc_store_path/||" | sed "s/.fnl$/.lua/")"
echo "Compiling $fnlfile ..." echo "Compiling $fnlfile -> $out/lua/nifoc/$file_out_path"
$fennel "$fnlfile" > "$out/lua/nifoc/$file_out_path" $fennel "$fnlfile" > "$out/lua/nifoc/$file_out_path"
done done
@ -332,7 +330,7 @@ in
for fnlfile in $ftplugin_store_fnl; do for fnlfile in $ftplugin_store_fnl; do
file_out_path="$(echo "$fnlfile" | sed "s|$ftplugin_store_path/||" | sed "s/.fnl$/.lua/")" file_out_path="$(echo "$fnlfile" | sed "s|$ftplugin_store_path/||" | sed "s/.fnl$/.lua/")"
echo "Compiling $fnlfile ..." echo "Compiling $fnlfile -> $out/ftplugin/$file_out_path"
$fennel "$fnlfile" > "$out/ftplugin/$file_out_path" $fennel "$fnlfile" > "$out/ftplugin/$file_out_path"
done done
@ -343,7 +341,7 @@ in
for fnlfile in $after_store_fnl; do for fnlfile in $after_store_fnl; do
file_out_path="$(echo "$fnlfile" | sed "s|$after_store_path/||" | sed "s/.fnl$/.lua/")" file_out_path="$(echo "$fnlfile" | sed "s|$after_store_path/||" | sed "s/.fnl$/.lua/")"
echo "Compiling $fnlfile ..." echo "Compiling $fnlfile -> $out/after/$file_out_path"
$fennel "$fnlfile" > "$out/after/$file_out_path" $fennel "$fnlfile" > "$out/after/$file_out_path"
done done
@ -354,7 +352,9 @@ in
${config.programs.neovim.generatedConfigs.fennel} ${config.programs.neovim.generatedConfigs.fennel}
nil nil
EOF EOF
} | $fennel - > "$out/lua/configuration/plugins.lua" } > "$out/lua/configuration/plugins.fnl"
$fennel "$out/lua/configuration/plugins.fnl" > "$out/lua/configuration/plugins.lua"
rm -f "$out/lua/configuration/plugins.fnl"
# Other # Other
echo "Copying tree-sitter queries ..." echo "Copying tree-sitter queries ..."

View file

@ -30,23 +30,23 @@ in
}; };
mini-nvim = buildVimPlugin { mini-nvim = buildVimPlugin {
pname = "mini.nvim"; pname = "mini.nvim";
version = "2023-10-03"; version = "2023-10-09";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "echasnovski"; owner = "echasnovski";
repo = "mini.nvim"; repo = "mini.nvim";
rev = "25c4bf4efac51aad5393e37949e68ac3e672a73c"; rev = "0c50ef9d0b7cae1145f3365e7bcf9ed01bc00005";
sha256 = "115s6wjd7c0y2fcc0wnzdp2gp1qmwzy417jqzjmlb1rphj7kn2hz"; sha256 = "1il169wvz995k42is9b6baz5ii0fh6513q42qmv927s04k6vja0w";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
nvim-web-devicons = buildVimPlugin { nvim-web-devicons = buildVimPlugin {
pname = "nvim-web-devicons"; pname = "nvim-web-devicons";
version = "2023-10-08"; version = "2023-10-09";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "kyazdani42"; owner = "kyazdani42";
repo = "nvim-web-devicons"; repo = "nvim-web-devicons";
rev = "56b3a6ce6d69329cbf603dd38ac59fb4d7774b01"; rev = "a1e6268779411048a87f767a27380089362a0ce2";
sha256 = "1gqfzbdkiv91wvb14ydspjh1z4w59lp8rdsyw3w1gskfjalwz6cq"; sha256 = "019i9iy9zri12whq5kdpfia8zbpp7x5p5snk4j6bb0p7hb7caljp";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
@ -96,12 +96,12 @@ in
}; };
substitute-nvim = buildVimPlugin { substitute-nvim = buildVimPlugin {
pname = "substitute.nvim"; pname = "substitute.nvim";
version = "2023-10-05"; version = "2023-10-09";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "gbprod"; owner = "gbprod";
repo = "substitute.nvim"; repo = "substitute.nvim";
rev = "49f6f4992f244242cd522572631c34b0a89c54cb"; rev = "523047fac696ab2acdbfd6fec707587c64d6f9d7";
sha256 = "0wmbx66d47dajc822g31xrqdymma9jky14s3dfxvbdwzzxab9ipp"; sha256 = "02c1w2yqi1dir59rvxyiyp9vk4r9n0vffvk3gm0lh31b9nf2lfhc";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
@ -118,12 +118,12 @@ in
}; };
nvim-treesitter = buildVimPlugin { nvim-treesitter = buildVimPlugin {
pname = "nvim-treesitter"; pname = "nvim-treesitter";
version = "2023-10-08"; version = "2023-10-09";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "nvim-treesitter"; owner = "nvim-treesitter";
repo = "nvim-treesitter"; repo = "nvim-treesitter";
rev = "a102053352bd958d84a3e1be3de0203d2af92984"; rev = "dfd4fb330f75595813e2a2f83000580a36dd10ff";
sha256 = "176k4hahgcjly971abrpf4q0279ll6my7qlxs0bdjf2g9095aijj"; sha256 = "08lvg3xxfdm4im5q63ahpnn2y22apznvy12ka599q89ilg20mj6b";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
@ -133,8 +133,8 @@ in
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "NoahTheDuke"; owner = "NoahTheDuke";
repo = "vim-just"; repo = "vim-just";
rev = "fbcfcf96cf7c67cf0d4ad15b7af5069f65440c4f"; rev = "b2113d846c1f5a400ac9c2f5ef762de8f99dc745";
sha256 = "1awabcqs6rci0xn7nhzy5ra69ppq8lql0k5ky7vxwv0jjwnn6ac0"; sha256 = "14nqxx0lmqbydcxxjxy2jw0nkgawlkcl4bjnm38wvyj5vibhmy8v";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
@ -284,25 +284,14 @@ in
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };
core-nvim = buildVimPlugin { conform-nvim = buildVimPlugin {
pname = "core.nvim"; pname = "conform.nvim";
version = "2023-09-09"; version = "2023-10-09";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "niuiic"; owner = "stevearc";
repo = "core.nvim"; repo = "conform.nvim";
rev = "dc1046613c7a0ce664b6ec3d5c8c94d6b6d2618d"; rev = "38da505fb0339f68734b659460ad49d4a60ecf34";
sha256 = "0lgg800rcrkg99wg7spj1r9ykyx31zr07f57ybyqaxwd7z0sx260"; sha256 = "16k2afb0kr7h5f643isngnkpm3dj3q77kar58nsxy0qryvwwdd4x";
fetchSubmodules = false;
};
};
format-nvim = buildVimPlugin {
pname = "format.nvim";
version = "2023-09-17";
src = fetchFromGitHub {
owner = "niuiic";
repo = "format.nvim";
rev = "5bc6846406dcb791edbf02f847fc687b74c45ec7";
sha256 = "0jizp5hb0clcdbl965b4pbci1cch00mj07w1cbg3j3yaz7d9lwf6";
fetchSubmodules = false; fetchSubmodules = false;
}; };
}; };

View file

@ -33,8 +33,7 @@
# Linter # Linter
- src: mfussenegger/nvim-lint - src: mfussenegger/nvim-lint
# Formatting # Formatting
- src: niuiic/core.nvim - src: stevearc/conform.nvim
- src: niuiic/format.nvim
# Pairs # Pairs
- src: andymass/vim-matchup - src: andymass/vim-matchup
# Textobjects # Textobjects