From c16794addd2dfebb727beaebbfe0ad4c125c94f7 Mon Sep 17 00:00:00 2001 From: krolyxon Date: Tue, 6 Sep 2022 22:36:57 +0530 Subject: [PATCH] lspconfig --- lua/core/mappings.lua | 127 ++++++++++++++++++ lua/core/utils.lua | 9 ++ lua/plugins/configs/lspconfig.lua | 215 ++++++++++++++++-------------- 3 files changed, 248 insertions(+), 103 deletions(-) diff --git a/lua/core/mappings.lua b/lua/core/mappings.lua index 28b5a7d..a71a936 100644 --- a/lua/core/mappings.lua +++ b/lua/core/mappings.lua @@ -156,6 +156,133 @@ M.comment = { }, } +M.lspconfig = { + plugin = true, + + -- See ` :help vim.lsp.*` for documentation on any of the below functions + + n = { + ["gD"] = { + function() + vim.lsp.buf.declaration() + end, + "lsp declaration", + }, + + ["gd"] = { + function() + vim.lsp.buf.definition() + end, + "lsp definition", + }, + + ["K"] = { + function() + vim.lsp.buf.hover() + end, + "lsp hover", + }, + + ["gi"] = { + function() + vim.lsp.buf.implementation() + end, + "lsp implementation", + }, + + ["ls"] = { + function() + vim.lsp.buf.signature_help() + end, + "lsp signature_help", + }, + + ["D"] = { + function() + vim.lsp.buf.type_definition() + end, + "lsp definition type", + }, + + ["ra"] = { + function() + require("nvchad_ui.renamer").open() + end, + "lsp rename", + }, + + ["ca"] = { + function() + vim.lsp.buf.code_action() + end, + "lsp code_action", + }, + + ["gr"] = { + function() + vim.lsp.buf.references() + end, + "lsp references", + }, + + ["f"] = { + function() + vim.diagnostic.open_float() + end, + "floating diagnostic", + }, + + ["[d"] = { + function() + vim.diagnostic.goto_prev() + end, + "goto prev", + }, + + ["d]"] = { + function() + vim.diagnostic.goto_next() + end, + "goto_next", + }, + + ["q"] = { + function() + vim.diagnostic.setloclist() + end, + "diagnostic setloclist", + }, + + ["fm"] = { + function() + vim.lsp.buf.formatting {} + end, + "lsp formatting", + }, + + ["wa"] = { + function() + vim.lsp.buf.add_workspace_folder() + end, + "add workspace folder", + }, + + ["wr"] = { + function() + vim.lsp.buf.remove_workspace_folder() + end, + "remove workspace folder", + }, + + ["wl"] = { + function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, + "list workspace folders", + }, + }, +} + M.blankline = { plugin = true, diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 26cd80d..a281163 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -1,6 +1,15 @@ local M = {} local merge_tb = vim.tbl_deep_extend +M.close_buffer = function(bufnr) + if vim.bo.buftype == "terminal" then + vim.cmd(vim.bo.buflisted and "set nobl | enew" or "hide") + else + bufnr = bufnr or vim.api.nvim_get_current_buf() + vim.cmd("confirm bd" .. bufnr) + end +end + M.load_mappings = function(section, mapping_opt) local function set_section_map(section_values) if section_values.plugin then diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua index dabfe7f..e9efb19 100644 --- a/lua/plugins/configs/lspconfig.lua +++ b/lua/plugins/configs/lspconfig.lua @@ -1,117 +1,126 @@ --- Mappings. --- See `:help vim.diagnostic.*` for documentation on any of the below functions -local opts = { noremap=true, silent=true } -vim.keymap.set('n', 'e', vim.diagnostic.open_float, opts) -vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts) -vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts) -vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) +local present, lspconfig = pcall(require, "lspconfig") --- 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) - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') - - -- Mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local bufopts = { noremap=true, silent=true, buffer=bufnr } - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) - vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) - vim.keymap.set('n', '', vim.lsp.buf.signature_help, bufopts) - vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, bufopts) - vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, bufopts) - vim.keymap.set('n', 'wl', function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end, bufopts) - vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, bufopts) - vim.keymap.set('n', 'rn', vim.lsp.buf.rename, bufopts) - vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, bufopts) - vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) - vim.keymap.set('n', 'f', vim.lsp.buf.formatting, bufopts) +if not present then + return end -local lsp_flags = { - -- This is the default in Nvim 0.7+ - debounce_text_changes = 150, -} +local M = {} +local utils = require "core.utils" -local function lspSymbol(name, icon) - local hl = "DiagnosticSign" .. name - vim.fn.sign_define(hl, { text = icon, numhl = hl, texthl = hl }) -end +-- export on_attach & capabilities for custom lspconfigs -lspSymbol("Error", "") -lspSymbol("Info", "") -lspSymbol("Hint", "") -lspSymbol("Warn", "") - -vim.diagnostic.config { - virtual_text = { - prefix = "", - }, - signs = true, - underline = true, - update_in_insert = false, -} - -vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { - border = "single", -}) -vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { - border = "single", - focusable = false, - relative = "cursor", -}) - --- suppress error messages from lang servers -vim.notify = function(msg, log_level) - if msg:match "exit code" then - return - end - if log_level == vim.log.levels.ERROR then - vim.api.nvim_err_writeln(msg) +M.on_attach = function(client, bufnr) + if vim.g.vim_version > 7 then + -- nightly + client.server_capabilities.documentFormattingProvider = false + client.server_capabilities.documentRangeFormattingProvider = false else - vim.api.nvim_echo({ { msg } }, true, {}) + -- stable + client.resolved_capabilities.document_formatting = false + client.resolved_capabilities.document_range_formatting = false + end + + utils.load_mappings("lspconfig", { buffer = bufnr }) + + if client.server_capabilities.signatureHelpProvider then + local function lspSymbol(name, icon) + local hl = "DiagnosticSign" .. name + vim.fn.sign_define(hl, { text = icon, numhl = hl, texthl = hl }) + end + + lspSymbol("Error", "") + lspSymbol("Info", "") + lspSymbol("Hint", "") + lspSymbol("Warn", "") + + vim.diagnostic.config { + virtual_text = { + prefix = "", + }, + signs = true, + underline = true, + update_in_insert = false, + } + + vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { + border = "single", + }) + vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { + border = "single", + focusable = false, + relative = "cursor", + }) + + -- suppress error messages from lang servers + vim.notify = function(msg, log_level) + if msg:match "exit code" then + return + end + if log_level == vim.log.levels.ERROR then + vim.api.nvim_err_writeln(msg) + else + vim.api.nvim_echo({ { msg } }, true, {}) + end + end + + -- Borders for LspInfo winodw + local win = require "lspconfig.ui.windows" + local _default_opts = win.default_opts + + win.default_opts = function(options) + local opts = _default_opts(options) + opts.border = "single" + return opts + end end end --- Borders for LspInfo winodw -local win = require "lspconfig.ui.windows" -local _default_opts = win.default_opts +M.capabilities = vim.lsp.protocol.make_client_capabilities() -win.default_opts = function(options) - local opts = _default_opts(options) - opts.border = "single" - return opts -end - -require('lspconfig')['sumneko_lua'].setup{ - on_attach = on_attach, - flags = lsp_flags, - settings = { - Lua = { - diagnostics = { - globals = { "vim" }, - }, - workspace = { - library = { - [vim.fn.expand "$VIMRUNTIME/lua"] = true, - [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true, - }, - maxpreload = 100000, - preloadFileSize = 10000, - }, - }, +M.capabilities.textDocument.completion.completionItem = { + documentationFormat = { "markdown", "plaintext" }, + snippetSupport = true, + preselectSupport = true, + insertReplaceSupport = true, + labelDetailsSupport = true, + deprecatedSupport = true, + commitCharactersSupport = true, + tagSupport = { valueSet = { 1 } }, + resolveSupport = { + properties = { + "documentation", + "detail", + "additionalTextEdits", }, + }, } -require('lspconfig')['rust_analyzer'].setup{ - on_attach = on_attach, - flags = lsp_flags, - -- Server-specific settings... - settings = { - ["rust-analyzer"] = {} - } +lspconfig.sumneko_lua.setup { + on_attach = M.on_attach, + capabilities = M.capabilities, + + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + workspace = { + library = { + [vim.fn.expand "$VIMRUNTIME/lua"] = true, + [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true, + }, + maxPreload = 100000, + preloadFileSize = 10000, + }, + }, + }, } + +lspconfig.rust_analyzer.setup { + on_attach = M.on_attach, + capabilities = M.capabilities, + + settings = {}, +} + +return M