vim-settings/neovim/lua/plugins/lsp.lua

305 lines
12 KiB
Lua
Raw Normal View History

2021-09-16 01:13:17 +00:00
-- luacheck: globals packer_plugins
2021-12-08 00:07:57 +00:00
local M = {}
local utils = require("utils")
function M.config_lsp_ui()
-- Add floating window boarders
2021-12-15 17:37:51 +00:00
vim.cmd([[autocmd ColorScheme * highlight NormalFloat guibg=#1f2335]])
vim.cmd([[autocmd ColorScheme * highlight FloatBorder guifg=white guibg=#1f2335]])
local border = {
2021-12-15 17:37:51 +00:00
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
{ "", "FloatBorder" },
}
local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
opts = opts or {}
opts.border = opts.border or border
return orig_util_open_floating_preview(contents, syntax, opts, ...)
end
-- Diagnostics signs
2022-01-01 01:31:46 +00:00
local signs = {
Error = "🔥",
Warn = "⚠️",
Hint = "🤔",
Info = "",
}
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
utils.try_require("trouble", function(trouble)
trouble.setup({
2021-12-11 01:10:28 +00:00
fold_open = "",
fold_closed = "",
2022-01-01 01:31:46 +00:00
icons = false,
use_diagnostic_signs = true,
2021-12-15 17:37:51 +00:00
})
end)
end
2021-08-24 17:38:14 +00:00
local function default_attach(client, bufnr)
2021-12-15 17:37:51 +00:00
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
2021-08-24 17:38:14 +00:00
2022-01-10 18:46:53 +00:00
-- Set built in features to use lsp functions
2021-12-15 17:37:51 +00:00
buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
2022-01-10 18:46:53 +00:00
if client.resolved_capabilities.goto_definition then
buf_set_option("tagfunc", "v:lua.vim.lsp.tagfunc")
end
2022-01-10 18:46:53 +00:00
if client.resolved_capabilities.document_formatting then
buf_set_option("formatexpr", "v:lua.vim.lsp.formatexpr()")
end
2021-08-24 17:38:14 +00:00
-- Mappings
2021-09-16 01:13:17 +00:00
-- TODO: Maybe prefix all of these for easier discovery
2021-12-15 17:37:51 +00:00
local opts = { noremap = true, silent = true }
local lsp_keymap = utils.keymap_group("n", "<leader>l", opts, bufnr)
lsp_keymap("h", "<cmd>lua vim.lsp.buf.hover()<CR>")
lsp_keymap("r", "<cmd>lua vim.lsp.buf.rename()<CR>")
lsp_keymap("e", "<cmd>lua vim.lsp.diagnostics.show_line_diagnostics()<CR>")
2021-12-15 17:37:51 +00:00
buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
buf_set_keymap("n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
buf_set_keymap("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
buf_set_keymap("n", "<leader>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
buf_set_keymap("n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
buf_set_keymap("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", "<leader>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
buf_set_keymap("n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
2021-08-24 17:38:14 +00:00
-- Open diagnostic on hold
if vim["diagnostic"] ~= nil then
2021-12-15 17:37:51 +00:00
vim.cmd([[autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focus=false})]])
end
2021-08-24 17:38:14 +00:00
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
buf_set_keymap("n", "<leader>lf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
2021-08-24 17:38:14 +00:00
vim.cmd([[
augroup lsp_format
autocmd!
autocmd BufWritePre *.rs,*.go,*.sh lua vim.lsp.buf.formatting_sync(nil, 1000)
" autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync(nil, 1000)
2021-08-24 17:38:14 +00:00
augroup END
]])
end
if client.resolved_capabilities.document_range_formatting then
buf_set_keymap("n", "<leader>lfr", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
2021-08-24 17:38:14 +00:00
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.cmd([[
:hi LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow
:hi LspReferenceText cterm=bold ctermbg=red guibg=LightYellow
:hi LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow
augroup lsp_document_highlight
autocmd!
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]])
end
-- Some override some fuzzy finder bindings to use lsp sources
if utils.try_require("telescope.nvim") ~= nil then
buf_set_keymap("n", "<leader>t", "<cmd>Telescope lsp_document_symbols<CR>", opts)
buf_set_keymap("n", "<leader>ft", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
end
2021-12-08 00:07:57 +00:00
-- Use LspSaga features, if possible
if utils.is_plugin_loaded("lspsaga.nvim") then
2021-12-15 17:37:51 +00:00
buf_set_keymap("n", "K", "<Cmd>lua require('lspsaga.hover').render_hover_doc()<CR>", opts)
buf_set_keymap("n", "<leader>rn", "<cmd>lua require('lspsaga.rename').rename()<CR>", opts)
buf_set_keymap("n", "<leader>e", "<cmd>lua require('lspsaga.diagnostic').show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua require('lspsaga.diagnostic').lsp_jump_diagnostic_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua require('lspsaga.diagnostic').lsp_jump_diagnostic_next()<CR>", opts)
buf_set_keymap("n", "<C-k>", "<cmd>lua require('lspsaga.signaturehelp').signature_help()<CR>", opts)
2021-12-08 00:07:57 +00:00
-- Code actions
2021-12-15 17:37:51 +00:00
buf_set_keymap("n", "<leader>ca", "<cmd>lua require('lspsaga.codeaction').code_action()<CR>", opts)
2021-12-08 00:07:57 +00:00
end
2021-08-24 17:38:14 +00:00
end
local function merged_capabilities()
2021-09-30 17:58:22 +00:00
-- Maybe update capabilities
local capabilities = vim.lsp.protocol.make_client_capabilities()
utils.try_require("cmp-nvim-lsp", function(cmp_nvim_lsp)
capabilities = cmp_nvim_lsp.update_capabilities(capabilities)
end)
return capabilities
end
function M.config_lsp()
utils.try_require("lspconfig", function(lsp_config)
local capabilities = merged_capabilities()
-- Configure each server
lsp_config.bashls.setup({ capabilities = capabilities, on_attach = default_attach })
lsp_config.gopls.setup({ capabilities = capabilities, on_attach = default_attach })
lsp_config.pyright.setup({ capabilities = capabilities, on_attach = default_attach })
lsp_config.rls.setup({
capabilities = capabilities,
on_attach = default_attach,
settings = {
rust = {
build_on_save = false,
all_features = true,
unstable_features = true,
},
2021-08-24 17:38:14 +00:00
},
})
end)
2021-08-24 17:38:14 +00:00
end
2021-12-08 00:07:57 +00:00
function M.config_lsp_saga()
utils.try_require("lspsaga", function(saga)
saga.init_lsp_saga({
2021-12-16 22:43:02 +00:00
error_sign = "🔥",
warn_sign = "⚠️",
hint_sign = "🤔",
dianostic_header_icon = " 💬 ",
code_action_icon = "💡",
code_action_prompt = {
enable = false,
sign = false,
},
})
end)
2021-12-08 00:07:57 +00:00
end
function M.config_null_ls()
utils.try_require("null-ls", function(null_ls)
local helpers = require("null-ls.helpers")
local alex_lint = {
name = "alex",
method = null_ls.methods.DIAGNOSTICS,
filetypes = { "markdown" },
generator = null_ls.generator({
command = "alex",
args = { "--stdin", "--quiet" },
to_stdin = true,
from_stderr = true,
format = "line",
check_exit_code = function(code)
return code <= 1
end,
on_output = helpers.diagnostics.from_patterns({
{
pattern = [[ *(%d+):(%d+)-(%d+):(%d+) *(%w+) *(.+) +[%w]+ +([-%l]+)]],
groups = { "row", "col", "end_row", "end_col", "severity", "message", "code" },
},
}),
}),
}
2022-01-03 06:13:15 +00:00
local sources = {
-- Generic
null_ls.builtins.formatting.preittier,
null_ls.builtins.formatting.trim_whitespace,
null_ls.builtins.formatting.trim_newlines,
2022-01-03 06:13:15 +00:00
-- Fish
null_ls.builtins.formatting.fish_indent,
2022-01-03 06:13:15 +00:00
-- Python
null_ls.builtins.formatting.reorder_python_imports,
null_ls.builtins.formatting.black,
null_ls.builtins.diagnostics.mypy,
-- Go
null_ls.builtins.diagnostics.golangci_lint,
-- Text
null_ls.builtins.diagnostics.proselint,
-- null_ls.builtins.diagnostics.write_good,
null_ls.builtins.code_actions.proselint,
alex_lint,
2022-01-03 06:13:15 +00:00
-- Ansible
null_ls.builtins.diagnostics.ansiblelint,
2022-01-03 06:13:15 +00:00
-- Shell
null_ls.builtins.diagnostics.shellcheck,
-- Rust
-- null_ls.builtins.formatting.rustfmt,
-- Lua
null_ls.builtins.diagnostics.luacheck,
null_ls.builtins.formatting.stylua,
}
-- HACK: Handle old versions of null_ls for vim < 0.6 that don't support `setup`
if null_ls["setup"] ~= nil then
null_ls.setup({
on_attach = default_attach,
capabilities = merged_capabilities(),
sources = sources,
})
else
null_ls.config({
sources = sources,
})
require("lspconfig")["null-ls"].setup({
on_attach = default_attach,
})
end
2021-12-16 22:43:02 +00:00
end)
end
2022-01-03 06:44:02 +00:00
local function get_luadev_config()
local luadev = utils.try_require("lua-dev")
if luadev ~= nil then
return luadev.setup({
-- add any options here, or leave empty to use the default settings
lspconfig = {
on_attach = default_attach,
settings = {
Lua = {
completion = {
callSnippet = "Disable",
keywordSnippet = "Disable",
},
},
},
},
})
end
return nil
end
function M.config_lsp_intaller()
utils.try_require("nvim-lsp-installer", function(lsp_installer)
2022-01-03 06:44:02 +00:00
-- Default options
local opts = {
on_attach = default_attach,
}
lsp_installer.on_server_ready(function(server)
2022-01-03 06:44:02 +00:00
-- Config luadev opts
if server.name == "sumneko_lua" then
local luadev = get_luadev_config()
if luadev ~= nil then
opts.settings = luadev.settings
end
end
server:setup(opts)
end)
end)
end
2021-12-08 00:07:57 +00:00
return M