I am trying to configure nvim-cmp (https://github.com/hrsh7th/nvim-cmp), by adding the following lines to ~/.config/nvim/lua/lsp-config.lua.
After this, when I start nvim, I get an error as shown in the image.
My lsp-config.lua looks like the following:
1 local nvim_lsp = require("lspconfig")
2 local format_async = function(err, _, result, _, bufnr)
3 if err ~= nil or result == nil then return end
4 if not vim.api.nvim_buf_get_option(bufnr, "modified") then
5 local view = vim.fn.winsaveview()
6 vim.lsp.util.apply_text_edits(result, bufnr)
7 vim.fn.winrestview(view)
8 if bufnr == vim.api.nvim_get_current_buf() then
9 vim.api.nvim_command("noautocmd :update")
10 end
11 end
12 end
13 vim.lsp.handlers["textDocument/formatting"] = format_async
14 _G.lsp_organize_imports = function()
15 local params = {
16 command = "_typescript.organizeImports",
17 arguments = {vim.api.nvim_buf_get_name(0)},
18 title = ""
19 }
20 vim.lsp.buf.execute_command(params)
21 end
22 local on_attach = function(client, bufnr)
23 local buf_map = vim.api.nvim_buf_set_keymap
24 vim.cmd("command! LspDef lua vim.lsp.buf.definition()")
25 vim.cmd("command! LspFormatting lua vim.lsp.buf.formatting()")
26 vim.cmd("command! LspCodeAction lua vim.lsp.buf.code_action()")
27 vim.cmd("command! LspHover lua vim.lsp.buf.hover()")
28 vim.cmd("command! LspRename lua vim.lsp.buf.rename()")
29 vim.cmd("command! LspOrganize lua lsp_organize_imports()")
30 vim.cmd("command! LspRefs lua vim.lsp.buf.references()")
31 vim.cmd("command! LspTypeDef lua vim.lsp.buf.type_definition()")
32 vim.cmd("command! LspImplementation lua vim.lsp.buf.implementation()")
33 vim.cmd("command! LspDiagPrev lua vim.lsp.diagnostic.goto_prev()")
34 vim.cmd("command! LspDiagNext lua vim.lsp.diagnostic.goto_next()")
35 vim.cmd(
36 "command! LspDiagLine lua vim.lsp.diagnostic.show_line_diagnostics()")
37 vim.cmd("command! LspSignatureHelp lua vim.lsp.buf.signature_help()")
38 buf_map(bufnr, "n", "gd", ":LspDef<CR>", {silent = true})
39 buf_map(bufnr, "n", "gr", ":LspRename<CR>", {silent = true})
40 buf_map(bufnr, "n", "gR", ":LspRefs<CR>", {silent = true})
41 buf_map(bufnr, "n", "gy", ":LspTypeDef<CR>", {silent = true})
42 buf_map(bufnr, "n", "K", ":LspHover<CR>", {silent = true})
43 buf_map(bufnr, "n", "gs", ":LspOrganize<CR>", {silent = true})
44 buf_map(bufnr, "n", "[a", ":LspDiagPrev<CR>", {silent = true})
45 buf_map(bufnr, "n", "]a", ":LspDiagNext<CR>", {silent = true})
46 buf_map(bufnr, "n", "ga", ":LspCodeAction<CR>", {silent = true})
47 buf_map(bufnr, "n", "<Leader>a", ":LspDiagLine<CR>", {silent = true})
48 buf_map(bufnr, "i", "<C-x><C-x>", "<cmd> LspSignatureHelp<CR>",
49 {silent = true})
50 if client.resolved_capabilities.document_formatting then
51 vim.api.nvim_exec([[
52 augroup LspAutocommands
53 autocmd! * <buffer>
54 autocmd BufWritePost <buffer> LspFormatting
55 augroup END
56 ]], true)
57 end
58 end
59 nvim_lsp.tsserver.setup {
60 on_attach = function(client)
61 client.resolved_capabilities.document_formatting = false
62 on_attach(client)
63 end
64 }
65 local filetypes = {
66 typescript = "eslint",
67 typescriptreact = "eslint",
68 }
69 local linters = {
70 eslint = {
71 sourceName = "eslint",
72 command = "eslint_d",
73 rootPatterns = {".eslintrc.js", "package.json"},
74 debounce = 100,
75 args = {"--stdin", "--stdin-filename", "%filepath", "--format", "json"},
76 parseJson = {
77 errorsRoot = "[0].messages",
78 line = "line",
79 column = "column",
80 endLine = "endLine",
81 endColumn = "endColumn",
82 message = "${message} [${ruleId}]",
83 security = "severity"
84 },
85 securities = {[2] = "error", [1] = "warning"}
86 }
87 }
88 local formatters = {
89 prettier = {command = "prettier", args = {"--stdin-filepath", "%filepath"}}
90 }
91
92 local formatFiletypes = {
93 typescript = "prettier",
94 typescriptreact = "prettier"
95 }
96 nvim_lsp.diagnosticls.setup {
97 on_attach = on_attach,
98 filetypes = vim.tbl_keys(filetypes),
99 init_options = {
100 filetypes = filetypes,
101 linters = linters,
102 formatters = formatters,
103 formatFiletypes = formatFiletypes
104 }
105 }
106
107
108 -- Setup nvim-cmp.
109 local cmp = require'cmp'
110
111 cmp.setup({
112 snippet = {
113 expand = function(args)
114 -- For `vsnip` user.
115 vim.fn["vsnip#anonymous"](args.body)
116
117 -- For `luasnip` user.
118 -- require('luasnip').lsp_expand(args.body)
119
120 -- For `ultisnips` user.
121 -- vim.fn["UltiSnips#Anon"](args.body)
122 end,
123 },
124 mapping = {
125 ['<C-d>'] = cmp.mapping.scroll_docs(-4),
126 ['<C-f>'] = cmp.mapping.scroll_docs(4),
127 ['<C-Space>'] = cmp.mapping.complete(),
128 ['<C-e>'] = cmp.mapping.close(),
129 ['<CR>'] = cmp.mapping.confirm({ select = true }),
130 },
131 sources = {
132 { name = 'nvim_lsp' },
133
134 -- For vsnip user.
135 { name = 'vsnip' },
136
137 -- For luasnip user.
138 -- { name = 'luasnip' },
139
140 -- For ultisnips user.
141 -- { name = 'ultisnips' },
142
143 { name = 'buffer' },
144 }
145 })
146
147 -- Setup lspconfig.
148 require('lspconfig')[tsserver].setup {
149 capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
150 }
If I remove the nvim-cmp specific code i.e. from line#108 until end of file, then there are no errors.
Am I missing some configuration here?
I replaced [%YOUR_LSP_SERVER%] with [tsserver]
Whatever whoever may think, Lua is a programming language and so using it (even for simple configuration purposes) requires at least basic syntax knowledge. And at the very least, the difference between variable and string literal is common, I believe, to every programming language out there.