mod counted;
#[cfg(all(test, feature = "visitor"))]
mod tests;
use crate::converter::form::spacing;
use counted::{ControlEnds, OutputText};
#[derive(Default)]
pub(super) struct PlainOutput {
text: OutputText,
control_ends: ControlEnds,
open_controls: Vec<ControlPlace>,
#[cfg(feature = "visitor")]
open_checkpoints: Vec<OpenCheckpoint>,
#[cfg(feature = "visitor")]
holes: Vec<Hole>,
blank_tail: usize,
}
struct ControlPlace {
at: usize,
separate: bool,
}
#[must_use = "a control stays open until `finish_control` takes it"]
pub(super) struct OpenControl {
index: usize,
}
impl OpenControl {
const fn into_index(self) -> usize {
self.index
}
}
#[cfg(feature = "visitor")]
struct OpenCheckpoint {
at: usize,
blank_tail: usize,
holes: usize,
}
#[cfg(feature = "visitor")]
struct Hole {
start: usize,
end: usize,
owner: usize,
}
#[cfg(feature = "visitor")]
#[must_use = "a checkpoint stays open until `rollback` or `commit` takes it"]
pub(super) struct Checkpoint {
index: usize,
}
#[cfg(feature = "visitor")]
impl Checkpoint {
const fn into_index(self) -> usize {
self.index
}
}
fn blank_at_end(text: &str) -> usize {
text.len() - text.trim_end_matches([' ', '\t']).len()
}
impl PlainOutput {
pub(super) fn with_capacity(capacity: usize) -> Self {
Self {
text: OutputText::with_capacity(capacity),
..Self::default()
}
}
#[cfg(feature = "visitor")]
pub(super) fn len(&self) -> usize {
self.text.len() - self.holes.iter().map(|hole| hole.end - hole.start).sum::<usize>()
}
#[cfg(not(feature = "visitor"))]
pub(super) const fn len(&self) -> usize {
self.text.len()
}
pub(super) fn ends_with_line_end(&self) -> bool {
self.text.char_before(self.visible_len()) == "\n"
}
pub(super) fn ends_with_space(&self) -> bool {
self.text.char_before(self.visible_len()) == " "
}
pub(super) fn into_text(self) -> String {
debug_assert!(
self.open_controls.is_empty(),
"a control is open at the end of the buffer"
);
#[cfg(feature = "visitor")]
debug_assert!(
self.open_checkpoints.is_empty() && self.holes.is_empty(),
"a checkpoint is open at the end of the buffer"
);
self.text.into_string()
}
pub(super) fn push_text(&mut self, text: &str) {
let end = self.visible_len();
if self.control_ends.last() == Some(end)
&& spacing::needs_space_before(self.text.char_before(end))
&& spacing::starts_with_word(text) == Some(true)
{
self.append(" ");
}
self.append(text);
}
pub(super) fn push_mark(&mut self, mark: &str) {
self.append(mark);
}
pub(super) fn start_control(&mut self) -> OpenControl {
self.open_controls.push(ControlPlace {
at: self.text.len(),
separate: spacing::needs_space_before(self.text.char_before(self.visible_len())),
});
OpenControl {
index: self.open_controls.len() - 1,
}
}
pub(super) fn finish_control(&mut self, control: OpenControl) {
debug_assert_eq!(
control.into_index() + 1,
self.open_controls.len(),
"the control that finishes is not the last one opened"
);
let Some(start) = self.open_controls.pop() else {
return;
};
let at = start.at;
#[cfg(feature = "visitor")]
let at = self.past_holes(at);
if let Some(space_owed) = spacing::space_owed_before(start.separate, self.text.char_at(at)) {
if space_owed {
self.insert_at(at, " ");
}
self.control_ends.push(self.text.len());
}
}
pub(super) fn write_option(&mut self, text: &str) {
let end = self.visible_len();
if let Some((separator, word)) = spacing::option_word(self.text.char_before(end), text) {
self.append(separator);
self.append(word);
}
}
pub(super) fn write_option_list(&mut self, options: &str, in_code: bool) {
if let Some(list) = spacing::option_list_text(options, in_code) {
let control = self.start_control();
self.append(list);
self.finish_control(control);
}
}
pub(super) fn end_line(&mut self) {
let end = self.visible_len();
if !(end == 0 || self.text.char_before(end) == "\n") {
self.append("\n");
}
}
pub(super) fn end_block(&mut self) {
let end = self.visible_len();
if end == 0 {
return;
}
if self.blank_tail > 0 {
let kept = end - self.blank_tail;
#[cfg(feature = "visitor")]
let hidden = self.hide_from(kept);
#[cfg(not(feature = "visitor"))]
let hidden = false;
if !hidden {
self.shrink_to(kept);
}
self.blank_tail = 0;
}
for _ in self.line_ends_at_end()..2 {
self.append("\n");
}
}
#[cfg(feature = "visitor")]
pub(super) fn open(&mut self) -> Checkpoint {
self.open_checkpoints.push(OpenCheckpoint {
at: self.text.len(),
blank_tail: self.blank_tail,
holes: self.holes.len(),
});
Checkpoint {
index: self.open_checkpoints.len() - 1,
}
}
#[cfg(feature = "visitor")]
pub(super) fn since(&self, checkpoint: &Checkpoint) -> &str {
let start = self
.open_checkpoints
.get(checkpoint.index)
.map_or(self.text.len(), |open| self.past_holes(open.at));
self.text.from(start)
}
#[cfg(feature = "visitor")]
pub(super) fn commit(&mut self, checkpoint: Checkpoint) {
let owner = checkpoint.index;
let Some(closed) = self.close(checkpoint) else {
return;
};
let mut index = self.holes.len();
while index > closed.holes {
index -= 1;
if self.holes[index].owner == owner {
let hole = self.holes.remove(index);
self.cut(hole.start, hole.end);
}
}
}
#[cfg(feature = "visitor")]
pub(super) fn rollback(&mut self, checkpoint: Checkpoint) {
let Some(closed) = self.close(checkpoint) else {
return;
};
self.holes.truncate(closed.holes);
self.shrink_to(closed.at);
self.blank_tail = closed.blank_tail;
}
#[cfg(feature = "visitor")]
fn close(&mut self, checkpoint: Checkpoint) -> Option<OpenCheckpoint> {
let index = checkpoint.into_index();
debug_assert_eq!(
index + 1,
self.open_checkpoints.len(),
"the checkpoint that closes is not the last one opened"
);
self.open_checkpoints.truncate(index + 1);
if self.open_checkpoints.len() > index {
self.open_checkpoints.pop()
} else {
None
}
}
#[cfg(feature = "visitor")]
fn visible_len(&self) -> usize {
self.before_holes(self.text.len())
}
#[cfg(not(feature = "visitor"))]
const fn visible_len(&self) -> usize {
self.text.len()
}
#[cfg(feature = "visitor")]
fn before_holes(&self, mut place: usize) -> usize {
for hole in self.holes.iter().rev() {
if hole.end == place {
place = hole.start;
}
}
place
}
#[cfg(feature = "visitor")]
fn past_holes(&self, mut place: usize) -> usize {
for hole in &self.holes {
if (hole.start..=hole.end).contains(&place) {
place = hole.end;
}
}
place
}
fn line_ends_at_end(&self) -> usize {
let mut end = self.visible_len();
let mut count = 0;
while count < 2 && self.text.char_before(end) == "\n" {
count += 1;
end -= 1;
#[cfg(feature = "visitor")]
{
end = self.before_holes(end);
}
}
count
}
fn append(&mut self, text: &str) {
let blank = blank_at_end(text);
self.blank_tail = if blank == text.len() {
self.blank_tail + blank
} else {
blank
};
self.text.push_str(text);
}
fn insert_at(&mut self, at: usize, text: &str) {
if at >= self.text.len() - self.blank_tail {
let blank = blank_at_end(text);
self.blank_tail = if blank == text.len() {
self.blank_tail + blank
} else {
self.text.len() - at + blank
};
}
self.text.insert_str(at, text);
let after = self.control_ends.first_after(at);
self.control_ends.raise_from(after, text.len());
for control in &mut self.open_controls {
if control.at > at {
control.at += text.len();
}
}
#[cfg(feature = "visitor")]
for open in &mut self.open_checkpoints {
if open.at > at {
open.at += text.len();
}
}
#[cfg(feature = "visitor")]
for hole in &mut self.holes {
if hole.start >= at {
hole.start += text.len();
hole.end += text.len();
}
}
}
fn shrink_to(&mut self, len: usize) {
#[cfg(feature = "visitor")]
debug_assert!(
self.open_checkpoints.iter().all(|open| open.at <= len),
"an open checkpoint stands in the text that is removed"
);
for control in &mut self.open_controls {
control.at = control.at.min(len);
}
self.text.truncate(len);
let kept = self.control_ends.first_after(len);
self.control_ends.truncate(kept);
}
#[cfg(feature = "visitor")]
fn hide_from(&mut self, kept: usize) -> bool {
let Some(owner) = self.open_checkpoints.iter().position(|open| open.at > kept) else {
return false;
};
for control in &mut self.open_controls {
control.at = control.at.min(kept);
}
let ends = self.control_ends.first_after(kept);
self.control_ends.truncate(ends);
self.holes.push(Hole {
start: kept,
end: self.text.len(),
owner,
});
true
}
#[cfg(feature = "visitor")]
fn cut(&mut self, start: usize, end: usize) {
debug_assert!(
self.open_checkpoints.iter().all(|open| open.at <= start),
"an open checkpoint stands in or after a hole that is removed"
);
debug_assert!(
self.holes.iter().all(|hole| hole.end <= start),
"a hole stands after a hole that is removed"
);
let by = end - start;
self.text.remove(start..end);
let after = self.control_ends.first_after(start);
self.control_ends.lower_from(after, by);
for control in &mut self.open_controls {
if control.at > start {
control.at -= by;
}
}
}
}