Skip to content

Commit

Permalink
feat(lsp): added support for showing locations from lsp execute commands
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 5, 2024
1 parent c147a75 commit b1d16ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ for _, mode in ipairs({ "incoming_calls", "outgoing_calls" }) do
}
end

for _, mode in ipairs({ "definitions", "references", "implementations", "type_definitions", "declarations" }) do
for _, mode in ipairs({ "definitions", "references", "implementations", "type_definitions", "declarations", "command" }) do
M.config.modes["lsp_" .. mode] = {
auto_jump = true,
mode = "lsp_base",
Expand All @@ -117,8 +117,6 @@ function M.request(method, params, opts)
---@type vim.lsp.Client[]
local clients = {}

-- Util.debug("LSP Request " .. method, params)

if opts.client then
clients = { opts.client }
else
Expand Down Expand Up @@ -151,8 +149,8 @@ end

---@param method string
---@param cb trouble.Source.Callback
---@param context? any lsp params context
function M.get_locations(method, cb, context)
---@param opts? {context?:any, params?:table<string,any>}
function M.get_locations(method, cb, opts)
local win = vim.api.nvim_get_current_win()
local buf = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(win)
Expand All @@ -163,13 +161,13 @@ function M.get_locations(method, cb, context)
col = col - 1
end

local id = table.concat({ buf, cursor[1], col, method, vim.inspect(context) }, "-")

opts = opts or {}
---@type lsp.TextDocumentPositionParams
local params = vim.lsp.util.make_position_params(win)
local params = opts.params or vim.lsp.util.make_position_params(win)
---@diagnostic disable-next-line: inject-field
params.context = context
params.context = params.context or opts.context or nil

local id = table.concat({ buf, cursor[1], col, method, vim.inspect(params) }, "-")
if Cache.locations[id] then
return cb(Cache.locations[id])
end
Expand Down Expand Up @@ -456,9 +454,25 @@ function M.locations_to_ranges(client, locs)
return ret
end

---@param cb trouble.Source.Callback
---@param ctx trouble.Source.ctx
function M.get.command(cb, ctx)
local err = "Missing command params for `lsp_command`.\n"
.. "You need to specify `opts.params = {command = 'the_command', arguments = {}}`"
if not ctx.opts.params then
return Util.error(err)
end
---@type lsp.ExecuteCommandParams
local params = ctx.opts.params
if not params.command then
return Util.error(err)
end
M.get_locations("workspace/executeCommand", cb, { params = params })
end

---@param cb trouble.Source.Callback
function M.get.references(cb)
M.get_locations("textDocument/references", cb, { includeDeclaration = true })
M.get_locations("textDocument/references", cb, { context = { includeDeclaration = true } })
end

---@param cb trouble.Source.Callback
Expand Down
3 changes: 3 additions & 0 deletions lua/trouble/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local Util = require("trouble.util")
---@field flatten? boolean when true, items with a natural hierarchy will be flattened
---@field format? string
---@field max_items? number
---@field params? table<string, any>

---@alias trouble.Filter table<string, any>|fun(items: trouble.Item[]): trouble.Item[]

Expand Down Expand Up @@ -47,6 +48,7 @@ local Util = require("trouble.util")
---@field sort? trouble.Sort[]
---@field filter? trouble.Filter
---@field max_items? number
---@field params? table<string, any>

local M = {}

Expand Down Expand Up @@ -76,6 +78,7 @@ function M.section(spec)
format = spec.format or "{filename} {pos}",
events = events,
flatten = spec.flatten,
params = spec.params,
}
-- A title is just a group without fields
if spec.title then
Expand Down

0 comments on commit b1d16ac

Please sign in to comment.