nvim-lua / completion-nvim

A async completion framework aims to provide completion to neovim's built in LSP written in Lua
Apache License 2.0
973 stars 78 forks source link

Javascript/React auto completion and auto import #366

Open andreicorpo opened 3 years ago

andreicorpo commented 3 years ago

Hello, I just can't figure out how to setup this plugin for javascript and react.

This is my config:

vim.o.completeopt = "menuone,noselect"
vim.o.shortmess = vim.o.shortmess .. "c"
vim.g.completion_enable_snippet = 'vim-vsnip'
vim.g.completion_matching_strategy_list = {'exact', 'substring', 'fuzzy'}
vim.g.completion_matching_ignore_case = 1
vim.g.completion_auto_change_source = 1

local t = function(str)
    return vim.api.nvim_replace_termcodes(str, true, true, true)
end

-- Use (s-)tab to:
--- move to prev/next item in completion menuone
--- jump to prev/next snippet's placeholder
_G.tab_complete = function()
    if vim.fn.pumvisible() == 1 then
        return t "<C-n>"
    elseif vim.fn.call("vsnip#available", {1}) == 1 then
        return t "<Plug>(vsnip-expand-or-jump)"
    else
        return t "<Tab>"
    end
end
_G.s_tab_complete = function()
    if vim.fn.pumvisible() == 1 then
        return t "<C-p>"
    elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then
        return t "<Plug>(vsnip-jump-prev)"
    else
        return t "<S-Tab>"
    end
end

vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})

-- Chain completion list
vim.g.completion_chain_complete_list = {
    default = {
        default = {{complete_items = {'lsp', 'snippet', 'buffers'}}, {mode = '<c-p>'}, {mode = '<c-n>'}},
        comment = {},
        string = {{complete_items = {'path'}}}
    }
}

Buffers and snippet support is working, but LSP doesn't seem to work for javascript/react. Although it is working for lua.

I am using to autocommand to setup completion.

ranjithshegde commented 3 years ago

what autocmd are you using to setup completion? do you have something like the following

au BufEnter * lua require'completion'.on_attach() or do you have it per filetype? or in an on_attach(client,bufnr) funtion?