From 7393fc696a040b613d132dfcfee37f40edfb3613 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sun, 15 Dec 2024 12:21:28 -0800 Subject: [PATCH] feat: conform.nvim --- nix/neovim-overlay.nix | 7 ++++ nvim/lua/plugins/formatting/conform-nvim.lua | 43 ++++++++++++++++++++ nvim/lua/plugins/formatting/init.lua | 5 +++ nvim/lua/snacks-nvim.lua | 30 ++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 nvim/lua/plugins/formatting/conform-nvim.lua create mode 100644 nvim/lua/plugins/formatting/init.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index 8049a98..f750758 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -60,6 +60,9 @@ with final.pkgs.lib; let ts-comments-nvim mini-ai + # Formatting + conform-nvim + # Treesitter nvim-treesitter-textobjects nvim-ts-autotag @@ -104,6 +107,10 @@ with final.pkgs.lib; let # Language servers lua-language-server nixd + + # Formatters + shfmt + stylua ]; in { # This is the neovim derivation returned by the overlay. diff --git a/nvim/lua/plugins/formatting/conform-nvim.lua b/nvim/lua/plugins/formatting/conform-nvim.lua new file mode 100644 index 0000000..a263ee8 --- /dev/null +++ b/nvim/lua/plugins/formatting/conform-nvim.lua @@ -0,0 +1,43 @@ +return { + 'conform.nvim', + event = { 'BufWritePre' }, + cmd = { 'ConformInfo' }, + keys = { + { + 'cf', + function() + require('conform').format() + end, + desc = 'format', + }, + { + 'cF', + function() + require('conform').format({ + formatters = { 'injected' }, + timeout_ms = 3000, + }) + end, + mode = { 'n', 'v' }, + desc = 'format injected langs', + }, + }, + after = function() + require('conform').setup({ + format_on_save = function(buf) + if (vim.g.autoformat == false) or (vim.b[buf].autoformat == false) then + return + end + + return { timeout_ms = 3000, lsp_format = 'fallback' } + end, + formatters_by_ft = { + lua = { 'stylua' }, + fish = { 'fish_indent' }, + sh = { 'shfmt' }, + }, + }) + + vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + end, +} diff --git a/nvim/lua/plugins/formatting/init.lua b/nvim/lua/plugins/formatting/init.lua new file mode 100644 index 0000000..4b2a62b --- /dev/null +++ b/nvim/lua/plugins/formatting/init.lua @@ -0,0 +1,5 @@ +local req = MarleyVim.local_require('plugins.formatting') + +return { + req('conform-nvim'), +} diff --git a/nvim/lua/snacks-nvim.lua b/nvim/lua/snacks-nvim.lua index ccd51e0..036b0e9 100644 --- a/nvim/lua/snacks-nvim.lua +++ b/nvim/lua/snacks-nvim.lua @@ -61,6 +61,36 @@ if vim.lsp.inlay_hint then toggle.inlay_hints():map('uh') end +toggle({ + name = 'auto format (global)', + get = function() + return vim.g.autoformat == nil or vim.g.autoformat + end, + set = function(enable) + vim.g.autoformat = (enable == nil and true) or enable + vim.b.autoformat = nil + end, +}):map('uf') + +toggle({ + name = 'auto format (buffer)', + get = function() + local buf = vim.api.nvim_get_current_buf() + + local gopt = vim.g.autoformat + local bopt = vim.b[buf].autoformat + + if bopt ~= nil then + return bopt + end + + return gopt == nil or gopt + end, + set = function(enable) + vim.b.autoformat = (enable == nil and true) or enable + end, +}):map('uF') + -- Git -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- set({ 'n' }, 'gb', function()