Neovim 选择语言提示

7
我正在使用带有LSP的Neovim,但保存任何tsx文件时遇到问题,会不断提示我选择语言服务器:

enter image description here

这是我配置语言服务器的方式

lspinstall.setup()
local servers = lspinstall.installed_servers()

for _, lsp in ipairs(servers) do
  if lsp == 'tsserver' then
    require('lsp.tsserver')
  elseif lsp == 'efm' then
    require('lsp.efm')
  elseif lsp == 'html' then
    require('lsp.html')
  else
    nvim_lsp[lsp].setup {on_attach = on_attach, settings = {Lua = {diagnostics = {globals = {'vim'}}}}}
  end
end

如果我执行:LspInfo,我会看到屏幕截图中看到的2个服务器。
EFM配置
local lspconfig = require 'lspconfig'

local prettier = {formatCommand = './node_modules/.bin/prettier --config-precedence prefer-file --stdin-filepath ${INPUT}', formatStdin = true}
local luaFormat = {
  formatCommand = 'lua-format -i --no-keep-simple-function-one-line --column-limit=120 --indent-width=2 --double-quote-to-single-quote',
  formatStdin = true
}

lspconfig.efm.setup {
  -- cmd = {'efm-langserver', '-logfile', '/tmp/efm.log', '-loglevel', '5'},
  on_attach = on_attach,
  init_options = {documentFormatting = true},
  filetypes = {'javascriptreact', 'javascript', 'lua', 'typescriptreact', 'typescript'},
  settings = {
    rootMarkers = {'.git/'},
    languages = {lua = {luaFormat}, typescript = {prettier}, typescriptreact = {prettier}}
  }
}

typescript配置

local lspconfig = require 'lspconfig'

lspconfig.tsserver.setup {
  on_attach = function(client, bufnr)
    client.resolved_capabilities.document_formatting = false

    on_attach(client, bufnr)
  end,
  settings = {diagnostics = {globals = {'on_attach'}}}
}

感谢任何帮助

2个回答

3

您需要对任何您不希望格式化的LSP(Language Server Protocol)进行报告。

例如,如果您正在使用tsserver,但仅想使用null-ls进行格式化:


require("lspconfig").tsserver.setup({
    on_attach = function(client)
        client.resolved_capabilities.document_formatting = false
        client.resolved_capabilities.document_range_formatting = false
    end,
})

这将使Neovim默认使用null-ls进行格式化,并禁用提示。

来源:https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Avoiding-LSP-formatting-conflicts


2

这似乎对我来说运行了错误的格式化程序,或者运行了多个格式化程序。 - PatKilg
formatting_seq_sync(nil, 1000, {"null-ls"}) 将确保 null-ls 语言服务器进行格式化。但是其他语言服务器将在您选择的服务器之前被调用,因此性能较差。 - Emmanuel Touzery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接