-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Changes from 5 commits
d6485c9
1a9dadb
1b5b146
4d933f8
fd2d28d
99edce0
194927e
86b6b6c
b7c3547
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,20 @@ namespace ts.NavigationBar { | |
for (let node of nodes) { | ||
switch (node.kind) { | ||
case SyntaxKind.ClassDeclaration: | ||
topLevelNodes.push(node); | ||
forEach((<ClassDeclaration>node).members, (node) => { | ||
if (node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.Constructor) { | ||
type FunctionLikeMember = MethodDeclaration | ConstructorDeclaration; | ||
if ((<FunctionLikeMember>node).body) { | ||
// We do not include methods that does not have child functions in it, because of duplications. | ||
if (hasNonAnonymousFunctionDeclarations((<Block>(<FunctionLikeMember>node).body).statements)) { | ||
topLevelNodes.push(node); | ||
} | ||
addTopLevelNodes((<Block>(<MethodDeclaration>node).body).statements, topLevelNodes); | ||
} | ||
} | ||
}); | ||
break; | ||
case SyntaxKind.EnumDeclaration: | ||
case SyntaxKind.InterfaceDeclaration: | ||
topLevelNodes.push(node); | ||
|
@@ -176,23 +190,36 @@ namespace ts.NavigationBar { | |
} | ||
} | ||
|
||
function hasNonAnonymousFunctionDeclarations(nodes: NodeArray<any>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it take a |
||
if (forEach(nodes, s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((<FunctionDeclaration>s).name.text))) { | ||
return true; | ||
} | ||
} | ||
|
||
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 (hasNonAnonymousFunctionDeclarations((<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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm starting to think this entire restriction is bogus, but we should take care of that in another PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is not an ideal solution here, because I check twice here for method declaration and constructor in |
||
// are methods and constructors). I.e all functions at module scope are 'top level'. | ||
if (!isFunctionBlock(functionDeclaration.parent)) { | ||
return true; | ||
} | ||
else { | ||
const grandParentKind = functionDeclaration.parent.parent.kind; | ||
if (grandParentKind === SyntaxKind.MethodDeclaration || | ||
grandParentKind === SyntaxKind.Constructor) { | ||
|
||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -369,12 +396,16 @@ 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); | ||
|
||
case SyntaxKind.InterfaceDeclaration: | ||
return createIterfaceItem(<InterfaceDeclaration>node); | ||
return createInterfaceItem(<InterfaceDeclaration>node); | ||
|
||
case SyntaxKind.ModuleDeclaration: | ||
return createModuleItem(<ModuleDeclaration>node); | ||
|
@@ -407,7 +438,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, | ||
|
@@ -422,7 +453,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)], | ||
|
@@ -432,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); | ||
|
@@ -493,7 +549,7 @@ namespace ts.NavigationBar { | |
getIndent(node)); | ||
} | ||
|
||
function createIterfaceItem(node: InterfaceDeclaration): ts.NavigationBarItem { | ||
function createInterfaceItem(node: InterfaceDeclaration): ts.NavigationBarItem { | ||
let childItems = getItemsWorker(sortNodes(removeDynamicallyNamedProperties(node)), createChildItem); | ||
return getNavigationBarItem( | ||
node.name.text, | ||
|
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
for-of
over forEach unless you're unsure whether the first argument isundefined