nvim: factor out autoformat and add indicator to lualine
This commit is contained in:
parent
ca89048a67
commit
e1adca48e0
3 changed files with 80 additions and 32 deletions
|
@ -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
|
||||
{ "<leader>FT", toggle_format_on_write, desc = icon.." [F]ormat on write [T]oggle" },
|
||||
{ "<leader>FF", "<cmd>Format<cr>", desc = icon.." [F]ormat [F]ile" },
|
||||
{ "<c-f>", "<cmd>Format<cr>", mode = "i", desc = icon.." [f]ormat file" },
|
||||
-- stylua: ignore end
|
||||
},
|
||||
keys = function()
|
||||
local icon = require("fschauen.util.icons").ui.Format
|
||||
return {
|
||||
{
|
||||
"<leader>F",
|
||||
require("fschauen.util.autoformat").toggle,
|
||||
desc = icon .. " Toggle auto [F]ormat on write",
|
||||
},
|
||||
{
|
||||
"<leader>=",
|
||||
"<cmd>Format<cr>",
|
||||
desc = icon .. " format file",
|
||||
},
|
||||
{
|
||||
"<c-f>",
|
||||
"<cmd>Format<cr>",
|
||||
mode = "i",
|
||||
desc = icon .. " [f]ormat file",
|
||||
},
|
||||
}
|
||||
end,
|
||||
|
||||
opts = function()
|
||||
local builtin = require("formatter.filetypes")
|
||||
|
|
|
@ -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",
|
||||
},
|
||||
|
|
54
config/nvim/lua/fschauen/util/autoformat.lua
Normal file
54
config/nvim/lua/fschauen/util/autoformat.lua
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue