From c76b4d8e681389408a9f14d666e205fab2ead88d Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Tue, 9 Apr 2024 11:35:15 -0300 Subject: [PATCH] feat: add formatter.nvim --- after/plugin/lsp.lua | 51 +++++++++++++++++++++++++++++++++++++------- lazy-lock.json | 1 + lua/pluginlist.lua | 1 + 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/after/plugin/lsp.lua b/after/plugin/lsp.lua index 187ebac..ee17fe8 100644 --- a/after/plugin/lsp.lua +++ b/after/plugin/lsp.lua @@ -15,15 +15,15 @@ local on_attach = function(_, bufnr) -- bmap('K', vim.lsp.buf.hover, 'Hover docs'); bmap('', vim.lsp.buf.signature_help, 'Signature docs'); - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, {}); + -- vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) + -- vim.lsp.buf.format() + -- end, {}); - local format_is_enabled = true; - vim.api.nvim_buf_create_user_command('AutoFormatToggle', function() - format_is_enabled = not format_is_enabled; - print('Setting autoformatting to:' .. tostring(format_is_enabled)); - end, {}); + -- local format_is_enabled = true; + -- vim.api.nvim_buf_create_user_command('AutoFormatToggle', function() + -- format_is_enabled = not format_is_enabled; + -- print('Setting autoformatting to:' .. tostring(format_is_enabled)); + -- end, {}); end require('which-key').register({ @@ -86,3 +86,38 @@ require('mason-lspconfig').setup_handlers({ }); end }); + +local formatter_util = require("formatter.util") +vim.api.nvim_create_augroup("__formatter__", { clear = true }) +vim.api.nvim_create_autocmd("BufWritePost", { + group = "__formatter__", + command = ":FormatWrite", +}) +require("formatter").setup({ + logging = true, + log_level = vim.log.levels.WARN, + filetype = { + lua = { + require("formatter.filetypes.lua").stylua, + function() + return { + exe = "stylua", + args = { + "--search-parent-directories", + "--stdin-filepath", + formatter_util.escape_path(formatter_util.get_current_buffer_file_path()), + "--", + "-", + }, + stdin = true, + } + end, + }, + nix = { + require("formatter.filetypes.nix").alejandra, + }, + ["*"] = { + require("formatter.filetypes.any").remove_trailing_whitespace, + }, + }, +}) diff --git a/lazy-lock.json b/lazy-lock.json index b1ce89a..9cde7f7 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -8,6 +8,7 @@ "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, "conform.nvim": { "branch": "master", "commit": "75e7c5c7eb5fbd53f8b12dc420b31ec70770b231" }, "fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" }, + "formatter.nvim": { "branch": "master", "commit": "ad246d34ce7a32f752071ed81b09b94e6b127fad" }, "friendly-snippets": { "branch": "main", "commit": "69a2c1675b66e002799f5eef803b87a12f593049" }, "gitsigns.nvim": { "branch": "main", "commit": "3e6e91b09f0468c32d3b96dcacf4b947f037ce25" }, "hover.nvim": { "branch": "main", "commit": "bbd59ddfae4e64459944acf2abcda4d81ba8bac6" }, diff --git a/lua/pluginlist.lua b/lua/pluginlist.lua index 9ecbebf..b3d34e6 100644 --- a/lua/pluginlist.lua +++ b/lua/pluginlist.lua @@ -196,6 +196,7 @@ return { event = { 'InsertLeave', 'TextChanged', }, opts = {}, }, + { 'mhartington/formatter.nvim' }, { 'tpope/vim-fugitive', }, { 'tpope/vim-rhubarb', }, {