@@ -14,19 +14,39 @@ return {
14
14
local mason_lspconfig = require (" mason-lspconfig" )
15
15
-- language server protocol configration --
16
16
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
30
50
-- used to enable autocompletion (assign to every lsp server config)
31
51
local capabilities = cmp_nvim_lsp .default_capabilities ()
32
52
-- vim diagnostic settings
@@ -51,6 +71,7 @@ return {
51
71
function (server_name )
52
72
lspconfig [server_name ].setup ({
53
73
capabilities = capabilities ,
74
+ on_attach = on_attach ,
54
75
})
55
76
end ,
56
77
})
0 commit comments