1
0
Fork 0

nvim: Move to formatter.nvim and use treefmt

This commit is contained in:
Daniel Kempkens 2023-07-12 20:39:50 +02:00
parent e2be831387
commit 45462f676e
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
12 changed files with 79 additions and 56 deletions

View file

@ -110,11 +110,11 @@
]
},
"locked": {
"lastModified": 1689116343,
"narHash": "sha256-eaYfwQTSEbuB7rs5/W227SbVeDP9cbcoT1TEbnmOgOk=",
"lastModified": 1689178160,
"narHash": "sha256-TVR0hn/JWo1qmtvgzqjfNerLPsIxfdFR3hf3QrnEj7U=",
"owner": "lnl7",
"repo": "nix-darwin",
"rev": "eb22022ba8faeeb7a9be8afe925511b88ad12ca5",
"rev": "795492c9a895762f36f6c1ff43d6e0de66fdffa8",
"type": "github"
},
"original": {
@ -377,11 +377,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1689078114,
"narHash": "sha256-osG8BrX5RpKJ7wH+vI6auOU+ctvNOblT4XXCgknK47c=",
"lastModified": 1689138773,
"narHash": "sha256-NEcJPQfwc1jNDI/ITvieJW2Y+IWdGIYCFHLJl5PGx3o=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "b6cc7ff8fee93789bc871a267ab876c3fca042cb",
"rev": "bf57c599729771cd23054a18c0f3a391ae85e193",
"type": "github"
},
"original": {

View file

@ -1,2 +0,0 @@
(let [lo vim.opt_local]
(set lo.formatprg "fnlfmt -"))

View file

@ -1,6 +1,5 @@
(let [lo vim.opt_local]
(set lo.tabstop 4)
(set lo.shiftwidth 4)
(set lo.softtabstop 4)
(set lo.formatprg :fish_indent))
(set lo.softtabstop 4))

View file

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

View file

@ -1,3 +0,0 @@
(let [lo vim.opt_local]
(set lo.formatprg "shfmt -i 2"))

View file

@ -2,7 +2,8 @@
b vim.b
cmd vim.cmd
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 []
(let [usercmd vim.api.nvim_create_user_command
augroup (vim.api.nvim_create_augroup :NifocFormatting {:clear true})
@ -11,14 +12,21 @@
{: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"})))
(aucmd :BufWritePre
{:callback mod.maybe-format-buffer-pre
:group augroup
:desc "Run Formatter before saving"})
(aucmd :BufWritePost
{:callback mod.maybe-format-buffer-post
:group augroup
:desc "Run Formatter after saving"})))
(fn run-neoformat [formatprg]
(let [neoformat (.. "Neoformat " formatprg)]
(cmd (.. "try | undojoin | " neoformat " | catch /E790/ | " neoformat
" | endtry"))))
(fn has-formatter-config? [ft]
(let [formatter-fts (vim.tbl_keys (. (require :formatter.config) :values
:filetype))]
(vim.tbl_contains formatter-fts ft)))
(fn run-formatter-exe [] (cmd :FormatWriteLock))
(fn run-lsp-format []
(if (not= b.nifoc_formatter_filter_lsp_client nil)
@ -34,10 +42,12 @@
(set-bufvar 0 :nifoc_formatter_disabled 1))
(fn mod.active? []
(if (= b.nifoc_formatter_disabled 1) false
(= b.nifoc_lsp_formatter_enabled 1) true
(not= (vim.opt_local.formatprg:get) "") true
false))
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) false
(= treefmt-exists 1) true
(= b.nifoc_lsp_formatter_enabled 1) true
(has-formatter-config? ft) true
false)))
(fn mod.maybe-enable-lsp [client bufnr]
(when (client.supports_method :textDocument/rangeFormatting)
@ -45,13 +55,17 @@
(when (client.supports_method :textDocument/formatting)
(set-bufvar bufnr :nifoc_lsp_formatter_enabled 1)))
(fn mod.maybe-format-buffer []
(let [formatprg (vim.opt_local.formatprg:get)
formatprg-exe (-> formatprg (vim.split " " {:trimempty true}) (. 1))]
(fn mod.maybe-format-buffer-pre []
(if (= b.nifoc_formatter_disabled 1) nil
(= treefmt-exists 1) nil
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format)))
(fn mod.maybe-format-buffer-post []
(let [ft vim.bo.filetype]
(if (= b.nifoc_formatter_disabled 1) nil
(= b.nifoc_formatter_force_formatprg 1) (run-neoformat formatprg-exe)
(= b.nifoc_lsp_formatter_enabled 1) (run-lsp-format)
(not= formatprg-exe nil) (run-neoformat formatprg-exe))))
(= treefmt-exists 1) (run-formatter-exe)
(= b.nifoc_lsp_formatter_enabled 1) nil
(has-formatter-config? ft) (run-formatter-exe))))
mod)

