forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goOutline.ts
210 lines (189 loc) · 6.39 KB
/
goOutline.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* 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 vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getBinPath, getFileArchive, makeMemoizedByteOffsetConverter } from './util';
import { killProcess } from './utils/processUtils';
// Keep in sync with https://github.com/ramya-rao-a/go-outline
export interface GoOutlineRange {
start: number;
end: number;
}
export interface GoOutlineDeclaration {
label: string;
type: string;
receiverType?: string;
icon?: string; // icon class or null to use the default images based on the type
start: number;
end: number;
children?: GoOutlineDeclaration[];
signature?: GoOutlineRange;
comment?: GoOutlineRange;
}
export enum GoOutlineImportsOptions {
Include,
Exclude,
Only
}
export interface GoOutlineOptions {
/**
* Path of the file for which outline is needed
*/
fileName: string;
/**
* Option to decide if the output includes, excludes or only includes imports
* If the option is to only include imports, then the file will be parsed only till imports are collected
*/
importsOption: GoOutlineImportsOptions;
/**
* Document to be parsed. If not provided, saved contents of the given fileName is used
*/
document?: vscode.TextDocument;
}
export async function documentSymbols(
options: GoOutlineOptions,
token: vscode.CancellationToken
): Promise<vscode.DocumentSymbol[]> {
const decls = await runGoOutline(options, token);
return convertToCodeSymbols(
options.document,
decls,
options.importsOption !== GoOutlineImportsOptions.Exclude,
makeMemoizedByteOffsetConverter(Buffer.from(options.document.getText()))
);
}
export function runGoOutline(
options: GoOutlineOptions,
token: vscode.CancellationToken
): Promise<GoOutlineDeclaration[]> {
return new Promise<GoOutlineDeclaration[]>((resolve, reject) => {
const gooutline = getBinPath('go-outline');
const gooutlineFlags = ['-f', options.fileName];
if (options.importsOption === GoOutlineImportsOptions.Only) {
gooutlineFlags.push('-imports-only');
}
if (options.document) {
gooutlineFlags.push('-modified');
}
let p: cp.ChildProcess;
if (token) {
token.onCancellationRequested(() => killProcess(p));
}
// Spawn `go-outline` process
p = cp.execFile(gooutline, gooutlineFlags, { env: toolExecutionEnvironment() }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('go-outline');
}
if (stderr && stderr.startsWith('flag provided but not defined: ')) {
promptForUpdatingTool('go-outline');
if (stderr.startsWith('flag provided but not defined: -imports-only')) {
options.importsOption = GoOutlineImportsOptions.Include;
}
if (stderr.startsWith('flag provided but not defined: -modified')) {
options.document = null;
}
p = null;
return runGoOutline(options, token).then((results) => {
return resolve(results);
});
}
if (err) {
return resolve(null);
}
const result = stdout.toString();
const decls = <GoOutlineDeclaration[]>JSON.parse(result);
return resolve(decls);
} catch (e) {
reject(e);
}
});
if (options.document && p.pid) {
p.stdin.end(getFileArchive(options.document));
}
});
}
const goKindToCodeKind: { [key: string]: vscode.SymbolKind } = {
package: vscode.SymbolKind.Package,
import: vscode.SymbolKind.Namespace,
variable: vscode.SymbolKind.Variable,
constant: vscode.SymbolKind.Constant,
type: vscode.SymbolKind.TypeParameter,
function: vscode.SymbolKind.Function,
struct: vscode.SymbolKind.Struct,
interface: vscode.SymbolKind.Interface
};
function convertToCodeSymbols(
document: vscode.TextDocument,
decls: GoOutlineDeclaration[],
includeImports: boolean,
byteOffsetToDocumentOffset: (byteOffset: number) => number
): vscode.DocumentSymbol[] {
const symbols: vscode.DocumentSymbol[] = [];
(decls || []).forEach((decl) => {
if (!includeImports && decl.type === 'import') {
return;
}
if (decl.label === '_' && decl.type === 'variable') {
return;
}
const label = decl.receiverType ? `(${decl.receiverType}).${decl.label}` : decl.label;
const start = byteOffsetToDocumentOffset(decl.start - 1);
const end = byteOffsetToDocumentOffset(decl.end - 1);
const startPosition = document.positionAt(start);
const endPosition = document.positionAt(end);
const symbolRange = new vscode.Range(startPosition, endPosition);
const selectionRange =
startPosition.line === endPosition.line
? symbolRange
: new vscode.Range(startPosition, document.lineAt(startPosition.line).range.end);
if (decl.type === 'type') {
const line = document.lineAt(document.positionAt(start));
const regexStruct = new RegExp(`^\\s*type\\s+${decl.label}\\s+struct\\b`);
const regexInterface = new RegExp(`^\\s*type\\s+${decl.label}\\s+interface\\b`);
decl.type = regexStruct.test(line.text) ? 'struct' : regexInterface.test(line.text) ? 'interface' : 'type';
}
const symbolInfo = new vscode.DocumentSymbol(
label,
decl.type,
goKindToCodeKind[decl.type],
symbolRange,
selectionRange
);
symbols.push(symbolInfo);
if (decl.children) {
symbolInfo.children = convertToCodeSymbols(
document,
decl.children,
includeImports,
byteOffsetToDocumentOffset
);
}
});
return symbols;
}
export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
constructor(private includeImports?: boolean) {}
public provideDocumentSymbols(
document: vscode.TextDocument,
token: vscode.CancellationToken
): Thenable<vscode.DocumentSymbol[]> {
if (typeof this.includeImports !== 'boolean') {
const gotoSymbolConfig = getGoConfig(document.uri)['gotoSymbol'];
this.includeImports = gotoSymbolConfig ? gotoSymbolConfig['includeImports'] : false;
}
const options: GoOutlineOptions = {
fileName: document.fileName,
document,
importsOption: this.includeImports ? GoOutlineImportsOptions.Include : GoOutlineImportsOptions.Exclude
};
return documentSymbols(options, token);
}
}