forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goImplementations.ts
135 lines (122 loc) · 4.45 KB
/
goImplementations.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable @typescript-eslint/no-explicit-any */
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import cp = require('child_process');
import path = require('path');
import vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { byteOffsetAt, canonicalizeGOPATHPrefix, getBinPath, getWorkspaceFolderPath } from './util';
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';
interface GoListOutput {
Dir: string;
ImportPath: string;
Root: string;
}
interface GuruImplementsRef {
name: string;
pos: string;
kind: string;
}
interface GuruImplementsOutput {
type: GuruImplementsRef;
to: GuruImplementsRef[];
to_method: GuruImplementsRef[];
from: GuruImplementsRef[];
fromptr: GuruImplementsRef[];
}
export class GoImplementationProvider implements vscode.ImplementationProvider {
public provideImplementation(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): Thenable<vscode.Definition> {
// To keep `guru implements` fast we want to restrict the scope of the search to current workspace
// If no workspace is open, then no-op
const root = getWorkspaceFolderPath(document.uri);
if (!root) {
vscode.window.showInformationMessage('Cannot find implementations when there is no workspace open.');
return;
}
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(
`Failed to run "go list" to get the scope to find implementations as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
);
return;
}
return new Promise<vscode.Definition>((resolve, reject) => {
if (token.isCancellationRequested) {
return resolve(null);
}
const env = toolExecutionEnvironment();
const listProcess = cp.execFile(
goRuntimePath,
['list', '-e', '-json'],
{ cwd: root, env },
(err, stdout) => {
if (err) {
return reject(err);
}
const listOutput = <GoListOutput>JSON.parse(stdout.toString());
const filename = canonicalizeGOPATHPrefix(document.fileName);
const cwd = path.dirname(filename);
const offset = byteOffsetAt(document, position);
const goGuru = getBinPath('guru');
const buildTags = getGoConfig(document.uri)['buildTags'];
const args = buildTags ? ['-tags', buildTags] : [];
if (listOutput.Root && listOutput.ImportPath) {
args.push('-scope', `${listOutput.ImportPath}/...`);
}
args.push('-json', 'implements', `${filename}:#${offset.toString()}`);
const guruProcess = cp.execFile(goGuru, args, { env }, (guruErr, guruStdOut) => {
if (guruErr && (<any>guruErr).code === 'ENOENT') {
promptForMissingTool('guru');
return resolve(null);
}
if (guruErr) {
return reject(guruErr);
}
const guruOutput = <GuruImplementsOutput>JSON.parse(guruStdOut.toString());
const results: vscode.Location[] = [];
const addResults = (list: GuruImplementsRef[]) => {
list.forEach((ref: GuruImplementsRef) => {
const match = /^(.*):(\d+):(\d+)/.exec(ref.pos);
if (!match) {
return;
}
const [, file, lineStartStr, colStartStr] = match;
const referenceResource = vscode.Uri.file(path.resolve(cwd, file));
const range = new vscode.Range(
+lineStartStr - 1,
+colStartStr - 1,
+lineStartStr - 1,
+colStartStr
);
results.push(new vscode.Location(referenceResource, range));
});
};
// If we looked for implementation of method go to method implementations only
if (guruOutput.to_method) {
addResults(guruOutput.to_method);
} else if (guruOutput.to) {
addResults(guruOutput.to);
} else if (guruOutput.from) {
addResults(guruOutput.from);
} else if (guruOutput.fromptr) {
addResults(guruOutput.fromptr);
}
return resolve(results);
});
token.onCancellationRequested(() => killProcessTree(guruProcess));
}
);
token.onCancellationRequested(() => killProcessTree(listProcess));
});
}
}