@@ -22,7 +22,8 @@ module ts.NavigationBar {
22
22
// to say it only has a single layer of depth
23
23
do {
24
24
current = current . parent ;
25
- } while ( current . kind === SyntaxKind . ModuleDeclaration ) ;
25
+ }
26
+ while ( current . kind === SyntaxKind . ModuleDeclaration ) ;
26
27
27
28
// fall through
28
29
case SyntaxKind . ClassDeclaration :
@@ -37,22 +38,27 @@ module ts.NavigationBar {
37
38
38
39
return indent ;
39
40
}
40
-
41
+
41
42
function getChildNodes ( nodes : Node [ ] ) : Node [ ] {
42
43
var childNodes : Node [ ] = [ ] ;
43
44
44
45
for ( var i = 0 , n = nodes . length ; i < n ; i ++ ) {
45
46
var node = nodes [ i ] ;
46
47
47
- if ( node . kind === SyntaxKind . FunctionDeclaration ) {
48
+ if ( node . kind === SyntaxKind . ClassDeclaration ||
49
+ node . kind === SyntaxKind . EnumDeclaration ||
50
+ node . kind === SyntaxKind . InterfaceDeclaration ||
51
+ node . kind === SyntaxKind . ModuleDeclaration ||
52
+ node . kind === SyntaxKind . FunctionDeclaration ) {
53
+
48
54
childNodes . push ( node ) ;
49
55
}
50
56
else if ( node . kind === SyntaxKind . VariableStatement ) {
51
57
childNodes . push . apply ( childNodes , ( < VariableStatement > node ) . declarations ) ;
52
58
}
53
59
}
54
60
55
- return childNodes ;
61
+ return sortNodes ( childNodes ) ;
56
62
}
57
63
58
64
function getTopLevelNodes ( node : SourceFile ) : Node [ ] {
@@ -63,8 +69,27 @@ module ts.NavigationBar {
63
69
64
70
return topLevelNodes ;
65
71
}
66
-
72
+
73
+ function sortNodes ( nodes : Node [ ] ) : Node [ ] {
74
+ return nodes . slice ( 0 ) . sort ( ( n1 : Declaration , n2 : Declaration ) => {
75
+ if ( n1 . name && n2 . name ) {
76
+ return n1 . name . text . localeCompare ( n2 . name . text ) ;
77
+ }
78
+ else if ( n1 . name ) {
79
+ return 1 ;
80
+ }
81
+ else if ( n2 . name ) {
82
+ - 1 ;
83
+ }
84
+ else {
85
+ return n1 . kind - n2 . kind ;
86
+ }
87
+ } ) ;
88
+ }
89
+
67
90
function addTopLevelNodes ( nodes : Node [ ] , topLevelNodes : Node [ ] ) : void {
91
+ nodes = sortNodes ( nodes ) ;
92
+
68
93
for ( var i = 0 , n = nodes . length ; i < n ; i ++ ) {
69
94
var node = nodes [ i ] ;
70
95
switch ( node . kind ) {
@@ -97,9 +122,10 @@ module ts.NavigationBar {
97
122
return functionDeclaration . kind === SyntaxKind . FunctionDeclaration &&
98
123
functionDeclaration . body &&
99
124
functionDeclaration . body . kind === SyntaxKind . FunctionBlock &&
100
- forEach ( ( < Block > functionDeclaration . body ) . statements , s => s . kind === SyntaxKind . FunctionDeclaration ) ;
125
+ forEach ( ( < Block > functionDeclaration . body ) . statements ,
126
+ s => s . kind === SyntaxKind . FunctionDeclaration && ! isEmpty ( ( < FunctionDeclaration > s ) . name . text ) ) ;
101
127
}
102
-
128
+
103
129
function getItemsWorker ( nodes : Node [ ] , createItem : ( n : Node ) => ts . NavigationBarItem ) : ts . NavigationBarItem [ ] {
104
130
var items : ts . NavigationBarItem [ ] = [ ] ;
105
131
@@ -161,31 +187,26 @@ module ts.NavigationBar {
161
187
function createChildItem ( node : Node ) : ts . NavigationBarItem {
162
188
switch ( node . kind ) {
163
189
case SyntaxKind . Parameter :
164
- var parameter = < ParameterDeclaration > node ;
165
190
if ( ( node . flags & NodeFlags . Modifier ) === 0 ) {
166
191
return undefined ;
167
192
}
168
193
169
- return createItem ( node , getTextOfNode ( parameter . name ) , ts . ScriptElementKind . memberVariableElement ) ;
194
+ return createItem ( node , getTextOfNode ( ( < ParameterDeclaration > node ) . name ) , ts . ScriptElementKind . memberVariableElement ) ;
170
195
171
196
case SyntaxKind . Method :
172
- var method = < MethodDeclaration > node ;
173
- return createItem ( node , getTextOfNode ( method . name ) , ts . ScriptElementKind . memberFunctionElement ) ;
197
+ return createItem ( node , getTextOfNode ( ( < MethodDeclaration > node ) . name ) , ts . ScriptElementKind . memberFunctionElement ) ;
174
198
175
199
case SyntaxKind . GetAccessor :
176
- var getAccessor = < AccessorDeclaration > node ;
177
- return createItem ( node , getTextOfNode ( getAccessor . name ) , ts . ScriptElementKind . memberGetAccessorElement ) ;
200
+ return createItem ( node , getTextOfNode ( ( < AccessorDeclaration > node ) . name ) , ts . ScriptElementKind . memberGetAccessorElement ) ;
178
201
179
202
case SyntaxKind . SetAccessor :
180
- var setAccessor = < AccessorDeclaration > node ;
181
- return createItem ( node , getTextOfNode ( setAccessor . name ) , ts . ScriptElementKind . memberSetAccessorElement ) ;
203
+ return createItem ( node , getTextOfNode ( ( < AccessorDeclaration > node ) . name ) , ts . ScriptElementKind . memberSetAccessorElement ) ;
182
204
183
205
case SyntaxKind . IndexSignature :
184
206
return createItem ( node , "[]" , ts . ScriptElementKind . indexSignatureElement ) ;
185
207
186
208
case SyntaxKind . EnumMember :
187
- var enumMember = < EnumMember > node ;
188
- return createItem ( node , getTextOfNode ( enumMember . name ) , ts . ScriptElementKind . memberVariableElement ) ;
209
+ return createItem ( node , getTextOfNode ( ( < EnumMember > node ) . name ) , ts . ScriptElementKind . memberVariableElement ) ;
189
210
190
211
case SyntaxKind . CallSignature :
191
212
return createItem ( node , "()" , ts . ScriptElementKind . callSignatureElement ) ;
@@ -194,19 +215,13 @@ module ts.NavigationBar {
194
215
return createItem ( node , "new()" , ts . ScriptElementKind . constructSignatureElement ) ;
195
216
196
217
case SyntaxKind . Property :
197
- var property = < PropertyDeclaration > node ;
198
- return createItem ( node , getTextOfNode ( property . name ) , ts . ScriptElementKind . memberVariableElement ) ;
218
+ return createItem ( node , getTextOfNode ( ( < PropertyDeclaration > node ) . name ) , ts . ScriptElementKind . memberVariableElement ) ;
199
219
200
220
case SyntaxKind . FunctionDeclaration :
201
- var functionDeclaration = < FunctionDeclaration > node ;
202
- if ( ! isTopLevelFunctionDeclaration ( functionDeclaration ) ) {
203
- return createItem ( node , getTextOfNode ( functionDeclaration . name ) , ts . ScriptElementKind . functionElement ) ;
204
- }
205
- break ;
221
+ return createItem ( node , getTextOfNode ( ( < FunctionDeclaration > node ) . name ) , ts . ScriptElementKind . functionElement ) ;
206
222
207
223
case SyntaxKind . VariableDeclaration :
208
- var variableDeclaration = < VariableDeclaration > node ;
209
- return createItem ( node , getTextOfNode ( variableDeclaration . name ) , ts . ScriptElementKind . variableElement ) ;
224
+ return createItem ( node , getTextOfNode ( ( < VariableDeclaration > node ) . name ) , ts . ScriptElementKind . variableElement ) ;
210
225
211
226
case SyntaxKind . Constructor :
212
227
return createItem ( node , "constructor" , ts . ScriptElementKind . constructorImplementationElement ) ;
@@ -219,7 +234,15 @@ module ts.NavigationBar {
219
234
}
220
235
}
221
236
237
+ function isEmpty ( text : string ) {
238
+ return ! text || text . trim ( ) === "" ;
239
+ }
240
+
222
241
function getNavigationBarItem ( text : string , kind : string , kindModifiers : string , spans : TypeScript . TextSpan [ ] , childItems : ts . NavigationBarItem [ ] = [ ] , indent : number = 0 ) : ts . NavigationBarItem {
242
+ if ( isEmpty ( text ) ) {
243
+ return undefined ;
244
+ }
245
+
223
246
return {
224
247
text : text ,
225
248
kind : kind ,
@@ -290,7 +313,7 @@ module ts.NavigationBar {
290
313
291
314
function createFunctionItem ( node : FunctionDeclaration ) {
292
315
if ( node . name && node . body && node . body . kind === SyntaxKind . FunctionBlock ) {
293
- var childItems = getItemsWorker ( ( < Block > node . body ) . statements , createChildItem ) ;
316
+ var childItems = getItemsWorker ( getChildNodes ( ( < Block > node . body ) . statements ) , createChildItem ) ;
294
317
295
318
return getNavigationBarItem ( node . name . text ,
296
319
ts . ScriptElementKind . functionElement ,
@@ -335,7 +358,7 @@ module ts.NavigationBar {
335
358
? constructor . parameters . concat ( node . members )
336
359
: node . members ;
337
360
338
- var childItems = getItemsWorker ( nodes , createChildItem ) ;
361
+ var childItems = getItemsWorker ( sortNodes ( nodes ) , createChildItem ) ;
339
362
}
340
363
341
364
return getNavigationBarItem (
@@ -348,7 +371,7 @@ module ts.NavigationBar {
348
371
}
349
372
350
373
function createEnumItem ( node : EnumDeclaration ) : ts . NavigationBarItem {
351
- var childItems = getItemsWorker ( node . members , createChildItem ) ;
374
+ var childItems = getItemsWorker ( sortNodes ( node . members ) , createChildItem ) ;
352
375
return getNavigationBarItem (
353
376
node . name . text ,
354
377
ts . ScriptElementKind . enumElement ,
@@ -359,7 +382,7 @@ module ts.NavigationBar {
359
382
}
360
383
361
384
function createIterfaceItem ( node : InterfaceDeclaration ) : ts . NavigationBarItem {
362
- var childItems = getItemsWorker ( node . members , createChildItem ) ;
385
+ var childItems = getItemsWorker ( sortNodes ( node . members ) , createChildItem ) ;
363
386
return getNavigationBarItem (
364
387
node . name . text ,
365
388
ts . ScriptElementKind . interfaceElement ,
0 commit comments