-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
36dcef7
commit 1f7b569
Showing
2 changed files
with
109 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
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, | ||
}) |
This file contains 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