dotfiles/dot_config/nvim/after/plugin/lsp.lua

104 lines
2.6 KiB
Lua

local ok, lsp = pcall(require, 'lsp-zero')
if not ok then
return
end
lsp.preset("recommended")
-- Fix Undefined global 'vim'
lsp.configure('sumneko_lua', {
settings = {
Lua = {
diagnostics = {
globals = { 'vim' }
}
}
}
})
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
local luasnip = require("luasnip")
local cmp = require('cmp')
local cmp_select = {behavior = cmp.SelectBehavior.Select}
local cmp_mappings = lsp.defaults.cmp_mappings({
['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
['<CR>'] = cmp.mapping.confirm({ select = true }),
["<C-e>"] = cmp.mapping.abort(),
["<C-Space>"] = cmp.mapping.complete(),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
})
local lspkind = require('lspkind')
local cmp_formatting = {
format = lspkind.cmp_format({
mode = 'symbol_text', -- show only symbol annotations
maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
})
}
lsp.setup_nvim_cmp({
mapping = cmp_mappings,
formatting = cmp_formatting
})
lsp.on_attach(function(client, bufnr)
local bufmap = function(mode, lhs, rhs)
local opts = {buffer = true, remap = false}
vim.keymap.set(mode, lhs, rhs, opts)
end
-- List diagnostics in Telescope
bufmap('n', 'gA', '<cmd>Telescope diagnostics<cr>')
-- Lists all the references
bufmap('n', 'gr', '<cmd>Telescope lsp_references<cr>')
end)
local on_attach_format = function(client)
require("lsp-format").on_attach(client)
end
lsp.configure('clangd', {
on_attach = on_attach_format
})
lsp.nvim_workspace()
lsp.setup()
vim.diagnostic.config({
virtual_text = false,
severity_sort = true,
float = {
border = 'rounded',
source = 'always',
header = '',
prefix = '',
},
})