@@ -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,12 @@ impl<'a> Iterator for LineClasses<'a> {
14051409 ( FullCodeCharKind :: InStringCommented , FullCodeCharKind :: InComment ) => {
14061410 FullCodeCharKind :: EndStringCommented
14071411 }
1412+ ( _, FullCodeCharKind :: Normal )
1413+ if prev_kind == FullCodeCharKind :: EndComment
1414+ && self . config_version == Version :: Two =>
1415+ {
1416+ FullCodeCharKind :: EndComment
1417+ }
14081418 _ => kind,
14091419 } ;
14101420 break ;
@@ -1585,9 +1595,9 @@ pub(crate) fn recover_comment_removed(
15851595 }
15861596}
15871597
1588- pub ( crate ) fn filter_normal_code ( code : & str ) -> String {
1598+ pub ( crate ) fn filter_normal_code ( code : & str , config_version : Version ) -> String {
15891599 let mut buffer = String :: with_capacity ( code. len ( ) ) ;
1590- LineClasses :: new ( code) . for_each ( |( kind, line) | match kind {
1600+ LineClasses :: new ( code, config_version ) . for_each ( |( kind, line) | match kind {
15911601 FullCodeCharKind :: Normal
15921602 | FullCodeCharKind :: StartString
15931603 | FullCodeCharKind :: InString
@@ -1905,13 +1915,32 @@ fn main() {
19051915 println!("hello, world");
19061916}
19071917"# ;
1908- assert_eq ! ( s, filter_normal_code( s) ) ;
1909- let s_with_comment = r#"
1918+ assert_eq ! ( s, filter_normal_code( s, Version :: Two ) ) ;
1919+ let s_with_line_comment = r#"
19101920fn main() {
19111921 // hello, world
19121922 println!("hello, world");
19131923}
19141924"# ;
1915- assert_eq ! ( s, filter_normal_code( s_with_comment) ) ;
1925+ assert_eq ! ( s, filter_normal_code( s_with_line_comment, Version :: Two ) ) ;
1926+ let s_with_block_comment = r#"
1927+ fn main() {
1928+ /* hello, world */
1929+ println!("hello, world");
1930+ }
1931+ "# ;
1932+ assert_eq ! ( s, filter_normal_code( s_with_block_comment, Version :: Two ) ) ;
1933+ let s_with_multi_line_comment = r#"
1934+ fn main() {
1935+ /* hello,
1936+ * world
1937+ */
1938+ println!("hello, world");
1939+ }
1940+ "# ;
1941+ assert_eq ! (
1942+ s,
1943+ filter_normal_code( s_with_multi_line_comment, Version :: Two )
1944+ ) ;
19161945 }
19171946}
0 commit comments