Skip to content

Commit

Permalink
Overhaul of lsp mappings
Browse files Browse the repository at this point in the history
Set lsp mappings only if the buffer is attached to an lsp server

set lsp mappings based on the functionalities provided by the server

use <localleader> for lsp mappings

setup highlight groups for lsp document highlighting
add mappings to enable and disable lsp document highlighting

make buffer formatting async

add mapping for checking if an lsp is attacted to a buffer

move mappings in on_attach fn to lsp mapping

Simplify on_attach fn for lsp server setup

move to cmp's new api for initializing capabilities, .ie default_capabilities fn

remove unused option in make_config fn
  • Loading branch information
Ultra-Code committed Oct 19, 2022
1 parent 36dcef7 commit 1f7b569
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 39 deletions.
127 changes: 107 additions & 20 deletions lua/mapping/lspconfig.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,110 @@
require("utils")
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local autocmd = vim.api.nvim_create_autocmd
local augroup = vim.api.nvim_create_augroup
-- map the following keys after the language server attaches to a buffer
-- See `:help vim.lsp.*` for documentation on any of the below functions
autocmd('LspAttach', {
callback = function(args)
local opts = { buffer = args.buf }

-- Mappings.
local opts = { silent = true }
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.server_capabilities.hoverProvider then
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
end
if client.server_capabilities.signatureHelpProvider then
map("n", "<localleader>k", vim.lsp.buf.signature_help, opts)
end
if client.server_capabilities.declarationProvider then
map("n", "<localleader>gD", vim.lsp.buf.declaration, opts)
end
if client.server_capabilities.definitionProvider then
map("n", "<localleader>gd", vim.lsp.buf.definition, opts)
end
if client.server_capabilities.typeDefinitionProvider then
map("n", "<localleader>td", vim.lsp.buf.type_definition, opts)
end
if client.server_capabilities.implementationProvider then
map("n", "<localleader>gi", vim.lsp.buf.implementation, opts)
end
if client.server_capabilities.referencesProvider then
map("n", "<localleader>gr", vim.lsp.buf.references, opts)
end
if client.server_capabilities.documentHighlightProvider then
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_set_hl(0, "LspReferenceRead", {
link = "DiffText"
})
vim.api.nvim_set_hl(0, "LspReferenceText", {
link = "IncSearch"
})
vim.api.nvim_set_hl(0, "LspRefDiffTexterenceWrite", {
link = "WildMenu"
})
local doc_highlight = augroup('lsp_document_highlight', {
clear = false,
})
local enable_highlight = function()
autocmd({ 'CursorHold', 'CursorHoldI' }, {
group = doc_highlight,
buffer = bufnr,
callback = vim.lsp.buf.document_highlight,
})
autocmd('CursorMoved', {
group = doc_highlight,
buffer = bufnr,
callback = vim.lsp.buf.clear_references,
})

-- See `:help vim.lsp.*` for documentation on any of the below functions
map("n", "<space>gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
map("n", "<space>gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
map("n", "<space>gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
-- set_keymap("n", "<space>gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
map("n", "<space>gr", "<cmd>Trouble lsp_references<cr>", opts)
map("n", "<space>lw", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
map("n", "<space>td", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
map("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
map("n", "<space>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
map("n", "<space>sl", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
map("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting_seq_sync()<CR>", opts)
map("n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
map("n", "<space>sh", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
map("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
map("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
end
local disable_highlight = function()
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds({
buffer = bufnr,
group = doc_highlight
})
end
map("n", "<localleader>dh", disable_highlight, opts)
map("n", "<localleader>eh", enable_highlight, opts)
end
if client.server_capabilities.documentSymbolProvider then
map("n", "<localleader>lds", vim.lsp.buf.document_symbol, opts)
end
if client.server_capabilities.codeActionProvider then
map("n", "<localleader>ca", vim.lsp.buf.code_action, opts)
map("v", "<localleader>ca", vim.lsp.buf.range_code_action, opts)
end
if client.server_capabilities.documentFormattingProvider then
map("n", "<localleader>f", function()
vim.lsp.buf.format({ async = true })
end, opts)
end
if client.server_capabilities.documentRangeFormattingProvider then
map("v", "<localleader>f", function()
vim.lsp.buf.format({ async = true })
end, opts)
end
if client.server_capabilities.renameProvider then
map("n", "<localleader>rn", vim.lsp.buf.rename, opts)
end
if client.server_capabilities.callHierarchyProvider then
map("n", "<localleader>ci", vim.lsp.buf.incoming_calls, opts)
map("n", "<localleader>co", vim.lsp.buf.outgoing_calls, opts)
end
if client.server_capabilities.workspaceSymbolProvider then
map("n", "<localleader>lws", vim.lsp.buf.workspace_symbol, opts)
end

map("n", "[d", vim.diagnostic.goto_prev, opts)
map("n", "]d", vim.diagnostic.goto_next, opts)
map("n", "<localleader>sl", vim.diagnostic.setloclist, opts)
map('n', '<localleader>of', vim.diagnostic.open_float, opts)

map("n", "<localleader>lw", function()
vim.pretty_print(vim.lsp.buf.list_workspace_folders())
end, opts)
map("n", "<localleader>rd", function()
print("Language server " .. (vim.lsp.buf.server_ready() and "is ready" or "is not ready"))
end, opts)

end,
})
21 changes: 2 additions & 19 deletions lua/plugrc/lspconfig/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local on_attach = function(client, buffer)
_ = client;
vim.diagnostic.config({
virtual_text = false,
float = { scope = "line", severity_sort = true, source = "if_many" },
Expand All @@ -8,34 +9,18 @@ local on_attach = function(client, buffer)
severity_sort = true,
})

local function set_keymap(...)
vim.api.nvim_buf_set_keymap(buffer, ...)
end

local function set_option(...)
vim.api.nvim_buf_set_option(buffer, ...)
end

set_option("omnifunc", "v:lua.vim.lsp.omnifunc")

local opts = { noremap = true, silent = true }

-- Set some keybinds conditional on server capabilities
if client.server_capabilities.document_range_formatting then
set_keymap(
"v",
"<leader>lf",
"<cmd>lua vim.lsp.buf.range_formatting()<CR>",
opts
)
end
end

-- config that activates keymaps and enables snippet support
local function make_config()
local capabilities = vim.lsp.protocol.make_client_capabilities()
-- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers..
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)

capabilities.textDocument.completion.completionItem.snippetSupport = true

Expand All @@ -44,8 +29,6 @@ local function make_config()
capabilities = capabilities,
-- map buffer local keybindings when the language server attaches
on_attach = on_attach,

-- rootdir = vim.loop.cwd
}
end

Expand Down

0 comments on commit 1f7b569

Please sign in to comment.