Skip to content

Commit e2c4d7d

Browse files
Fixing issue where the top level source file item was not being selected.
1 parent e1bb169 commit e2c4d7d

5 files changed

+26
-12
lines changed

src/services/navigationBar.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,25 @@ module ts.NavigationBar {
117117
}
118118

119119
function isTopLevelFunctionDeclaration(functionDeclaration: FunctionDeclaration) {
120-
// A function declaration is 'top level' if it contains any function declarations
121-
// within it.
122-
return functionDeclaration.kind === SyntaxKind.FunctionDeclaration &&
123-
functionDeclaration.body &&
124-
functionDeclaration.body.kind === SyntaxKind.FunctionBlock &&
125-
forEach((<Block>functionDeclaration.body).statements,
126-
s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((<FunctionDeclaration>s).name.text));
120+
if (functionDeclaration.kind === SyntaxKind.FunctionDeclaration) {
121+
// A function declaration is 'top level' if it contains any function declarations
122+
// within it.
123+
if (functionDeclaration.body && functionDeclaration.body.kind === SyntaxKind.FunctionBlock) {
124+
if (forEach((<Block>functionDeclaration.body).statements,
125+
s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((<FunctionDeclaration>s).name.text))) {
126+
127+
return true;
128+
}
129+
130+
// Or if it is not parented by another function. i.e all functions
131+
// at module scope are 'top level'.
132+
if (functionDeclaration.parent.kind !== SyntaxKind.FunctionBlock) {
133+
return true;
134+
}
135+
}
136+
}
137+
138+
return false;
127139
}
128140

129141
function getItemsWorker(nodes: Node[], createItem: (n: Node) => ts.NavigationBarItem): ts.NavigationBarItem[] {
@@ -402,7 +414,9 @@ module ts.NavigationBar {
402414
}
403415

404416
function getNodeSpan(node: Node) {
405-
return TypeScript.TextSpan.fromBounds(node.getStart(), node.getEnd());
417+
return node.kind === SyntaxKind.SourceFile
418+
? TypeScript.TextSpan.fromBounds(node.getFullStart(), node.getEnd())
419+
: TypeScript.TextSpan.fromBounds(node.getStart(), node.getEnd());
406420
}
407421

408422
function getTextOfNode(node: Node): string {

tests/cases/fourslash/scriptLexicalStructureFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ test.markers().forEach((marker) => {
2020
verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName);
2121
});
2222

23-
verify.getScriptLexicalStructureListCount(7); // 4 functions + global. Note: there are 7 because of the functions show up at the top level and as child items.
23+
verify.getScriptLexicalStructureListCount(8); // 4 functions + global. Note: there are 8 because of the functions show up at the top level and as child items.

tests/cases/fourslash/scriptLexicalStructureFunctionsBroken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ test.markers().forEach((marker) => {
99
verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName);
1010
});
1111

12-
verify.getScriptLexicalStructureListCount(2); // <global> and 'f'.
12+
verify.getScriptLexicalStructureListCount(3); // <global> and 'f'.

tests/cases/fourslash/scriptLexicalStructureFunctionsBroken2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ test.markers().forEach((marker) => {
1010
verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName);
1111
});
1212

13-
verify.getScriptLexicalStructureListCount(2); // <global> and 'f'
13+
verify.getScriptLexicalStructureListCount(3); // <global> and 'f'

tests/cases/fourslash/scriptLexicalStructureItemsContainsNoAnonymousFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ goTo.marker("file3");
4141
verify.getScriptLexicalStructureListContains("<global>", "module");
4242
verify.getScriptLexicalStructureListContains("foo", "function");
4343
verify.getScriptLexicalStructureListContains("bar", "function");
44-
verify.getScriptLexicalStructureListCount(3);
44+
verify.getScriptLexicalStructureListCount(5);

0 commit comments

Comments
 (0)