@@ -43,11 +43,53 @@ pub struct NoAnn;
43
43
44
44
impl PpAnn for NoAnn { }
45
45
46
+ pub struct Comments < ' a > {
47
+ cm : & ' a SourceMap ,
48
+ comments : Vec < comments:: Comment > ,
49
+ current : usize ,
50
+ }
51
+
52
+ impl < ' a > Comments < ' a > {
53
+ pub fn new (
54
+ cm : & ' a SourceMap ,
55
+ sess : & ParseSess ,
56
+ filename : FileName ,
57
+ input : String ,
58
+ ) -> Comments < ' a > {
59
+ let comments = comments:: gather_comments ( sess, filename, input) ;
60
+ Comments {
61
+ cm,
62
+ comments,
63
+ current : 0 ,
64
+ }
65
+ }
66
+
67
+ pub fn next ( & self ) -> Option < comments:: Comment > {
68
+ self . comments . get ( self . current ) . cloned ( )
69
+ }
70
+
71
+ pub fn trailing_comment (
72
+ & mut self ,
73
+ span : syntax_pos:: Span ,
74
+ next_pos : Option < BytePos > ,
75
+ ) -> Option < comments:: Comment > {
76
+ if let Some ( cmnt) = self . next ( ) {
77
+ if cmnt. style != comments:: Trailing { return None ; }
78
+ let span_line = self . cm . lookup_char_pos ( span. hi ( ) ) ;
79
+ let comment_line = self . cm . lookup_char_pos ( cmnt. pos ) ;
80
+ let next = next_pos. unwrap_or_else ( || cmnt. pos + BytePos ( 1 ) ) ;
81
+ if span. hi ( ) < cmnt. pos && cmnt. pos < next && span_line. line == comment_line. line {
82
+ return Some ( cmnt) ;
83
+ }
84
+ }
85
+
86
+ None
87
+ }
88
+ }
89
+
46
90
pub struct State < ' a > {
47
91
pub s : pp:: Printer < ' a > ,
48
- cm : Option < & ' a SourceMap > ,
49
- comments : Vec < comments:: Comment > ,
50
- cur_cmnt : usize ,
92
+ comments : Option < Comments < ' a > > ,
51
93
ann : & ' a ( dyn PpAnn +' a ) ,
52
94
is_expanded : bool
53
95
}
@@ -98,12 +140,9 @@ impl<'a> State<'a> {
98
140
out : & ' a mut String ,
99
141
ann : & ' a dyn PpAnn ,
100
142
is_expanded : bool ) -> State < ' a > {
101
- let comments = comments:: gather_comments ( sess, filename, input) ;
102
143
State {
103
144
s : pp:: mk_printer ( out) ,
104
- cm : Some ( cm) ,
105
- comments,
106
- cur_cmnt : 0 ,
145
+ comments : Some ( Comments :: new ( cm, sess, filename, input) ) ,
107
146
ann,
108
147
is_expanded,
109
148
}
@@ -117,9 +156,7 @@ pub fn to_string<F>(f: F) -> String where
117
156
{
118
157
let mut printer = State {
119
158
s : pp:: mk_printer ( & mut wr) ,
120
- cm : None ,
121
- comments : Vec :: new ( ) ,
122
- cur_cmnt : 0 ,
159
+ comments : None ,
123
160
ann : & NoAnn ,
124
161
is_expanded : false
125
162
} ;
@@ -415,8 +452,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
415
452
416
453
pub trait PrintState < ' a > {
417
454
fn writer ( & mut self ) -> & mut pp:: Printer < ' a > ;
418
- fn comments ( & mut self ) -> & mut Vec < comments:: Comment > ;
419
- fn cur_cmnt ( & mut self ) -> & mut usize ;
455
+ fn comments ( & mut self ) -> & mut Option < Comments < ' a > > ;
420
456
421
457
fn word_space < S : Into < Cow < ' static , str > > > ( & mut self , w : S ) {
422
458
self . writer ( ) . word ( w) ;
@@ -537,17 +573,13 @@ pub trait PrintState<'a> {
537
573
self . writer ( ) . hardbreak ( ) ;
538
574
}
539
575
}
540
- * self . cur_cmnt ( ) = * self . cur_cmnt ( ) + 1 ;
576
+ if let Some ( cm) = self . comments ( ) {
577
+ cm. current += 1 ;
578
+ }
541
579
}
542
580
543
581
fn next_comment ( & mut self ) -> Option < comments:: Comment > {
544
- let cur_cmnt = * self . cur_cmnt ( ) ;
545
- let cmnts = & * self . comments ( ) ;
546
- if cur_cmnt < cmnts. len ( ) {
547
- Some ( cmnts[ cur_cmnt] . clone ( ) )
548
- } else {
549
- None
550
- }
582
+ self . comments ( ) . as_mut ( ) . and_then ( |c| c. next ( ) )
551
583
}
552
584
553
585
fn print_literal ( & mut self , lit : & ast:: Lit ) {
@@ -744,13 +776,9 @@ impl<'a> PrintState<'a> for State<'a> {
744
776
& mut self . s
745
777
}
746
778
747
- fn comments ( & mut self ) -> & mut Vec < comments :: Comment > {
779
+ fn comments ( & mut self ) -> & mut Option < Comments < ' a > > {
748
780
& mut self . comments
749
781
}
750
-
751
- fn cur_cmnt ( & mut self ) -> & mut usize {
752
- & mut self . cur_cmnt
753
- }
754
782
}
755
783
756
784
impl < ' a > State < ' a > {
@@ -2913,18 +2941,10 @@ impl<'a> State<'a> {
2913
2941
2914
2942
crate fn maybe_print_trailing_comment ( & mut self , span : syntax_pos:: Span ,
2915
2943
next_pos : Option < BytePos > )
2916
- {
2917
- let cm = match self . cm {
2918
- Some ( cm) => cm,
2919
- _ => return ,
2920
- } ;
2921
- if let Some ( ref cmnt) = self . next_comment ( ) {
2922
- if cmnt. style != comments:: Trailing { return ; }
2923
- let span_line = cm. lookup_char_pos ( span. hi ( ) ) ;
2924
- let comment_line = cm. lookup_char_pos ( cmnt. pos ) ;
2925
- let next = next_pos. unwrap_or_else ( || cmnt. pos + BytePos ( 1 ) ) ;
2926
- if span. hi ( ) < cmnt. pos && cmnt. pos < next && span_line. line == comment_line. line {
2927
- self . print_comment ( cmnt) ;
2944
+ {
2945
+ if let Some ( cmnts) = self . comments ( ) {
2946
+ if let Some ( cmnt) = cmnts. trailing_comment ( span, next_pos) {
2947
+ self . print_comment ( & cmnt) ;
2928
2948
}
2929
2949
}
2930
2950
}
0 commit comments