switch to conform.nvim

Wed, 15 May 2024 13:49:17 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 15 May 2024 13:49:17 -0500
changeset 1163
40367220022e
parent 1162
e6da5f7156c2
child 1164
a8b6384c8738

switch to conform.nvim

.config/nvim/after/ftplugin/lua.vim file | annotate | diff | comparison | revisions
.config/nvim/after/ftplugin/mail.vim file | annotate | diff | comparison | revisions
.config/nvim/after/ftplugin/markdown.vim file | annotate | diff | comparison | revisions
.config/nvim/after/ftplugin/perl.vim file | annotate | diff | comparison | revisions
.config/nvim/after/ftplugin/sh.vim file | annotate | diff | comparison | revisions
.config/nvim/after/ftplugin/sql.vim file | annotate | diff | comparison | revisions
.config/nvim/lua/config/lsp.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/config/options.lua file | annotate | diff | comparison | revisions
.config/nvim/lua/plugins/formatter.lua file | annotate | diff | comparison | revisions
--- a/.config/nvim/after/ftplugin/lua.vim
+++ b/.config/nvim/after/ftplugin/lua.vim
@@ -1,5 +1,1 @@
 setl et sw=2
-
-if executable("stylua")
-  setl formatprg=stylua\ -s\ -
-endif
--- a/.config/nvim/after/ftplugin/mail.vim
+++ b/.config/nvim/after/ftplugin/mail.vim
@@ -3,8 +3,6 @@ setl formatoptions=jaw12tcql
 setl spell
 setl noundofile noswapfile nobackup
 
-setl formatprg=pandoc\ -f\ markdown\ -t\ markdown
-
 " http://cedricduval.free.fr/download/mail.vim
 function! Mail_Erase_Sig()
   " search for the signature pattern (takes into account signature delimiters
--- a/.config/nvim/after/ftplugin/markdown.vim
+++ b/.config/nvim/after/ftplugin/markdown.vim
@@ -1,5 +1,4 @@
 runtime ftplugin/text.vim
-setl equalprg=pandoc\ -f\ markdown\ -t\ markdown
 
 if !exists('b:markdown_autohtml')
   let b:markdown_autohtml = 0
--- a/.config/nvim/after/ftplugin/perl.vim
+++ b/.config/nvim/after/ftplugin/perl.vim
@@ -1,2 +1,1 @@
 compiler perl
-setl formatprg=perltidy\ -q
deleted file mode 100644
--- a/.config/nvim/after/ftplugin/sh.vim
+++ /dev/null
@@ -1,1 +0,0 @@
-let &l:formatprg = "shfmt -i " . &shiftwidth
--- a/.config/nvim/after/ftplugin/sql.vim
+++ b/.config/nvim/after/ftplugin/sql.vim
@@ -8,5 +8,5 @@
 " augroup END
 
 " cpanm https://github.com/darold/pgFormatter.git
-let &l:formatprg = "pg_format -"
+"let &l:formatprg = "pg_format -"
 let &l:omnifunc = "syntaxcomplete#Complete"
--- a/.config/nvim/lua/config/lsp.lua
+++ b/.config/nvim/lua/config/lsp.lua
@@ -33,15 +33,3 @@ vim.api.nvim_create_autocmd("LspAttach",
     })
   end,
 })
-
--- Format on write, but only certain languages
-local autoformat_filetypes = { elixir = true, go = true }
-
-vim.api.nvim_create_autocmd("BufWritePre", {
-  group = vim.api.nvim_create_augroup("lsp_autoformat", { clear = true }),
-  callback = function()
-    if autoformat_filetypes[vim.bo.filetype] then
-      vim.lsp.buf.format({ timeout_ms = 100 })
-    end
-  end,
-})
--- a/.config/nvim/lua/config/options.lua
+++ b/.config/nvim/lua/config/options.lua
@@ -7,6 +7,7 @@ local o = vim.opt
 o.expandtab = true
 o.shiftround = true
 o.shiftwidth = 2
+o.formatexpr = "v:lua.require'conform'.formatexpr()"
 
 -- Display
 o.breakindent = true
new file mode 100644
--- /dev/null
+++ b/.config/nvim/lua/plugins/formatter.lua
@@ -0,0 +1,48 @@
+return {
+  {
+    "stevearc/conform.nvim",
+    opts = {
+      formatters_by_ft = {
+        lua = { "stylua" },
+        mail = { "pandoc_markdown" },
+        markdown = { "pandoc_markdown" },
+        perl = { "perltidy" },
+        ruby = { { "standardrb", "rubocop" } },
+        sh = { "shfmt" },
+        sql = { "pg_format" },
+        -- LSP-handled:
+        -- elixir = { "mix" },
+        -- go = { "goimports", "gofumpt" },
+      },
+      formatters = {
+        pandoc_markdown = {
+          command = "pandoc",
+          args = { "-f", "markdown", "-t", "markdown" },
+          stdin = true,
+        },
+        shfmt = {
+          prepend_args = function(_, ctx)
+            return { "-i", vim.bo[ctx.buf].shiftwidth }
+          end,
+        },
+        perltidy = {
+          prepend_args = function(_, ctx)
+            return { "-i", vim.bo[ctx.buf].shiftwidth }
+          end,
+        },
+      },
+      format_on_save = function(bufnr)
+        local autoformat_filetypes = { "elixir", "go" }
+
+        if not vim.tbl_contains(autoformat_filetypes, vim.bo[bufnr].filetype) then
+          return
+        end
+
+        return {
+          timeout_ms = 500,
+          lsp_fallback = true,
+        }
+      end
+    },
+  },
+}

mercurial