|
9 | 9 | --- opam install ocaml-lsp-server |
10 | 10 | --- ``` |
11 | 11 |
|
| 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 | + |
12 | 47 | local language_id_of = { |
13 | 48 | menhir = 'ocaml.menhir', |
14 | 49 | ocaml = 'ocaml', |
@@ -45,4 +80,9 @@ return { |
45 | 80 | root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers1, root_markers2, root_markers3 } |
46 | 81 | or vim.list_extend(vim.list_extend(root_markers1, root_markers2), root_markers3), |
47 | 82 | 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, |
48 | 88 | } |
0 commit comments