diff --git a/config/nvim/lua/fschauen/plugins/formatter.lua b/config/nvim/lua/fschauen/plugins/formatter.lua index 122e2b5..9d01470 100644 --- a/config/nvim/lua/fschauen/plugins/formatter.lua +++ b/config/nvim/lua/fschauen/plugins/formatter.lua @@ -1,9 +1,9 @@ local shfmt = function() local indent = 0 -- Assume tabs initially. - if vim.opt.expandtab:get() then + if vim.o.expandtab then local shiftwidth = vim.opt.shiftwidth:get() if shiftwidth == 0 then - indent = vim.opt.tabstop:get() + indent = vim.o.tabstop else indent = shiftwidth end @@ -21,28 +21,6 @@ local shfmt = function() } end -local toggle_format_on_write = (function() - local augroup = nil - return function() - if augroup then - vim.api.nvim_del_augroup_by_id(augroup) - augroup = nil - vim.notify("Format on write DISABLED", vim.log.levels.WARN) - else - augroup = vim.api.nvim_create_augroup("fschauen.format_on_write", { clear = true }) - vim.api.nvim_create_autocmd("BufWritePost", { - desc = "Format files on write.", - group = augroup, - pattern = "*", - command = ":FormatWrite", - }) - vim.notify("Format on write enabled", vim.log.levels.INFO) - end - end -end)() - -local icon = require("fschauen.util.icons").ui.Format - return { "mhartington/formatter.nvim", @@ -53,13 +31,27 @@ return { "FormatWriteLock", }, - keys = { - -- stylua: ignore start - { "FT", toggle_format_on_write, desc = icon.." [F]ormat on write [T]oggle" }, - { "FF", "Format", desc = icon.." [F]ormat [F]ile" }, - { "", "Format", mode = "i", desc = icon.." [f]ormat file" }, - -- stylua: ignore end - }, + keys = function() + local icon = require("fschauen.util.icons").ui.Format + return { + { + "F", + require("fschauen.util.autoformat").toggle, + desc = icon .. " Toggle auto [F]ormat on write", + }, + { + "=", + "Format", + desc = icon .. " format file", + }, + { + "", + "Format", + mode = "i", + desc = icon .. " [f]ormat file", + }, + } + end, opts = function() local builtin = require("formatter.filetypes") diff --git a/config/nvim/lua/fschauen/plugins/lualine.lua b/config/nvim/lua/fschauen/plugins/lualine.lua index 41acf1e..bcef571 100644 --- a/config/nvim/lua/fschauen/plugins/lualine.lua +++ b/config/nvim/lua/fschauen/plugins/lualine.lua @@ -4,10 +4,11 @@ return { dependencies = "nvim-tree/nvim-web-devicons", opts = function() - local component = require("lualine.fschauen.component") local icons = require("fschauen.util.icons") local window = require("fschauen.window") + local autoformat = require("fschauen.util.autoformat").lualine() + local component = require("lualine.fschauen.component") local diagnostics = component.colored_if_focused("diagnostics") local filetype = component.colored_if_focused("filetype") local status = component.colored_if_focused("fschauen.status") @@ -42,6 +43,7 @@ return { lualine_y = { spell, wrap, + autoformat, { "fileformat", cond = window.is_medium }, "progress", }, diff --git a/config/nvim/lua/fschauen/util/autoformat.lua b/config/nvim/lua/fschauen/util/autoformat.lua new file mode 100644 index 0000000..922ebf8 --- /dev/null +++ b/config/nvim/lua/fschauen/util/autoformat.lua @@ -0,0 +1,54 @@ +local M = {} + +M._augroup = nil + +---Whether autoformatting is enabled. +---@return boolean +local is_enabled = function() return M._augroup ~= nil end + +---Disable autoformatting. +M.disable = function() + if not is_enabled() then return end + vim.api.nvim_del_augroup_by_id(M._augroup) + M._augroup = nil +end + +---Enable autoformatting. +M.enable = function() + if is_enabled() then return end + + local ok, formatter = pcall(require, "formatter.format") + if not ok then + vim.notify("formatter.nvim not installed!", vim.log.levels.ERROR) + return + end + + M._augroup = vim.api.nvim_create_augroup("fschauen.autoformat", { clear = true }) + vim.api.nvim_create_autocmd("BufWritePost", { + desc = "Format files on write.", + group = M._augroup, + pattern = "*", + callback = function() formatter.format("", nil, 1, vim.fn.line("$")) end, + }) +end + +---Toggle autoformatting. +M.toggle = function() + if is_enabled() then + M.disable() + else + M.enable() + end +end + +---Crate a lualine component that shows an icon when autoformatting is enabled. +---@return table component +M.lualine = function() + local icon = require("fschauen.util.icons").ui.Format + return { + function() return icon end, + cond = is_enabled, + } +end + +return M