@@ -5,7 +5,7 @@ use std::{self, borrow::Cow, iter};
55use itertools:: { multipeek, MultiPeek } ;
66use rustc_span:: Span ;
77
8- use crate :: config:: Config ;
8+ use crate :: config:: { Config , Version } ;
99use crate :: rewrite:: RewriteContext ;
1010use crate :: shape:: { Indent , Shape } ;
1111use crate :: string:: { rewrite_string, StringFormat } ;
@@ -1364,13 +1364,15 @@ where
13641364pub ( crate ) struct LineClasses < ' a > {
13651365 base : iter:: Peekable < CharClasses < std:: str:: Chars < ' a > > > ,
13661366 kind : FullCodeCharKind ,
1367+ config_version : Version ,
13671368}
13681369
13691370impl < ' a > LineClasses < ' a > {
1370- pub ( crate ) fn new ( s : & ' a str ) -> Self {
1371+ pub ( crate ) fn new ( s : & ' a str , config_version : Version ) -> Self {
13711372 LineClasses {
13721373 base : CharClasses :: new ( s. chars ( ) ) . peekable ( ) ,
13731374 kind : FullCodeCharKind :: Normal ,
1375+ config_version,
13741376 }
13751377 }
13761378}
@@ -1388,7 +1390,9 @@ impl<'a> Iterator for LineClasses<'a> {
13881390 None => unreachable ! ( ) ,
13891391 } ;
13901392
1393+ let mut prev_kind = FullCodeCharKind :: Normal ;
13911394 while let Some ( ( kind, c) ) = self . base . next ( ) {
1395+ prev_kind = self . kind ;
13921396 // needed to set the kind of the ending character on the last line
13931397 self . kind = kind;
13941398 if c == '\n' {
@@ -1405,6 +1409,10 @@ impl<'a> Iterator for LineClasses<'a> {
14051409 ( FullCodeCharKind :: InStringCommented , FullCodeCharKind :: InComment ) => {
14061410 FullCodeCharKind :: EndStringCommented
14071411 }
1412+ ( _, FullCodeCharKind :: Normal )
1413+ if prev_kind == FullCodeCharKind :: EndComment && self . config_version == Version :: Two => {
1414+ FullCodeCharKind :: EndComment
1415+ }
14081416 _ => kind,
14091417 } ;
14101418 break ;
@@ -1585,9 +1593,9 @@ pub(crate) fn recover_comment_removed(
15851593 }
15861594}
15871595
1588- pub ( crate ) fn filter_normal_code ( code : & str ) -> String {
1596+ pub ( crate ) fn filter_normal_code ( code : & str , config_version : Version ) -> String {
15891597 let mut buffer = String :: with_capacity ( code. len ( ) ) ;
1590- LineClasses :: new ( code) . for_each ( |( kind, line) | match kind {
1598+ LineClasses :: new ( code, config_version ) . for_each ( |( kind, line) | match kind {
15911599 FullCodeCharKind :: Normal
15921600 | FullCodeCharKind :: StartString
15931601 | FullCodeCharKind :: InString
@@ -1905,13 +1913,32 @@ fn main() {
19051913 println!("hello, world");
19061914}
19071915"# ;
1908- assert_eq ! ( s, filter_normal_code( s) ) ;
1909- let s_with_comment = r#"
1916+ assert_eq ! ( s, filter_normal_code( s, Version :: Two ) ) ;
1917+ let s_with_line_comment = r#"
19101918fn main() {
19111919 // hello, world
19121920 println!("hello, world");
19131921}
19141922"# ;
1915- assert_eq ! ( s, filter_normal_code( s_with_comment) ) ;
1923+ assert_eq ! ( s, filter_normal_code( s_with_line_comment, Version :: Two ) ) ;
1924+ let s_with_block_comment = r#"
1925+ fn main() {
1926+ /* hello, world */
1927+ println!("hello, world");
1928+ }
1929+ "# ;
1930+ assert_eq ! ( s, filter_normal_code( s_with_block_comment, Version :: Two ) ) ;
1931+ let s_with_multi_line_comment = r#"
1932+ fn main() {
1933+ /* hello,
1934+ * world
1935+ */
1936+ println!("hello, world");
1937+ }
1938+ "# ;
1939+ assert_eq ! (
1940+ s,
1941+ filter_normal_code( s_with_multi_line_comment, Version :: Two )
1942+ ) ;
19161943 }
19171944}
0 commit comments