Same for lsp and cmp
This commit is contained in:
118
lua/setup/cmp.lua
Normal file
118
lua/setup/cmp.lua
Normal file
@@ -0,0 +1,118 @@
|
||||
local cmp = require('cmp')
|
||||
local luasnip = require("luasnip")
|
||||
|
||||
local t = function(str)
|
||||
return vim.api.nvim_replace_termcodes(str, true, true, true)
|
||||
end
|
||||
|
||||
local check_back_space = function()
|
||||
local col = vim.fn.col(".") - 1
|
||||
return col == 0 or vim.fn.getline("."):sub(col, col):match("%s") ~= nil
|
||||
end
|
||||
|
||||
cmp.setup {
|
||||
|
||||
formatting = {
|
||||
format = function(entry, vim_item)
|
||||
-- fancy icons and a name of kind
|
||||
vim_item.kind = require("lspkind").presets.default[vim_item.kind] ..
|
||||
" " .. vim_item.kind
|
||||
-- set a name for each source
|
||||
vim_item.menu = ({
|
||||
buffer = "[Buffer]",
|
||||
nvim_lsp = "[LSP]",
|
||||
ultisnips = "[UltiSnips]",
|
||||
nvim_lua = "[Lua]",
|
||||
cmp_tabnine = "[TabNine]",
|
||||
look = "[Look]",
|
||||
path = "[Path]",
|
||||
spell = "[Spell]",
|
||||
calc = "[Calc]",
|
||||
emoji = "[Emoji]"
|
||||
})[entry.source.name]
|
||||
return vim_item
|
||||
end
|
||||
},
|
||||
mapping = {
|
||||
['<Up>'] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 'c', 's'}),
|
||||
['<Down>'] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 'c', 's' }),
|
||||
['<C-d>'] = cmp.mapping.scroll_docs(-4),
|
||||
['<C-f>'] = cmp.mapping.scroll_docs(4),
|
||||
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c', 's' }),
|
||||
['<C-e>'] = cmp.mapping(cmp.mapping.close(), { 'i', 'c', 's' }),
|
||||
['<CR>'] = cmp.mapping({
|
||||
i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true }),
|
||||
c = cmp.mapping.confirm({ select = false }),
|
||||
s = cmp.mapping.confirm({ select = false }),
|
||||
}),
|
||||
["<Tab>"] = cmp.mapping(function(fallback)
|
||||
if luasnip.expand_or_jumpable() then
|
||||
vim.api.nvim_feedkeys(t("<Plug>luasnip-expand-or-jump"), "", true)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, {
|
||||
"i",
|
||||
"s",
|
||||
}),
|
||||
["<S-Tab>"] = cmp.mapping(function(fallback)
|
||||
if luasnip.jumpable(-1) then
|
||||
vim.api.nvim_feedkeys(t("<Plug>luasnip-jump-prev"), "", true)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, {
|
||||
"i",
|
||||
"s",
|
||||
}),
|
||||
},
|
||||
snippet = {
|
||||
expand = function(args)
|
||||
require'luasnip'.lsp_expand(args.body)
|
||||
end
|
||||
},
|
||||
sources = {
|
||||
{name = "luasnip"}, {name = 'nvim_lsp'}, {name = 'buffer'},
|
||||
{name = "nvim_lua"}, {name = "look"}, {name = "path"},
|
||||
{name = 'cmp_tabnine'}, {name = "calc"}, {name = "spell"},
|
||||
{name = "emoji"}
|
||||
},
|
||||
completion = {completeopt = 'menu,menuone,noinsert, noselect'},
|
||||
-- experimental = { native_menu = true }
|
||||
}
|
||||
|
||||
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
|
||||
cmp.setup.cmdline('/', {
|
||||
sources = {
|
||||
{ name = 'buffer' }
|
||||
},
|
||||
})
|
||||
|
||||
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
|
||||
cmp.setup.cmdline(':', {
|
||||
sources = cmp.config.sources({
|
||||
{ name = 'path' }
|
||||
}, {
|
||||
{ name = 'cmdline' }
|
||||
}, {
|
||||
{ name = 'buffer' }
|
||||
}),
|
||||
})
|
||||
|
||||
-- Autopairs
|
||||
--require("nvim-autopairs.completion.cmp").setup({
|
||||
-- map_cr = true,
|
||||
-- map_complete = true,
|
||||
-- auto_select = true
|
||||
--})
|
||||
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
|
||||
cmp.event:on( 'confirm_done', cmp_autopairs.on_confirm_done({ map_char = { tex = '' } }))
|
||||
|
||||
-- TabNine
|
||||
--local tabnine = require('cmp_tabnine.config')
|
||||
--tabnine:setup({max_lines = 1000, max_num_results = 20, sort = true})
|
||||
|
||||
-- Database completion
|
||||
vim.api.nvim_exec([[
|
||||
autocmd FileType sql,mysql,plsql lua require('cmp').setup.buffer({ sources = {{ name = 'vim-dadbod-completion' }} })
|
||||
]], false)
|
||||
134
lua/setup/lspinstall.lua
Normal file
134
lua/setup/lspinstall.lua
Normal file
@@ -0,0 +1,134 @@
|
||||
local utils = require('utils')
|
||||
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
capabilities.textDocument.completion.completionItem.documentationFormat = { 'markdown', 'plaintext' }
|
||||
capabilities.textDocument.completion.completionItem.snippetSupport = true
|
||||
capabilities.textDocument.completion.completionItem.preselectSupport = true
|
||||
capabilities.textDocument.completion.completionItem.insertReplaceSupport = true
|
||||
capabilities.textDocument.completion.completionItem.labelDetailsSupport = true
|
||||
capabilities.textDocument.completion.completionItem.deprecatedSupport = true
|
||||
capabilities.textDocument.completion.completionItem.commitCharactersSupport = true
|
||||
capabilities.textDocument.completion.completionItem.tagSupport = { valueSet = { 1 } }
|
||||
capabilities.offsetEncoding = { "utf-16" }
|
||||
capabilities.textDocument.completion.completionItem.resolveSupport = {
|
||||
properties = {
|
||||
'documentation',
|
||||
'detail',
|
||||
'additionalTextEdits',
|
||||
},
|
||||
}
|
||||
|
||||
local on_attach = function(client, bufnr)
|
||||
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
|
||||
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
|
||||
|
||||
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
|
||||
vim.api.nvim_buf_set_option(0, 'formatexpr', 'v:lua.vim.lsp.formatexpr()')
|
||||
|
||||
-- Mappings.
|
||||
local opts = { noremap=true, silent=true }
|
||||
buf_set_keymap('n', '<space>,', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>;', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>a', '<cmd>Telescope lsp_code_actions<CR>', opts)
|
||||
buf_set_keymap('n', '<space>d', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>e', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>h', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>c', '<cmd>lua vim.lsp.buf.outgoing_calls()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>C', '<cmd>lua vim.lsp.buf.incoming_calls()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>m', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
|
||||
-- buf_set_keymap('n', '<space>r', '<cmd>lua vim.lsp.buf.references()<CR, opts>')
|
||||
buf_set_keymap('n', '<space>s', '<cmd>lua vim.lsp.buf.document_symbol()<CR>', opts)
|
||||
buf_set_keymap('n', '<C-t>', '<cmd>Telescope lsp_dynamic_workspace_symbols<CR>', opts)
|
||||
buf_set_keymap('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
|
||||
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
|
||||
buf_set_keymap('n', '<space>r', '<cmd>Telescope lsp_references<cr>', opts)
|
||||
buf_set_keymap('n', '<C-S-o>', '<cmd>Telescope lsp_document_symbols<cr>', opts)
|
||||
buf_set_keymap('n', '<A-m>', '<cmd>Telescope lsp_document_symbols<cr>', opts)
|
||||
buf_set_keymap('n', '<space>v', '<cmd>Telescope diagnostics bufnr=0<cr>', opts)
|
||||
buf_set_keymap('n', '<A-o>', ':ClangdSwitchSourceHeader<CR>', opts)
|
||||
|
||||
vim.cmd [[autocmd CursorHold <buffer> lua vim.diagnostic.open_float({focusable = false})]]
|
||||
|
||||
-- Set some keybinds conditional on server capabilities
|
||||
if client.resolved_capabilities.document_formatting then
|
||||
buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
|
||||
end
|
||||
if client.resolved_capabilities.document_range_formatting then
|
||||
buf_set_keymap('v', '<space>f', '<esc><cmd>lua vim.lsp.buf.range_formatting()<CR>', opts)
|
||||
end
|
||||
|
||||
-- Set autocommands conditional on server_capabilities
|
||||
if client.resolved_capabilities.document_highlight then
|
||||
vim.api.nvim_exec([[
|
||||
hi LspReferenceRead cterm=bold ctermbg=red guibg=DarkGreen
|
||||
hi LspReferenceWrite cterm=bold ctermbg=red guibg=DarkRed
|
||||
augroup lsp_document_highlight
|
||||
autocmd! * <buffer>
|
||||
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
||||
autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
|
||||
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
||||
augroup END
|
||||
]], false)
|
||||
end
|
||||
require "lsp_signature".on_attach({
|
||||
bind = true, -- This is mandatory, otherwise border config won't get registered.
|
||||
handler_opts = {
|
||||
border = "single"
|
||||
},
|
||||
hi_parameter = "IncSearch"
|
||||
}, bufnr)
|
||||
end
|
||||
|
||||
local lsp_installer = require("nvim-lsp-installer")
|
||||
|
||||
local servers = {
|
||||
"pyright",
|
||||
"cmake",
|
||||
"clangd",
|
||||
"jsonls",
|
||||
"groovyls",
|
||||
"sumneko_lua",
|
||||
"dockerls",
|
||||
}
|
||||
|
||||
for _, name in pairs(servers) do
|
||||
local server_is_found, server = lsp_installer.get_server(name)
|
||||
if server_is_found then
|
||||
if not server:is_installed() then
|
||||
print("Installing " .. name)
|
||||
server:install()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
lsp_installer.on_server_ready(function(server)
|
||||
local opts = {
|
||||
on_attach = on_attach,
|
||||
capabilities = capabilities,
|
||||
}
|
||||
|
||||
-- (optional) Customize the options passed to the server
|
||||
if server.name == "clangd" then
|
||||
opts.cmd = { "clangd" , "--compile-commands-dir=build_nvim"}
|
||||
end
|
||||
|
||||
-- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart)
|
||||
server:setup(opts)
|
||||
vim.cmd [[ do User LspAttachBuffers ]]
|
||||
end)
|
||||
|
||||
local null_ls = require("null-ls")
|
||||
null_ls.setup {
|
||||
sources = {
|
||||
null_ls.builtins.code_actions.gitsigns,
|
||||
null_ls.builtins.formatting.autopep8,
|
||||
null_ls.builtins.formatting.prettier,
|
||||
null_ls.builtins.formatting.stylua,
|
||||
null_ls.builtins.diagnostics.flake8,
|
||||
null_ls.builtins.formatting.isort,
|
||||
null_ls.builtins.formatting.cmake_format
|
||||
},
|
||||
on_attach = on_attach,
|
||||
capabilities = capabilities
|
||||
}
|
||||
vim.diagnostic.config({virtual_text = false})
|
||||
Reference in New Issue
Block a user