Skip to content

Commit 89070a2

Browse files
committed
Implement LineClasses
1 parent a43ac40 commit 89070a2

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/comment.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,45 @@ where
901901
}
902902
}
903903

904+
/// An iterator over the lines of a string, paired with the char kind at the
905+
/// end of the line.
906+
pub struct LineClasses<'a> {
907+
base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
908+
kind: FullCodeCharKind,
909+
}
910+
911+
impl<'a> LineClasses<'a> {
912+
pub fn new(s: &'a str) -> Self {
913+
LineClasses {
914+
base: CharClasses::new(s.chars()).peekable(),
915+
kind: FullCodeCharKind::Normal,
916+
}
917+
}
918+
}
919+
920+
impl<'a> Iterator for LineClasses<'a> {
921+
type Item = (FullCodeCharKind, String);
922+
923+
fn next(&mut self) -> Option<Self::Item> {
924+
if self.base.peek().is_none() {
925+
return None;
926+
}
927+
928+
let mut line = String::new();
929+
930+
while let Some((kind, c)) = self.base.next() {
931+
self.kind = kind;
932+
if c == '\n' {
933+
break;
934+
} else {
935+
line.push(c);
936+
}
937+
}
938+
939+
Some((self.kind, line))
940+
}
941+
}
942+
904943
/// Iterator over functional and commented parts of a string. Any part of a string is either
905944
/// functional code, either *one* block comment, either *one* line comment. Whitespace between
906945
/// comments is functional code. Line comments contain their ending newlines.
@@ -1141,8 +1180,7 @@ fn remove_comment_header(comment: &str) -> &str {
11411180

11421181
#[cfg(test)]
11431182
mod test {
1144-
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
1145-
FindUncommented, FullCodeCharKind};
1183+
use super::*;
11461184
use shape::{Indent, Shape};
11471185

11481186
#[test]
@@ -1298,4 +1336,10 @@ mod test {
12981336
check("\"/* abc */\"", "abc", Some(4));
12991337
check("\"/* abc", "abc", Some(4));
13001338
}
1339+
1340+
#[test]
1341+
fn test_remove_trailing_white_spaces() {
1342+
let s = format!(" r#\"\n test\n \"#");
1343+
assert_eq!(remove_trailing_white_spaces(&s), s);
1344+
}
13011345
}

0 commit comments

Comments
 (0)