Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of antispace in post-processing. #550

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 87 additions & 6 deletions topiary/src/atom_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ pub struct AtomCollection {
}

impl AtomCollection {
/// Returns a basic AtomCollection with the supplied atoms. Only used for
/// testing. Normally you should use `AtomCollection::collect_leafs`
/// instead.
#[cfg(test)]
pub fn new(atoms: Vec<Atom>) -> Self {
Self {
atoms,
prepend: HashMap::new(),
append: HashMap::new(),
specified_leaf_nodes: HashSet::new(),
parent_leaf_nodes: HashMap::new(),
multi_line_nodes: HashSet::new(),
blank_lines_before: HashSet::new(),
line_break_before: HashSet::new(),
line_break_after: HashSet::new(),
scope_begin: HashMap::new(),
scope_end: HashMap::new(),
counter: 0,
}
}

/// Use this to create an initial `AtomCollection`
pub fn collect_leafs(
root: &Node,
Expand Down Expand Up @@ -740,6 +761,20 @@ impl AtomCollection {
pub fn post_process(&mut self) {
self.post_process_scopes();
self.post_process_deletes();
self.post_process_inner();

// We have taken care of spaces following an antispace. Now fix the
// preceding spaces.
collapse_spaces_before_antispace(&mut self.atoms);

// We have to do one more post-processing pass, as the collapsing of
// antispaces may have produced more empty atoms.
self.post_process_inner();

log::debug!("List of atoms after post-processing: {:?}", self.atoms);
}

fn post_process_inner(&mut self) {
let mut prev: Option<&mut Atom> = None;
for next in &mut self.atoms {
if let Some(prev) = prev.as_mut() {
Expand Down Expand Up @@ -804,12 +839,6 @@ impl AtomCollection {
prev = Some(next);
}
}

// We have taken care of spaces following an antispace. Now fix the
// preceding spaces.
collapse_spaces_before_antispace(&mut self.atoms);

log::debug!("List of atoms after post-processing: {:?}", self.atoms);
}

fn next_id(&mut self) -> usize {
Expand Down Expand Up @@ -1074,3 +1103,55 @@ where
&self.atoms[index]
}
}

#[cfg(test)]
mod test {
use crate::{atom_collection::AtomCollection, Atom};
use test_log::test;

#[test]
fn post_process_indent_before_hardline() {
let mut atom_collection = AtomCollection::new(vec![
Atom::Literal("foo".into()),
Atom::Hardline,
Atom::IndentEnd,
Atom::Literal("foo".into()),
]);

atom_collection.post_process();

assert_eq!(
atom_collection.atoms,
vec![
Atom::Literal("foo".into()),
Atom::IndentEnd,
Atom::Hardline,
Atom::Literal("foo".into()),
]
);
}

#[test]
fn issue_549_post_process_indent_before_hardline_with_antispace_in_between() {
let mut atom_collection = AtomCollection::new(vec![
Atom::Literal("foo".into()),
Atom::Hardline,
Atom::Antispace,
Atom::IndentEnd,
Atom::Literal("foo".into()),
]);

atom_collection.post_process();

assert_eq!(
atom_collection.atoms,
vec![
Atom::Literal("foo".into()),
Atom::IndentEnd,
Atom::Empty,
Atom::Hardline,
Atom::Literal("foo".into()),
]
);
}
}