Repository navigation
Expand file tree
/
Copy pathconform.lua
More file actions
138 lines (132 loc) · 3.03 KB
/
Copy pathconform.lua
File metadata and controls
138 lines (132 loc) · 3.03 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
-- Configuration for conform.nvim
return function()
local conform = require("conform")
conform.setup({
formatters_by_ft = {
-- C/C++ files
c = { "clang_format" },
cpp = { "clang_format" },
h = { "clang_format" },
-- Lua files
lua = { "lua_format" },
-- JSON files
json = { "jq" },
jsonc = { "jq" },
-- ProtoBuf files
proto = { "buf_format" },
-- Bash/Shell files
sh = { "shfmt" },
bash = { "shfmt" },
-- Python files
python = { "black" },
-- Javascript files
javascript = { "prettier" },
},
formatters = {
-- C/C++ formatter
clang_format = {
command = "/usr/bin/clang-format-18",
args = {
"--assume-filename=$FILENAME",
"--style=file",
"--fallback-style=none",
},
stdin = true,
cwd = require("conform.util").root_file({
".clang-format",
".clang-format-18",
".clang-tidy",
"compile_commands.json",
"Makefile",
"CMakeLists.txt",
}),
},
-- Lua formatter
lua_format = {
command = "lua-format",
args = {
"--indent-width=2",
"--tab-width=2",
"--continuation-indent-width=2",
"--spaces-before-call=1",
-- "--double-quote-to-single-quote=true",
"--column-limit=80",
},
stdin = true,
},
-- JSON formatter
jq = {
command = "jq",
args = { "." },
stdin = true,
},
-- ProtoBuf formatter
buf_format = {
command = "buf",
args = { "format", "-" },
stdin = true,
},
-- Bash/Shell formatter
shfmt = {
command = "shfmt",
args = {
"-i", "2",
"-bn",
"-ci",
"-sr",
},
stdin = true,
},
-- Python formatter - Black
black = {
command = "black",
args = {
"--quiet", -- Suppress output
"-", -- Read from stdin
},
stdin = true,
},
-- Javascript formatter - Prettierd
prettierd = {
command = "prettierd",
args = { "$FILENAME" },
stdin = true,
},
},
})
-- Keymap to format current buffer
vim.keymap.set("n", "<leader>cf", function()
conform.format({
lsp_fallback = true,
async = false,
timeout_ms = 1000,
})
end, { desc = "Format file" })
-- Visual mode keymap to format selection
vim.keymap.set("v", "<leader>cf", function()
conform.format({
range = true,
async = false,
timeout_ms = 1000,
})
end, { desc = "Format selection" })
-- Format on save for various file types
-- vim.api.nvim_create_autocmd("BufWritePre", {
-- pattern = {
-- "*.c", "*.cpp", "*.h", "*.hpp", -- C/C++
-- "*.lua", -- Lua
-- "*.json", "*.jsonc", -- JSON
-- "*.proto", -- ProtoBuf
-- "*.sh", "*.bash", -- Shell scripts
-- },
-- callback = function(args)
-- conform.format({
-- bufnr = args.buf,
-- lsp_fallback = true,
-- async = false,
-- timeout_ms = 1000,
--
-- })
-- end,
-- })
end