关于neovim配置spacevim的lspconfig问题踩坑与解决方法记录
7月 24, 2022
在archlinux下做开发已经很长时间了,对于小型项目的开发一直使用的是vim和gvim. 偶然尝试了一下neovim, 发现其在UI性能方面相较于gvim更加强大, 界面滚动也更加丝滑, 随即产生了从Vim切换到neovim的想法. 由于我直接使用的Spacevim, 在切换到neovim时产生了一些问题, 主要是lspconfig的报错问题. 这里记录一下我搜索到的资料与解决方法, 以便后面再出现此类错误, 更容易修补.
首先贴上我在使用gvim时的Spacevim的配置文件代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
[options] colorscheme = "onedark" colorscheme_bg = "dark" enable_guicolors = true statusline_separator = "nil" statusline_iseparator = "bar" buffer_index_type = 4 windows_index_type = 3 enable_tabline_filetype_icon = false enable_statusline_mode = false statusline_unicode = false vimcompatible = true disbaled_plugins = ["delimitMate"] expand_tab = true relativenumber = false autocomplete_method = "coc" bootstrap_before = "myspacevim#before" default_indent = 4
[[custom_plugins]] repo = "jiangmiao/auto-pairs" merged = false
[[custom_plugins]] repo = "ryanoasis/vim-devicons" merged = 0
[[layers]] name = "colorscheme"
[[layers]] name = "sudo"
[[layers]] name = "treesitter"
[[layers]] name = 'autocomplete' auto_completion_return_key_behavior = "complete" auto_completion_tab_key_behavior = "cycle"
[[layers]] name = 'shell' default_position = 'top' default_height = 30
[[layers]] name = "lang#javascript"
[[layers]] name = "lang#typescript"
[[layers]] name = "lang#python"
[[layers]] name = "lang#c" enable_clang_syntax_highlight = true [layer.clang_std] cpp = "c11" [[layers]] name = "lang#go"
[[layers]] name = "lsp" enable_clients = [ 'clangd', 'pyls', 'gopls', 'typescript-language-server' ] filetypes = [ "c", "cpp", "python", "go", "javascript" ] [layers.override_cmd] c = ["clangd"] cpp = ["clangd"] python = ["pyls"] go = ["gopls"] javascript = ["typescript-language-server", "--stdio"]
[[layers]] name = "format"
|
下面是我修改完成使用neovim的配置文件代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
[options] colorscheme = "onedark" colorscheme_bg = "dark" enable_guicolors = true statusline_separator = "nil" statusline_iseparator = "bar" buffer_index_type = 4 windows_index_type = 3 enable_tabline_filetype_icon = false enable_statusline_mode = false statusline_unicode = false vimcompatible = true disbaled_plugins = ["delimitMate"] expand_tab = true relativenumber = false autocomplete_method = "nvim-cmp" bootstrap_before = "myspacevim#before" default_indent = 4 snippet_engine = "ultisnips"
[[custom_plugins]] repo = 'https://github.com/hrsh7th/nvim-cmp.git' merged = 0
[[custom_plugins]] repo = "jiangmiao/auto-pairs" merged = false [[custom_plugins]] repo = "chrisbra/csv.vim" merged = false
[[custom_plugins]] repo = "ryanoasis/vim-devicons" merged = 0
[[layers]] name = "colorscheme"
[[layers]] name = "sudo"
[[layers]] name = "treesitter"
[[layers]] name = 'autocomplete' auto_completion_return_key_behavior = "complete" auto_completion_tab_key_behavior = "cycle"
[[layers]] name = 'shell' default_position = 'top' default_height = 30
[[layers]] name = "lang#javascript"
[[layers]] name = "lang#typescript"
[[layers]] name = "lang#python"
[[layers]] name = "lang#c" enable_clang_syntax_highlight = true [layer.clang_std] cpp = "c11"
[[layers]] name = "lang#go"
[[layers]] name = "lsp" enable_clients = [ 'clangd', 'pylsp' 'gopls', 'tsserver' ] filetypes = [ "c", "cpp", "python", "go", "javascript", "typescript" ] [layers.override_client_cmds] clangd = ["clangd"] pylsp = ["pylsp"] gopls = ["gopls", "serve"] tsserver = ["typescript-language-server", "--stdio"]
[[layers]] name = "format"
|
下面说明一下遇到的问题
首先是lspconfig问题
1 2 3 4 5 6 7 8 9 10 11
| [dein] Error occurred while executing hook: nvim-lspconfig
[dein] Vim(lua):E5108: Error executing lua ...b/.SpaceVim/bundle/nvim-lspconfig/lua/lspconfig.lua:80: attempt to co
ncatenate local 'k' (a boolean value)
[dein] stack traceback:
[dein] ^I...b/.SpaceVim/bundle/nvim-lspconfig/lua/lspconfig.lua:80: in function '__index'
[dein] ^I[string ":lua"]:49: in main chunk
|
每次使用neovim都会报这个错误, 目前最新版本为0.7.2-3, 有的人切换到0.7.0-3即可正常运行, 然而在我这里却没有用, 无论我切换到什么版本都会报这个错误. 查找了很久的解决方案, 最终在spacevim 的google groups上面找到了一个有效的解决方案. 原链接见此处
翻译过来的解决方法就是
在options里面设置自动不全的引擎为nvim-cmp, 由于Spacevim并没有内置nvim-cmp故需要我们手动添加配置让spacevim安装这个插件, 即下面的[[custom_plugins]]处. 这里修改完之后还需要修改lsp layers里面的layers.override_cmd
为layers.override_client_cmds
并且其中的内容也需要修改,原本的是filetype对应client, 而修改后应为server_name对应client, 看不懂的可以仔细对比文章最上面的两个配置文件的区别.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| [options] autocomplete_method = "nvim-cmp" [[custom_plugins]] repo = "https://github.com/hrsh7th/nvim-cmp.git" merged = 0 [[layers]] name = "lsp" enabled_clients = [
'gopls', 'intelephense', 'rust_analyzer', 'tsserver' ] filetypes = [
"php", "rust", "go", "typescript", "javascript" ] [layers.override_client_cmds] intelephense = ["intelephense", "--stdio"]
rust_analyzer = ["rust-analyzer"] gopls = ["gopls", "serve"] tsserver = ['typescript-language-server', '--stdio']
[[layers]] name = "lang#php"
|
修改完成之后, 应该已经不会出现上面所显示的错误了. 但是还会继续遇到问题, 首先就是nvim-cmp的代码提示不完善, 它优先提示的是buffer, 而我们真正需要的是lsp的代码提示, 并且还存在的问题是代码提示中的选项, 我们无法通过方向键或者tab键来进行选择, 并且的并且我们还需要手动设置snippet engine, 否则我们在使用代码补全的时候,仍然会出现错误. 上述的这些问题需要修改nvim-cmp的配置文件, 这里直接放出我的配置文件, 参照着修改即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| lua <<EOF local cmp = require'cmp'
cmp.setup({ mapping = cmp.mapping.preset.insert({ ['<C-d>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), ['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), ['<C-y>'] = cmp.config.disable, ['<C-e>'] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close(), }), ['<CR>'] = cmp.mapping.confirm({ select = true }), ['<Tab>'] = function(fallback) if cmp.visible() then cmp.select_next_item() else fallback() end end, ['<S-Tab>'] = function(fallback) if cmp.visible() then cmp.select_prev_item() else fallback() end end, }), formatting = { format = require("lspkind").cmp_format({with_text = true, menu = ({ buffer = "[Buffer]", })}), }, sources = cmp.config.sources({
{ name = 'nvim_lsp' } }, { { name = 'buffer' }, }, { { name = 'path' }, }, { { name = 'ultisnips' } }), snippet = { expand = function(args) vim.fn["UltiSnips#Anon"](args.body) end, }, })
local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
require'lspconfig'.clangd.setup { capabilities = capabilities, } EOF
|
参考地址:
https://groups.google.com/g/spacevim?pli=1
https://groups.google.com/g/spacevim/c/Q8LBeNBBm60
https://groups.google.com/g/spacevim/c/E5C9zXU_Xk8
https://github.com/hrsh7th/nvim-cmp/issues/102
https://github.com/hrsh7th/nvim-cmp/wiki/List-of-sources
https://github.com/hrsh7th/nvim-cmp#basic-configuration