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

View file

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

View file

@ -52,10 +52,7 @@
{:desc "Show Diagnostics"})
(keymap.set :n :<leader>dtp :<cmd>TSPlaygroundToggle<CR>
{:desc "Toggle Treetsitter Playground"})
; (keymap.set :n :<leader>dn #(noice.cmd :telescope)
; {:desc "Display Notifications"})
;; 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 :<A-Left> :b)
(keymap.set :n :<A-Right> :w)
@ -109,4 +106,3 @@
(keymap.set :t :<C-l> "<C-\\><C-n><C-W>l" map-opts)))
mod)

View file

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

View file

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

View file

@ -224,10 +224,8 @@ in
# Formatting
core-nvim
{
plugin = format-nvim;
plugin = conform-nvim;
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
type = "fennel";
}
@ -321,7 +319,7 @@ in
for fnlfile in $nifoc_store_fnl; do
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"
done
@ -332,7 +330,7 @@ in
for fnlfile in $ftplugin_store_fnl; do
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"
done
@ -343,7 +341,7 @@ in
for fnlfile in $after_store_fnl; do
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"
done
@ -354,7 +352,9 @@ in
${config.programs.neovim.generatedConfigs.fennel}
nil
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
echo "Copying tree-sitter queries ..."

View file

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

View file

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