-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconform.lua
More file actions
298 lines (282 loc) · 10.2 KB
/
Copy pathconform.lua
File metadata and controls
298 lines (282 loc) · 10.2 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
return function()
local settings = require("core.settings")
local disabled_workspaces = settings.format_disabled_dirs
local format_on_save_enabled = settings.format_on_save
local format_notify = settings.format_notify
local format_modifications_only = settings.format_modifications_only
local format_timeout = settings.format_timeout
local block_list = settings.formatter_block_list
-- Load clang_format extra_args from user or default config
local function clang_format_args()
local ok, args = pcall(require, "user.configs.formatters.clang_format")
if not ok then
-- Distinguish "not found" from "has errors"
if type(args) == "string" and not args:find("module .* not found") then
vim.notify("[Conform] Error loading user clang_format config: " .. args, vim.log.levels.ERROR)
end
args = require("completion.formatters.clang_format")
end
return args
end
---Check if the current file is in a disabled workspace
---@param bufnr integer
---@return boolean
local function is_disabled_workspace(bufnr)
local filedir = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":h")
for _, dir in ipairs(disabled_workspaces) do
if vim.regex(vim.fs.normalize(dir)):match_str(filedir) ~= nil then
if format_notify then
vim.notify(
string.format("[Conform] Formatting disabled for files under [%s].", vim.fs.normalize(dir)),
vim.log.levels.WARN,
{ title = "Conform" }
)
end
return true
end
end
return false
end
---The gates a buffer must pass before an automatic format, grouped into one
---named predicate for the format_on_save callback's readability. The
---format_on_save SETTING is not re-checked here: its gate lives at the
---single place the callback is installed (format_on_save = enabled and …).
---@param bufnr integer
---@return boolean
local function autoformat_allowed(bufnr)
return block_list[vim.bo[bufnr].filetype] ~= true
and not is_disabled_workspace(bufnr)
and not vim.g.disable_autoformat
and not vim.b[bufnr].disable_autoformat
end
---Format only git-modified lines using gitsigns hunks + conform range format
---@param bufnr integer
---@return boolean @true if modifications were formatted
local function format_modifications(bufnr)
local ok, gitsigns = pcall(require, "gitsigns")
if not ok then
vim.notify("[Conform] gitsigns unavailable, falling back to full-buffer format", vim.log.levels.WARN)
return false
end
local hunks = gitsigns.get_hunks(bufnr)
if not hunks or #hunks == 0 then
return false
end
-- Format hunks in reverse to avoid line offset issues
local has_error = false
for i = #hunks, 1, -1 do
local hunk = hunks[i]
if hunk.added and hunk.added.count > 0 then
local start_line = hunk.added.start
local end_line = start_line + hunk.added.count - 1
local ok_fmt, err = pcall(require("conform").format, {
bufnr = bufnr,
range = {
start = { start_line, 0 },
["end"] = { end_line, math.huge },
},
quiet = true,
})
if not ok_fmt then
has_error = true
vim.notify(
string.format("[Conform] Failed to format hunk at line %d: %s", start_line, err),
vim.log.levels.WARN,
{ title = "Conform" }
)
end
end
end
if format_notify and not has_error then
vim.notify("[Conform] Formatted changed lines successfully!", vim.log.levels.INFO, { title = "Conform" })
end
return true
end
local tools = require("modules.utils.tools")
require("modules.utils").load_plugin("conform", {
default_format_opts = {
timeout_ms = format_timeout,
lsp_format = "fallback",
},
formatters_by_ft = {
c = { "clang-format" },
cmake = { "cmake_format" },
cpp = { "clang-format" },
cs = { "clang-format" },
css = { "prettier" },
go = { "goimports", "gofumpt" },
cuda = { "clang-format" },
graphql = { "prettier" },
html = { "superhtml" },
javascript = { "prettier" },
javascriptreact = { "prettier" },
json = { "fixjson", "prettier" },
jsonc = {}, -- fixjson/prettier strip comments; fallback to LSP (jsonls) for JSONC formatting
lua = { "stylua" },
markdown = { "mdsf" },
nix = { "nixfmt", "statix" },
objc = { "clang-format" },
objcpp = { "clang-format" },
proto = { "clang-format" },
sh = { "shellharden" },
angular = { "prettier" },
handlebars = { "prettier" },
less = { "prettier" },
scss = { "prettier" },
typescript = { "prettier" },
typescriptreact = { "prettier" },
vue = { "prettier" },
yaml = { "prettier" },
zsh = { "beautysh" },
},
formatters = {
["clang-format"] = {
prepend_args = clang_format_args(),
},
statix = {
command = "statix",
args = { "fix", "--stdin" },
stdin = true,
},
-- prettier: stdin is broken under bun's node shim, so use --write on
-- conform's temp copy (`stdin = false` points $FILENAME at a
-- `.conform.$RANDOM.*` copy; the real file is never touched).
prettier = {
command = "prettier",
args = { "--write", "$FILENAME" },
stdin = false,
},
},
format_on_save = format_on_save_enabled and function(bufnr)
-- Disabled filetypes, disabled workspaces, and the global/buffer toggles
if not autoformat_allowed(bufnr) then
return
end
-- Format only modified lines if enabled
if format_modifications_only then
if format_modifications(bufnr) then
return
end
-- Fall through to full format if no hunks found
end
return {}
end or false,
})
-- Resolve `formatter_deps` (conform formatter names) discovery-first against
-- conform's own registry, so a missing formatter is installed / reported.
-- The probe only drives install/warn — nothing on the save path reads it —
-- so `defer` moves the resolve off the BufWritePre tick that lazy-loaded
-- conform; the resolver itself keeps the same-tick guarantee that Mason's
-- bin dir is on $PATH before the replayed save's spawns.
tools.resolve_runtime_tools("conform.nvim", settings.formatter_deps, function(name)
-- get_formatter_config is conform's @private API; if it vanishes, every
-- formatter is UNVERIFIABLE — report unresolved with the reason (missing
-- bucket, immediate flush) instead of silently classifying them all as
-- self-resolving, which would turn off installs and warnings wholesale.
local conform = require("conform")
if type(conform.get_formatter_config) ~= "function" then
return {
unresolved = true,
reason = "conform.get_formatter_config is unavailable (conform API drift?) — formatters cannot be verified",
}
end
-- get_formatter_config runs a function-form override directly, so pcall keeps a
-- throwing override (a broken config) from being misread as an unknown name.
local ok, config, err = pcall(conform.get_formatter_config, name)
if not ok then
return { broken = tostring(config) }
end
if config then
-- A function-form command resolves per buffer at format time (e.g. the
-- builtin from_node_modules): treat it as self-resolving rather than
-- evaluating it for a representative binary — a node_modules command
-- shouldn't map to a Mason install anyway.
if type(config.command) == "function" then
return { binary = nil }
end
return { binary = config.command }
end
-- (nil, err) is a real formatter with a broken config; bare nil is an unknown name.
if type(err) == "string" then
return { broken = err }
end
-- A function-form override may legitimately return nil for the
-- probe-time buffer (this probe runs on a scheduled tick against
-- whatever buffer happens to be current): its existence proves the
-- name real, but nothing is verifiable — report it unresolved
-- (missing bucket, tailored reason) instead of a typo or a silent pass.
local overrides = conform.formatters
if type(overrides) == "table" and type(overrides[name]) == "function" then
-- The reason rides on the probe result: the phrasing is conform's,
-- not the shared resolver's (nvim-lint shares resolve_runtime_tools).
return { unresolved = true, reason = "config resolves per buffer and could not be verified at startup" }
end
return nil
end, nil, { defer = true })
-- User commands
vim.api.nvim_create_user_command("Format", function(args)
local range = nil
if args.count ~= -1 then
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
range = {
start = { args.line1, 0 },
["end"] = { args.line2, end_line:len() },
}
end
require("conform").format({
async = true,
range = range,
}, function(err)
if not err and format_notify then
vim.notify("[Conform] Format successfully!", vim.log.levels.INFO, { title = "Conform" })
elseif err then
vim.notify(
string.format("[Conform] Format error: %s", err),
vim.log.levels.ERROR,
{ title = "Conform" }
)
end
end)
end, { range = true })
vim.api.nvim_create_user_command("FormatToggle", function()
if vim.g.disable_autoformat then
vim.g.disable_autoformat = false
vim.notify("Format-on-save enabled", vim.log.levels.INFO, { title = "Conform" })
else
vim.g.disable_autoformat = true
vim.notify("Format-on-save disabled", vim.log.levels.WARN, { title = "Conform" })
end
end, {})
vim.api.nvim_create_user_command("FormatterToggleFt", function(opts)
if block_list[opts.args] == nil then
vim.notify(
string.format("[Conform] Formatter for [%s] recorded and disabled.", opts.args),
vim.log.levels.WARN,
{ title = "Conform" }
)
block_list[opts.args] = true
else
block_list[opts.args] = not block_list[opts.args]
vim.notify(
string.format(
"[Conform] Formatter for [%s] %s.",
opts.args,
not block_list[opts.args] and "enabled" or "disabled"
),
not block_list[opts.args] and vim.log.levels.INFO or vim.log.levels.WARN,
{ title = "Conform" }
)
end
end, { nargs = 1, complete = "filetype" })
-- Auto stop shell LSPs for .env files (migrated from null-ls config).
-- Both bashls and shuck attach to .env's `sh` filetype and only add noise there.
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(event)
local bufname = vim.api.nvim_buf_get_name(event.buf)
if bufname:match("%.env$") or bufname:match("%.env%.") then
vim.cmd.LspStop("bashls")
vim.cmd.LspStop("shuck")
end
end,
})
end