View file

@ -61,7 +61,6 @@
;; 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

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

View file

@ -1,3 +0,0 @@
(let [g vim.g]
(set g.neoformat_try_formatprg 1)
(set g.neoformat_basic_format_trim 1))

View file

@ -247,8 +247,8 @@ in
# Formatting
{
plugin = neoformat;
config = builtins.readFile ../../config/nvim/plugins/neoformat.fnl;
plugin = formatter-nvim;
config = builtins.readFile ../../config/nvim/plugins/formatter.fnl;
type = "fennel";
}

View file

@ -107,12 +107,12 @@ in
};
nvim-treesitter = buildVimPluginFrom2Nix {
pname = "nvim-treesitter";
version = "2023-07-11";
version = "2023-07-12";
src = fetchFromGitHub {
owner = "nvim-treesitter";
repo = "nvim-treesitter";
rev = "b4b94ec387525cb9df4b56eb1a8bf33231a0df47";
sha256 = "0z3nqqwvnfvi6nqjl9cjpik7cw3xf8armmgn627v9l8jqgz3a6lv";
rev = "575f5a4b1fcb60c1ac4f560c46a5171454d674ca";
sha256 = "0i9rn6gz09rk49gpq305p740xza4mgpb3nwmi2zm4rg9psxamcsr";
fetchSubmodules = false;
};
};
@ -416,14 +416,14 @@ in
fetchSubmodules = false;
};
};
neoformat = buildVimPluginFrom2Nix {
pname = "neoformat";
version = "2023-07-01";
formatter-nvim = buildVimPluginFrom2Nix {
pname = "formatter.nvim";
version = "2023-04-30";
src = fetchFromGitHub {
owner = "sbdchd";
repo = "neoformat";
rev = "b35ae45f9425c817126be4cc946a950c1cffe6fa";
sha256 = "1qcrladfl3zvci58aiv4w6bq6dc8ibfjgp95l4q10bx8gjkdipkq";
owner = "mhartington";
repo = "formatter.nvim";
rev = "fa4f2729cc2909db599169f22d8e55632d4c8d59";
sha256 = "0hv79gdg7cgqr3a8vw1ysc48f6i3b8xabbglxspm5mbpf22c8xbk";
fetchSubmodules = false;
};
};
@ -451,12 +451,12 @@ in
};
vim-matchup = buildVimPluginFrom2Nix {
pname = "vim-matchup";
version = "2023-07-11";
version = "2023-07-12";
src = fetchFromGitHub {
owner = "andymass";
repo = "vim-matchup";
rev = "3c4ccc489002fe72b5d2e2ffc4b3b5a7d3ab65d8";
sha256 = "0mpvqlk8a7vms2j29mpfpkqa1pxsncqr7flsg6q6z3cdqcrgh1j2";
rev = "17cc05867cb3314761e4baa20115a27120f8e82c";
sha256 = "0hxp9akqxz7h2ly1qpiviqbgbk52mbxk88vvq6rxf2kb12sgbiag";
fetchSubmodules = false;
};
};

View file

@ -59,7 +59,7 @@
- src: hrsh7th/cmp-nvim-lsp-document-symbol
# Formatting
- src: sbdchd/neoformat
- src: mhartington/formatter.nvim
# Pairs
- src: windwp/nvim-autopairs