nvim/after/plugin/lsp.lua
Louis Hollingworth c5000e27eb
New neovim config.
I've been using neovim for a while but the config got messy so here is a
cleaner config.

Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
2024-03-02 13:31:01 +00:00

136 lines
4.1 KiB
Lua

local keymap = vim.keymap.set
require('mason').setup({
ui = {
icons = {
server = "",
lsp = "",
linter = "",
formatter = "",
package_installed = "",
package_pending = "",
package_uninstalled = ""
},
},
})
require("mason-lspconfig").setup({
automatic_installation = true,
})
local lsp = require('lspconfig')
local cmp = require('cmp')
local cmp_select = {behavior = cmp.SelectBehavior.Select}
local opts = { noremap=true, silent=true }
--keymap('i', '<C-Space>', vim.diagnostic.open_float, opts)
--keymap('i', '<C-p>', vim.diagnostic.goto_prev, opts)
--keymap('i', '<C-n>', vim.diagnostic.goto_next, opts)
--keymap('i', '<C-Space>', cmp.mapping.complete(), opts)
--keymap('i', '<CR>', vim.diagnostic.setloclist, opts)
keymap('n', '<C-Space>', vim.diagnostic.open_float, opts)
keymap('n', '<C-p>', vim.diagnostic.goto_prev, opts)
keymap('n', '<C-n>', vim.diagnostic.goto_next, opts)
--keymap('i', '<C-Space>', cmp.mapping.complete(), opts)
keymap('n', '<CR>', vim.diagnostic.setloclist, opts)
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
mapping = {
['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
['<C-Space>'] = cmp.mapping.complete(cmp_select),
['<C-e>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}),
},
sources = {
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
{ name = 'buffer' },
},
})
vim.diagnostic.config({
virtual_text = true,
})
local on_attach = function(client, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
-- Mappings.
local opts = { noremap = true, silent = true, buffer = bufnr}
-- See `:help vim.lsp.*` for documentation on any of the below functions
vim.keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
vim.keymap.set("n", "gr", "<cmd>Telescope lsp_references<CR>", opts)
vim.keymap.set("n", "<C-j>", "<cmd>Telescope lsp_document_symbols<CR>", opts)
vim.keymap.set("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
vim.keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts)
vim.keymap.set("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
vim.keymap.set("n", "<leader>D", "<cmd>Telescope lsp_type_definitions<CR>", opts)
vim.keymap.set("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
vim.keymap.set("n", "<leader>ca", "<cmd>Telescope lsp_code_actions<CR>", opts)
vim.keymap.set("n", "gt", vim.lsp.buf.type_definition, {buffer=0})
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, {buffer=0})
vim.keymap.set("n", "<leader>dj", vim.diagnostic.goto_next, {buffer=0})
vim.keymap.set("n", "<leader>dk", vim.diagnostic.goto_prev, {buffer=0})
vim.keymap.set("n", "<leader>dl", "<cmd>Telescope diagnostics<cr>", {buffer=0})
vim.keymap.set("n", "<leader>r", vim.lsp.buf.rename, {buffer=0})
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, {buffer=0})
-- Set autocommands conditional on server_capabilities
if client.server_capabilities.document_highlight then
vim.cmd([[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]])
end
end
lsp.gopls.setup({
on_attach = on_attach,
flags = lsp_flags,
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
},
},
})
lsp['lua_ls'].setup{
on_attach = on_attach,
flags = lsp_flags,
}
lsp['rust_analyzer'].setup{
on_attach = on_attach,
flags = lsp_flags,
cmd = {
"rustup", "run", "stable", "rust-analyzer",
}
}
lsp.tsserver.setup{
on_attach = on_attach,
flags = lsp_flags,
}