s.lines().next().map_or("", |first_line| {
first_line
.find(' ')
- .map_or(first_line, |space_index| &first_line[0..space_index + 1])
+ .map_or(first_line, |space_index| &first_line[0..=space_index])
})
}
InComment,
/// Last character of a comment, '\n' for a line comment, '/' for a block comment.
EndComment,
+ /// Start of a mutlitine string
+ StartString,
+ /// End of a mutlitine string
+ EndString,
/// Inside a string.
InString,
}
}
pub fn is_string(self) -> bool {
- self == FullCodeCharKind::InString
+ self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
}
fn to_codecharkind(self) -> CodeCharKind {
_ => CharClassesStatus::Normal, // Unreachable
}
}
- CharClassesStatus::LitString => match chr {
- '"' => CharClassesStatus::Normal,
- '\\' => {
- char_kind = FullCodeCharKind::InString;
- CharClassesStatus::LitStringEscape
- }
- _ => {
- char_kind = FullCodeCharKind::InString;
- CharClassesStatus::LitString
+ CharClassesStatus::LitString => {
+ char_kind = FullCodeCharKind::InString;
+ match chr {
+ '"' => CharClassesStatus::Normal,
+ '\\' => CharClassesStatus::LitStringEscape,
+ _ => CharClassesStatus::LitString,
}
- },
+ }
CharClassesStatus::LitStringEscape => {
char_kind = FullCodeCharKind::InString;
CharClassesStatus::LitString
let mut line = String::new();
+ let start_class = match self.base.peek() {
+ Some((kind, _)) => *kind,
+ None => FullCodeCharKind::Normal,
+ };
+
while let Some((kind, c)) = self.base.next() {
- self.kind = kind;
if c == '\n' {
+ self.kind = match (start_class, kind) {
+ (FullCodeCharKind::Normal, FullCodeCharKind::InString) => {
+ FullCodeCharKind::StartString
+ }
+ (FullCodeCharKind::InString, FullCodeCharKind::Normal) => {
+ FullCodeCharKind::EndString
+ }
+ _ => kind,
+ };
break;
} else {
line.push(c);
context.report.append(
context.source_map.span_to_filename(span).into(),
vec![FormattingError::from_span(
- &span,
+ span,
&context.source_map,
ErrorKind::LostComment,
)],
pub fn filter_normal_code(code: &str) -> String {
let mut buffer = String::with_capacity(code.len());
LineClasses::new(code).for_each(|(kind, line)| match kind {
- FullCodeCharKind::Normal | FullCodeCharKind::InString => {
+ FullCodeCharKind::Normal
+ | FullCodeCharKind::StartString
+ | FullCodeCharKind::InString
+ | FullCodeCharKind::EndString => {
buffer.push_str(&line);
buffer.push('\n');
}
#[test]
fn test_remove_trailing_white_spaces() {
- let s = format!(" r#\"\n test\n \"#");
+ let s = " r#\"\n test\n \"#";
assert_eq!(remove_trailing_white_spaces(&s), s);
}