Skip to content

Commit 87c8cd6

Browse files
committed
feat(lsp): add type heirarchy picker
1 parent 3333a52 commit 87c8cd6

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

lua/telescope/builtin/__lsp.lua

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,85 @@ local function calls(opts, direction)
117117
end)
118118
end
119119

120+
local function type_hierarchy(opts, method, title, item)
121+
lsp.buf_request(opts.bufnr, method, { item = item }, function(err, result)
122+
if err then
123+
utils.notify("lsp.type_hierarchy", { msg = title .. ": " .. err.message, level = "ERROR" })
124+
return
125+
end
126+
127+
if not result or vim.tbl_isempty(result) then
128+
return
129+
end
130+
131+
local locations = {}
132+
for _, unit in pairs(result) do
133+
local rng = unit.range
134+
table.insert(locations, {
135+
filename = vim.uri_to_fname(unit.uri),
136+
text = unit.name,
137+
lnum = rng.start.line + 1,
138+
col = rng.start.character + 1,
139+
})
140+
end
141+
142+
pickers
143+
.new(opts, {
144+
prompt_title = title,
145+
finder = finders.new_table {
146+
results = locations,
147+
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
148+
},
149+
previewer = conf.qflist_previewer(opts),
150+
sorter = conf.generic_sorter(opts),
151+
push_cursor_on_edit = true,
152+
push_tagstack_on_edit = true,
153+
})
154+
:find()
155+
end)
156+
end
157+
158+
local function pick_type_hierarchy_item(type_hierarchy_items)
159+
if not type_hierarchy_items or vim.tbl_isempty(type_hierarchy_items) then
160+
return
161+
end
162+
if #type_hierarchy_items == 1 then
163+
return type_hierarchy_items[1]
164+
end
165+
local items = {}
166+
for i, item in pairs(type_hierarchy_items) do
167+
local entry = item.detail or item.name
168+
table.insert(items, string.format("%d. %s", i, entry))
169+
end
170+
local choice = vim.fn.inputlist(items)
171+
if choice < 1 or choice > #items then
172+
return
173+
end
174+
return type_hierarchy_items[choice]
175+
end
176+
177+
---@param direction 'super'|'sub'
178+
local function types(opts, direction)
179+
local params = client_position_params()
180+
lsp.buf_request(opts.bufnr, "textDocument/prepareTypeHierarchy", params, function(err, result)
181+
if err then
182+
utils.notify("lsp.types", { msg = err.message, level = "ERROR" })
183+
return
184+
end
185+
186+
local type_hierarchy_item = pick_type_hierarchy_item(result)
187+
if not type_hierarchy_item then
188+
return
189+
end
190+
191+
if direction == "super" then
192+
type_hierarchy(opts, "typeHierarchy/supertypes", "LSP Super Types", type_hierarchy_item)
193+
else
194+
type_hierarchy(opts, "typeHierarchy/subtypes", "LSP Sub Types", type_hierarchy_item)
195+
end
196+
end)
197+
end
198+
120199
M.incoming_calls = function(opts)
121200
calls(opts, "from")
122201
end
@@ -125,6 +204,14 @@ M.outgoing_calls = function(opts)
125204
calls(opts, "to")
126205
end
127206

207+
M.super_types = function(opts)
208+
types(opts, "super")
209+
end
210+
211+
M.sub_types = function(opts)
212+
types(opts, "sub")
213+
end
214+
128215
---@alias telescope.lsp.list_or_jump_action
129216
---| "textDocument/references"
130217
---| "textDocument/definition"

lua/telescope/builtin/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,14 @@ builtin.lsp_incoming_calls = require_on_exported_call("telescope.builtin.__lsp")
444444
---@field file_encoding string: file encoding for the previewer
445445
builtin.lsp_outgoing_calls = require_on_exported_call("telescope.builtin.__lsp").outgoing_calls
446446

447+
--- Lists LSP Supertypes for symbol under the cursor, jumps to reference on `<cr>`
448+
---@param opts table: options to pass to the picker
449+
builtin.lsp_super_types = require_on_exported_call("telescope.builtin.__lsp").super_types
450+
451+
--- Lists LSP Subtypes for symbol under the cursor, jumps to reference on `<cr>`
452+
---@param opts table: options to pass to the picker
453+
builtin.lsp_sub_types = require_on_exported_call("telescope.builtin.__lsp").sub_types
454+
447455
--- Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope
448456
---@param opts table: options to pass to the picker
449457
---@field jump_type string: how to goto definition if there is only one and the definition file is different from the current file, values: "tab", "tab drop", "split", "vsplit", "never"

0 commit comments

Comments
 (0)