Skip to content

Commit 4748c08

Browse files
committed
finder: add references_opts option
1 parent 8efe00d commit 4748c08

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

lua/lspsaga/finder/init.lua

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,25 @@ function fd:new(args)
489489
self.inexist = config.finder.sp_inexist
490490
end
491491
self.layout = layout or config.finder.layout
492+
493+
local curbuf = api.nvim_get_current_buf()
494+
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = curbuf }))
495+
params.context = {
496+
includeDeclaration = true,
497+
}
498+
if type(config.finder.references_opts) == 'boolean' then
499+
params.context = {
500+
includeDeclaration = config.finder.references_opts,
501+
}
502+
end
503+
492504
if #meth == 0 then
493505
meth = vim.split(config.finder.default, '+', { plain = true })
494506
end
495507
local methods = box.get_methods(meth)
496-
497508
methods = vim.tbl_filter(function(method)
498509
return #util.get_client_by_method(method) > 0
499510
end, methods)
500-
local curbuf = api.nvim_get_current_buf()
501-
self.ft = vim.bo[curbuf].filetype
502511
if #methods == 0 then
503512
vim.notify(
504513
('[lspsaga] no servers of buffer %s makes these methods available %s'):format(
@@ -510,23 +519,24 @@ function fd:new(args)
510519
return
511520
end
512521

522+
local methods_with_params = vim.tbl_map(function(method)
523+
return util.gen_param_by_config(method, params, config.finder.references_opts)
524+
end, methods)
525+
526+
self.ft = vim.bo[curbuf].filetype
513527
self.list = slist.new()
514-
local params = lsp.util.make_position_params(0, util.get_offset_encoding({ bufnr = curbuf }))
515-
params.context = {
516-
includeDeclaration = true,
517-
}
518528

519529
local spin_close = box.spinner()
520530
local count = 0
521531
coroutine.resume(coroutine.create(function()
522532
local retval = {}
523533
local co = coroutine.running()
524-
for _, method in ipairs(methods) do
525-
lsp.buf_request_all(curbuf, method, params, function(results)
534+
for _, item in ipairs(methods_with_params) do
535+
lsp.buf_request_all(curbuf, item[1], item[2], function(results)
526536
count = count + 1
527-
results = box.filter(method, results)
537+
results = box.filter(item[1], results)
528538
if results and not util.res_isempty(results) then
529-
retval[method] = results
539+
retval[item[1]] = results
530540
end
531541
if count == #methods then
532542
coroutine.resume(co)

lua/lspsaga/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ local default_config = {
8686
ly_botright = false,
8787
number = vim.o.number,
8888
relativenumber = vim.o.relativenumber,
89+
references_opts = true,
8990
keys = {
9091
shuttle = '[w',
9192
toggle_or_open = 'o',

lua/lspsaga/util.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,35 @@ function M.get_client_by_method(method)
7373
return supports
7474
end
7575

76+
--- generate LSP RPC method `textDocument/references` parameters by configuration.
77+
---@param method string LSP RPC method name
78+
---@param params table parameters for LSP RPC method
79+
---@param config table|boolean configuration for config.finder.references_opts
80+
---@return table
81+
function M.gen_param_by_config(method, params, config)
82+
if type(config) == 'table' and method == 'textDocument/references' then
83+
local bufnr = vim.api.nvim_get_current_buf()
84+
local clients = lsp.get_clients({ bufnr = bufnr })
85+
86+
for _, client in ipairs(clients or {}) do
87+
-- Replace characters dash('-') to underscore('_') for LSP client name,
88+
-- which is useful for some LSP client like `rust-analyzer`.
89+
local client_name = string.gsub(client.name, '-', '_')
90+
local v = vim.tbl_get(config, client_name)
91+
if v ~= nil then
92+
params.context = {
93+
includeDeclaration = v,
94+
}
95+
break
96+
end
97+
end
98+
99+
return { method, params }
100+
end
101+
102+
return { method, params }
103+
end
104+
76105
function M.feedkeys(key)
77106
local k = api.nvim_replace_termcodes(key, true, false, true)
78107
api.nvim_feedkeys(k, 'nx', false)

0 commit comments

Comments
 (0)