Skip to content

Commit b9fef87

Browse files
authored
feat(ocamllsp): :LspOcamllspSwitchImplIntf command #4117
1 parent 3b6c90d commit b9fef87

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

lsp/ocamllsp.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,41 @@
99
--- opam install ocaml-lsp-server
1010
--- ```
1111

12+
-- https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/docs/ocamllsp/switchImplIntf-spec.md
13+
local function switch_impl_intf(bufnr, client)
14+
local method_name = 'ocamllsp/switchImplIntf'
15+
---@diagnostic disable-next-line:param-type-mismatch
16+
if not client or not client:supports_method(method_name) then
17+
return vim.notify(('method %s is not supported by any servers active on the current buffer'):format(method_name))
18+
end
19+
local uri = vim.lsp.util.make_given_range_params(nil, nil, bufnr, client.offset_encoding).textDocument.uri
20+
if not uri then
21+
return vim.notify('could not get URI for current buffer')
22+
end
23+
local params = { uri }
24+
---@diagnostic disable-next-line:param-type-mismatch
25+
client:request(method_name, params, function(err, result)
26+
if err then
27+
error(tostring(err))
28+
end
29+
if not result or #result == 0 then
30+
vim.notify('corresponding file cannot be determined')
31+
elseif #result == 1 then
32+
vim.cmd.edit(vim.uri_to_fname(result[1]))
33+
else
34+
vim.ui.select(
35+
result,
36+
{ prompt = 'Select an implementation/interface:', format_item = vim.uri_to_fname },
37+
function(choice)
38+
if choice then
39+
vim.cmd.edit(vim.uri_to_fname(choice))
40+
end
41+
end
42+
)
43+
end
44+
end, bufnr)
45+
end
46+
1247
local language_id_of = {
1348
menhir = 'ocaml.menhir',
1449
ocaml = 'ocaml',
@@ -45,4 +80,9 @@ return {
4580
root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers1, root_markers2, root_markers3 }
4681
or vim.list_extend(vim.list_extend(root_markers1, root_markers2), root_markers3),
4782
get_language_id = get_language_id,
83+
on_attach = function(client, bufnr)
84+
vim.api.nvim_buf_create_user_command(bufnr, 'LspOcamllspSwitchImplIntf', function()
85+
switch_impl_intf(bufnr, client)
86+
end, { desc = 'Switch between implementation/interface' })
87+
end,
4888
}

0 commit comments

Comments
 (0)