Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
119 changes: 40 additions & 79 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1959,71 +1959,47 @@ namespace FourSlash {
}
}

public verifyNavigationBarCount(expected: number) {
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;
public verifyNavigationBar(json: any) {
let items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
items = this.simplifyNavigationBar(items);
if (JSON.stringify(items) !== JSON.stringify(json)) {
this.raiseError(`verifyNavigationBar failed - expected: ${JSON.stringify(json, undefined, 2)}, got: ${JSON.stringify(items, undefined, 2)}`);
}
}

// Remove any properties that tend to all have the same value so that test data is easier to read.
private simplifyNavigationBar(items: ts.NavigationBarItem[]): any {
return items.map(item => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is only for comparison purposes, i would do this in the replacer function for JSON.stringify(items, (k,v)=> { if (key === "indent") return undefined; return v; });

item = ts.clone(item);
if (item.kindModifiers === "") {
delete item.kindModifiers;
}
// We won't check this.
delete item.spans;
item.childItems = item.childItems.map(child => {
child = ts.clone(child);
delete child.spans;
ts.Debug.assert(child.childItems.length === 0);
delete child.childItems;
ts.Debug.assert(child.indent === 0);
delete child.indent;
ts.Debug.assert(child.bolded === false);
delete child.bolded;
ts.Debug.assert(child.grayed === false);
delete child.grayed;
if (child.kindModifiers === "") {
delete child.kindModifiers;
}
return child;
});
if (item.bolded === false) {
delete item.bolded;
}
return false;
}
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)})`);
if (item.grayed === false) {
delete item.grayed;
}
}
return item;
});
}

public printNavigationItems(searchValue: string) {
Expand Down Expand Up @@ -3042,23 +3018,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
33 changes: 32 additions & 1 deletion tests/cases/fourslash/deleteClassWithEnumPresent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,35 @@

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"
}
],
"indent": 0
},
{
"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
32 changes: 28 additions & 4 deletions tests/cases/fourslash/getNavigationBarItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@
//// ["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"
}
],
"indent": 0
},
{
"text": "C",
"kind": "class",
"childItems": [
{
"text": "[\"bar\"]",
"kind": "property"
},
{
"text": "foo",
"kind": "property"
}
],
"indent": 1
}
])
24 changes: 14 additions & 10 deletions tests/cases/fourslash/navbar_const.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/// <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"
}
],
"indent": 0
}
]);
Loading