This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 651
/
goSymbol.ts
134 lines (119 loc) · 4.21 KB
/
goSymbol.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
/*---------------------------------------------------------
* 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 vscode = require('vscode');
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getBinPath, getGoConfig, getToolsEnvVars, getWorkspaceFolderPath, killTree } from './util';
// Keep in sync with github.com/acroca/go-symbols'
interface GoSymbolDeclaration {
name: string;
kind: string;
package: string;
path: string;
line: number;
character: number;
}
export class GoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
private goKindToCodeKind: { [key: string]: vscode.SymbolKind } = {
package: vscode.SymbolKind.Package,
import: vscode.SymbolKind.Namespace,
var: vscode.SymbolKind.Variable,
type: vscode.SymbolKind.Interface,
func: vscode.SymbolKind.Function,
const: vscode.SymbolKind.Constant
};
public provideWorkspaceSymbols(
query: string,
token: vscode.CancellationToken
): Thenable<vscode.SymbolInformation[]> {
const convertToCodeSymbols = (decls: GoSymbolDeclaration[], symbols: vscode.SymbolInformation[]): void => {
if (!decls) {
return;
}
for (const decl of decls) {
let kind: vscode.SymbolKind;
if (decl.kind !== '') {
kind = this.goKindToCodeKind[decl.kind];
}
const pos = new vscode.Position(decl.line, decl.character);
const symbolInfo = new vscode.SymbolInformation(
decl.name,
kind,
new vscode.Range(pos, pos),
vscode.Uri.file(decl.path),
''
);
symbols.push(symbolInfo);
}
};
const root = getWorkspaceFolderPath(
vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri
);
const goConfig = getGoConfig();
if (!root && !goConfig.gotoSymbol.includeGoroot) {
vscode.window.showInformationMessage('No workspace is open to find symbols.');
return;
}
return getWorkspaceSymbols(root, query, token, goConfig).then((results) => {
const symbols: vscode.SymbolInformation[] = [];
convertToCodeSymbols(results, symbols);
return symbols;
});
}
}
export function getWorkspaceSymbols(
workspacePath: string,
query: string,
token: vscode.CancellationToken,
goConfig?: vscode.WorkspaceConfiguration,
ignoreFolderFeatureOn: boolean = true
): Thenable<GoSymbolDeclaration[]> {
if (!goConfig) {
goConfig = getGoConfig();
}
const gotoSymbolConfig = goConfig['gotoSymbol'];
const calls: Promise<GoSymbolDeclaration[]>[] = [];
const ignoreFolders: string[] = gotoSymbolConfig ? gotoSymbolConfig['ignoreFolders'] : [];
const baseArgs =
ignoreFolderFeatureOn && ignoreFolders && ignoreFolders.length > 0 ? ['-ignore', ignoreFolders.join(',')] : [];
calls.push(callGoSymbols([...baseArgs, workspacePath, query], token));
if (gotoSymbolConfig.includeGoroot) {
const goRoot = process.env['GOROOT'];
const gorootCall = callGoSymbols([...baseArgs, goRoot, query], token);
calls.push(gorootCall);
}
return Promise.all(calls)
.then(([...results]) => <GoSymbolDeclaration[]>[].concat(...results))
.catch((err: Error) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-symbols');
}
if (err.message.startsWith('flag provided but not defined: -ignore')) {
promptForUpdatingTool('go-symbols');
return getWorkspaceSymbols(workspacePath, query, token, goConfig, false);
}
});
}
function callGoSymbols(args: string[], token: vscode.CancellationToken): Promise<GoSymbolDeclaration[]> {
const gosyms = getBinPath('go-symbols');
const env = getToolsEnvVars();
let p: cp.ChildProcess;
if (token) {
token.onCancellationRequested(() => killTree(p.pid));
}
return new Promise((resolve, reject) => {
p = cp.execFile(gosyms, args, { maxBuffer: 1024 * 1024, env }, (err, stdout, stderr) => {
if (err && stderr && stderr.startsWith('flag provided but not defined: -ignore')) {
return reject(new Error(stderr));
} else if (err) {
return reject(err);
}
const result = stdout.toString();
const decls = <GoSymbolDeclaration[]>JSON.parse(result);
return resolve(decls);
});
});
}