nvim: Move to formatter.nvim and use treefmt
This commit is contained in:
parent
e2be831387
commit
45462f676e
12 changed files with 79 additions and 56 deletions
12
flake.lock
12
flake.lock
|
@ -110,11 +110,11 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1689116343,
|
"lastModified": 1689178160,
|
||||||
"narHash": "sha256-eaYfwQTSEbuB7rs5/W227SbVeDP9cbcoT1TEbnmOgOk=",
|
"narHash": "sha256-TVR0hn/JWo1qmtvgzqjfNerLPsIxfdFR3hf3QrnEj7U=",
|
||||||
"owner": "lnl7",
|
"owner": "lnl7",
|
||||||
"repo": "nix-darwin",
|
"repo": "nix-darwin",
|
||||||
"rev": "eb22022ba8faeeb7a9be8afe925511b88ad12ca5",
|
"rev": "795492c9a895762f36f6c1ff43d6e0de66fdffa8",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -377,11 +377,11 @@
|
||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1689078114,
|
"lastModified": 1689138773,
|
||||||
"narHash": "sha256-osG8BrX5RpKJ7wH+vI6auOU+ctvNOblT4XXCgknK47c=",
|
"narHash": "sha256-NEcJPQfwc1jNDI/ITvieJW2Y+IWdGIYCFHLJl5PGx3o=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "b6cc7ff8fee93789bc871a267ab876c3fca042cb",
|
"rev": "bf57c599729771cd23054a18c0f3a391ae85e193",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
(let [lo vim.opt_local]
|
|
||||||
(set lo.formatprg "fnlfmt -"))
|
|
|
@ -1,6 +1,5 @@
|
||||||
(let [lo vim.opt_local]
|
(let [lo vim.opt_local]
|
||||||
(set lo.tabstop 4)
|
(set lo.tabstop 4)
|
||||||
(set lo.shiftwidth 4)
|
(set lo.shiftwidth 4)
|
||||||
(set lo.softtabstop 4)
|
(set lo.softtabstop 4))
|
||||||
(set lo.formatprg :fish_indent))
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
(let [lo vim.opt_local
|
|
||||||
b vim.b]
|
|
||||||
(set lo.formatprg "eslint_d --stdin --fix-to-stdout")
|
|
||||||
(set b.nifoc_formatter_force_formatprg 1))
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
(let [lo vim.opt_local]
|
|
||||||
(set lo.formatprg "shfmt -i 2"))
|
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
b vim.b
|
b vim.b
|
||||||
cmd vim.cmd
|
cmd vim.cmd
|
||||||
api vim.api
|
api vim.api
|
||||||
set-bufvar vim.api.nvim_buf_set_var]
|
set-bufvar vim.api.nvim_buf_set_var
|
||||||
|
treefmt-exists (vim.fn.executable :treefmt)]
|
||||||
(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})
|
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
|
||||||
|
@ -11,14 +12,21 @@
|
||||||
{: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 {:callback mod.maybe-format-buffer
|
(aucmd :BufWritePre
|
||||||
|
{:callback mod.maybe-format-buffer-pre
|
||||||
:group augroup
|
:group augroup
|
||||||
:desc "Run Formatter"})))
|
:desc "Run Formatter before saving"})
|
||||||
|
(aucmd :BufWritePost
|
||||||
|
{:callback mod.maybe-format-buffer-post
|
||||||
|
:group augroup
|
||||||
|
:desc "Run Formatter after saving"})))
|
||||||
|
|
||||||
(fn run-neoformat [formatprg]
|
(fn has-formatter-config? [ft]
|
||||||
(let [neoformat (.. "Neoformat " formatprg)]
|
(let [formatter-fts (vim.tbl_keys (. (require :formatter.config) :values
|
||||||
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | " neoformat
|
:filetype))]
|
||||||
" | endtry"))))
|
(vim.tbl_contains formatter-fts ft)))
|
||||||
|
|
||||||
|
(fn run-formatter-exe [] (cmd :FormatWriteLock))
|
||||||
|
|
||||||
(fn run-lsp-format []
|
(fn run-lsp-format []
|
||||||
(if (not= b.nifoc_formatter_filter_lsp_client nil)
|
(if (not= b.nifoc_formatter_filter_lsp_client nil)
|
||||||
|
@ -34,10 +42,12 @@
|
||||||
(set-bufvar 0 :nifoc_formatter_disabled 1))
|
(set-bufvar 0 :nifoc_formatter_disabled 1))
|
||||||
|
|
||||||
(fn mod.active? []
|
(fn mod.active? []
|
||||||
|
(let [ft vim.bo.filetype]
|
||||||
(if (= b.nifoc_formatter_disabled 1) false
|
(if (= b.nifoc_formatter_disabled 1) false
|
||||||
|
(= treefmt-exists 1) true
|
||||||
(= b.nifoc_lsp_formatter_enabled 1) true
|
(= b.nifoc_lsp_formatter_enabled 1) true
|
||||||
(not= (vim.opt_local.formatprg:get) "") true
|
(has-formatter-config? ft) true
|
||||||
false))
|
false)))
|
||||||
|
|
||||||
(fn mod.maybe-enable-lsp [client bufnr]
|
(fn mod.maybe-enable-lsp [client bufnr]
|
||||||
(when (client.supports_method :textDocument/rangeFormatting)
|
(when (client.supports_method :textDocument/rangeFormatting)
|
||||||
|
@ -45,13 +55,17 @@
|
||||||
(when (client.supports_method :textDocument/formatting)
|
(when (client.supports_method :textDocument/formatting)
|
||||||
(set-bufvar bufnr :nifoc_lsp_formatter_enabled 1)))
|
(set-bufvar bufnr :nifoc_lsp_formatter_enabled 1)))
|
||||||
|
|
||||||
(fn mod.maybe-format-buffer []
|
(fn mod.maybe-format-buffer-pre []
|
||||||
(let [formatprg (vim.opt_local.formatprg:get)
|
|
||||||
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))]
|
|
||||||
(if (= b.nifoc_formatter_disabled 1) nil
|
(if (= b.nifoc_formatter_disabled 1) nil
|
||||||
(= b.nifoc_formatter_force_formatprg 1) (run-neoformat formatprg-exe)
|
(= treefmt-exists 1) nil
|
||||||
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format)
|
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format)))
|
||||||
(not= formatprg-exe nil) (run-neoformat formatprg-exe))))
|
|
||||||
|
(fn mod.maybe-format-buffer-post []
|
||||||
|
(let [ft vim.bo.filetype]
|
||||||
|
(if (= b.nifoc_formatter_disabled 1) nil
|
||||||
|
(= treefmt-exists 1) (run-formatter-exe)
|
||||||
|
(= b.nifoc_lsp_formatter_enabled 1) nil
|
||||||
|
(has-formatter-config? ft) (run-formatter-exe))))
|
||||||
|
|
||||||
mod)
|
mod)
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,6 @@
|
||||||
;; 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> "^")
|
||||||
|
|
24
home/config/nvim/plugins/formatter.fnl
Normal file
24
home/config/nvim/plugins/formatter.fnl
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
(let [formatter (require :formatter)
|
||||||
|
formatter-utils (require :formatter.util)
|
||||||
|
treefmt-exists (vim.fn.executable :treefmt)
|
||||||
|
treefmt-formatter #{:exe :treefmt
|
||||||
|
:stdin false
|
||||||
|
:tempfile_prefix :treefmt
|
||||||
|
:tempfile_postfix (.. "."
|
||||||
|
(formatter-utils.get_current_buffer_file_extension))}]
|
||||||
|
(fn builtin-formatter [ft formatter]
|
||||||
|
(let [file (.. :formatter.filetypes. ft)]
|
||||||
|
(. (require file) formatter)))
|
||||||
|
|
||||||
|
(fn prefer-treefmt [ft formatter]
|
||||||
|
(if (= treefmt-exists 1)
|
||||||
|
(treefmt-formatter)
|
||||||
|
((builtin-formatter ft formatter))))
|
||||||
|
|
||||||
|
(formatter.setup {:logging true
|
||||||
|
:log_level vim.log.levels.WARN
|
||||||
|
:filetype {:fennel [#{:exe :fnlfmt :args ["-"] :stdin true}]
|
||||||
|
:fish [(builtin-formatter :fish :fishindent)]
|
||||||
|
:sh [(prefer-treefmt :sh :shfmt)]
|
||||||
|
:typescript [treefmt-formatter]}}))
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
(let [g vim.g]
|
|
||||||
(set g.neoformat_try_formatprg 1)
|
|
||||||
(set g.neoformat_basic_format_trim 1))
|
|
|
@ -247,8 +247,8 @@ in
|
||||||
|
|
||||||
# Formatting
|
# Formatting
|
||||||
{
|
{
|
||||||
plugin = neoformat;
|
plugin = formatter-nvim;
|
||||||
config = builtins.readFile ../../config/nvim/plugins/neoformat.fnl;
|
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
|
||||||
type = "fennel";
|
type = "fennel";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,12 +107,12 @@ in
|
||||||
};
|
};
|
||||||
nvim-treesitter = buildVimPluginFrom2Nix {
|
nvim-treesitter = buildVimPluginFrom2Nix {
|
||||||
pname = "nvim-treesitter";
|
pname = "nvim-treesitter";
|
||||||
version = "2023-07-11";
|
version = "2023-07-12";
|
||||||
src = fetchFromGitHub {
|
src = fetchFromGitHub {
|
||||||
owner = "nvim-treesitter";
|
owner = "nvim-treesitter";
|
||||||
repo = "nvim-treesitter";
|
repo = "nvim-treesitter";
|
||||||
rev = "b4b94ec387525cb9df4b56eb1a8bf33231a0df47";
|
rev = "575f5a4b1fcb60c1ac4f560c46a5171454d674ca";
|
||||||
sha256 = "0z3nqqwvnfvi6nqjl9cjpik7cw3xf8armmgn627v9l8jqgz3a6lv";
|
sha256 = "0i9rn6gz09rk49gpq305p740xza4mgpb3nwmi2zm4rg9psxamcsr";
|
||||||
fetchSubmodules = false;
|
fetchSubmodules = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -416,14 +416,14 @@ in
|
||||||
fetchSubmodules = false;
|
fetchSubmodules = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
neoformat = buildVimPluginFrom2Nix {
|
formatter-nvim = buildVimPluginFrom2Nix {
|
||||||
pname = "neoformat";
|
pname = "formatter.nvim";
|
||||||
version = "2023-07-01";
|
version = "2023-04-30";
|
||||||
src = fetchFromGitHub {
|
src = fetchFromGitHub {
|
||||||
owner = "sbdchd";
|
owner = "mhartington";
|
||||||
repo = "neoformat";
|
repo = "formatter.nvim";
|
||||||
rev = "b35ae45f9425c817126be4cc946a950c1cffe6fa";
|
rev = "fa4f2729cc2909db599169f22d8e55632d4c8d59";
|
||||||
sha256 = "1qcrladfl3zvci58aiv4w6bq6dc8ibfjgp95l4q10bx8gjkdipkq";
|
sha256 = "0hv79gdg7cgqr3a8vw1ysc48f6i3b8xabbglxspm5mbpf22c8xbk";
|
||||||
fetchSubmodules = false;
|
fetchSubmodules = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -451,12 +451,12 @@ in
|
||||||
};
|
};
|
||||||
vim-matchup = buildVimPluginFrom2Nix {
|
vim-matchup = buildVimPluginFrom2Nix {
|
||||||
pname = "vim-matchup";
|
pname = "vim-matchup";
|
||||||
version = "2023-07-11";
|
version = "2023-07-12";
|
||||||
src = fetchFromGitHub {
|
src = fetchFromGitHub {
|
||||||
owner = "andymass";
|
owner = "andymass";
|
||||||
repo = "vim-matchup";
|
repo = "vim-matchup";
|
||||||
rev = "3c4ccc489002fe72b5d2e2ffc4b3b5a7d3ab65d8";
|
rev = "17cc05867cb3314761e4baa20115a27120f8e82c";
|
||||||
sha256 = "0mpvqlk8a7vms2j29mpfpkqa1pxsncqr7flsg6q6z3cdqcrgh1j2";
|
sha256 = "0hxp9akqxz7h2ly1qpiviqbgbk52mbxk88vvq6rxf2kb12sgbiag";
|
||||||
fetchSubmodules = false;
|
fetchSubmodules = false;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -59,7 +59,7 @@
|
||||||
- src: hrsh7th/cmp-nvim-lsp-document-symbol
|
- src: hrsh7th/cmp-nvim-lsp-document-symbol
|
||||||
|
|
||||||
# Formatting
|
# Formatting
|
||||||
- src: sbdchd/neoformat
|
- src: mhartington/formatter.nvim
|
||||||
|
|
||||||
# Pairs
|
# Pairs
|
||||||
- src: windwp/nvim-autopairs
|
- src: windwp/nvim-autopairs
|
||||||
|
|
Loading…
Reference in a new issue