aboutsummaryrefslogtreecommitdiff
path: root/lua/config/conform.lua
blob: a65a9c4cb2231251382ab45882965bcf629cb161 (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
vim.pack.add({
	"https://github.com/stevearc/conform.nvim",
})

vim.keymap.set("n", "<leader>bf", function()
	require("conform").format({
		async = true,
		lsp_format = "fallback",
	})
end)

require("conform").setup({
	formatters_by_ft = {
		lua = { "stylua" },
		swift = { "swift" },
		javascript = { "prettierd", "prettier" },
		javascriptreact = { "prettierd", "prettier" },
		typescript = { "prettierd", "prettier" },
		typescriptreact = { "prettierd", "prettier" },
		json = { "prettierd", "prettier" },
		html = { "prettierd", "prettier" },
		css = { "prettierd", "prettier" },
		svelte = { "prettierd", "prettier" },
		astro = { "prettierd", "prettier" },
		rust = { "rustfmt", lsp_format = "fallbackk" },
		go = { "goimports", "gofmt" },
		proto = { "buf", lsp_format = "fallback" },
		odin = { "odinfmt" },
	},
	format_on_save = function(bufnr)
		if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
			return
		end
		return { timeout_ms = 1001, lsp_format = "fallback" }
	end,
})

vim.api.nvim_create_user_command("FormatDisable", function(args)
	if args.bang then
		vim.b.disable_autoformat = true
	else
		vim.g.disable_autoformat = true
	end
end, { desc = "Disable autoformat-on-save", bang = true })
vim.api.nvim_create_user_command("FormatEnable", function()
	vim.b.disable_autoformat = false
	vim.g.disable_autoformat = false
end, { desc = "Re-enable autoformat-on-save" })

vim.keymap.set("n", "<leader>afd", "<CMD>FormatDisable<CR>")
vim.keymap.set("n", "<leader>afD", "<CMD>FormatDisable!<CR>")
vim.keymap.set("n", "<leader>afe", "<CMD>FormatEnable<CR>")