@@ -43,11 +43,53 @@ pub struct NoAnn;
4343
4444impl PpAnn for NoAnn { }
4545
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+
4690pub struct State < ' a > {
4791 pub s : pp:: Printer < ' a > ,
48- cm : Option < & ' a SourceMap > ,
49- comments : Vec < comments:: Comment > ,
50- cur_cmnt : usize ,
92+ comments : Option < Comments < ' a > > ,
5193 ann : & ' a ( dyn PpAnn +' a ) ,
5294 is_expanded : bool
5395}
@@ -98,12 +140,9 @@ impl<'a> State<'a> {
98140 out : & ' a mut String ,
99141 ann : & ' a dyn PpAnn ,
100142 is_expanded : bool ) -> State < ' a > {
101- let comments = comments:: gather_comments ( sess, filename, input) ;
102143 State {
103144 s : pp:: mk_printer ( out) ,
104- cm : Some ( cm) ,
105- comments,
106- cur_cmnt : 0 ,
145+ comments : Some ( Comments :: new ( cm, sess, filename, input) ) ,
107146 ann,
108147 is_expanded,
109148 }
@@ -117,9 +156,7 @@ pub fn to_string<F>(f: F) -> String where
117156 {
118157 let mut printer = State {
119158 s : pp:: mk_printer ( & mut wr) ,
120- cm : None ,
121- comments : Vec :: new ( ) ,
122- cur_cmnt : 0 ,
159+ comments : None ,
123160 ann : & NoAnn ,
124161 is_expanded : false
125162 } ;
@@ -415,8 +452,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
415452
416453pub trait PrintState < ' a > {
417454 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 > > ;
420456
421457 fn word_space < S : Into < Cow < ' static , str > > > ( & mut self , w : S ) {
422458 self . writer ( ) . word ( w) ;
@@ -537,17 +573,13 @@ pub trait PrintState<'a> {
537573 self . writer ( ) . hardbreak ( ) ;
538574 }
539575 }
540- * self . cur_cmnt ( ) = * self . cur_cmnt ( ) + 1 ;
576+ if let Some ( cm) = self . comments ( ) {
577+ cm. current += 1 ;
578+ }
541579 }
542580
543581 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 ( ) )
551583 }
552584
553585 fn print_literal ( & mut self , lit : & ast:: Lit ) {
@@ -744,13 +776,9 @@ impl<'a> PrintState<'a> for State<'a> {
744776 & mut self . s
745777 }
746778
747- fn comments ( & mut self ) -> & mut Vec < comments :: Comment > {
779+ fn comments ( & mut self ) -> & mut Option < Comments < ' a > > {
748780 & mut self . comments
749781 }
750-
751- fn cur_cmnt ( & mut self ) -> & mut usize {
752- & mut self . cur_cmnt
753- }
754782}
755783
756784impl < ' a > State < ' a > {
@@ -2913,18 +2941,10 @@ impl<'a> State<'a> {
29132941
29142942 crate fn maybe_print_trailing_comment ( & mut self , span : syntax_pos:: Span ,
29152943 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) ;
29282948 }
29292949 }
29302950 }
0 commit comments