Files
dotfiles/nvim/lua/plugins/conform.lua
T
2026-09-25 20:54:28 +08:00

45 lines
1.6 KiB
Lua

-- =============================================================================
-- conform.lua — 自动格式化
-- =============================================================================
return {
{
"stevearc/conform.nvim",
event = { "BufWritePre" },
cmd = { "ConformInfo" },
keys = {
{ "<leader>lf", function() require("conform").format({ async = true, lsp_fallback = true }) end, desc = "格式化" },
},
opts = {
format_on_save = {
timeout_ms = 500,
lsp_fallback = true,
},
formatters_by_ft = {
rust = { "rustfmt" },
lua = { "stylua" },
c = { "clang-format" },
cpp = { "clang-format" },
python = { "black", "isort" },
javascript = { "prettier" },
typescript = { "prettier" },
json = { "prettier" },
yaml = { "prettier" },
markdown = { "prettier" },
html = { "prettier" },
css = { "prettier" },
go = { "gofmt", "goimports" },
sh = { "shfmt" },
},
formatters = {
["clang-format"] = {
-- 用 prepend_args (不是 args!): 保留 conform 默认的 "-assume-filename $FILENAME".
-- 若用 args 会覆盖默认值, 导致 clang-format 读 stdin 时丢失文件名 → 语言检测/样式查找出问题.
-- 优先读项目 .clang-format 文件; 没有该文件时用此内联 4 空格样式兜底.
prepend_args = { "--style={BasedOnStyle: LLVM, IndentWidth: 4, TabWidth: 4, UseTab: Never, ColumnLimit: 0}" },
},
},
},
},
}