From e2c4d7d0d9a9f5f0ea3e5a8dca1797352d7df655 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 6 Oct 2014 14:22:08 -0700 Subject: [PATCH] Fixing issue where the top level source file item was not being selected. --- src/services/navigationBar.ts | 30 ++++++++++++++----- .../scriptLexicalStructureFunctions.ts | 2 +- .../scriptLexicalStructureFunctionsBroken.ts | 2 +- .../scriptLexicalStructureFunctionsBroken2.ts | 2 +- ...uctureItemsContainsNoAnonymousFunctions.ts | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 53eed356ceb..ef2b5e9334a 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -117,13 +117,25 @@ module ts.NavigationBar { } function isTopLevelFunctionDeclaration(functionDeclaration: FunctionDeclaration) { - // A function declaration is 'top level' if it contains any function declarations - // within it. - return functionDeclaration.kind === SyntaxKind.FunctionDeclaration && - functionDeclaration.body && - functionDeclaration.body.kind === SyntaxKind.FunctionBlock && - forEach((functionDeclaration.body).statements, - s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text)); + if (functionDeclaration.kind === SyntaxKind.FunctionDeclaration) { + // A function declaration is 'top level' if it contains any function declarations + // within it. + if (functionDeclaration.body && functionDeclaration.body.kind === SyntaxKind.FunctionBlock) { + if (forEach((functionDeclaration.body).statements, + s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text))) { + + return true; + } + + // Or if it is not parented by another function. i.e all functions + // at module scope are 'top level'. + if (functionDeclaration.parent.kind !== SyntaxKind.FunctionBlock) { + return true; + } + } + } + + return false; } function getItemsWorker(nodes: Node[], createItem: (n: Node) => ts.NavigationBarItem): ts.NavigationBarItem[] { @@ -402,7 +414,9 @@ module ts.NavigationBar { } function getNodeSpan(node: Node) { - return TypeScript.TextSpan.fromBounds(node.getStart(), node.getEnd()); + return node.kind === SyntaxKind.SourceFile + ? TypeScript.TextSpan.fromBounds(node.getFullStart(), node.getEnd()) + : TypeScript.TextSpan.fromBounds(node.getStart(), node.getEnd()); } function getTextOfNode(node: Node): string { diff --git a/tests/cases/fourslash/scriptLexicalStructureFunctions.ts b/tests/cases/fourslash/scriptLexicalStructureFunctions.ts index 298cfc657f0..b683790c60d 100644 --- a/tests/cases/fourslash/scriptLexicalStructureFunctions.ts +++ b/tests/cases/fourslash/scriptLexicalStructureFunctions.ts @@ -20,4 +20,4 @@ test.markers().forEach((marker) => { verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName); }); -verify.getScriptLexicalStructureListCount(7); // 4 functions + global. Note: there are 7 because of the functions show up at the top level and as child items. +verify.getScriptLexicalStructureListCount(8); // 4 functions + global. Note: there are 8 because of the functions show up at the top level and as child items. diff --git a/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken.ts b/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken.ts index f8d5f0bf27a..7846a9723d1 100644 --- a/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken.ts +++ b/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken.ts @@ -9,4 +9,4 @@ test.markers().forEach((marker) => { verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName); }); -verify.getScriptLexicalStructureListCount(2); // and 'f'. \ No newline at end of file +verify.getScriptLexicalStructureListCount(3); // and 'f'. \ No newline at end of file diff --git a/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken2.ts b/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken2.ts index cab6a055a5b..50bf91e02d6 100644 --- a/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken2.ts +++ b/tests/cases/fourslash/scriptLexicalStructureFunctionsBroken2.ts @@ -10,4 +10,4 @@ test.markers().forEach((marker) => { verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName); }); -verify.getScriptLexicalStructureListCount(2); // and 'f' \ No newline at end of file +verify.getScriptLexicalStructureListCount(3); // and 'f' \ No newline at end of file diff --git a/tests/cases/fourslash/scriptLexicalStructureItemsContainsNoAnonymousFunctions.ts b/tests/cases/fourslash/scriptLexicalStructureItemsContainsNoAnonymousFunctions.ts index 531575d6d10..a4086450176 100644 --- a/tests/cases/fourslash/scriptLexicalStructureItemsContainsNoAnonymousFunctions.ts +++ b/tests/cases/fourslash/scriptLexicalStructureItemsContainsNoAnonymousFunctions.ts @@ -41,4 +41,4 @@ goTo.marker("file3"); verify.getScriptLexicalStructureListContains("", "module"); verify.getScriptLexicalStructureListContains("foo", "function"); verify.getScriptLexicalStructureListContains("bar", "function"); -verify.getScriptLexicalStructureListCount(3); \ No newline at end of file +verify.getScriptLexicalStructureListCount(5); \ No newline at end of file