This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
go-to-source-definition.ts
65 lines (56 loc) · 1.57 KB
/
go-to-source-definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { executeCommand } from "@ts/execute-command";
import { Methods } from "@ts/types/methods";
import { WorkspaceCommands } from "@ts/types/workspace-commands";
import { getClient, resolveHandler } from "@ts/utils";
import { Location } from "vscode-languageserver-types";
interface Opts {
fallback?: boolean;
}
export const goToSourceDefinition = (
winnr: number,
{ fallback }: Opts
): boolean => {
const bufnr = vim.api.nvim_win_get_buf(winnr);
const client = getClient(bufnr);
if (!client) {
return false;
}
const positionParams = vim.lsp.util.make_position_params(
winnr,
client.offset_encoding
);
const requestOk = executeCommand<Location>(
bufnr,
{
command: WorkspaceCommands.GO_TO_SOURCE_DEFINITION,
arguments: [positionParams.textDocument.uri, positionParams.position],
},
(...args) => {
const handler = resolveHandler(bufnr, Methods.DEFINITION);
if (!handler) {
print(
"failed to go to source definition: could not resolve definition handler"
);
return;
}
const res = args[1] || [];
if (vim.tbl_isempty(res)) {
if (fallback === true) {
return client.request(
Methods.DEFINITION,
positionParams,
handler,
bufnr
);
}
print("failed to go to source definition: no source definitions found");
return;
}
handler(...args);
}
);
if (!requestOk) {
print("failed to go to source definition: tsserver request failed");
}
return requestOk;
};