Skip to content

Do not indent or unindent inside string literal #2589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,45 @@ where
}
}

/// An iterator over the lines of a string, paired with the char kind at the
/// end of the line.
pub struct LineClasses<'a> {
base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
kind: FullCodeCharKind,
}

impl<'a> LineClasses<'a> {
pub fn new(s: &'a str) -> Self {
LineClasses {
base: CharClasses::new(s.chars()).peekable(),
kind: FullCodeCharKind::Normal,
}
}
}

impl<'a> Iterator for LineClasses<'a> {
type Item = (FullCodeCharKind, String);

fn next(&mut self) -> Option<Self::Item> {
if self.base.peek().is_none() {
return None;
}

let mut line = String::new();

while let Some((kind, c)) = self.base.next() {
self.kind = kind;
if c == '\n' {
break;
} else {
line.push(c);
}
}

Some((self.kind, line))
}
}

/// Iterator over functional and commented parts of a string. Any part of a string is either
/// functional code, either *one* block comment, either *one* line comment. Whitespace between
/// comments is functional code. Line comments contain their ending newlines.
Expand Down Expand Up @@ -1141,8 +1180,7 @@ fn remove_comment_header(comment: &str) -> &str {

#[cfg(test)]
mod test {
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
FindUncommented, FullCodeCharKind};
use super::*;
use shape::{Indent, Shape};

#[test]
Expand Down Expand Up @@ -1298,4 +1336,10 @@ mod test {
check("\"/* abc */\"", "abc", Some(4));
check("\"/* abc", "abc", Some(4));
}

#[test]
fn test_remove_trailing_white_spaces() {
let s = format!(" r#\"\n test\n \"#");
assert_eq!(remove_trailing_white_spaces(&s), s);
}
}
27 changes: 20 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use syntax::errors::{DiagnosticBuilder, Handler};
use syntax::parse::{self, ParseSess};

use checkstyle::{output_footer, output_header};
use comment::{CharClasses, FullCodeCharKind};
use comment::{CharClasses, FullCodeCharKind, LineClasses};
use issues::{BadIssueSeeker, Issue};
use shape::Indent;
use utils::use_colored_tty;
Expand Down Expand Up @@ -605,10 +605,19 @@ const FN_MAIN_PREFIX: &str = "fn main() {\n";

fn enclose_in_main_block(s: &str, config: &Config) -> String {
let indent = Indent::from_width(config, config.tab_spaces());
FN_MAIN_PREFIX.to_owned() + &indent.to_string(config)
+ &s.lines()
.collect::<Vec<_>>()
.join(&indent.to_string_with_newline(config)) + "\n}"
let mut result = String::with_capacity(s.len() * 2);
result.push_str(FN_MAIN_PREFIX);
let mut need_indent = true;
for (kind, line) in LineClasses::new(s) {
if need_indent {
result.push_str(&indent.to_string(config));
}
result.push_str(&line);
result.push('\n');
need_indent = !(kind.is_string() && !line.ends_with('\\'));
}
result.push('}');
result
}

