Blob config/nvim/lua/config/lsp.lua
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
local on_attach = function(client, bufnr)
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
vim.o.foldmethod = "expr"
vim.o.foldexpr = "v:lua.vim.lsp.foldexpr()"
-- Run cursor+timer triggers faster
vim.o.updatetime = 300
-- So that the window doesn't shift around in the presence of
-- diagnostics
vim.wo.signcolumn = "yes"
local function map(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local opts = { silent = true, noremap = true }
map('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
map('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
map('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
map('n', 'gy', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
map('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
map('n', '<leader>r', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
map('n', '<leader>a', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
map('n', '<leader>s', '<cmd>Telescope lsp_document_symbols<CR>', opts)
map('n', '<leader>S', '<cmd>Telescope lsp_dynamic_workspace_symbols<CR>', opts)
local cmd = vim.api.nvim_command
-- Format right before saving
cmd([[autocmd BufWritePre <buffer> lua vim.lsp.buf.format()]])
-- Highlight current "word" under cursor
cmd([[autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()]])
-- And clear the highlight after move
cmd([[autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()]])
end
local config = require('lspconfig')
local ok, _ = pcall(require, 'cmp_nvim_lsp')
if ok then
local caps = require('cmp_nvim_lsp').default_capabilities()
caps["textDocument"]["completion"]["completionItem"]["snippetSupport"] = false
config.util.default_config = vim.tbl_deep_extend('force', config.util.default_config, {
capabilities = caps
})
end
config.util.default_config = vim.tbl_deep_extend('force', config.util.default_config, {
on_attach = on_attach,
flags = {
debounce_text_changes = 150,
}
})
config.rust_analyzer.setup({
cmd = {"rustup", "run", "stable", "rust-analyzer"},
settings = {
["rust-analyzer"] = {
checkOnSave = {
command = "clippy",
},
cargo = {
allFeatures = true,
},
},
}
})
config.gopls.setup({
cmd = {
'gopls',
'-remote=auto',
},
settings = {
gopls = {
analyses = {
unusedparams = true,
nilness = true,
shadow = true,
unusedwrite = true,
},
staticcheck = true,
},
},
})
local function home_cache_dir(tail)
local base = vim.env.XDG_CACHE_HOME
if not base and vim.env.HOME then
base = vim.env.HOME .. "/.cache"
end
-- fallback to a hidden dir on cwd
if not base then
return '.' .. tail
end
return base .. '/' .. tail
end
config.ccls.setup({
init_options = {
cache = {
directory = home_cache_dir('ccls'),
},
},
})
|