@@ -26,7 +26,7 @@ use syntax_pos::Span;
2626
2727use hir:: * ;
2828use hir:: intravisit:: Visitor ;
29- use hir:: print as pprust ;
29+ use hir:: print:: Nested ;
3030
3131use arena:: TypedArena ;
3232use std:: cell:: RefCell ;
@@ -842,6 +842,10 @@ impl<'ast> Map<'ast> {
842842 pub fn node_to_user_string ( & self , id : NodeId ) -> String {
843843 node_id_to_string ( self , id, false )
844844 }
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+ }
845849}
846850
847851pub struct NodesMatchingSuffix < ' a , ' ast : ' a > {
@@ -1004,13 +1008,23 @@ pub fn map_decoded_body<'ast>(map: &Map<'ast>,
10041008 & ii. body
10051009}
10061010
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+ }
10091023}
10101024
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 {
10141028 NodeItem ( a) => self . print_item ( & a) ,
10151029 NodeForeignItem ( a) => self . print_foreign_item ( & a) ,
10161030 NodeTraitItem ( a) => self . print_trait_item ( a) ,
@@ -1020,16 +1034,24 @@ impl<'a> NodePrinter for pprust::State<'a> {
10201034 NodeStmt ( a) => self . print_stmt ( & a) ,
10211035 NodeTy ( a) => self . print_type ( & a) ,
10221036 NodeTraitRef ( a) => self . print_trait_ref ( & a) ,
1037+ NodeLocal ( a) |
10231038 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+ }
10251048 NodeLifetime ( a) => self . print_lifetime ( & a) ,
10261049 NodeVisibility ( a) => self . print_visibility ( & a) ,
10271050 NodeTyParam ( _) => bug ! ( "cannot print TyParam" ) ,
10281051 NodeField ( _) => bug ! ( "cannot print StructField" ) ,
10291052 // these cases do not carry enough information in the
10301053 // ast_map to reconstruct their full structure for pretty
10311054 // printing.
1032- NodeLocal ( _) => bug ! ( "cannot print isolated Local" ) ,
10331055 NodeStructCtor ( _) => bug ! ( "cannot print isolated StructCtor" ) ,
10341056 }
10351057 }
@@ -1110,33 +1132,32 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11101132 field. name,
11111133 path_str( ) , id_str)
11121134 }
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)
11151137 }
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)
11181140 }
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)
11211143 }
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)
11241146 }
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)
11271149 }
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)
11301152 }
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)
11331155 }
11341156 Some ( NodeStructCtor ( _) ) => {
11351157 format ! ( "struct_ctor {}{}" , path_str( ) , id_str)
11361158 }
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)
11401161 }
11411162 Some ( NodeTyParam ( ref ty_param) ) => {
11421163 format ! ( "typaram {:?}{}" , ty_param, id_str)
0 commit comments