【问题标题】:Cannot start clangd lsp server with neovim on windows无法在 Windows 上使用 neovim 启动 clangd lsp 服务器
【发布时间】:2022-08-10 23:31:24
【问题描述】:

我正在尝试创建一个 neovim 配置,让我可以使用由 cmake 生成的 c++ 项目。

我已经安装了 nvim-lspconfig 插件,并且我已经使用 Visual Studio 2022 安装程序安装了 clang(两种声音,clang-cl and clang-compiler)。但是当我在 neovim 中 cd 一个 cmake 根文件夹时,当我打开一个 c++ 文件时,什么都不会发生。我什至看不到错误。这就像服务器不工作。

如何设置 neovim 以使用 Visual Studio 2022 附带的 clang?

这是我的init.lua

--- Require ---

local utils = require(\'utils\') -- just to set some keys, not related to the clang problem.

--- Options ---

-- Add number to rows

utils.opt(\'o\', \'number\', true)

-- Set indentation of files
local indent = 2
utils.opt(\'b\', \'expandtab\', true)
utils.opt(\'b\', \'shiftwidth\', indent)
utils.opt(\'b\', \'smartindent\', true)
utils.opt(\'b\', \'tabstop\', indent)
utils.opt(\'b\', \'autoindent\', true)
utils.opt(\'o\', \'smarttab\', true)
utils.opt(\'b\', \'softtabstop\', indent)

-- Enable the mouse
utils.opt(\'o\', \'mouse\', \'a\')

-- Set nocompatible mode for more powerful commands
utils.opt(\'o\', \'compatible\', false)

-- Set some search options
utils.opt(\'o\', \'showmatch\', true)
utils.opt(\'o\', \'ignorecase\', true)
utils.opt(\'o\', \'hlsearch\', true)
utils.opt(\'o\', \'incsearch\', true)

-- Set options for color scheme
utils.opt(\'o\', \'termguicolors\', true)

--- Keymappings ---

-- Remap jj to escape in insert mode
utils.map(\'i\', \'jj\', \'<Esc>\')
utils.map(\'n\', \'JJJJ\', \'<Nop>\')

-- Swap ; and :
utils.map(\'n\', \':\', \';\')
utils.map(\'n\', \';\', \':\')

-- Start plugin section. Use this section in order to install new plugins to
-- neovim.
--
-- In order to install a new plugin, you need to put in this section the
-- repository where it can be found, and then refresh the plugin list by
-- installing them with the command:
--
-- :PlugInstall

-- Auto install vim-plug that\'s a plugin manager
local vimplugrepository = \'\'
local installpath = vim.fn.stdpath(\'config\')..\'/autoload\'
local vimpluginstallpath = installpath..\'/plug.vim\'
local vimplugrepository = \'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim\'
if vim.fn.empty(vim.fn.glob(vimpluginstallpath)) > 0 then
  vim.api.nvim_command(\'!curl -flo \'..vimpluginstallpath..\' --create-dirs \'..vimplugrepository)
  vim.cmd \'autocmd VimEnter * PlugInstall\'
end
local Plug = vim.fn[\'plug#\']

-- Put plugins in this section. Define a Plug with the repository of the plugin that you want

vim.call(\'plug#begin\', installpath)

-- Vim airline. This plugin creates a nice status bar with more features than
-- standard one.
Plug \'https://github.com/vim-airline/vim-airline\'

-- NerdTree is a plugin for showing a tree folder structure of the filesystem.
Plug \'https://github.com/preservim/nerdtree\'

-- Vim color schemes
Plug \'https://github.com/rafi/awesome-vim-colorschemes\'

-- Developer icons
Plug \'https://github.com/ryanoasis/vim-devicons\'

-- Surrounding with parenthesis and xml tags with cs command and more
Plug \'https://github.com/tpope/vim-surround\'

-- Show trailing whitespaces and use the command :StripWhitespace for removing
-- them
Plug \'https://github.com/ntpeters/vim-better-whitespace.git\'

-- Install the LSP server for configuring it with clangd for code completition
-- in C++
Plug \'https://github.com/neovim/nvim-lspconfig\'

-- An interesting theme
Plug \'https://github.com/Pocco81/Catppuccino.nvim\'

-- CMake integration. It includes following commands:
-- :CMakeGenerate - it generates the project
-- :CMakeBuild - It builds the project.
-- :CMakeSwitch <config> - It switches between configurations
Plug \'https://github.com/cdelledonne/vim-cmake\'

vim.call(\'plug#end\')

--- PLUGINS CONFIGURATION ---

-- Nerdtree

-- Configure keys so with ctrlf go to the tree, with ctrl+n open the tree, and
-- ctrl+t toggle the tree
utils.map(\'n\', \'<C-f>\', \':NERDTreeFocus<CR>\')
utils.map(\'n\', \'<C-n>\', \':NERDTree<CR>\')
utils.map(\'n\', \'<C-t>\', \':NERDTreeToggle<CR>\')

--- LSP CONFIG ---

-- Main configuration
local lspremapopts = { noremap = true, silent = true }
vim.keymap.set(\'n\', \'<space>e\', vim.diagnostic.open_float, lspremapopts)
vim.keymap.set(\'n\', \'[d\', vim.diagnostic.goto_prev, lspremapopts)
vim.keymap.set(\'n\', \']d\', vim.diagnostic.goto_next, lspremapopts)
vim.keymap.set(\'n\', \'<space>q\', vim.diagnostic.setloclist, lspremapopts)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  -- Enable completition triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, \'omnifunc\', \'v:lua.vim.lsp.omnifunc\')

  -- Mappings
  -- See \':help vim.lsp.*
  local bufopts = { noremap = true, silent = true, buffer = bufnr }
  vim.keymap.set(\'n\', \'gD\', vim.lsp.buf.declaration, bufopts)
  vim.keymap.set(\'n\', \'gd\', vim.lsp.buf.definition, bufopts)
  vim.keymap.set(\'n\', \'K\', vim.lsp.buf.hover, bufopts)
  vim.keymap.set(\'n\', \'gi\', vim.lsp.buf.implementation, bufopts)
  vim.keymap.set(\'n\', \'<C-k>\', vim.lsp.buf.signature_help, bufopts)
  vim.keymap.set(\'n\', \'<space>wa\', vim.lsp.buf.add_workspace_folder, bufopts)
  vim.keymap.set(\'n\', \'<space>wr\', vim.lsp.buf.remove_workspace_folder, bufopts)
  vim.keymap.set(\'n\', \'<space>wl\', function ()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, bufopts)
  vim.keymap.set(\'n\', \'<space>D\', vim.lsp.buf.type_definition, bufopts)
  vim.keymap.set(\'n\', \'<space>rn\', vim.lsp.buf.rename, bufopts)
  vim.keymap.set(\'n\', \'<space>ca\', vim.lsp.buf.code_action, bufopts)
  vim.keymap.set(\'n\', \'gr\', vim.lsp.buf.references, bufopts)
  vim.keymap.set(\'n\', \'<space>f\', vim.lsp.buf.formatting, bufopts)
end

-- Now the servers must be defined and set. In order to load them it\'s
-- convenient to define them in an array and use a loop.
local servers = { \'pyright\', \'clangd\' }
for _, lsp in pairs(servers) do
  require(\'lspconfig\')[lsp].setup {
    on_attach = on_attach,
    flags = {
      debounce_text_changes = 150
    }
  }
end


--- COLOR SCHEME ---

vim.cmd[[colorscheme catppuccin]]

    标签: c++ lua clang visual-studio-2022 neovim


    【解决方案1】:

    您可以运行:LspInfo 以查看客户端是否已连接到缓冲区,并运行:LspLog 以获取更多信息。

    clangd 要么需要在你的路径中,要么你可以指定完整路径:

    require('lspconfig')['clangd'].setup {
      on_attach = on_attach,
      cmd = { '<full path to clangd>' }
      flags = {
        debounce_text_changes = 150
      }
    }
    

    VS 安装的 clang 不在你的路径中。如果您从 Visual Studio 开发人员(命令提示符|PowerShell)运行 neovim,它将被添加到您的路径中。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2019-06-14
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      相关资源
      最近更新 更多