add block comment support to line classifier
This commit is contained in:
parent
fd10b7e8ae
commit
19a231d6d3
|
|
@ -81,10 +81,35 @@ fn is_ident(s: &str) -> bool {
|
|||
}
|
||||
|
||||
pub fn classify_document(text: &str) -> Vec<ClassifiedLine> {
|
||||
text.lines()
|
||||
.enumerate()
|
||||
.map(|(i, line)| classify_line(i, line))
|
||||
.collect()
|
||||
let mut result = Vec::new();
|
||||
let mut in_block_comment = false;
|
||||
|
||||
for (i, line) in text.lines().enumerate() {
|
||||
if in_block_comment {
|
||||
let cl = ClassifiedLine { index: i, kind: LineKind::Comment, content: line.to_string() };
|
||||
if line.contains("*/") {
|
||||
in_block_comment = false;
|
||||
}
|
||||
result.push(cl);
|
||||
continue;
|
||||
}
|
||||
|
||||
let trimmed = line.trim();
|
||||
|
||||
if trimmed.starts_with("/*") {
|
||||
if trimmed.contains("*/") && trimmed.find("*/").unwrap() > trimmed.find("/*").unwrap() {
|
||||
// single-line block comment
|
||||
} else {
|
||||
in_block_comment = true;
|
||||
}
|
||||
result.push(ClassifiedLine { index: i, kind: LineKind::Comment, content: line.to_string() });
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push(classify_line(i, line));
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -132,4 +157,28 @@ mod tests {
|
|||
let c = classify_line(0, "Some notes about the project");
|
||||
assert_eq!(c.kind, LineKind::Markdown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_line_block_comment() {
|
||||
let lines = classify_document("/* hello */");
|
||||
assert_eq!(lines.len(), 1);
|
||||
assert_eq!(lines[0].kind, LineKind::Comment);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiline_block_comment() {
|
||||
let lines = classify_document("/* start\nmiddle\nend */\nlet x = 5");
|
||||
assert_eq!(lines.len(), 4);
|
||||
assert_eq!(lines[0].kind, LineKind::Comment);
|
||||
assert_eq!(lines[1].kind, LineKind::Comment);
|
||||
assert_eq!(lines[2].kind, LineKind::Comment);
|
||||
assert_eq!(lines[3].kind, LineKind::Cordial);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn block_comment_then_code() {
|
||||
let lines = classify_document("/* comment */\n/= 2 + 3");
|
||||
assert_eq!(lines[0].kind, LineKind::Comment);
|
||||
assert_eq!(lines[1].kind, LineKind::Eval);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue