Skip to content

Commit 2222461

Browse files
committed
src/goOutline.ts: use regexp to find package decl
In order to get the correct location of the package declaration we can use a regexp as a close heuristic. Updates #1020 Change-Id: I12a6fe296cf72ec5a331d7e2b70a5050d0882e63 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/386218 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 7bc14ec commit 2222461

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/goOutline.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,28 @@ export class GoDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
218218

219219
// Stitch the results together to make the results look like
220220
// go-outline.
221-
// TODO(suzmue): use regexp to find the package declaration.
221+
let pkgDeclRng = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
222+
let pkgName = '';
223+
224+
// Try to find the package statement.
225+
const text = document.getText();
226+
const packageStatement = new RegExp('^[ \\t]*package[ \\t]*(\\S+)', 'm');
227+
const match = packageStatement.exec(text);
228+
if (match && match.length === 2) {
229+
const packageDecl = match[0];
230+
const start = text.indexOf(packageDecl);
231+
pkgDeclRng = new vscode.Range(
232+
document.positionAt(start),
233+
document.positionAt(start + packageDecl.length)
234+
);
235+
pkgName = packageDecl[1];
236+
}
222237
const packageSymbol = new vscode.DocumentSymbol(
223-
'unknown',
238+
pkgName,
224239
'package',
225240
vscode.SymbolKind.Package,
226-
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)),
227-
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0))
241+
pkgDeclRng,
242+
pkgDeclRng
228243
);
229244
packageSymbol.children = symbols;
230245
if (this.includeImports) {

0 commit comments

Comments
 (0)