use super::EmitContext;
pub struct Writer<'a> {
context: &'a EmitContext<'a>,
out: String,
line: usize,
}
impl<'a> Writer<'a> {
pub fn new(context: &'a EmitContext<'a>, start: usize) -> Self {
Self {
context,
out: String::new(),
line: context.line_of(start),
}
}
pub fn finish(self) -> String {
self.out
}
pub fn push(&mut self, text: &str) {
self.line += text.matches('\n').count();
self.out.push_str(text);
}
pub fn to(&mut self, offset: usize) -> bool {
let target = self.context.line_of(offset);
if target <= self.line {
return false;
}
while self.line < target {
self.out.push('\n');
self.line += 1;
}
self.out.push_str(self.context.indent_of(target));
true
}
pub fn break_or_space(&mut self, offset: usize) {
if !self.to(offset) {
self.out.push(' ');
}
}
pub fn will_break(&self, offset: usize) -> bool {
self.context.line_of(offset) > self.line
}
}