Skip to content

Navigation bar items in methods #7178

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
Merged
Show file tree
Hide file tree
Changes from 7 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
69 changes: 62 additions & 7 deletions src/services/navigationBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ namespace ts.NavigationBar {
for (let node of nodes) {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
topLevelNodes.push(node);
for (const member of (<ClassDeclaration>node).members) {
if (member.kind === SyntaxKind.MethodDeclaration || member.kind === SyntaxKind.Constructor) {
type FunctionLikeMember = MethodDeclaration | ConstructorDeclaration;
if ((<FunctionLikeMember>member).body) {
// We do not include methods that does not have child functions in it, because of duplications.
if (hasNamedFunctionDeclarations((<Block>(<FunctionLikeMember>member).body).statements)) {
topLevelNodes.push(member);
}
addTopLevelNodes((<Block>(<MethodDeclaration>member).body).statements, topLevelNodes);
}
}
}
break;
case SyntaxKind.EnumDeclaration:
case SyntaxKind.InterfaceDeclaration:
topLevelNodes.push(node);
Expand All @@ -176,23 +190,35 @@ namespace ts.NavigationBar {
}
}

function hasNamedFunctionDeclarations(nodes: NodeArray<Statement>) {
if (forEach(nodes, s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((<FunctionDeclaration>s).name.text))) {
Copy link
Member

Choose a reason for hiding this comment

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

You could instead just return the result of the forEach, or just convert this to a for-of loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created for-of-loop here, because forEach is returning a truthy value or undefined. With a for-of I can return boolean.

return true;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Explicitly return false


function isTopLevelFunctionDeclaration(functionDeclaration: FunctionLikeDeclaration) {
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.Block) {
// Proper function declarations can only have identifier names
if (forEach((<Block>functionDeclaration.body).statements,
s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((<FunctionDeclaration>s).name.text))) {

if (hasNamedFunctionDeclarations((<Block>functionDeclaration.body).statements)) {
return true;
}

// Or if it is not parented by another function. i.e all functions
// at module scope are 'top level'.
// Or if it is not parented by another function (except for parent functions that
// are methods and constructors). I.e all functions at module scope are 'top level'.
Copy link
Member

Choose a reason for hiding this comment

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

Wait, why is it desirable to consider these top-level exactly?

Copy link
Member

Choose a reason for hiding this comment

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

In other words, why are functions in methods/constructors able to be top level, but not functions in other functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Functions in function are considered in the above if statement:

if (hasNamedFunctionDeclarations((<Block>functionDeclaration.body).statements)) {
    return true;
}

Just note that. The current implementation, top-level functions are just a classification of functions. They are not actual top-level functions. This follows the previous logic with nested functions. If a nested function has a function declarations inside, even though itself is nested. It is classified as top-level.

Though the comment here can probably be improved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DanielRosenwasser I improved the commenting here. Please take another look.

if (!isFunctionBlock(functionDeclaration.parent)) {
return true;
}
else {
const grandParentKind = functionDeclaration.parent.parent.kind;
if (grandParentKind === SyntaxKind.MethodDeclaration ||
grandParentKind === SyntaxKind.Constructor) {

return true;
}
}
}
}

Expand Down Expand Up @@ -370,6 +396,10 @@ namespace ts.NavigationBar {
case SyntaxKind.ClassDeclaration:
return createClassItem(<ClassDeclaration>node);

case SyntaxKind.MethodDeclaration:
case SyntaxKind.Constructor:
return createMemberFunctionLikeItem(<MethodDeclaration | ConstructorDeclaration>node);

case SyntaxKind.EnumDeclaration:
return createEnumItem(<EnumDeclaration>node);

Expand Down Expand Up @@ -407,7 +437,7 @@ namespace ts.NavigationBar {

function createModuleItem(node: ModuleDeclaration): NavigationBarItem {
let moduleName = getModuleName(node);

let childItems = getItemsWorker(getChildNodes((<Block>getInnermostModule(node).body).statements), createChildItem);

return getNavigationBarItem(moduleName,
Expand All @@ -422,7 +452,7 @@ namespace ts.NavigationBar {
if (node.body && node.body.kind === SyntaxKind.Block) {
let childItems = getItemsWorker(sortNodes((<Block>node.body).statements), createChildItem);

return getNavigationBarItem(!node.name ? "default": node.name.text ,
return getNavigationBarItem(!node.name ? "default": node.name.text,
ts.ScriptElementKind.functionElement,
getNodeModifiers(node),
[getNodeSpan(node)],
Expand All @@ -433,6 +463,31 @@ namespace ts.NavigationBar {
return undefined;
}

function createMemberFunctionLikeItem(node: MethodDeclaration | ConstructorDeclaration) {
if (node.body && node.body.kind === SyntaxKind.Block) {
let childItems = getItemsWorker(sortNodes((<Block>node.body).statements), createChildItem);
let scriptElementKind: string;
let memberFunctionName: string;
if (node.kind === SyntaxKind.MethodDeclaration) {
memberFunctionName = getPropertyNameForPropertyNameNode(node.name);
scriptElementKind = ts.ScriptElementKind.memberFunctionElement;
}
else {
memberFunctionName = "constructor";
scriptElementKind = ts.ScriptElementKind.constructorImplementationElement;
}

return getNavigationBarItem(memberFunctionName,
scriptElementKind,
getNodeModifiers(node),
[getNodeSpan(node)],
childItems,
getIndent(node));
}

return undefined;
}

function createSourceFileItem(node: SourceFile): ts.NavigationBarItem {
let childItems = getItemsWorker(getChildNodes(node.statements), createChildItem);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference path="fourslash.ts"/>

////class Class {
//// constructor() {
//// {| "itemName": "LocalFunctionInConstructor", "kind": "function", "parentName": "Class"|}function LocalFunctionInConstructor() {
////
//// }
////
//// {| "itemName": "LocalInterfaceInConstrcutor", "kind": "interface", "parentName": "foo"|}interface LocalInterfaceInConstrcutor {
//// }
////
//// enum LocalEnumInConstructor {
//// {| "itemName": "LocalEnumMemberInConstructor", "kind": "property", "parentName": "LocalEnumInConstructor"|}LocalEnumMemberInConstructor,
//// }
//// }
////
//// method() {
//// {| "itemName": "LocalFunctionInMethod", "kind": "function", "parentName": "foo"|}function LocalFunctionInMethod() {
//// {| "itemName": "LocalFunctionInLocalFunctionInMethod", "kind": "function", "parentName": "bar"|}function LocalFunctionInLocalFunctionInMethod() {
////
//// }
//// }
////
//// {| "itemName": "LocalInterfaceInMethod", "kind": "interface", "parentName": "foo"|}interface LocalInterfaceInMethod {
//// }
////
//// enum LocalEnumInMethod {
//// {| "itemName": "LocalEnumMemberInMethod", "kind": "property", "parentName": "foo"|}LocalEnumMemberInMethod,
//// }
//// }
////
//// emptyMethod() { // Non child functions method should not be duplicated
////
//// }
////}

test.markers().forEach((marker) => {
verify.getScriptLexicalStructureListContains(marker.data.itemName, marker.data.kind, marker.fileName, marker.data.parentName);
});

// no other items
verify.getScriptLexicalStructureListCount(17);