I currently use coc.nvim, but I was interested in the native Neovim LSP tools. I tried setting it up like this (only relevant parts shown):
-- configure typescript
-- install with `sudo npm install -g typescript-language-server typescript`
vim.lsp.config('typescript', {
cmd = { 'typescript-language-server', '--stdio' },
filetypes = { 'typescript', 'typescriptreact' },
root_markers = { 'package.json' },
settings = {},
})
-- enable LSP
vim.lsp.enable({ 'typescript' })
-- set autocomplete behavior.
-- fuzzy = fuzzy search in results
-- menuone = show menu, even if there is only 1 item
-- popup = show extra info in popup
-- noselect = don't insert the text until an item is selected
vim.cmd('set completeopt=fuzzy,menuone,popup,noselect')
-- set up stuff when the LSP client attaches
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('lsp', {}),
callback = function(args)
local client = assert(
vim.lsp.get_client_by_id(args.data.client_id),
'Failed to get LSP client'
)
-- enable autocomplete
if client:supports_method('textDocument/completion') then
local chars = { '.', '"', "'", '/', '@', '<' }
for i = 65, 90 do -- A-Z
table.insert(chars, string.char(i))
end
for i = 97, 122 do -- a-z
table.insert(chars, string.char(i))
end
client.server_capabilities.completionProvider.triggerCharacters =
chars
vim.lsp.completion.enable(
true,
client.id,
args.buf,
{ autotrigger = true }
)
end
end,
})
-- open autocomplete menu when pressing <C-n>
vim.keymap.set('i', '<C-n>', function()
vim.lsp.completion.get()
end)
By default, the autocomplete characters are { '.', '"', "'", '/', '@', '<' }
, but I also added all the upper- and lowercase letters as well using the recommended method.
The autocomplete comes up as it should, but as soon as I press Backspace, it goes away. When using autocomplete with coc.nvim, the autocomplete stays up even after I press Backspace, which is a feature I use a lot.
I tried adding '<BS>'
and '\\<BS>'
to the list of trigger characters, and I tried pressing <C-n>
to call vim.lsp.completion.get()
after Backspace, but it didn't work. Nothing popped up.