My lspconfig
works fine with typescript files, but not with Javascript.
- Autocomplete shows
.constructor
and other standard props/methods instead of the expected ones defined on the type - This happens even though the correct type is set (through jsdoc) or inferred from imported typescript modules
- If I simply set the filetype to typescript (
:set ft=typescript
), it starts working
I used the Array
class in the example, but the issue is the same with user-defined types, even when the type definition is written using jsdoc.
How can I make autocomplete work as expected with Javascript files too?
Note: I use native LSP only, other plugins like nvim-cmp
and coq-nvim
work as expected, but I prefer to avoid them. Since they all use LSP, I suppose I am misconfiguring something.
My relevant configs:
init.vim
" LSP config
set omnifunc=v:lua.vim.lsp.omnifunc
set tagfunc=v:lua.vim.lsp.tagfunc
" open LSP definition
map <F9> :lua vim.lsp.buf.hover()<CR>
inoremap <F9> <C-o>:lua vim.lsp.buf.hover()<CR>
" go to definition
map <F12> :lua vim.lsp.buf.definition()<CR>
inoremap <F12> <C-o>:lua vim.lsp.buf.definition()<CR>
" code completion with omni function
inoremap <C-Space> <C-x><C-o>
inoremap <C-@> <C-x><C-o>
config.lua
-- LSP config
local lspconfig = require('lspconfig')
local servers = { 'bashls', 'sumneko_lua', 'solargraph', 'tsserver', 'vimls' }
for _, lsp in ipairs(servers) do
lspconfig[lsp].setup({})
end
Full config files:
- https://github.com/paulodiovani/dot-files/blob/1f4166898a839a630e5aa7fd3bbd50fffc855609/home/user/.config/nvim/init.vim#L15-L17
- https://github.com/paulodiovani/dot-files/blob/1f4166898a839a630e5aa7fd3bbd50fffc855609/home/user/.config/nvim/init.vim#L34-L44
- https://github.com/paulodiovani/dot-files/blob/1f4166898a839a630e5aa7fd3bbd50fffc855609/home/user/.config/nvim/lua/config.lua#L54-L59
Output of :LspInfo
Language client log: /home/diovani/.cache/nvim/lsp.log
Detected filetype: javascript
2 client(s) attached to this buffer:
Client: tsserver (id: 1, pid: 114182, bufnr: [1])
filetypes: javascript, javascriptreact, javascript.jsx, typescript, typescriptreact, typescript.tsx
autostart: true
root directory: /home/diovani/Development/Node/interactive-pbta-bot
cmd: typescript-language-server --stdio
Client: null-ls (id: 2, pid: 5001, bufnr: [1])
filetypes: typescriptreact, typescript, javascriptreact, javascript, vue, lua, ruby, sh, less, scss, sass, css, yaml, json
autostart: false
root directory: /home/diovani/Development/Node/interactive-pbta-bot
cmd: nvim
Configured servers list: bashls, sumneko_lua, solargraph, tsserver, vimls
tsserver
in there, it may only be getting set up for typescript files. Also, consider that a JS LSP can only do so much (like imported functions) because of all the dynamic "stuff":LspInfo
Object
andArray
does not work.Math
does. :/