Skip to content

Commit ce1d863

Browse files
committed
document_highlight() features has been added.
- when user hold cursor on a document for two second then it highlights its references.
1 parent 20c2ce3 commit ce1d863

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

lua/plugins/lsp_config.lua

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,39 @@ return {
1414
local mason_lspconfig = require("mason-lspconfig")
1515
-- language server protocol configration --
1616
local keymap = vim.keymap -- for conciseness
17-
-- lsp keybinds
18-
vim.api.nvim_create_autocmd("LspAttach", {
19-
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
20-
callback = function(ev)
21-
-- see :help vim.lsp.*
22-
local opts = { buffer = ev.buf, silent = true }
23-
-- see :help vim.lsp.buf
24-
opts.desc = "Show LSP definitions"
25-
keymap.set("n", "<CR>", vim.lsp.buf.definition, opts)
26-
opts.desc = "Show documentation for what is under cursor"
27-
keymap.set("n", "<CR>", vim.lsp.buf.hover, opts)
28-
end,
29-
})
17+
local opts = { noremap = true, silent = true } -- keymaps opts
18+
local on_attach = function(client, bufnr)
19+
-- see :help vim.lsp.buf
20+
opts.desc = "Show LSP definitions"
21+
keymap.set("n", "<CR>", vim.lsp.buf.definition, opts)
22+
opts.desc = "Show documentation for what is under cursor"
23+
keymap.set("n", "<CR>", vim.lsp.buf.hover, opts)
24+
-- Enable document highlights if the server supports it
25+
if client.server_capabilities.documentHighlightProvider then
26+
local group = vim.api.nvim_create_augroup("LSPDocumentHighlight", {})
27+
vim.opt.updatetime = 2000 -- cursor hold delay time
28+
-- we can link highlights to an existing group.
29+
vim.api.nvim_set_hl(0, 'LspReferenceRead', {link = 'Search'})
30+
vim.api.nvim_set_hl(0, 'LspReferenceText', {link = 'Search'})
31+
vim.api.nvim_set_hl(0, 'LspReferenceWrite', {link = 'Search'})
32+
-- this will highlight text when cursor hold.
33+
vim.api.nvim_create_autocmd("CursorHold", {
34+
group = group,
35+
buffer = bufnr,
36+
callback = function()
37+
vim.lsp.buf.document_highlight()
38+
end,
39+
})
40+
-- this will clear highlight when cursor moved.
41+
vim.api.nvim_create_autocmd("CursorMoved", {
42+
group = group,
43+
buffer = bufnr,
44+
callback = function()
45+
vim.lsp.buf.clear_references()
46+
end,
47+
})
48+
end
49+
end
3050
-- used to enable autocompletion (assign to every lsp server config)
3151
local capabilities = cmp_nvim_lsp.default_capabilities()
3252
-- vim diagnostic settings
@@ -51,6 +71,7 @@ return {
5171
function(server_name)
5272
lspconfig[server_name].setup({
5373
capabilities = capabilities,
74+
on_attach = on_attach,
5475
})
5576
end,
5677
})

0 commit comments

Comments
 (0)