Neovim中denols与ts_ls同时附着TypeScript项目的问题求助
Denols与ts_ls在Neovim中重复附着缓冲区的问题
我在使用Neovim的lspconfig插件管理LSP服务器时,遇到了denols和ts_ls同时附着到TypeScript文件缓冲区的冲突问题,具体场景如下:
- 包含
deno.json的Deno项目中,denols与ts_ls均会附着到同一缓冲区 - 包含
package.json的Node.js项目中,两者仍会同时附着 - 非项目文件或单文件场景下,两者依然会自动附着
即使完全移除lspconfig的配置,这两个LSP服务器仍会自动附着到缓冲区。我曾尝试过Deno官方文档提供的简易配置:
local lspconfig = require("lspconfig") local util = lspconfig.util lspconfig.denols.setup({ root_dir = util.root_pattern("deno.json", "deno.jsonc"), -- Deno projects only single_file_support = false, }) lspconfig.tsserver.setup({ root_dir = util.root_pattern("package.json"), -- Node.js projects only single_file_support = false, })
当前我的lspconfig配置如下:
-- Deno Language Server lspconfig.denols.setup({ capabilities = capabilities, on_attach = function(client) -- Enable document formatting for Deno if client.server_capabilities.documentFormattingProvider then client.server_capabilities.documentFormattingProvider = true end end, root_dir = lspconfig.util.root_pattern("deno.json", "deno.jsonc"), settings = { deno = { enable = true, lint = true, fmt = true, inlayHints = { parameterNames = { enabled = "all", suppressWhenArgumentMatchesName = true }, parameterTypes = { enabled = false }, variableTypes = { enabled = false, suppressWhenTypeMatchesName = false }, propertyDeclarationTypes = { enabled = false }, functionLikeReturnTypes = { enabled = true }, enumMemberValues = { enabled = true }, }, }, }, }) -- Ts_ls Language Server lspconfig.ts_ls.setup({ capabilities = capabilities, on_attach = on_attach, root_dir = util.root_pattern("package.json"), single_file_support = false, settings = { typescript = { format = { enable = false, }, }, javascript = { format = { enable = false, }, }, completions = { completeFunctionCalls = true, }, }, })
通过:LspInfo查看的缓冲区LSP信息如下:
LSP configs active in this buffer (bufnr: 4) ~ - Language client log: ~/.local/state/nvim/lsp.log - Detected filetype: `typescript` - 2 client(s) attached to this buffer - Client: `denols` (id: 1, bufnr: [4]) root directory: /mnt/library/development/projects/obsidian-programming-advices/ filetypes: javascript, javascriptreact, javascript.jsx, typescript, typescriptreact, typescript.tsx cmd: ~/.local/share/nvim/mason/bin/deno lsp version: `deno 2.1.5 (stable, release, x86_64-unknown-linux-gnu)` executable: true autostart: true - Client: `ts_ls` (id: 2, bufnr: [4]) root directory: /mnt/library/development/projects/obsidian-programming-advices/ filetypes: javascript, javascriptreact, javascript.jsx, typescript, typescriptreact, typescript.tsx cmd: ~/.local/share/nvim/mason/bin/typescript-language-server --stdio version: `4.3.3` executable: true autostart: true
目前我只能通过以下临时方案规避问题:
vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) local client = vim.lsp.get_client_by_id(args.data.client_id) local is_node_project = vim.fn.filereadable("package.json") == 1 local is_deno_project = vim.fn.filereadable("deno.json") == 1 -- Stop `denols` in Node.js projects if client.name == "denols" and is_node_project then client.stop() end -- Stop `tsserver` in Deno projects if client.name == "ts_ls" and is_deno_project then client.stop() end end, })
我需要一个规范的解决方案。经排查,非Deno项目中两者同时附着可能源于denols的根目录检测不当,我曾尝试严格化两者的根目录检测并通过:LspInfo验证,但问题仍未解决。
内容的提问来源于stack exchange,提问作者Muhannad Elbolaky
相关产品推荐
相关产品推荐

