Skip to content

Always include a root node in the navigation bar. #8812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
6 commits merged into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 16 additions & 77 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1959,69 +1959,23 @@ namespace FourSlash {
}
}

public verifyNavigationBarCount(expected: number) {
public verifyNavigationBar(json: any) {
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
const actual = this.getNavigationBarItemsCount(items);

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

private getNavigationBarItemsCount(items: ts.NavigationBarItem[]) {
let result = 0;
if (items) {
for (let i = 0, n = items.length; i < n; i++) {
result++;
result += this.getNavigationBarItemsCount(items[i].childItems);
}
}

return result;
}

public verifyNavigationBarContains(name: string, kind: string, fileName?: string, parentName?: string, isAdditionalSpan?: boolean, markerPosition?: number) {
fileName = fileName || this.activeFile.fileName;
const items = this.languageService.getNavigationBarItems(fileName);

if (!items || items.length === 0) {
this.raiseError("verifyNavigationBarContains failed - found 0 navigation items, expected at least one.");
}

if (this.navigationBarItemsContains(items, name, kind, parentName)) {
return;
}

const missingItem = { name, kind, parentName };
this.raiseError(`verifyNavigationBarContains failed - could not find the item: ${JSON.stringify(missingItem, undefined, 2)} in the returned list: (${JSON.stringify(items, undefined, 2)})`);
}

private navigationBarItemsContains(items: ts.NavigationBarItem[], name: string, kind: string, parentName?: string) {
function recur(items: ts.NavigationBarItem[], curParentName: string) {
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item && item.text === name && item.kind === kind && (!parentName || curParentName === parentName)) {
return true;
}
if (recur(item.childItems, item.text)) {
return true;
}
}
return false;
if (JSON.stringify(items, replacer) !== JSON.stringify(json)) {
this.raiseError(`verifyNavigationBar failed - expected: ${JSON.stringify(json, undefined, 2)}, got: ${JSON.stringify(items, replacer, 2)}`);
}
return recur(items, "");
}

public verifyNavigationBarChildItem(parent: string, name: string, kind: string) {
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);

for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.text === parent) {
if (this.navigationBarItemsContains(item.childItems, name, kind))
return;
const missingItem = { name, kind };
this.raiseError(`verifyNavigationBarChildItem failed - could not find the item: ${JSON.stringify(missingItem)} in the children list: (${JSON.stringify(item.childItems, undefined, 2)})`);
// Make the data easier to read.
function replacer(key: string, value: any) {
switch (key) {
case "spans":
// We won't ever check this.
return undefined;
case "childItems":
return value.length === 0 ? undefined : value;
default:
// Omit falsy values, those are presumed to be the default.
return value || undefined;
}
}
}
Expand Down Expand Up @@ -3042,23 +2996,8 @@ namespace FourSlashInterface {
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
}

public navigationBarCount(count: number) {
this.state.verifyNavigationBarCount(count);
}

// TODO: figure out what to do with the unused arguments.
public navigationBarContains(
name: string,
kind: string,
fileName?: string,
parentName?: string,
isAdditionalSpan?: boolean,
markerPosition?: number) {
this.state.verifyNavigationBarContains(name, kind, fileName, parentName, isAdditionalSpan, markerPosition);
}

