feat: setup nvim-cmp for autocompletion and lspconfig
This commit is contained in:
parent
4d37817667
commit
7459b2c0f7
@ -4,5 +4,6 @@
|
||||
## Requirements
|
||||
* ripgrep - for telescope
|
||||
* [A patched font](https://www.nerdfonts.com/) - for dev icons
|
||||
* [Sumenko language server](https://github.com/sumneko/lua-language-server/wiki/Build-and-Run-(Standalone))
|
||||
|
||||
|
||||
|
@ -1,18 +1,8 @@
|
||||
local map = require 'utils.map'
|
||||
|
||||
local silent = {silent = true}
|
||||
|
||||
-- Window movement
|
||||
map('n', '<c-h>', '<c-w>h')
|
||||
map('n', '<c-j>', '<c-w>j')
|
||||
map('n', '<c-k>', '<c-w>k')
|
||||
map('n', '<c-l>', '<c-w>l')
|
||||
|
||||
|
||||
-- Toggle whitespace characters
|
||||
map('n', '<leader>tw', ':setlocal list!<cr>')
|
||||
|
||||
|
||||
-- Toggle numbers and sign column
|
||||
map('n', '<leader>tn', ':setlocal relativenumber!<cr>:setlocal number!<cr>')
|
||||
|
||||
|
71
lua/config/cmp.lua
Normal file
71
lua/config/cmp.lua
Normal file
@ -0,0 +1,71 @@
|
||||
-- Setup nvim-cmp.
|
||||
local lspkind = require('lspkind')
|
||||
lspkind.init()
|
||||
|
||||
local cmp = require('cmp')
|
||||
|
||||
cmp.setup{
|
||||
completion = { completeopt = 'menu,menuone,noinsert' },
|
||||
snippet = {
|
||||
expand = function(args)
|
||||
require("luasnip").lsp_expand(args.body)
|
||||
end,
|
||||
},
|
||||
mapping = {
|
||||
-- ['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
|
||||
-- ['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
|
||||
['<C-d>'] = cmp.mapping.scroll_docs(-4),
|
||||
['<C-f>'] = cmp.mapping.scroll_docs(4),
|
||||
['<C-Space>'] = cmp.mapping.complete(),
|
||||
['<C-e>'] = cmp.mapping.close(),
|
||||
['<CR>'] = cmp.mapping.confirm({
|
||||
behavior = cmp.ConfirmBehavior.Replace,
|
||||
select = true,
|
||||
})
|
||||
},
|
||||
sources = {
|
||||
{ name = 'nvim_lua' },
|
||||
|
||||
{ name = 'nvim_lsp', max_item_count = 20 },
|
||||
{ name = 'cmp_tabnine' },
|
||||
{ name = 'path' },
|
||||
{ name = 'luasnip' },
|
||||
{ name = 'buffer', max_item_count = 10, keyword_length = 5 },
|
||||
},
|
||||
formatting = {
|
||||
format = lspkind.cmp_format{
|
||||
with_text = true,
|
||||
menu = {
|
||||
buffer = "[buf]",
|
||||
cmp_tabnine = "[tab9]",
|
||||
nvim_lsp = "[lsp]",
|
||||
nvim_lua = "[api]",
|
||||
path = "[path]",
|
||||
luasnip = "[snip]",
|
||||
}
|
||||
}
|
||||
},
|
||||
experimental = {
|
||||
native_menu = false,
|
||||
ghost_text = true,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-- Use buffer source for `/`.
|
||||
cmp.setup.cmdline('/', {
|
||||
sources = {
|
||||
{ name = 'buffer' }
|
||||
}
|
||||
})
|
||||
|
||||
-- Use cmdline & path source for ':'.
|
||||
-- cmp.setup.cmdline(':', {
|
||||
-- sources = cmp.config.sources({
|
||||
-- { name = 'path' }
|
||||
-- }, {
|
||||
-- { name = 'cmdline' }
|
||||
-- })
|
||||
-- })
|
||||
|
||||
|
126
lua/config/lspconfig.lua
Normal file
126
lua/config/lspconfig.lua
Normal file
@ -0,0 +1,126 @@
|
||||
local lspconfig = require('lspconfig')
|
||||
local M = {}
|
||||
|
||||
-- debounce_text_changes = delay, between changing something and lsp updating
|
||||
M.flags = { debounce_text_changes = 150 }
|
||||
|
||||
M.default_settings = {
|
||||
Lua = {
|
||||
runtime = {
|
||||
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
|
||||
version = 'LuaJIT',
|
||||
-- Setup your lua path
|
||||
path = vim.split(package.path, ';')
|
||||
},
|
||||
diagnostics = {
|
||||
-- Get the language server to recognize the `vim` global
|
||||
globals = {'vim', 'packer_plugins'}
|
||||
},
|
||||
workspace = {
|
||||
-- Make the server aware of Neovim runtime files
|
||||
library = vim.api.nvim_get_runtime_file("", true)
|
||||
},
|
||||
telemetry = { enable = false }
|
||||
}
|
||||
}
|
||||
|
||||
-- Use an on_attach function to only map the following keys
|
||||
-- after the language server attaches to the current buffer
|
||||
---@diagnostic disable-next-line: unused-local
|
||||
local function on_attach(client, bufnr)
|
||||
-- Mappings.
|
||||
local opts = { noremap = true, silent = true }
|
||||
|
||||
local function buf_set_keymap(mode, lhs, rhs) vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts) end
|
||||
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
|
||||
|
||||
-- Enable completion triggered by <c-x><c-o>
|
||||
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
|
||||
|
||||
-- See `:help vim.lsp.*` for documentation on any of the below functions
|
||||
buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>')
|
||||
buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>')
|
||||
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>')
|
||||
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>')
|
||||
buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>')
|
||||
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>')
|
||||
-- buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>')
|
||||
-- buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>')
|
||||
-- buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>')
|
||||
-- buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>')
|
||||
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>')
|
||||
buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>')
|
||||
buf_set_keymap('n', '<space>d', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>')
|
||||
-- buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>')
|
||||
-- buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>')
|
||||
-- buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>')
|
||||
buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>')
|
||||
|
||||
-- " auto-format
|
||||
-- autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100)
|
||||
-- autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100)
|
||||
-- autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100)
|
||||
end
|
||||
|
||||
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
if packer_plugins['cmp-nvim-lsp'] and packer_plugins['cmp-nvim-lsp'].loaded then
|
||||
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
|
||||
end
|
||||
|
||||
local function on_init(initialize_params, config)
|
||||
-- print(vim.inspect(capabilities))
|
||||
-- print(vim.inspect(config))
|
||||
end
|
||||
|
||||
-- https://github.com/sumneko/lua-language-server/wiki/Build-and-Run-(Standalone)
|
||||
local function getSumnekoPaths()
|
||||
USER = vim.fn.expand('$USER')
|
||||
|
||||
if vim.fn.has("mac") == 1 then
|
||||
local sumneko_root_path = "/Users/" .. USER .. "/.config/nvim/lua-language-server"
|
||||
local sumneko_binary = "/Users/" .. USER .. "/.config/nvim/lua-language-server/bin/macOS/lua-language-server"
|
||||
return sumneko_root_path, sumneko_binary
|
||||
elseif vim.fn.has("unix") == 1 then
|
||||
local sumneko_root_path = "/home/" .. USER .. "/.config/nvim/lua-language-server"
|
||||
local sumneko_binary = "/home/" .. USER .. "/.config/nvim/lua-language-server/bin/Linux/lua-language-server"
|
||||
return sumneko_root_path, sumneko_binary
|
||||
end
|
||||
end
|
||||
|
||||
local function setupLuaServer()
|
||||
local sumneko_root_path, sumneko_binary = getSumnekoPaths()
|
||||
|
||||
if not sumneko_root_path then
|
||||
print("Unsupported system for sumneko")
|
||||
return
|
||||
end
|
||||
|
||||
lspconfig.sumneko_lua.setup {
|
||||
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
|
||||
on_attach = on_attach,
|
||||
filetypes = {"lua"},
|
||||
capabilities = capabilities,
|
||||
on_init = on_init,
|
||||
flags = M.flags,
|
||||
settings = M.default_settings
|
||||
}
|
||||
end
|
||||
|
||||
setupLuaServer()
|
||||
|
||||
-- Use a loop to conveniently call 'setup' on multiple servers and
|
||||
-- map buffer local keybindings when the language server attaches
|
||||
local servers = { 'tsserver' }
|
||||
for _, name in ipairs(servers) do
|
||||
lspconfig[name].setup {
|
||||
on_attach = on_attach,
|
||||
capabilities = capabilities,
|
||||
on_init = on_init,
|
||||
flags = M.flags,
|
||||
settings = M.default_settings
|
||||
}
|
||||
end
|
||||
|
||||
return M
|
||||
|
10
lua/config/tabnine.lua
Normal file
10
lua/config/tabnine.lua
Normal file
@ -0,0 +1,10 @@
|
||||
local tabnine = require('cmp_tabnine.config')
|
||||
|
||||
tabnine:setup{
|
||||
max_lines = 1000,
|
||||
max_num_results = 20,
|
||||
sort = true,
|
||||
run_on_every_keystroke = true,
|
||||
snippet_placeholder = '..'
|
||||
}
|
||||
|
@ -9,6 +9,9 @@ cmd [[filetype plugin on]]
|
||||
local buffer = { o, bo }
|
||||
local window = { o, wo }
|
||||
|
||||
-- List of possible completion options
|
||||
opt('completeopt', 'menu,menuone,noselect')
|
||||
|
||||
-- Pseudo transparent popup window
|
||||
opt('pumblend', 15)
|
||||
cmd [[highlight PmenuSel blend=0]]
|
||||
|
@ -10,7 +10,7 @@ local function usePlugins(use)
|
||||
-- Reload lua configs
|
||||
use {
|
||||
'famiu/nvim-reload',
|
||||
requires = { 'nvim-lua/plenary.nvim' },
|
||||
requires = 'nvim-lua/plenary.nvim',
|
||||
config = [[require 'config.reload']]
|
||||
}
|
||||
|
||||
@ -69,9 +69,36 @@ local function usePlugins(use)
|
||||
-- UNIX commands
|
||||
use 'tpope/vim-eunuch'
|
||||
|
||||
-- Snippets
|
||||
use 'L3MON4D3/LuaSnip'
|
||||
|
||||
-- Movement utilities
|
||||
use 'tpope/vim-unimpaired'
|
||||
|
||||
-- LSP config
|
||||
use {
|
||||
'neovim/nvim-lspconfig',
|
||||
config = [[require 'config.lspconfig']],
|
||||
}
|
||||
|
||||
-- Completion
|
||||
use { 'onsails/lspkind-nvim' }
|
||||
use {
|
||||
'hrsh7th/nvim-cmp',
|
||||
-- requires = 'onsails/lspkind-nvim', -- For some reason breaks with this line
|
||||
requires = {'saadparwaiz1/cmp_luasnip'},
|
||||
after = 'lspkind-nvim',
|
||||
config = [[require 'config.cmp']],
|
||||
event = 'InsertEnter *'
|
||||
}
|
||||
use {'tzachar/cmp-tabnine', after = 'nvim-cmp', run='./install.sh', requires = 'hrsh7th/nvim-cmp', config = [[require 'config.tabnine']]}
|
||||
use {'hrsh7th/cmp-nvim-lsp', requires = {'hrsh7th/nvim-cmp', 'nvim-lspconfig'}}
|
||||
use {'hrsh7th/cmp-nvim-lua', after = 'nvim-cmp', requires = 'hrsh7th/nvim-cmp'}
|
||||
use {'hrsh7th/cmp-buffer', after = 'nvim-cmp', requires = 'hrsh7th/nvim-cmp'}
|
||||
use {'hrsh7th/cmp-path', after = 'nvim-cmp', requires = 'hrsh7th/nvim-cmp'}
|
||||
use {'hrsh7th/cmp-cmdline', after = 'nvim-cmp', requires = 'hrsh7th/nvim-cmp'}
|
||||
use {'saadparwaiz1/cmp_luasnip', after = 'nvim-cmp', requires = {'L3MON4D3/LuaSnip', 'nvim-cmp'}}
|
||||
|
||||
-- Color themes
|
||||
use 'morhetz/gruvbox'
|
||||
use 'tomasr/molokai'
|
||||
|
Loading…
Reference in New Issue
Block a user