From 38f84a6dd9e1168170ecb96a6a0b7f20893e1215 Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Thu, 12 Dec 2024 16:28:41 +0000 Subject: [PATCH] fix autoformat --- vim/.vim/lua/config/tmpl.lua | 29 ++++++++++++ vim/.vim/lua/plugins/conform.lua | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 vim/.vim/lua/config/tmpl.lua create mode 100644 vim/.vim/lua/plugins/conform.lua diff --git a/vim/.vim/lua/config/tmpl.lua b/vim/.vim/lua/config/tmpl.lua new file mode 100644 index 0000000..61c5ea2 --- /dev/null +++ b/vim/.vim/lua/config/tmpl.lua @@ -0,0 +1,29 @@ +vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { + group = vim.api.nvim_create_augroup('gotmpl_highlight', { clear = true }), + pattern = '*.tmpl', + callback = function() + local filename = vim.fn.expand('%:t') + local ext = filename:match('.*%.(.-)%.tmpl$') + + -- Add more extension to syntax mappings here if you need to. + local ext_filetypes = { + go = 'go', + html = 'html', + md = 'markdown', + yaml = 'yaml', + yml = 'yaml', + } + + if ext and ext_filetypes[ext] then + -- Set the primary filetype + vim.bo.filetype = ext_filetypes[ext] + + -- Define embedded Go template syntax + vim.cmd([[ + syntax include @gotmpl syntax/gotmpl.vim + syntax region gotmpl start="{{" end="}}" contains=@gotmpl containedin=ALL + syntax region gotmpl start="{%" end="%}" contains=@gotmpl containedin=ALL + ]]) + end + end, +}) diff --git a/vim/.vim/lua/plugins/conform.lua b/vim/.vim/lua/plugins/conform.lua new file mode 100644 index 0000000..8e7504a --- /dev/null +++ b/vim/.vim/lua/plugins/conform.lua @@ -0,0 +1,77 @@ +local use_google = require("utils").use_google + +vim.g.disable_autoformat = false +vim.api.nvim_create_user_command("FormatDisable", function(args) + if args.bang then + -- FormatDisable! will disable formatting just for this buffer + vim.b.disable_autoformat = true + else + vim.g.disable_autoformat = true + end +end, { + desc = "Disable autoformat-on-save", + bang = true, +}) + +vim.api.nvim_create_user_command("FormatEnable", function() + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false +end, { + desc = "Re-enable autoformat-on-save", +}) + +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*", + callback = function(args) + if vim.g.disable_autoformat or vim.b[args.buf].disable_autoformat then + return + end + require("conform").format({ bufnr = args.buf }) + end, +}) + +return { + { + "stevearc/conform.nvim", + event = { "BufWritePre" }, + cmd = { "ConformInfo", "FormatDisable", "FormatEnable" }, + keys = { + -- stylua: ignore + { "fmt", function() require("conform").format({ async = true, lsp_fallback = true }) end, mode = "", desc = "Format buffer", }, + }, + -- This will provide type hinting with LuaLS + ---@module "conform" + ---@type conform.setupOpts + opts = { + log_level = vim.log.levels.DEBUG, + formatters_by_ft = { + + rust = { "rustfmt", lsp_format = "fallback" }, + -- Conform will run multiple formatters sequentially + -- go = { "goimports", "gofmt" }, + -- Use a sub-list to run only the first available formatter + -- javascript = { { "prettierd", "prettier" } }, + lua = { "stylua" }, + -- Conform will run multiple formatters sequentially + python = { "isort", "black" }, + -- Use a sub-list to run only the first available formatter + javascript = { { "prettierd", "prettier" } }, + gdscript = { "gdformat" }, + dashboard = {}, + -- Use the "*" filetype to run formatters on all filetypes. + ["*"] = { "codespell" }, + -- Use the "_" filetype to run formatters on filetypes that don't + -- have other formatters configured. + ["_"] = { "trim_whitespace" }, + }, + format_on_save = function(bufnr) + -- Disable with a global or buffer-local variable + end, + formatters = { + gdformat = { + prepend_args = { "-l", "100" }, + }, + }, + }, + }, +}