Skip to content

Commit fe77f54

Browse files
author
Andy Hanson
committed
Always include a root node in the navigation bar.
This lets us change the navigation bar counting algorithm to traverse from the root only, so it never has duplicate nodes.
1 parent 27a1e91 commit fe77f54

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/harness/fourslash.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,23 +1961,22 @@ namespace FourSlash {
19611961

19621962
public verifyNavigationBarCount(expected: number) {
19631963
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
1964-
const actual = this.getNavigationBarItemsCount(items);
1964+
const actual = this.getNavigationBarItemsCount(items[0]);
19651965

19661966
if (expected !== actual) {
19671967
this.raiseError(`verifyNavigationBarCount failed - found: ${actual} navigation items, expected: ${expected}.`);
19681968
}
19691969
}
19701970

1971-
private getNavigationBarItemsCount(items: ts.NavigationBarItem[]) {
1972-
let result = 0;
1973-
if (items) {
1974-
for (let i = 0, n = items.length; i < n; i++) {
1975-
result++;
1976-
result += this.getNavigationBarItemsCount(items[i].childItems);
1977-
}
1971+
private getNavigationBarItemsCount(root: ts.NavigationBarItem) {
1972+
ts.Debug.assert(root.kind === ts.ScriptElementKind.moduleElement);
1973+
function recur(item: ts.NavigationBarItem) {
1974+
let count = 1;
1975+
for (const child of item.childItems)
1976+
count += recur(child);
1977+
return count;
19781978
}
1979-
1980-
return result;
1979+
return recur(root);
19811980
}
19821981

19831982
public verifyNavigationBarContains(name: string, kind: string, fileName?: string, parentName?: string, isAdditionalSpan?: boolean, markerPosition?: number) {

src/services/navigationBar.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ namespace ts.NavigationBar {
99
return getJsNavigationBarItems(sourceFile, compilerOptions);
1010
}
1111

12-
// If the source file has any child items, then it included in the tree
13-
// and takes lexical ownership of all other top-level items.
14-
let hasGlobalNode = false;
15-
1612
return getItemsWorker(getTopLevelNodes(sourceFile), createTopLevelItem);
1713

1814
function getIndent(node: Node): number {
19-
// If we have a global node in the tree,
20-
// then it adds an extra layer of depth to all subnodes.
21-
let indent = hasGlobalNode ? 1 : 0;
15+
let indent = 1; // Global node is the only one with indent 0.
2216

2317
let current = node.parent;
2418
while (current) {
@@ -508,11 +502,6 @@ namespace ts.NavigationBar {
508502
function createSourceFileItem(node: SourceFile): ts.NavigationBarItem {
509503
const childItems = getItemsWorker(getChildNodes(node.statements), createChildItem);
510504

511-
if (childItems === undefined || childItems.length === 0) {
512-
return undefined;
513-
}
514-
515-
hasGlobalNode = true;
516505
const rootName = isExternalModule(node)
517506
? "\"" + escapeString(getBaseFileName(removeFileExtension(normalizePath(node.fileName)))) + "\""
518507
: "<global>";

0 commit comments

Comments
 (0)