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 shfmt = function()
|
||||||
local indent = 0 -- Assume tabs initially.
|
local indent = 0 -- Assume tabs initially.
|
||||||
if vim.opt.expandtab:get() then
|
if vim.o.expandtab then
|
||||||
local shiftwidth = vim.opt.shiftwidth:get()
|
local shiftwidth = vim.opt.shiftwidth:get()
|
||||||
if shiftwidth == 0 then
|
if shiftwidth == 0 then
|
||||||
indent = vim.opt.tabstop:get()
|
indent = vim.o.tabstop
|
||||||
else
|
else
|
||||||
indent = shiftwidth
|
indent = shiftwidth
|
||||||
end
|
end
|
||||||
|
@ -21,28 +21,6 @@ local shfmt = function()
|
||||||
}
|
}
|
||||||
end
|
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 {
|
return {
|
||||||
"mhartington/formatter.nvim",
|
"mhartington/formatter.nvim",
|
||||||
|
|
||||||
|
@ -53,13 +31,27 @@ return {
|
||||||
"FormatWriteLock",
|
"FormatWriteLock",
|
||||||
},
|
},
|
||||||
|
|
||||||
keys = {
|
keys = function()
|
||||||
-- stylua: ignore start
|
local icon = require("fschauen.util.icons").ui.Format
|
||||||
{ "<leader>FT", toggle_format_on_write, desc = icon.." [F]ormat on write [T]oggle" },
|
return {
|
||||||
{ "<leader>FF", "<cmd>Format<cr>", desc = icon.." [F]ormat [F]ile" },
|
{
|
||||||
{ "<c-f>", "<cmd>Format<cr>", mode = "i", desc = icon.." [f]ormat file" },
|
"<leader>F",
|
||||||
-- stylua: ignore end
|
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()
|
opts = function()
|
||||||
local builtin = require("formatter.filetypes")
|
local builtin = require("formatter.filetypes")
|
||||||
|
|
|
@ -4,10 +4,11 @@ return {
|
||||||
dependencies = "nvim-tree/nvim-web-devicons",
|
dependencies = "nvim-tree/nvim-web-devicons",
|
||||||
|
|
||||||
opts = function()
|
opts = function()
|
||||||
local component = require("lualine.fschauen.component")
|
|
||||||
local icons = require("fschauen.util.icons")
|
local icons = require("fschauen.util.icons")
|
||||||
local window = require("fschauen.window")
|
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 diagnostics = component.colored_if_focused("diagnostics")
|
||||||
local filetype = component.colored_if_focused("filetype")
|
local filetype = component.colored_if_focused("filetype")
|
||||||
local status = component.colored_if_focused("fschauen.status")
|
local status = component.colored_if_focused("fschauen.status")
|
||||||
|
@ -42,6 +43,7 @@ return {
|
||||||
lualine_y = {
|
lualine_y = {
|
||||||
spell,
|
spell,
|
||||||
wrap,
|
wrap,
|
||||||
|
autoformat,
|
||||||
{ "fileformat", cond = window.is_medium },
|
{ "fileformat", cond = window.is_medium },
|
||||||
"progress",
|
"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