@@ -20,7 +20,45 @@ vim.api.nvim_create_autocmd("LspAttach", {
2020 vim .keymap .set (mode , l , r , opts )
2121 end
2222
23- map (" n" , " gd" , vim .lsp .buf .definition , { desc = " go to definition" })
23+ map (" n" , " gd" , function ()
24+ vim .lsp .buf .definition {
25+ on_list = function (options )
26+ -- custom logic to avoid showing multiple definition when you use this style of code:
27+ -- `local M.my_fn_name = function() ... end`.
28+ -- See also post here: https://www.reddit.com/r/neovim/comments/19cvgtp/any_way_to_remove_redundant_definition_in_lua_file/
29+
30+ -- vim.print(options.items)
31+ local unique_defs = {}
32+ local def_loc_hash = {}
33+
34+ -- each item in options.items contain the location info for a definition provided by LSP server
35+ for _ , def_location in pairs (options .items ) do
36+ -- use filename and line number to uniquelly indentify a definition,
37+ -- we do not expect/want multiple definition in single line!
38+ local hash_key = def_location .filename .. def_location .lnum
39+
40+ if not def_loc_hash [hash_key ] then
41+ def_loc_hash [hash_key ] = true
42+ table.insert (unique_defs , def_location )
43+ end
44+ end
45+
46+ options .items = unique_defs
47+
48+ -- set the location list
49+ --- @diagnostic disable-next-line : param-type-mismatch
50+ vim .fn .setloclist (0 , {}, " " , options )
51+
52+ -- open the location list when we have more than 1 definitions found,
53+ -- otherwise, jump directly to the definition
54+ if # options .items > 1 then
55+ vim .cmd .lopen ()
56+ else
57+ vim .cmd ([[ silent! lfirst]] )
58+ end
59+ end ,
60+ }
61+ end , { desc = " go to definition" })
2462 map (" n" , " <C-]>" , vim .lsp .buf .definition )
2563 map (" n" , " K" , function ()
2664 vim .lsp .buf .hover { border = " single" , max_height = 25 , max_width = 120 }
@@ -88,6 +126,7 @@ local enabled_lsp_servers = {
88126 -- clangd = "clangd",
89127 vimls = " vim-language-server" ,
90128 bashls = " bash-language-server" ,
129+ yamlls = " yaml-language-server" ,
91130}
92131
93132for server_name , lsp_executable in pairs (enabled_lsp_servers ) do
0 commit comments