Add conform.nvim for auto-formatting
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
861fd3c03c
commit
a282113ccf
45
lua/lsp.lua
45
lua/lsp.lua
@ -1,5 +1,45 @@
|
|||||||
local lazy = require("lazy")
|
local lazy = require("lazy")
|
||||||
|
|
||||||
|
-- conform.nvim - BufWritePre 懒加载
|
||||||
|
local function setup_conform()
|
||||||
|
local function biome_or_prettier()
|
||||||
|
if vim.fs.find({ "biome.json", "biome.jsonc" }, { upward = true, stop = vim.uv.os_homedir() })[1] then
|
||||||
|
return { "biome-check", "biome", stop_after_first = true }
|
||||||
|
end
|
||||||
|
return { "prettier" }
|
||||||
|
end
|
||||||
|
|
||||||
|
require("conform").setup({
|
||||||
|
formatters_by_ft = {
|
||||||
|
lua = { "stylua" },
|
||||||
|
go = { "gofumpt", "goimports" },
|
||||||
|
javascript = biome_or_prettier,
|
||||||
|
typescript = biome_or_prettier,
|
||||||
|
javascriptreact = biome_or_prettier,
|
||||||
|
typescriptreact = biome_or_prettier,
|
||||||
|
json = biome_or_prettier,
|
||||||
|
css = { "prettier" },
|
||||||
|
html = { "prettier" },
|
||||||
|
markdown = { "prettier" },
|
||||||
|
toml = { "taplo" },
|
||||||
|
},
|
||||||
|
format_on_save = function(bufnr)
|
||||||
|
if vim.b[bufnr].autoformat == false or vim.g.autoformat == false then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
return { timeout_ms = 500, lsp_fallback = true }
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
lazy.on_event("conform", "BufWritePre", "*", function(args)
|
||||||
|
setup_conform()
|
||||||
|
-- 首次保存时手动触发格式化(setup 注册的 autocmd 不会在同一次事件中生效)
|
||||||
|
if vim.b[args.buf].autoformat ~= false and vim.g.autoformat ~= false then
|
||||||
|
require("conform").format({ bufnr = args.buf, timeout_ms = 500, lsp_fallback = true })
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- mason + LSP 配置延迟到 VimEnter,避免启动时加载
|
-- mason + LSP 配置延迟到 VimEnter,避免启动时加载
|
||||||
lazy.on_event("lsp", "VimEnter", "*", function()
|
lazy.on_event("lsp", "VimEnter", "*", function()
|
||||||
require("mason").setup()
|
require("mason").setup()
|
||||||
@ -34,7 +74,10 @@ end)
|
|||||||
-- LSP keymaps
|
-- LSP keymaps
|
||||||
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" })
|
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { desc = "Go to definition" })
|
||||||
vim.keymap.set("n", "gh", vim.lsp.buf.hover, { desc = "Hover" })
|
vim.keymap.set("n", "gh", vim.lsp.buf.hover, { desc = "Hover" })
|
||||||
vim.keymap.set("n", "<leader>fm", vim.lsp.buf.format, { desc = "Format Local buffer" })
|
vim.keymap.set("n", "<leader>fm", function()
|
||||||
|
lazy.load("conform", setup_conform)
|
||||||
|
require("conform").format({ lsp_fallback = true })
|
||||||
|
end, { desc = "Format buffer" })
|
||||||
vim.keymap.set("n", "df", vim.diagnostic.open_float, { desc = "Show line diagnostics" })
|
vim.keymap.set("n", "df", vim.diagnostic.open_float, { desc = "Show line diagnostics" })
|
||||||
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, { desc = "Code action" })
|
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, { desc = "Code action" })
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ vim.pack.add({
|
|||||||
{ src = "https://github.com/nvim-treesitter/nvim-treesitter", branch = "main" },
|
{ src = "https://github.com/nvim-treesitter/nvim-treesitter", branch = "main" },
|
||||||
"https://github.com/neovim/nvim-lspconfig",
|
"https://github.com/neovim/nvim-lspconfig",
|
||||||
"https://github.com/mason-org/mason.nvim",
|
"https://github.com/mason-org/mason.nvim",
|
||||||
|
"https://github.com/stevearc/conform.nvim",
|
||||||
"https://github.com/tpope/vim-fugitive",
|
"https://github.com/tpope/vim-fugitive",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
{
|
{
|
||||||
"plugins": {
|
"plugins": {
|
||||||
|
"conform.nvim": {
|
||||||
|
"rev": "619363c30309d29ffa631e67c8183f2a72caa373",
|
||||||
|
"src": "https://github.com/stevearc/conform.nvim"
|
||||||
|
},
|
||||||
"friendly-snippets": {
|
"friendly-snippets": {
|
||||||
"rev": "6cd7280adead7f586db6fccbd15d2cac7e2188b9",
|
"rev": "6cd7280adead7f586db6fccbd15d2cac7e2188b9",
|
||||||
"src": "https://github.com/rafamadriz/friendly-snippets"
|
"src": "https://github.com/rafamadriz/friendly-snippets"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user