use crate::lang::{Language, Spec};
use crate::options::Options;
use crate::reflow::reflow;
pub fn reflow_source(input: &str, language: Language, opts: &Options) -> String {
if language == Language::Text {
return reflow(input, opts);
}
let spec = language.spec();
if spec.line_markers.is_empty() {
return reflow(input, opts);
}
let lines: Vec<&str> = input.split_inclusive('\n').collect();
let mut out = String::with_capacity(input.len());
let mut i = 0;
while i < lines.len() {
if is_comment_line(strip_newline(lines[i]), &spec) {
let start = i;
while i < lines.len() && is_comment_line(strip_newline(lines[i]), &spec) {
i += 1;
}
let block: String = lines[start..i].concat();
out.push_str(&reflow(&block, opts));
} else {
out.push_str(lines[i]);
i += 1;
}
}
out
}
fn strip_newline(s: &str) -> &str {
s.strip_suffix('\n').unwrap_or(s)
}
fn is_comment_line(line: &str, spec: &Spec) -> bool {
let trimmed = line.trim_start();
spec.line_markers.iter().any(|m| trimmed.starts_with(m))
}