yamled 0.0.3

Format-preserving YAML edits: change what you meant, keep every other byte
Documentation
//! Edits that rearrange a block collection's lines without writing values:
//! reordering its children and moving it to another column.

use super::Document;
use crate::index::{Index, Style};
use crate::path::Segment;
use crate::text::{LF, column, dedent, line_end, pad, starts_line, terminated};
use crate::{Error, Path};

impl Document {
    /// Reorder some children of a block sequence or mapping. The children
    /// `order` names, by index or key, trade places among the slots they
    /// hold, in that order, each with its comments. Children it does not
    /// name, and the blank lines and loose comments between children, stay
    /// where they are.
    ///
    /// # Errors
    ///
    /// [`Error::NoNode`] for a missing collection or child,
    /// [`Error::WrongKind`] when the path is not a block collection,
    /// [`Error::Repeated`] for a child named twice, [`Error::Unsupported`]
    /// for a child that shares its line with its parent's dash or ends in a
    /// block scalar that keeps its trailing lines (`|+`), and
    /// [`Error::Invalid`].
    pub fn reorder(&mut self, path: &Path, order: &[Segment]) -> Result<(), Error> {
        let id = self.block_collection(path)?;
        let children = self.index.nodes[id].children.clone();
        let mut named: Vec<usize> = Vec::with_capacity(order.len());
        for segment in order {
            let child_path = match segment {
                Segment::Key(key) => path.clone().key(key.clone()),
                Segment::Index(index) => path.clone().index(*index),
            };
            let slot = self
                .index
                .find(&child_path)
                .and_then(|child| children.iter().position(|&c| c == child))
                .ok_or_else(|| Error::NoNode {
                    path: child_path.clone(),
                })?;
            if named.contains(&slot) {
                return Err(Error::Repeated { path: child_path });
            }
            named.push(slot);
        }
        if named
            .iter()
            .any(|&slot| self.ends_in_kept_lines(children[slot]))
        {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: "reorder a child that ends in a block scalar keeping its trailing lines",
            });
        }
        if let Some(&child) = children.iter().find(|&&child| !self.starts_own_line(child)) {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: match self.index.nodes[child].key {
                    Some(_) => "reorder a mapping whose first key shares its dash's line",
                    None => "reorder a sequence whose first item shares its parent's line",
                },
            });
        }
        let mut slots = named.clone();
        slots.sort_unstable();
        let mut placed: Vec<usize> = (0..children.len()).collect();
        for (&slot, &child) in slots.iter().zip(&named) {
            placed[slot] = child;
        }
        let spans: Vec<(usize, usize)> = children
            .iter()
            .map(|&child| {
                let start = self.index.owned_start(&self.source, child);
                (
                    start,
                    line_end(&self.source, self.index.nodes[child].value.end),
                )
            })
            .collect();
        let (from, to) = (spans[0].0, spans[spans.len() - 1].1);
        let mut text = String::new();
        for (slot, &child) in placed.iter().enumerate() {
            let (start, end) = spans[child];
            text.push_str(&terminated(self.source[start..end].trim_end_matches(LF)));
            if let Some(&(next, _)) = spans.get(slot + 1) {
                text.push_str(&self.source[spans[slot].1..next]);
            }
        }
        if !self.source[..to].ends_with(LF) {
            text.pop();
        }
        self.commit_splice(from, to, &text)
    }

    /// Move a block sequence or mapping so its children start at `column`.
    /// Every line of the children, with their comments, nested collections,
    /// and block scalars, shifts by the same number of columns; empty lines
    /// stay empty.
    ///
    /// # Errors
    ///
    /// [`Error::NoNode`], [`Error::WrongKind`] when the path is not a block
    /// collection, [`Error::Unsupported`] for a collection that starts on its
    /// parent's line, and [`Error::Invalid`] when the shifted text does not
    /// parse or parses as a different structure.
    pub fn reindent(&mut self, path: &Path, column: usize) -> Result<(), Error> {
        let id = self.block_collection(path)?;
        let children = &self.index.nodes[id].children;
        let (Some(&first), Some(&last)) = (children.first(), children.last()) else {
            return Err(Error::WrongKind {
                path: path.clone(),
                expected: "a collection with children",
            });
        };
        if !self.starts_own_line(first) {
            return Err(Error::Unsupported {
                path: path.clone(),
                what: "reindent a collection that starts on its parent's line",
            });
        }
        let current = self.child_column(first);
        let from = self.index.owned_start(&self.source, first);
        let to = line_end(&self.source, self.index.nodes[last].value.end);
        let block = &self.source[from..to];
        let shifted = if column >= current {
            pad(block, column - current)
        } else {
            dedent(block, current - column)
        };
        let before = shape(&self.index);
        let edited = [&self.source[..from], &shifted, &self.source[to..]].concat();
        let index = Index::build(&edited).map_err(|error| Error::Invalid {
            message: error.to_string(),
        })?;
        if shape(&index) != before {
            return Err(Error::Invalid {
                message: format!("re-indenting {path} would change the document's structure"),
            });
        }
        self.commit(edited, index);
        Ok(())
    }

    fn block_collection(&self, path: &Path) -> Result<usize, Error> {
        let id = self.id(path)?;
        match self.index.nodes[id].style {
            Style::BlockSequence | Style::BlockMapping => Ok(id),
            _ => Err(Error::WrongKind {
                path: path.clone(),
                expected: "a block sequence or mapping",
            }),
        }
    }

    /// Whether a child's entry, or its dash for a sequence item, is the
    /// first thing on its line.
    fn starts_own_line(&self, child: usize) -> bool {
        let at = match self.index.nodes[child].key {
            Some(_) => self.index.entry_start(child),
            None => self.dash(child),
        };
        starts_line(&self.source, at)
    }

    /// The column of a child's key, or of its dash for a sequence item.
    fn child_column(&self, child: usize) -> usize {
        match self.index.nodes[child].key {
            Some(_) => column(&self.source, self.index.entry_start(child)),
            None => column(&self.source, self.dash(child)),
        }
    }
}

/// Each node's style, child count, and key: what must survive a re-indent.
fn shape(index: &Index) -> Vec<(Style, usize, Option<&str>)> {
    index
        .nodes
        .iter()
        .map(|node| {
            let key = node.key.as_ref().map(|key| key.text.as_str());
            (node.style, node.children.len(), key)
        })
        .collect()
}