@@ -26,7 +26,7 @@ use syntax_pos::Span;
26
26
27
27
use hir:: * ;
28
28
use hir:: intravisit:: Visitor ;
29
- use hir:: print as pprust ;
29
+ use hir:: print:: Nested ;
30
30
31
31
use arena:: TypedArena ;
32
32
use std:: cell:: RefCell ;
@@ -842,6 +842,10 @@ impl<'ast> Map<'ast> {
842
842
pub fn node_to_user_string ( & self , id : NodeId ) -> String {
843
843
node_id_to_string ( self , id, false )
844
844
}
845
+
846
+ pub fn node_to_pretty_string ( & self , id : NodeId ) -> String {
847
+ print:: to_string ( self , |s| s. print_node ( self . get ( id) ) )
848
+ }
845
849
}
846
850
847
851
pub struct NodesMatchingSuffix < ' a , ' ast : ' a > {
@@ -1004,13 +1008,23 @@ pub fn map_decoded_body<'ast>(map: &Map<'ast>,
1004
1008
& ii. body
1005
1009
}
1006
1010
1007
- pub trait NodePrinter {
1008
- fn print_node ( & mut self , node : & Node ) -> io:: Result < ( ) > ;
1011
+ /// Identical to the `PpAnn` implementation for `hir::Crate`,
1012
+ /// except it avoids creating a dependency on the whole crate.
1013
+ impl < ' ast > print:: PpAnn for Map < ' ast > {
1014
+ fn nested ( & self , state : & mut print:: State , nested : print:: Nested ) -> io:: Result < ( ) > {
1015
+ match nested {
1016
+ Nested :: Item ( id) => state. print_item ( self . expect_item ( id. id ) ) ,
1017
+ Nested :: TraitItem ( id) => state. print_trait_item ( self . trait_item ( id) ) ,
1018
+ Nested :: ImplItem ( id) => state. print_impl_item ( self . impl_item ( id) ) ,
1019
+ Nested :: Body ( id) => state. print_expr ( & self . body ( id) . value ) ,
1020
+ Nested :: BodyArgPat ( id, i) => state. print_pat ( & self . body ( id) . arguments [ i] . pat )
1021
+ }
1022
+ }
1009
1023
}
1010
1024
1011
- impl < ' a > NodePrinter for pprust :: State < ' a > {
1012
- fn print_node ( & mut self , node : & Node ) -> io:: Result < ( ) > {
1013
- match * node {
1025
+ impl < ' a > print :: State < ' a > {
1026
+ pub fn print_node ( & mut self , node : Node ) -> io:: Result < ( ) > {
1027
+ match node {
1014
1028
NodeItem ( a) => self . print_item ( & a) ,
1015
1029
NodeForeignItem ( a) => self . print_foreign_item ( & a) ,
1016
1030
NodeTraitItem ( a) => self . print_trait_item ( a) ,
@@ -1020,16 +1034,24 @@ impl<'a> NodePrinter for pprust::State<'a> {
1020
1034
NodeStmt ( a) => self . print_stmt ( & a) ,
1021
1035
NodeTy ( a) => self . print_type ( & a) ,
1022
1036
NodeTraitRef ( a) => self . print_trait_ref ( & a) ,
1037
+ NodeLocal ( a) |
1023
1038
NodePat ( a) => self . print_pat ( & a) ,
1024
- NodeBlock ( a) => self . print_block ( & a) ,
1039
+ NodeBlock ( a) => {
1040
+ use syntax:: print:: pprust:: PrintState ;
1041
+
1042
+ // containing cbox, will be closed by print-block at }
1043
+ self . cbox ( print:: indent_unit) ?;
1044
+ // head-ibox, will be closed by print-block after {
1045
+ self . ibox ( 0 ) ?;
1046
+ self . print_block ( & a)
1047
+ }
1025
1048
NodeLifetime ( a) => self . print_lifetime ( & a) ,
1026
1049
NodeVisibility ( a) => self . print_visibility ( & a) ,
1027
1050
NodeTyParam ( _) => bug ! ( "cannot print TyParam" ) ,
1028
1051
NodeField ( _) => bug ! ( "cannot print StructField" ) ,
1029
1052
// these cases do not carry enough information in the
1030
1053
// ast_map to reconstruct their full structure for pretty
1031
1054
// printing.
1032
- NodeLocal ( _) => bug ! ( "cannot print isolated Local" ) ,
1033
1055
NodeStructCtor ( _) => bug ! ( "cannot print isolated StructCtor" ) ,
1034
1056
}
1035
1057
}
@@ -1110,33 +1132,32 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
1110
1132
field. name,
1111
1133
path_str( ) , id_str)
1112
1134
}
1113
- Some ( NodeExpr ( ref expr ) ) => {
1114
- format ! ( "expr {}{}" , pprust :: expr_to_string ( & expr ) , id_str)
1135
+ Some ( NodeExpr ( _ ) ) => {
1136
+ format ! ( "expr {}{}" , map . node_to_pretty_string ( id ) , id_str)
1115
1137
}
1116
- Some ( NodeStmt ( ref stmt ) ) => {
1117
- format ! ( "stmt {}{}" , pprust :: stmt_to_string ( & stmt ) , id_str)
1138
+ Some ( NodeStmt ( _ ) ) => {
1139
+ format ! ( "stmt {}{}" , map . node_to_pretty_string ( id ) , id_str)
1118
1140
}
1119
- Some ( NodeTy ( ref ty ) ) => {
1120
- format ! ( "type {}{}" , pprust :: ty_to_string ( & ty ) , id_str)
1141
+ Some ( NodeTy ( _ ) ) => {
1142
+ format ! ( "type {}{}" , map . node_to_pretty_string ( id ) , id_str)
1121
1143
}
1122
- Some ( NodeTraitRef ( ref tr ) ) => {
1123
- format ! ( "trait_ref {}{}" , pprust :: path_to_string ( & tr . path ) , id_str)
1144
+ Some ( NodeTraitRef ( _ ) ) => {
1145
+ format ! ( "trait_ref {}{}" , map . node_to_pretty_string ( id ) , id_str)
1124
1146
}
1125
- Some ( NodeLocal ( ref pat ) ) => {
1126
- format ! ( "local {}{}" , pprust :: pat_to_string ( & pat ) , id_str)
1147
+ Some ( NodeLocal ( _ ) ) => {
1148
+ format ! ( "local {}{}" , map . node_to_pretty_string ( id ) , id_str)
1127
1149
}
1128
- Some ( NodePat ( ref pat ) ) => {
1129
- format ! ( "pat {}{}" , pprust :: pat_to_string ( & pat ) , id_str)
1150
+ Some ( NodePat ( _ ) ) => {
1151
+ format ! ( "pat {}{}" , map . node_to_pretty_string ( id ) , id_str)
1130
1152
}
1131
- Some ( NodeBlock ( ref block ) ) => {
1132
- format ! ( "block {}{}" , pprust :: block_to_string ( & block ) , id_str)
1153
+ Some ( NodeBlock ( _ ) ) => {
1154
+ format ! ( "block {}{}" , map . node_to_pretty_string ( id ) , id_str)
1133
1155
}
1134
1156
Some ( NodeStructCtor ( _) ) => {
1135
1157
format ! ( "struct_ctor {}{}" , path_str( ) , id_str)
1136
1158
}
1137
- Some ( NodeLifetime ( ref l) ) => {
1138
- format ! ( "lifetime {}{}" ,
1139
- pprust:: lifetime_to_string( & l) , id_str)
1159
+ Some ( NodeLifetime ( _) ) => {
1160
+ format ! ( "lifetime {}{}" , map. node_to_pretty_string( id) , id_str)
1140
1161
}
1141
1162
Some ( NodeTyParam ( ref ty_param) ) => {
1142
1163
format ! ( "typaram {:?}{}" , ty_param, id_str)
0 commit comments