/// Format the given code block. Mainly targeted for code block in comment.
Expand All @@ -626,13 +635,16 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option<String>
let formatted = format_snippet(&snippet, config)?;
// 2 = "}\n"
let block_len = formatted.len().checked_sub(2).unwrap_or(0);
for line in formatted[FN_MAIN_PREFIX.len()..block_len].lines() {
let mut is_indented = true;
for (kind, ref line) in LineClasses::new(&formatted[FN_MAIN_PREFIX.len()..block_len]) {
if !is_first {
result.push('\n');
} else {
is_first = false;
}
let trimmed_line = if line.len() > config.max_width() {
let trimmed_line = if !is_indented {
line
} else if line.len() > config.max_width() {
// If there are lines that are larger than max width, we cannot tell
// whether we have succeeded but have some comments or strings that
// are too long, or we have failed to format code block. We will be
Expand All @@ -655,6 +667,7 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option<String>
line
};
result.push_str(trimmed_line);
is_indented = !(kind.is_string() && !line.ends_with('\\'));
}
Some(result)
}
Expand Down
68 changes: 41 additions & 27 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use syntax::{ast, ptr};

use codemap::SpanUtils;
use comment::{contains_comment, remove_trailing_white_spaces, CharClasses, FindUncommented,
FullCodeCharKind};
FullCodeCharKind, LineClasses};
use expr::rewrite_array;
use lists::{itemize_list, write_list, ListFormatting};
use overflow;
Expand Down Expand Up @@ -1054,18 +1054,27 @@ fn indent_macro_snippet(
macro_str: &str,
indent: Indent,
) -> Option<String> {
let mut lines = macro_str.lines();
let first_line = lines.next().map(|s| s.trim_right())?;
let mut lines = LineClasses::new(macro_str);
let first_line = lines.next().map(|(_, s)| s.trim_right().to_owned())?;
let mut trimmed_lines = Vec::with_capacity(16);

let mut veto_trim = false;
let min_prefix_space_width = lines
.filter_map(|line| {
let prefix_space_width = if is_empty_line(line) {
.filter_map(|(kind, line)| {
let mut trimmed = true;
let prefix_space_width = if is_empty_line(&line) {
None
} else {
Some(get_prefix_space_width(context, line))
Some(get_prefix_space_width(context, &line))
};
trimmed_lines.push((line.trim(), prefix_space_width));
let line = if veto_trim || (kind.is_string() && !line.ends_with('\\')) {
veto_trim = kind.is_string() && !line.ends_with('\\');
trimmed = false;
line
} else {
line.trim().to_owned()
};
trimmed_lines.push((trimmed, line, prefix_space_width));
prefix_space_width
})
.min()?;
Expand All @@ -1074,17 +1083,20 @@ fn indent_macro_snippet(
String::from(first_line) + "\n"
+ &trimmed_lines
.iter()
.map(|&(line, prefix_space_width)| match prefix_space_width {
Some(original_indent_width) => {
let new_indent_width = indent.width()
+ original_indent_width
.checked_sub(min_prefix_space_width)
.unwrap_or(0);
let new_indent = Indent::from_width(context.config, new_indent_width);
format!("{}{}", new_indent.to_string(context.config), line.trim())
}
None => String::new(),
})
.map(
|&(trimmed, ref line, prefix_space_width)| match prefix_space_width {
_ if !trimmed => line.to_owned(),
Some(original_indent_width) => {
let new_indent_width = indent.width()
+ original_indent_width
.checked_sub(min_prefix_space_width)
.unwrap_or(0);
let new_indent = Indent::from_width(context.config, new_indent_width);
format!("{}{}", new_indent.to_string(context.config), line.trim())
}
None => String::new(),
},
)
.collect::<Vec<_>>()
.join("\n"),
)
Expand Down Expand Up @@ -1231,15 +1243,17 @@ impl MacroBranch {

// Indent the body since it is in a block.
let indent_str = body_indent.to_string(&config);
let mut new_body = new_body
.trim_right()
.lines()
.fold(String::new(), |mut s, l| {
if !l.is_empty() {
s += &indent_str;
}
s + l + "\n"
});
let mut new_body = LineClasses::new(new_body.trim_right())
.fold(
(String::new(), true),
|(mut s, need_indent), (kind, ref l)| {
if !l.is_empty() && need_indent {
s += &indent_str;
}
(s + l + "\n", !(kind.is_string() && !l.ends_with('\\')))
},
)
.0;

// Undo our replacement of macro variables.
// FIXME: this could be *much* more efficient.
Expand Down
14 changes: 14 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ macro lex_err($kind: ident $(, $body: expr)*) {
methods![ get, post, delete, ];
methods!( get, post, delete, );

// #2588
macro_rules! m {
() => {
r#"
test
"#
};
}
fn foo() {
f!{r#"
test
"#};
}

// #2591
fn foo() {
match 0u32 {
Expand Down
14 changes: 14 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,20 @@ macro lex_err($kind: ident $(, $body: expr)*) {
methods![get, post, delete,];
methods!(get, post, delete,);

// #2588
macro_rules! m {
() => {
r#"
test
"#
};
}
fn foo() {
f!{r#"
test
"#};
}

// #2591
fn foo() {
match 0u32 {
Expand Down