Skip to content

Commit 1864a98

Browse files
committed
Filter out block comment in filter_normal_code if configured to use rustfmt 2 (#4668)
1 parent c3a5111 commit 1864a98

File tree

8 files changed

+60
-38
lines changed

8 files changed

+60
-38
lines changed

src/chains.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl Rewrite for Chain {
442442

443443
formatter.format_root(&self.parent, context, shape)?;
444444
if let Some(result) = formatter.pure_root() {
445-
return wrap_str(result, context.config.max_width(), shape);
445+
return wrap_str(result, context.config, shape);
446446
}
447447

448448
// Decide how to layout the rest of the chain.
@@ -452,7 +452,7 @@ impl Rewrite for Chain {
452452
formatter.format_last_child(context, shape, child_shape)?;
453453

454454
let result = formatter.join_rewrites(context, child_shape)?;
455-
wrap_str(result, context.config.max_width(), shape)
455+
wrap_str(result, context.config, shape)
456456
}
457457
}
458458

@@ -813,7 +813,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
813813
.visual_indent(self.offset)
814814
.sub_width(self.offset)?;
815815
let rewrite = item.rewrite(context, child_shape)?;
816-
match wrap_str(rewrite, context.config.max_width(), shape) {
816+
match wrap_str(rewrite, context.config, shape) {
817817
Some(rewrite) => root_rewrite.push_str(&rewrite),
818818
None => {
819819
// We couldn't fit in at the visual indent, try the last

src/comment.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{self, borrow::Cow, iter};
55
use itertools::{multipeek, MultiPeek};
66
use rustc_span::Span;
77

8-
use crate::config::Config;
8+
use crate::config::{Config, Version};
99
use crate::rewrite::RewriteContext;
1010
use crate::shape::{Indent, Shape};
1111
use crate::string::{rewrite_string, StringFormat};
@@ -1364,13 +1364,15 @@ where
13641364
pub(crate) struct LineClasses<'a> {
13651365
base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
13661366
kind: FullCodeCharKind,
1367+
config_version: Version,
13671368
}
13681369

13691370
impl<'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#"
19101920
fn 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
}

src/expr.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,8 @@ pub(crate) fn format_expr(
203203
rewrite_chain(expr, context, shape)
204204
}
205205
ast::ExprKind::MacCall(ref mac) => {
206-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
207-
wrap_str(
208-
context.snippet(expr.span).to_owned(),
209-
context.config.max_width(),
210-
shape,
211-
)
212-
})
206+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
207+
.or_else(|| wrap_str(context.snippet(expr.span).to_owned(), context.config, shape))
213208
}
214209
ast::ExprKind::Ret(None) => Some("return".to_owned()),
215210
ast::ExprKind::Ret(Some(ref expr)) => {
@@ -1192,11 +1187,7 @@ pub(crate) fn rewrite_literal(
11921187
) -> Option<String> {
11931188
match l.kind {
11941189
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
1195-
_ => wrap_str(
1196-
context.snippet(l.span).to_owned(),
1197-
context.config.max_width(),
1198-
shape,
1199-
),
1190+
_ => wrap_str(context.snippet(l.span).to_owned(), context.config, shape),
12001191
}
12011192
}
12021193

@@ -1212,7 +1203,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
12121203
{
12131204
return Some(string_lit.to_owned());
12141205
} else {
1215-
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
1206+
return wrap_str(string_lit.to_owned(), context.config, shape);
12161207
}
12171208
}
12181209

@@ -1976,8 +1967,7 @@ fn choose_rhs<R: Rewrite>(
19761967

19771968
match (orig_rhs, new_rhs) {
19781969
(Some(ref orig_rhs), Some(ref new_rhs))
1979-
if wrap_str(new_rhs.clone(), context.config.max_width(), new_shape)
1980-
.is_none() =>
1970+
if wrap_str(new_rhs.clone(), context.config, new_shape).is_none() =>
19811971
{
19821972
Some(format!("{}{}", before_space_str, orig_rhs))
19831973
}

src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn format_code_block(
348348
let mut result = String::with_capacity(s.len() * 2);
349349
result.push_str(FN_MAIN_PREFIX);
350350
let mut need_indent = true;
351-
for (kind, line) in LineClasses::new(s) {
351+
for (kind, line) in LineClasses::new(s, config.version()) {
352352
if need_indent {
353353
result.push_str(&indent.to_string(config));
354354
}
@@ -385,7 +385,10 @@ fn format_code_block(
385385
.unwrap_or_else(|| formatted.snippet.len());
386386
let mut is_indented = true;
387387
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
388-
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
388+
for (kind, ref line) in LineClasses::new(
389+
&formatted.snippet[FN_MAIN_PREFIX.len()..block_len],
390+
config.version(),
391+
) {
389392
if !is_first {
390393
result.push('\n');
391394
} else {

src/macros.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1372,15 +1372,11 @@ impl MacroBranch {
13721372
}
13731373
}
13741374
};
1375-
let new_body = wrap_str(
1376-
new_body_snippet.snippet.to_string(),
1377-
config.max_width(),
1378-
shape,
1379-
)?;
1375+
let new_body = wrap_str(new_body_snippet.snippet.to_string(), &config, shape)?;
13801376

13811377
// Indent the body since it is in a block.
13821378
let indent_str = body_indent.to_string(&config);
1383-
let mut new_body = LineClasses::new(new_body.trim_end())
1379+
let mut new_body = LineClasses::new(new_body.trim_end(), config.version())
13841380
.enumerate()
13851381
.fold(
13861382
(String::new(), true),

src/pairs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn rewrite_pairs_one_line<T: Rewrite>(
8686
return None;
8787
}
8888

89-
wrap_str(result, context.config.max_width(), shape)
89+
wrap_str(result, context.config, shape)
9090
}
9191

9292
fn rewrite_pairs_multiline<T: Rewrite>(

src/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub(crate) fn rewrite_string<'a>(
150150
}
151151

152152
result.push_str(fmt.closer);
153-
wrap_str(result, fmt.config.max_width(), fmt.shape)
153+
wrap_str(result, fmt.config, fmt.shape)
154154
}
155155

156156
/// Returns the index to the end of the URL if the split at index of the given string includes an

src/utils.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,12 @@ macro_rules! skip_out_of_file_lines_range_visitor {
390390

391391
// Wraps String in an Option. Returns Some when the string adheres to the
392392
// Rewrite constraints defined for the Rewrite trait and None otherwise.
393-
pub(crate) fn wrap_str(s: String, max_width: usize, shape: Shape) -> Option<String> {
394-
if is_valid_str(&filter_normal_code(&s), max_width, shape) {
393+
pub(crate) fn wrap_str(s: String, config: &Config, shape: Shape) -> Option<String> {
394+
if is_valid_str(
395+
&filter_normal_code(&s, config.version()),
396+
config.max_width(),
397+
shape,
398+
) {
395399
Some(s)
396400
} else {
397401
None
@@ -579,7 +583,7 @@ pub(crate) fn trim_left_preserve_layout(
579583
indent: Indent,
580584
config: &Config,
581585
) -> Option<String> {
582-
let mut lines = LineClasses::new(orig);
586+
let mut lines = LineClasses::new(orig, config.version());
583587
let first_line = lines.next().map(|(_, s)| s.trim_end().to_owned())?;
584588
let mut trimmed_lines = Vec::with_capacity(16);
585589

0 commit comments

Comments
 (0)