mirror of
https://github.com/DefectingCat/nvim
synced 2025-07-15 16:51:33 +00:00
refactor user cmd
This commit is contained in:
@ -1,34 +1,50 @@
|
||||
local user_command = vim.api.nvim_create_user_command
|
||||
|
||||
-- 定义 Difft 命令
|
||||
user_command("Difft", function()
|
||||
vim.cmd("windo diffthis")
|
||||
end, {
|
||||
desc = "windo Diffthis",
|
||||
})
|
||||
|
||||
-- 定义 Diffo 命令
|
||||
user_command("Diffo", function()
|
||||
vim.cmd("windo diffoff")
|
||||
end, {
|
||||
desc = "windo Diffoff",
|
||||
})
|
||||
|
||||
-- Toggle conform format on save
|
||||
user_command("FormatDisable", function(args)
|
||||
if args.bang then
|
||||
-- FormatDisable! will disable formatting just for this buffer
|
||||
-- 处理格式化相关命令的辅助函数
|
||||
local function toggle_formatting(disable_buffer_only, notify)
|
||||
if disable_buffer_only then
|
||||
vim.b.disable_autoformat = true
|
||||
else
|
||||
vim.g.disable_autoformat = true
|
||||
end
|
||||
if notify then
|
||||
vim.notify("autoformat disabled")
|
||||
end
|
||||
end
|
||||
|
||||
local function enable_formatting()
|
||||
vim.b.disable_autoformat = false
|
||||
vim.g.disable_autoformat = false
|
||||
end
|
||||
|
||||
-- 定义 FormatDisable 命令
|
||||
user_command("FormatDisable", function(args)
|
||||
toggle_formatting(args.bang, args.bang)
|
||||
end, {
|
||||
desc = "Disable autoformat-on-save",
|
||||
bang = true,
|
||||
})
|
||||
user_command("FormatEnable", function()
|
||||
vim.b.disable_autoformat = false
|
||||
vim.g.disable_autoformat = false
|
||||
end, {
|
||||
|
||||
-- 定义 FormatEnable 命令
|
||||
user_command("FormatEnable", enable_formatting, {
|
||||
desc = "Re-enable autoformat-on-save",
|
||||
})
|
||||
|
||||
-- 定义 FormatToggle 命令
|
||||
user_command("FormatToggle", function()
|
||||
vim.b.disable_autoformat = not vim.b.disable_autoformat
|
||||
vim.g.disable_autoformat = not vim.g.disable_autoformat
|
||||
@ -38,49 +54,70 @@ user_command("FormatToggle", function()
|
||||
vim.notify("autoformat enabled")
|
||||
end
|
||||
end, {
|
||||
desc = "Re-enable autoformat-on-save",
|
||||
desc = "Toggle autoformat-on-save",
|
||||
})
|
||||
|
||||
-- mason install command
|
||||
-- create install all command
|
||||
-- 优化点:将需要安装的工具列表按照语言或类型分组,提高可读性
|
||||
local ensure_installed = {
|
||||
"gopls",
|
||||
"lua-language-server",
|
||||
"rust-analyzer",
|
||||
"html-lsp",
|
||||
"vue-language-server", -- vue
|
||||
"vtsls", -- typescript
|
||||
"tailwindcss-language-server",
|
||||
"eslint-lsp",
|
||||
"css-lsp",
|
||||
"cssmodules-language-server",
|
||||
"json-lsp",
|
||||
"yaml-language-server",
|
||||
"docker-compose-language-service",
|
||||
"dockerfile-language-server",
|
||||
"bash-language-server",
|
||||
"clangd",
|
||||
"lemminx", -- xml svg
|
||||
"deno",
|
||||
-- tools
|
||||
"prettier", -- prettier formatter
|
||||
"stylua", -- lua formatter
|
||||
"isort", -- python formatter
|
||||
-- "black", -- python formatter
|
||||
"pylint",
|
||||
"shfmt",
|
||||
"goimports",
|
||||
"gofumpt",
|
||||
"golines",
|
||||
"gomodifytags",
|
||||
"impl", -- go
|
||||
"clang-format",
|
||||
"taplo", -- toml
|
||||
"delve", -- golang debug adapter
|
||||
-- LSP servers
|
||||
lsp_servers = {
|
||||
"gopls",
|
||||
"lua-language-server",
|
||||
"rust-analyzer",
|
||||
"html-lsp",
|
||||
"vue-language-server", -- vue
|
||||
"vtsls", -- typescript
|
||||
"tailwindcss-language-server",
|
||||
"eslint-lsp",
|
||||
"css-lsp",
|
||||
"cssmodules-language-server",
|
||||
"json-lsp",
|
||||
"yaml-language-server",
|
||||
"docker-compose-language-service",
|
||||
"dockerfile-language-server",
|
||||
"bash-language-server",
|
||||
"clangd",
|
||||
"lemminx", -- xml svg
|
||||
"deno",
|
||||
},
|
||||
-- Formatters
|
||||
formatters = {
|
||||
"prettier", -- prettier formatter
|
||||
"stylua", -- lua formatter
|
||||
"isort", -- python formatter
|
||||
-- "black", -- python formatter
|
||||
"shfmt",
|
||||
"goimports",
|
||||
"gofumpt",
|
||||
"golines",
|
||||
"clang-format",
|
||||
"taplo", -- toml
|
||||
},
|
||||
-- Linters
|
||||
linters = {
|
||||
"pylint",
|
||||
},
|
||||
-- Tools
|
||||
tools = {
|
||||
"gomodifytags",
|
||||
"impl", -- go
|
||||
"delve", -- golang debug adapter
|
||||
},
|
||||
}
|
||||
|
||||
-- 扁平化 ensure_installed 列表
|
||||
local flattened_ensure_installed = {}
|
||||
for _, group in pairs(ensure_installed) do
|
||||
for _, tool in ipairs(group) do
|
||||
table.insert(flattened_ensure_installed, tool)
|
||||
end
|
||||
end
|
||||
|
||||
-- Create user command to synchronously install all Mason tools in `opts.ensure_installed`.
|
||||
user_command("MasonInstallAll", function()
|
||||
for _, tool in ipairs(ensure_installed) do
|
||||
for _, tool in ipairs(flattened_ensure_installed) do
|
||||
vim.cmd("MasonInstall " .. tool)
|
||||
end
|
||||
end, {})
|
||||
|
Reference in New Issue
Block a user