Implement LineClasses

This commit is contained in:
Seiichi Uchida 2018-04-02 22:53:16 +09:00 committed by Seiichi Uchida
parent a43ac40a78
commit 89070a2244

View file

@ -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.
@ -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]
@ -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);
}
}