From 4d94c9cad7be9f8f2924bd2f51bff56c298c65e1 Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Tue, 11 Feb 2025 12:53:27 -0600 Subject: [PATCH] format RON files --- vim/.vim/lua/plugins/conform.lua | 114 ++++++++++++++++++------------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/vim/.vim/lua/plugins/conform.lua b/vim/.vim/lua/plugins/conform.lua index 07b49f6..ea4c512 100644 --- a/vim/.vim/lua/plugins/conform.lua +++ b/vim/.vim/lua/plugins/conform.lua @@ -2,39 +2,51 @@ 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 + 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, + 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 + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false end, { - desc = "Re-enable autoformat-on-save", + 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, + 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, }) +vim.api.nvim_create_user_command("Format", function(args) + local range = nil + if args.count ~= -1 then + local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1] + range = { + start = { args.line1, 0 }, + ["end"] = { args.line2, end_line:len() }, + } + end + require("conform").format({ async = true, lsp_format = "fallback", range = range }) +end, { range = true }) + return { - { - "stevearc/conform.nvim", - event = { "BufWritePre" }, - cmd = { "ConformInfo", "FormatDisable", "FormatEnable" }, - keys = { + { + "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", }, { "fj", ":%!python -m json.tool" }, @@ -45,29 +57,35 @@ return { 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" }, - }, - formatters = { - gdformat = { - prepend_args = { "-l", "100" }, - }, - }, - }, - }, + 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" }, + ron = { "ronfmt" }, + 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" }, + }, + formatters = { + ronfmt = { + command = "ronfmt", + args = { "$FILENAME" }, + stdin = false, + }, + gdformat = { + prepend_args = { "-l", "100" }, + }, + }, + }, + }, }