@@ -19,7 +19,8 @@ fn run(
19
19
fold_fn: fold_fn,
20
20
fold_const: fold_const,
21
21
fold_enum: fold_enum,
22
- fold_res: fold_res
22
+ fold_res: fold_res,
23
+ fold_iface: fold_iface
23
24
with * fold:: default_seq_fold ( srv)
24
25
} ) ;
25
26
fold. fold_crate ( fold, doc)
@@ -84,17 +85,21 @@ fn get_ret_ty(srv: astsrv::srv, fn_id: doc::ast_id) -> option<str> {
84
85
ast_map:: node_item ( @{
85
86
node: ast:: item_fn ( decl, _, _) , _
86
87
} ) {
87
- if decl. output . node != ast:: ty_nil {
88
- some ( pprust:: ty_to_str ( decl. output ) )
89
- } else {
90
- // Nil-typed return values are not interesting
91
- none
92
- }
88
+ ret_ty_to_str ( decl)
93
89
}
94
90
}
95
91
}
96
92
}
97
93
94
+ fn ret_ty_to_str ( decl : ast:: fn_decl ) -> option < str > {
95
+ if decl. output . node != ast:: ty_nil {
96
+ some ( pprust:: ty_to_str ( decl. output ) )
97
+ } else {
98
+ // Nil-typed return values are not interesting
99
+ none
100
+ }
101
+ }
102
+
98
103
#[ test]
99
104
fn should_add_fn_ret_types ( ) {
100
105
let source = "fn a() -> int { }" ;
@@ -138,14 +143,18 @@ fn get_arg_tys(srv: astsrv::srv, fn_id: doc::ast_id) -> [(str, str)] {
138
143
ast_map:: node_item ( @{
139
144
node: ast:: item_res ( decl, _, _, _, _) , _
140
145
} ) {
141
- vec:: map ( decl. inputs ) { |arg|
142
- ( arg. ident , pprust:: ty_to_str ( arg. ty ) )
143
- }
146
+ decl_arg_tys ( decl)
144
147
}
145
148
}
146
149
}
147
150
}
148
151
152
+ fn decl_arg_tys ( decl : ast:: fn_decl ) -> [ ( str , str ) ] {
153
+ vec:: map ( decl. inputs ) { |arg|
154
+ ( arg. ident , pprust:: ty_to_str ( arg. ty ) )
155
+ }
156
+ }
157
+
149
158
#[ test]
150
159
fn should_add_arg_types ( ) {
151
160
let source = "fn a(b: int, c: bool) { }" ;
@@ -264,4 +273,169 @@ fn should_add_resource_arg_tys() {
264
273
let doc = extract:: from_srv ( srv, "" ) ;
265
274
let doc = run ( srv, doc) ;
266
275
assert doc. topmod . resources ( ) [ 0 ] . args [ 0 ] . ty == some ( "bool" ) ;
267
- }
276
+ }
277
+
278
+ fn fold_iface (
279
+ fold : fold:: fold < astsrv:: srv > ,
280
+ doc : doc:: ifacedoc
281
+ ) -> doc:: ifacedoc {
282
+
283
+ let srv = fold. ctxt ;
284
+
285
+ {
286
+ methods: vec:: map ( doc. methods ) { |methoddoc|
287
+ {
288
+ args: merge_method_arg_tys (
289
+ srv,
290
+ doc. id ,
291
+ methoddoc. args ,
292
+ methoddoc. name ) ,
293
+ return : merge_method_ret_ty (
294
+ srv,
295
+ doc. id ,
296
+ methoddoc. return ,
297
+ methoddoc. name ) ,
298
+ sig: get_method_sig ( srv, doc. id , methoddoc. name )
299
+ with methoddoc
300
+ }
301
+ }
302
+ with doc
303
+ }
304
+ }
305
+
306
+ fn merge_method_ret_ty (
307
+ srv : astsrv:: srv ,
308
+ item_id : doc:: ast_id ,
309
+ doc : doc:: retdoc ,
310
+ method_name : str
311
+ ) -> doc:: retdoc {
312
+ alt get_method_ret_ty ( srv, item_id, method_name) {
313
+ some ( ty) {
314
+ {
315
+ ty: some ( ty)
316
+ with doc
317
+ }
318
+ }
319
+ none { doc }
320
+ }
321
+ }
322
+
323
+ fn get_method_ret_ty (
324
+ srv : astsrv:: srv ,
325
+ item_id : doc:: ast_id ,
326
+ method_name : str
327
+ ) -> option < str > {
328
+ astsrv:: exec ( srv) { |ctxt|
329
+ alt ctxt. ast_map . get ( item_id) {
330
+ ast_map:: node_item ( @{
331
+ node: ast:: item_iface ( _, methods) , _
332
+ } ) {
333
+ alt vec:: find ( methods) { |method|
334
+ method. ident == method_name
335
+ } {
336
+ some ( method) {
337
+ ret_ty_to_str ( method. decl )
338
+ }
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+
345
+ fn get_method_sig (
346
+ srv : astsrv:: srv ,
347
+ item_id : doc:: ast_id ,
348
+ method_name : str
349
+ ) -> option < str > {
350
+ astsrv:: exec ( srv) { |ctxt|
351
+ alt ctxt. ast_map . get ( item_id) {
352
+ ast_map:: node_item ( @{
353
+ node: ast:: item_iface ( _, methods) , _
354
+ } ) {
355
+ alt vec:: find ( methods) { |method|
356
+ method. ident == method_name
357
+ } {
358
+ some ( method) {
359
+ some ( pprust:: fun_to_str ( method. decl , method. ident , [ ] ) )
360
+ }
361
+ }
362
+ }
363
+ }
364
+ }
365
+ }
366
+
367
+ fn merge_method_arg_tys (
368
+ srv : astsrv:: srv ,
369
+ item_id : doc:: ast_id ,
370
+ args : [ doc:: argdoc ] ,
371
+ method_name : str
372
+ ) -> [ doc:: argdoc ] {
373
+ let tys = get_method_arg_tys ( srv, item_id, method_name) ;
374
+ vec:: map2 ( args, tys) { |arg, ty|
375
+ assert arg. name == tuple:: first ( ty) ;
376
+ {
377
+ ty: some ( tuple:: second ( ty) )
378
+ with arg
379
+ }
380
+ }
381
+ }
382
+
383
+ fn get_method_arg_tys (
384
+ srv : astsrv:: srv ,
385
+ item_id : doc:: ast_id ,
386
+ method_name : str
387
+ ) -> [ ( str , str ) ] {
388
+ astsrv:: exec ( srv) { |ctxt|
389
+ alt ctxt. ast_map . get ( item_id) {
390
+ ast_map:: node_item ( @{
391
+ node: ast:: item_iface ( _, methods) , _
392
+ } ) {
393
+ alt vec:: find ( methods) { |method|
394
+ method. ident == method_name
395
+ } {
396
+ some ( method) {
397
+ decl_arg_tys ( method. decl )
398
+ }
399
+ }
400
+ }
401
+ }
402
+ }
403
+ }
404
+
405
+ #[ test]
406
+ fn should_add_iface_method_sigs ( ) {
407
+ let source = "iface i { fn a() -> int; }" ;
408
+ let srv = astsrv:: mk_srv_from_str ( source) ;
409
+ let doc = extract:: from_srv ( srv, "" ) ;
410
+ let doc = run ( srv, doc) ;
411
+ assert doc. topmod . ifaces ( ) [ 0 ] . methods [ 0 ] . sig == some ( "fn a() -> int" ) ;
412
+ }
413
+
414
+ #[ test]
415
+ fn should_add_iface_method_ret_types ( ) {
416
+ let source = "iface i { fn a() -> int; }" ;
417
+ let srv = astsrv:: mk_srv_from_str ( source) ;
418
+ let doc = extract:: from_srv ( srv, "" ) ;
419
+ let doc = run ( srv, doc) ;
420
+ assert doc. topmod . ifaces ( ) [ 0 ] . methods [ 0 ] . return . ty == some ( "int" ) ;
421
+ }
422
+
423
+ #[ test]
424
+ fn should_not_add_iface_method_nil_ret_type ( ) {
425
+ let source = "iface i { fn a(); }" ;
426
+ let srv = astsrv:: mk_srv_from_str ( source) ;
427
+ let doc = extract:: from_srv ( srv, "" ) ;
428
+ let doc = run ( srv, doc) ;
429
+ assert doc. topmod . ifaces ( ) [ 0 ] . methods [ 0 ] . return . ty == none;
430
+ }
431
+
432
+ #[ test]
433
+ fn should_add_iface_method_arg_types ( ) {
434
+ let source = "iface i { fn a(b: int, c: bool); }" ;
435
+ let srv = astsrv:: mk_srv_from_str ( source) ;
436
+ let doc = extract:: from_srv ( srv, "" ) ;
437
+ let doc = run ( srv, doc) ;
438
+ let fn_ = doc. topmod . ifaces ( ) [ 0 ] . methods [ 0 ] ;
439
+ assert fn_. args [ 0 ] . ty == some ( "int" ) ;
440
+ assert fn_. args [ 1 ] . ty == some ( "bool" ) ;
441
+ }
0 commit comments