summaryrefslogtreecommitdiffstats
path: root/plugin/50-format.lua
blob: efcaea6998a0b3563baced9e9df707f4b8a81b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
--
-- 50-format.lua
--
-- * Registers a stdin formatter command for each supported file type.
-- * Formatting is applied on write by the dotfiles.format engine; toggle it per buffer with
--   `FormatToggle`.
--
-- User commands:
--   FormatToggle: toggle formatting on write for the current buffer
--

local format = require("dotfiles.format")

format.register("sh", { "shfmt", "-" })
format.register("bash", { "shfmt", "-" })
format.register("lua", { "stylua", "--search-parent-directories", "-" })
format.register("markdown", { "mdformat", "-" })
format.register("python", { "black", "-" })

-- Toggle formatting on write for the current buffer.
local function toggle_format()
	vim.b.format = not vim.b.format
end

----------------------------------------------------------------------------------------------------

vim.api.nvim_create_user_command("FormatToggle", toggle_format, { desc = "Toggle format on save" })