summaryrefslogtreecommitdiff
path: root/after/plugin/conform.lua
blob: 7a8bbce682a91ca2c3dd5cc7558548454fdbf1d2 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
local conform = require("conform")
conform.setup({
	formatters_by_ft = {
		lua = { "stylua" },
		-- html = { "prettier" },
		go = { "goimports", "gofmt" },
	},
	format_on_save = {
		-- These options will be passed to conform.format()
		timeout_ms = 500,
		lsp_format = "never",
	},
})

conform.formatters["clang-format"] = {
	prepend_args = {
		"--style",
		"{"
			.. "IndentWidth: 4, "
			.. "AlignAfterOpenBracket: BlockIndent, "
			.. "AlignArrayOfStructures: Right, "
			.. "BreakBeforeBraces: Linux, "
			.. "PointerAlignment: Left,"
			.. "AllowShortIfStatementsOnASingleLine: true, "
			.. "AllowShortLoopsOnASingleLine: true, "
			.. "AllowAllArgumentsOnNextLine: true, "
			.. "AllowShortCaseLabelsOnASingleLine: true, "
			.. "BreakAfterReturnType: AllDefinitions, "
			.. "ColumnLimit: "
			.. vim.o.tw
			.. "}",
	},
}

-- ID of autocmd for CFormat
vim.b.CFormatID = nil
-- Enable formatting on save for C only when using the :CFormat command
vim.api.nvim_create_user_command("CFormat", function()
	if vim.b.CFormatID == nil then
		vim.b.CFormatID = vim.api.nvim_create_autocmd("BufWritePre", {
			buffer = 0,
			callback = function()
				conform.format({ formatters = { "clang-format" } })
			end,
		})
		conform.format({ formatters = { "clang-format" } })
		print("Auto formatting enabled.")
	else
		vim.api.nvim_del_autocmd(vim.b.CFormatID)
		print("Auto formatting disabled.")
		vim.b.CFormatID = nil
	end
end, {})