Windows 上带有 Neovim 的 Omnisharp C# LSP

问题描述 投票:0回答:1

正如标题所说,我需要有关使用 Omnisharp 设置 C# LSP 的帮助。我已经安装了最新版本。我从

releases
页面下载了 omnisharp-win-x64.zip 并将其解压到自定义目录。

我安装了 nvim-lspconfig,这就是我的配置。

for _, lsp in ipairs(servers) do
  if vim.loop.os_uname().sysname == "Windows_NT" and lsp == "omnisharp" then
    local pid = vim.fn.getpid()
    local omnisharp_bin = "C:\\Users\\yapji\\lsp\\omnisharp\\OmniSharp.exe"
    lspconfig[lsp].setup {
      handlers = handlers,
      on_attach = on_attach,
      capabilities = capabilities,
      cmd = { omnisharp_bin, "--languageserver", "--hostPID", tostring(pid) },
    }
  else
    lspconfig[lsp].setup {
      handlers = handlers,
      on_attach = on_attach,
      capabilities = capabilities,
    }
  end
end

我更多地查看了关于设置的降价文件

omnisharp
可以阅读here我想我发现了问题。正如文件中特别说明的那样。

默认情况下,omnisharp-roslyn 没有设置 cmd。这是因为 nvim-lspconfig 不会对您的路径做出假设。您必须将以下内容添加到您的 init.vim 或 init.lua 以将 cmd 设置为解压缩的运行脚本或二进制文件的绝对路径($HOME 和 ~ 未展开)。

我需要设置

cmd
属性。但是通过 omnisharp file 看,没有
cmd
属性。我拥有它的原因是因为我遵循了 this 指南并且我尽了最大努力使它适应 Windows 但就像我提到的那样它不起作用。如果需要更多信息,我将更新它的问题。谢谢。我也找到了 this 答案,但是没有任何明确的 neovim 支持的答案,我会把它留到最后。

我从发布页面安装了 omnisharp 并将其解压缩并设置我的 neovim 配置,如上所示。

c# windows neovim omnisharp nvim-lspconfig
1个回答
0
投票

你必须定义 cmd 属性,否则 neovim 将不知道如何启动 omnisharp 语言服务器,这是正确的。

一些可能不合适的事情,应该检查一下:

  • 我不是 lua 专家,但请检查路径斜杠 ("") 是否正确转义。我个人使用环境变量来定义语言服务器的路径(我在各种 windows\linux 机器上使用我的配置)它是这样的:

    local os = require('os')
    local omnisharp_server_location = os.getenv('OMNISHARP_LANGUAGE_SERVER')
    ...
    ...
    -- somewhere in the code where server configuration is defined
    require('lspconfig').omnisharp.setup({
        on_attach = on_attach,
        capabilities = capabilities,
        cmd = { omnisharp_server_location, "--languageserver" , "--hostPID", tostring(pid) },
     })
    
  • 据我所知,omnisharp 有几个发行版,请确保您使用的是正确的体系结构 (arm64\x64\x86),尝试从 cmd 手动执行它,看看是否有任何不需要的故障。正如我在评论中提到的,请在此处检查 lsp server 日志:%USERPROFILE%\AppData\Local vim-data\lsp.log

  • 确保您的配置已实际设置,检查 :LspInfo 并在 neovim 中打开 csharp 文件。

  • 更重要的一点,omnisharp 服务器(或 roslyn 本身)中存在一个错误,这里是未解决的问题:

底线

这里是 neovim 中简约 omnisharp 配置的示例,因此您可以使用它,只需将其复制到一个单独的文件并使用 -u 标志启动 neovim,如下所示:

nvim -u /path/to/the/config.lua

它有 packer bootstrapped 所以你必须运行它两次,然后运行 :PackerSync 来安装所需的插件。

配置:

local os = require('os')
local fn = vim.fn

-- bootstrapping packer for simplicity of the example
-- packer installation bootstrapping
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
  packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
end

-- bare minimum for the demo installation
require('packer').startup(function(use)
  use 'neovim/nvim-lspconfig' -- language server configurations
  use 'hrsh7th/nvim-cmp' -- autocompletion framework
  use 'hrsh7th/cmp-nvim-lsp' -- LSP autocompletion provider

  if packer_bootstrap then
    require('packer').sync()
  end
end)


-- autocomplete setup
vim.cmd [[
    set completeopt=menu,menuone,noselect
]]

local cmp = require('cmp')
cmp.setup({
  mapping = {
    ['<Tab>'] = cmp.mapping.select_next_item(),
    ['<S-Tab>'] = cmp.mapping.select_prev_item(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },
  sources = cmp.config.sources({
      { name = 'nvim_lsp' }, 
  }),
})


local capabilities = require('cmp_nvim_lsp')
    .default_capabilities(vim.lsp.protocol.make_client_capabilities())


local on_attach = function(client, bufnr)
    -- temporary fix for a roslyn issue in omnisharp
    -- opened issues:
    -- https://github.com/OmniSharp/omnisharp-roslyn/issues/2483
    -- https://github.com/neovim/neovim/issues/21391
    if client.name == "omnisharp" then
        client.server_capabilities.semanticTokensProvider = {
          full = vim.empty_dict(),
          legend = {
            tokenModifiers = { "static_symbol" },
            tokenTypes = {
              "comment",
              "excluded_code",
              "identifier",
              "keyword",
              "keyword_control",
              "number",
              "operator",
              "operator_overloaded",
              "preprocessor_keyword",
              "string",
              "whitespace",
              "text",
              "static_symbol",
              "preprocessor_text",
              "punctuation",
              "string_verbatim",
              "string_escape_character",
              "class_name",
              "delegate_name",
              "enum_name",
              "interface_name",
              "module_name",
              "struct_name",
              "type_parameter_name",
              "field_name",
              "enum_member_name",
              "constant_name",
              "local_name",
              "parameter_name",
              "method_name",
              "extension_method_name",
              "property_name",
              "event_name",
              "namespace_name",
              "label_name",
              "xml_doc_comment_attribute_name",
              "xml_doc_comment_attribute_quotes",
              "xml_doc_comment_attribute_value",
              "xml_doc_comment_cdata_section",
              "xml_doc_comment_comment",
              "xml_doc_comment_delimiter",
              "xml_doc_comment_entity_reference",
              "xml_doc_comment_name",
              "xml_doc_comment_processing_instruction",
              "xml_doc_comment_text",
              "xml_literal_attribute_name",
              "xml_literal_attribute_quotes",
              "xml_literal_attribute_value",
              "xml_literal_cdata_section",
              "xml_literal_comment",
              "xml_literal_delimiter",
              "xml_literal_embedded_expression",
              "xml_literal_entity_reference",
              "xml_literal_name",
              "xml_literal_processing_instruction",
              "xml_literal_text",
              "regex_comment",
              "regex_character_class",
              "regex_anchor",
              "regex_quantifier",
              "regex_grouping",
              "regex_alternation",
              "regex_text",
              "regex_self_escaped_character",
              "regex_other_escape",
            },
          },
          range = true,
        }
    end

    -- specifies what to do when language server attaches to the buffer
    vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
end


local omnisharp_server_location = os.getenv('OMNISHARP_LANGUAGE_SERVER')

local pid = vim.fn.getpid()
require('lspconfig').omnisharp.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  cmd = { omnisharp_server_location, "--languageserver" , "--hostPID", tostring(pid) },
})

© www.soinside.com 2019 - 2024. All rights reserved.