2021-09-16 01:13:17 +00:00
|
|
|
-- luacheck: globals packer_plugins
|
2021-09-30 17:57:37 +00:00
|
|
|
local utils = require("utils")
|
|
|
|
|
2021-08-24 17:38:14 +00:00
|
|
|
local function default_attach(client, bufnr)
|
2021-09-30 17:57:37 +00:00
|
|
|
if utils.is_plugin_loaded("completion-nvim") then
|
|
|
|
require('completion').on_attach()
|
|
|
|
end
|
2021-08-24 17:38:14 +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
|
|
|
|
|
|
|
|
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
|
|
|
|
|
|
|
|
-- Mappings
|
2021-09-16 01:13:17 +00:00
|
|
|
-- TODO: Maybe prefix all of these for easier discovery
|
2021-08-24 17:38:14 +00:00
|
|
|
local opts = { noremap=true, silent=true }
|
|
|
|
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)
|
|
|
|
|
|
|
|
-- Set some keybinds conditional on server capabilities
|
|
|
|
if client.resolved_capabilities.document_formatting then
|
|
|
|
buf_set_keymap("n", "<leader>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
|
|
|
|
vim.cmd([[
|
|
|
|
augroup lsp_format
|
|
|
|
autocmd!
|
2021-09-09 23:09:38 +00:00
|
|
|
autocmd BufWritePre *.rs,*.go 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
|
|
|
|
]])
|
|
|
|
elseif client.resolved_capabilities.document_range_formatting then
|
|
|
|
buf_set_keymap("n", "<leader>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
|
|
|
|
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
|
|
|
|
|
2021-08-25 23:28:24 +00:00
|
|
|
-- Some override some fuzzy finder bindings to use lsp sources
|
|
|
|
if packer_plugins["nvim-lspfuzzy"] then
|
|
|
|
buf_set_keymap("n", "<leader>t", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", opts)
|
|
|
|
-- buf_set_keymap("n", "<leader>ft", "<cmd>lua vim.lsp.buf.workspace_symbol()<CR>", opts)
|
|
|
|
elseif packer_plugins["telescope.nvim"] 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-08-24 17:38:14 +00:00
|
|
|
end
|
|
|
|
|
2021-08-25 23:21:50 +00:00
|
|
|
local function config_lsp()
|
2021-08-24 17:38:14 +00:00
|
|
|
local language_servers = {
|
|
|
|
"bashls",
|
|
|
|
"gopls",
|
2021-09-30 17:56:18 +00:00
|
|
|
-- "pylsp",
|
|
|
|
"pyright",
|
2021-08-24 17:38:14 +00:00
|
|
|
"rust_analyzer",
|
|
|
|
}
|
|
|
|
local lsp_config = require("lspconfig")
|
|
|
|
|
2021-09-30 17:58:22 +00:00
|
|
|
-- Maybe update capabilities
|
|
|
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
|
|
|
if utils.is_plugin_loaded("cmp-nvim-lsp") then
|
2021-12-01 17:49:21 +00:00
|
|
|
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)
|
2021-09-30 17:58:22 +00:00
|
|
|
end
|
|
|
|
|
2021-08-24 17:38:14 +00:00
|
|
|
for _, ls in ipairs(language_servers) do
|
|
|
|
lsp_config[ls].setup{
|
2021-09-30 17:58:22 +00:00
|
|
|
capabilities = capabilities,
|
2021-08-24 17:38:14 +00:00
|
|
|
on_attach=default_attach,
|
|
|
|
settings={
|
|
|
|
pylsp={
|
2021-08-24 23:13:10 +00:00
|
|
|
-- configurationSources = {"flake8"},
|
|
|
|
configurationSources = {"black"},
|
2021-08-24 17:38:14 +00:00
|
|
|
formatCommand = {"black"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-25 23:21:50 +00:00
|
|
|
config_lsp()
|