public navigationBarChildItem(parent: string, name: string, kind: string) {
this.state.verifyNavigationBarChildItem(parent, name, kind);
public navigationBar(json: any) {
this.state.verifyNavigationBar(json);
}

public navigationItemsListCount(count: number, searchValue: string, matchKind?: string) {
Expand Down
25 changes: 12 additions & 13 deletions src/services/navigationBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ namespace ts.NavigationBar {
return getJsNavigationBarItems(sourceFile, compilerOptions);
}

// If the source file has any child items, then it included in the tree
// and takes lexical ownership of all other top-level items.
let hasGlobalNode = false;

return getItemsWorker(getTopLevelNodes(sourceFile), createTopLevelItem);

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

let current = node.parent;
while (current) {
Expand Down Expand Up @@ -141,7 +135,7 @@ namespace ts.NavigationBar {
function sortNodes(nodes: Node[]): Node[] {
return nodes.slice(0).sort((n1: Declaration, n2: Declaration) => {
if (n1.name && n2.name) {
return getPropertyNameForPropertyNameNode(n1.name).localeCompare(getPropertyNameForPropertyNameNode(n2.name));
return localeCompareFix(getPropertyNameForPropertyNameNode(n1.name), getPropertyNameForPropertyNameNode(n2.name));
}
else if (n1.name) {
return 1;
Expand All @@ -153,6 +147,16 @@ namespace ts.NavigationBar {
return n1.kind - n2.kind;
}
});

// node 0.10 treats "a" as greater than "B".
// For consistency, sort alphabetically, falling back to which is lower-case.
function localeCompareFix(a: string, b: string) {
const cmp = a.toLowerCase().localeCompare(b.toLowerCase());
if (cmp !== 0)
return cmp;
// Return the *opposite* of the `<` operator, which works the same in node 0.10 and 6.0.
return a < b ? 1 : a > b ? -1 : 0;
}
}

function addTopLevelNodes(nodes: Node[], topLevelNodes: Node[]): void {
Expand Down Expand Up @@ -511,11 +515,6 @@ namespace ts.NavigationBar {
function createSourceFileItem(node: SourceFile): ts.NavigationBarItem {
const childItems = getItemsWorker(getChildNodes(node.statements), createChildItem);

if (childItems === undefined || childItems.length === 0) {
return undefined;
}

hasGlobalNode = true;
const rootName = isExternalModule(node)
? "\"" + escapeString(getBaseFileName(removeFileExtension(normalizePath(node.fileName)))) + "\""
: "<global>";
Expand Down
32 changes: 31 additions & 1 deletion tests/cases/fourslash/deleteClassWithEnumPresent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,34 @@

goTo.marker();
edit.deleteAtCaret('class Bar { }'.length);
verify.navigationBarContains('Foo', 'enum', 'tests/cases/fourslash/deleteClassWithEnumPresent.ts', '');
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"childItems": [
{
"text": "Foo",
"kind": "enum"
}
]
},
{
"text": "Foo",
"kind": "enum",
"childItems": [
{
"text": "a",
"kind": "property"
},
{
"text": "b",
"kind": "property"
},
{
"text": "c",
"kind": "property"
}
],
"indent": 1
}
]);
4 changes: 1 addition & 3 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ declare namespace FourSlashInterface {
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
noDocCommentTemplate(): void;

navigationBarCount(count: number): void;
navigationBarContains(name: string, kind: string, fileName?: string, parentName?: string, isAdditionalSpan?: boolean, markerPosition?: number): void;
navigationBarChildItem(parent: string, text: string, kind: string): void;
navigationBar(json: any): void;
navigationItemsListCount(count: number, searchValue: string, matchKind?: string): void;
navigationItemsListContains(name: string, kind: string, searchValue: string, matchKind: string, fileName?: string, parentName?: string): void;
occurrencesAtPositionContains(range: Range, isWriteAccess?: boolean): void;
Expand Down
31 changes: 27 additions & 4 deletions tests/cases/fourslash/getNavigationBarItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@
//// ["bar"]: string;
////}

verify.navigationBarCount(5);
verify.navigationBarContains("C", "class");
verify.navigationBarChildItem("C", "[\"bar\"]", "property");
verify.navigationBarChildItem("C", "foo", "property");
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"childItems": [
{
"text": "C",
"kind": "class"
}
]
},
{
"text": "C",
"kind": "class",
"childItems": [
{
"text": "[\"bar\"]",
"kind": "property"
},
{
"text": "foo",
"kind": "property"
}
],
"indent": 1
}
])
23 changes: 13 additions & 10 deletions tests/cases/fourslash/navbar_const.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/// <reference path="fourslash.ts" />

//// {| "itemName": "c", "kind": "const", "parentName": "<global>" |}const c = 0;
//// const c = 0;

test.markers().forEach(marker => {
verify.navigationBarContains(
marker.data.itemName,
marker.data.kind,
marker.fileName,
marker.data.parentName,
marker.data.isAdditionalRange,
marker.position);
});
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"childItems": [
{
"text": "c",
"kind": "const"
}
]
}
]);
Loading