mirror of
https://github.com/cap153/nvim.git
synced 2026-07-16 22:16:54 +08:00
ts和js使用deno来格式化
This commit is contained in:
parent
bd07b6ef70
commit
b7c38200f4
4 changed files with 37 additions and 14 deletions
|
|
@ -17,8 +17,15 @@ local formatters_by_ft = {
|
||||||
html = { "djlint" },
|
html = { "djlint" },
|
||||||
sh = { "shfmt" },
|
sh = { "shfmt" },
|
||||||
zsh = { "shfmt" },
|
zsh = { "shfmt" },
|
||||||
|
typescript = { "deno_fmt" },
|
||||||
|
javascript = { "deno_fmt" },
|
||||||
|
markdown = { "deno_fmt_markdown" },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local real_executable_map = {
|
||||||
|
deno_fmt = "deno",
|
||||||
|
deno_fmt_markdown = "deno",
|
||||||
|
}
|
||||||
|
|
||||||
-- 辅助函数:从指定文件类型的配置中提取所有工具名称(去重)
|
-- 辅助函数:从指定文件类型的配置中提取所有工具名称(去重)
|
||||||
local function get_ensure_installed_for_ft(ft, ft_table)
|
local function get_ensure_installed_for_ft(ft, ft_table)
|
||||||
|
|
@ -44,17 +51,33 @@ end
|
||||||
vim.keymap.set({ "n", "x" }, "<leader>f", function()
|
vim.keymap.set({ "n", "x" }, "<leader>f", function()
|
||||||
PackUtils.load(P, function()
|
PackUtils.load(P, function()
|
||||||
require("conform").setup({ -- At a minimum, you will need to set up some formatters by filetype
|
require("conform").setup({ -- At a minimum, you will need to set up some formatters by filetype
|
||||||
formatters_by_ft = formatters_by_ft
|
formatters_by_ft = formatters_by_ft,
|
||||||
})
|
formatters = {
|
||||||
|
deno_fmt_markdown = {
|
||||||
|
inherit = "deno_fmt", -- 继承格式化程序
|
||||||
|
append_args = { "--indent-width", "4" },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
end)
|
end)
|
||||||
local registry = require("mason-registry")
|
|
||||||
local ft = vim.bo.filetype
|
local ft = vim.bo.filetype
|
||||||
local tools = get_ensure_installed_for_ft(ft, formatters_by_ft)
|
local raw_tools = get_ensure_installed_for_ft(ft, formatters_by_ft)
|
||||||
for _, tool in ipairs(tools) do
|
local need_install = false
|
||||||
if not registry.is_installed(tool) then
|
for _, raw_tool in ipairs(raw_tools) do
|
||||||
vim.notify("Installing formatter: " .. tool, vim.log.levels.INFO)
|
local real_tool = real_executable_map[raw_tool] or raw_tool
|
||||||
registry.get_package(tool):install()
|
if vim.fn.executable(real_tool) == 0 then
|
||||||
|
need_install = true
|
||||||
|
local registry = require("mason-registry")
|
||||||
|
if not registry.is_installed(real_tool) then
|
||||||
|
vim.notify("⬇️ Installing formatter: " .. real_tool, vim.log.levels.INFO)
|
||||||
|
registry.get_package(real_tool):install()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
require("conform").format({ async = true, lsp_fallback = true })
|
if need_install then
|
||||||
|
vim.notify("⏳ 格式化工具正在后台安装,请稍后重试...", vim.log.levels.WARN)
|
||||||
|
else
|
||||||
|
require("conform").format({ async = true, lsp_fallback = true })
|
||||||
|
end
|
||||||
end, { desc = "格式化代码(检测安装缺失工具)" })
|
end, { desc = "格式化代码(检测安装缺失工具)" })
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ if vim.g.vscode then return end
|
||||||
local arch = jit and jit.arch or ""
|
local arch = jit and jit.arch or ""
|
||||||
local is_arm = arch:match("arm") or arch:match("aarch64")
|
local is_arm = arch:match("arm") or arch:match("aarch64")
|
||||||
|
|
||||||
local servers = { "lua_ls", "rust_analyzer", "pylsp" }
|
local servers = { "lua_ls", "rust_analyzer", "pylsp", "denols", }
|
||||||
if not is_arm then
|
if not is_arm then
|
||||||
vim.list_extend(servers, { "marksman", "ts_ls", "svelte", "cssls", "html" })
|
vim.list_extend(servers, { "marksman", "svelte", "cssls", "html" })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 2. 插件配置清单
|
-- 2. 插件配置清单
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ vim.api.nvim_create_autocmd({
|
||||||
input = { '%*().-()%*' },
|
input = { '%*().-()%*' },
|
||||||
output = { left = '*', right = '*' }
|
output = { left = '*', right = '*' }
|
||||||
},
|
},
|
||||||
c = {
|
d = {
|
||||||
input = { '`().-()`' },
|
input = { '`().-()`' },
|
||||||
output = { left = '`', right = '`' }
|
output = { left = '`', right = '`' }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@ vim.api.nvim_create_user_command("PeekOpen", function()
|
||||||
PackUtils.load(P, function()
|
PackUtils.load(P, function()
|
||||||
require("peek").setup({
|
require("peek").setup({
|
||||||
port = 9000,
|
port = 9000,
|
||||||
app = { "zen", "-private-window" },
|
-- app = { "zen", "-private-window" },
|
||||||
-- app = { "firefox-esr", "-private-window" },
|
-- app = { "firefox-esr", "-private-window" },
|
||||||
-- app = { "google-chrome-stable", "--app=http://localhost:9000/?theme=dark", "--incognito" },
|
app = { "chromium", "--app=http://localhost:9000/?theme=dark", "--incognito" },
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
require("peek").open()
|
require("peek").open()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue