Created
January 22, 2022 16:29
-
-
Save RayZ0rr/554829178447b80f30044267a9748982 to your computer and use it in GitHub Desktop.
Nvim lsp settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--########################################################################################################## | |
-- LSP Keybindings and completion--------------------------------------------------------------------------- | |
--################################################################################################################### | |
local nvim_lsp = require('lspconfig') | |
vim.lsp.set_log_level 'debug' | |
-- Use an on_attach function to only map the following keys | |
-- after the language server attaches to the current buffer | |
local on_attach = function(client, bufnr) | |
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 | |
-- Enable completion triggered by <c-x><c-o> | |
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') | |
-- Mappings. | |
local opts = { noremap=true, silent=true } | |
-- See `:help vim.lsp.*` for documentation on any of the below functions | |
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', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts) | |
buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts) | |
buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts) | |
buf_set_keymap('n', '<space>lD', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts) | |
buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts) | |
buf_set_keymap('n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts) | |
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts) | |
buf_set_keymap('n', '<space>le', '<cmd>lua vim.diagnostic.open_float()<CR>', opts) | |
buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts) | |
buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts) | |
buf_set_keymap('n', '<space>lq', '<cmd>lua vim.diagnostic.setloclist()<CR>', opts) | |
buf_set_keymap('n', '<space>lf', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts) | |
-- require 'illuminate'.on_attach(client) | |
require'aerial'.on_attach(client, bufnr) | |
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! * <buffer> | |
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() | |
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references() | |
augroup END | |
]] | |
end | |
end | |
vim.diagnostic.config({ | |
virtual_text = true, | |
signs = true, | |
underline = true, | |
update_in_insert = false, | |
severity_sort = false, | |
}) | |
-- Change diagnostic symbols in the sign column (gutter) | |
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 | |
vim.o.updatetime = 250 | |
vim.cmd [[autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focus=false})]] | |
-- Set completeopt to have a better completion experience | |
vim.o.completeopt = 'menu,menuone,noselect' | |
-- Add additional capabilities supported by nvim-cmp | |
local capabilities = vim.lsp.protocol.make_client_capabilities() | |
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) | |
-- local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()) | |
--########################################################################################################## | |
-- General lang server setup ----------------------------------------- | |
--################################################################################################################### | |
local server_config = { | |
capabilities = capabilities; | |
on_attach = on_attach; | |
flags = { | |
debounce_text_changes = 150, | |
}, | |
} | |
--########################################################################################################## | |
-- C++ config------------------------------------------ | |
--################################################################################################################### | |
nvim_lsp.clangd.setup { | |
capabilities = capabilities; | |
on_attach = on_attach; | |
flags = { | |
debounce_text_changes = 150, | |
}, | |
cmd = { | |
"clangd", | |
"--background-index", | |
}, | |
filetypes = { "c", "cpp", "objc", "objcpp" }, | |
-- on_init = function to handle changing offsetEncoding | |
-- root_dir = require('lspconfig/util').root_pattern("compile_commands.json", "compile_flags.txt", ".ccls"), | |
-- root_dir = root_pattern("compile_commands.json", "compile_flags.txt", ".git") or dirname | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment