use std::fmt;
use std::ops::Range;
use crate::index::{Index, Location, Style};
use crate::text::{
LF, NEWLINE, column, comment_start, dash_before, dedent, has_blank_line, indent, join,
line_end, line_start, spaced, starts_line, terminated,
};
use crate::{Error, Path};
mod arrange;
#[cfg(feature = "serde")]
mod values;
#[cfg(feature = "serde")]
pub use values::Position;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Spacing {
#[default]
Tight,
Blank,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Fragment {
text: String,
}
impl Fragment {
#[must_use]
pub fn as_str(&self) -> &str {
&self.text
}
}
struct Cut {
from: usize,
to: usize,
with: String,
text: String,
}
#[derive(Clone, Copy, Debug)]
pub struct Node<'a> {
document: &'a Document,
id: usize,
}
impl Node<'_> {
#[must_use]
pub fn value(&self) -> Location {
let node = &self.document.index.nodes[self.id];
Location::of(&self.document.source, node.value.clone())
}
#[must_use]
pub fn key(&self) -> Option<Location> {
let node = &self.document.index.nodes[self.id];
let key = node.key.as_ref()?;
Some(Location::of(&self.document.source, key.range.clone()))
}
#[must_use]
pub fn owned(&self) -> Location {
let source = &self.document.source;
let index = &self.document.index;
let start = index.owned_start(source, self.id);
let end = line_end(source, self.document.item_end(self.id));
Location::of(source, start..end)
}
#[must_use]
pub fn style(&self) -> Style {
self.document.index.nodes[self.id].style
}
#[must_use]
pub fn len(&self) -> usize {
self.document.index.nodes[self.id].children.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn text(&self) -> &str {
let node = &self.document.index.nodes[self.id];
&self.document.source[node.value.clone()]
}
}
#[derive(Clone, Debug)]
pub struct Document {
source: String,
index: Index,
compact: bool,
spacing: Spacing,
#[cfg(feature = "serde")]
quotes: crate::Quotes,
pending: Option<Pending>,
}
#[derive(Clone, Debug, Default)]
struct Pending {
splices: Vec<Splice>,
#[cfg(feature = "serde")]
keys: Vec<(usize, String)>,
}
#[derive(Clone, Debug)]
struct Splice {
from: usize,
to: usize,
with: String,
}
impl Document {
pub fn parse(source: impl Into<String>) -> Result<Self, Error> {
let source = source.into();
if source.contains('\r') {
return Err(Error::Unsupported {
path: Path::root(),
what: "edit text with carriage returns",
});
}
let index = Index::build(&source)?;
let compact = compact_lists(&source, &index);
Ok(Self {
source,
index,
compact,
spacing: Spacing::default(),
#[cfg(feature = "serde")]
quotes: crate::Quotes::default(),
pending: None,
})
}
#[must_use]
pub fn with_spacing(mut self, spacing: Spacing) -> Self {
self.spacing = spacing;
self
}
#[cfg(feature = "serde")]
#[must_use]
pub fn with_quotes(mut self, quotes: crate::Quotes) -> Self {
self.quotes = quotes;
self
}
pub fn batch<R, E: From<Error>>(
&mut self,
edits: impl FnOnce(&mut Self) -> Result<R, E>,
) -> Result<R, E> {
if self.pending.is_some() {
return edits(self);
}
self.pending = Some(Pending::default());
let result = edits(self);
let pending = self.pending.take().unwrap_or_default();
let value = result?;
self.apply(pending.splices)?;
Ok(value)
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.source
}
#[must_use]
pub fn into_string(self) -> String {
self.source
}
#[must_use]
pub fn node(&self, path: &Path) -> Option<Node<'_>> {
self.index.find(path).map(|id| Node { document: self, id })
}
#[must_use]
pub fn locate(&self, path: &Path) -> Option<Location> {
self.node(path).map(|node| node.value())
}
#[must_use]
pub fn locate_nearest(&self, path: &Path) -> Option<Location> {
let id = match self.index.walk(path) {
Ok(id) => id,
Err((id, _)) if !self.index.nodes.is_empty() => id,
Err(_) => return None,
};
Some(Node { document: self, id }.value())
}
pub fn remove(&mut self, path: &Path) -> Result<(), Error> {
let id = self.id(path)?;
let parent = self.parent(path, id)?;
match self.index.nodes[parent].style {
Style::BlockSequence => {
let cut = self.cut(path, id, parent)?;
self.commit_splice(cut.from, cut.to, &cut.with)
}
Style::BlockMapping => self.remove_entry(path, id, parent),
Style::FlowSequence => self.remove_flow_item(path, id, parent),
Style::Plain
| Style::SingleQuoted
| Style::DoubleQuoted
| Style::Literal
| Style::Folded
| Style::Alias
| Style::FlowMapping => Err(Error::Unsupported {
path: path.clone(),
what: "remove from a flow mapping",
}),
}
}
pub fn take(&mut self, path: &Path) -> Result<Fragment, Error> {
let id = self.id(path)?;
let parent = self.parent(path, id)?;
if self.index.nodes[parent].style != Style::BlockSequence {
return Err(Error::WrongKind {
path: path.clone(),
expected: "an item of a block sequence",
});
}
let cut = self.cut(path, id, parent)?;
self.commit_splice(cut.from, cut.to, &cut.with)?;
Ok(Fragment { text: cut.text })
}
pub fn put(&mut self, path: &Path, index: usize, fragment: &Fragment) -> Result<(), Error> {
let id = self.id(path)?;
let node = &self.index.nodes[id];
let lines: Vec<String> = fragment.text.lines().map(str::to_owned).collect();
match node.style {
Style::BlockSequence => {
let dash = node.children.first().map_or_else(
|| column(&self.source, node.value.start),
|&first| column(&self.source, self.dash(first)),
);
let block = join(&indent(&lines, dash));
let gap = match self.item_spacing(id) {
Spacing::Tight => "",
Spacing::Blank => NEWLINE,
};
let kept = lines.last().is_some_and(|line| line.trim().is_empty());
if let Some(&child) = node.children.get(index) {
let at = self.index.owned_start(&self.source, child);
let gap = if kept { "" } else { gap };
let text = format!("{block}{LF}{gap}");
self.commit_splice(at, at, &text)
} else if let Some(&last) = node.children.last()
&& index == node.children.len()
{
let at = line_end(&self.source, self.item_end(last));
let lead = if self.source[..at].ends_with(LF) {
""
} else {
NEWLINE
};
let gap = if self.ends_in_kept_lines(last) {
""
} else {
gap
};
let to = if kept {
blank_run_end(&self.source, at)
} else {
at
};
let text = format!("{lead}{gap}{block}{LF}");
self.commit_splice(at, to, &text)
} else {
Err(Error::NoNode {
path: path.clone().index(index),
})
}
}
Style::FlowSequence | Style::Plain
if node.children.is_empty() && self.is_empty_value(id) =>
{
if index != 0 {
return Err(Error::NoNode {
path: path.clone().index(index),
});
}
self.fill_empty(path, id, &lines)
}
Style::Plain
| Style::SingleQuoted
| Style::DoubleQuoted
| Style::Literal
| Style::Folded
| Style::Alias
| Style::FlowSequence
| Style::BlockMapping
| Style::FlowMapping => Err(Error::WrongKind {
path: path.clone(),
expected: "a block sequence or an empty value",
}),
}
}
fn item_spacing(&self, sequence: usize) -> Spacing {
let mut gaps = self.index.nodes[sequence].children.windows(2).map(|pair| {
let after = line_end(&self.source, self.item_end(pair[0]));
has_blank_line(
&self.source,
after,
self.index.owned_start(&self.source, pair[1]),
)
});
match gaps.next() {
Some(first) if gaps.all(|gap| gap == first) => {
if first {
Spacing::Blank
} else {
Spacing::Tight
}
}
_ => self.spacing,
}
}
fn id(&self, path: &Path) -> Result<usize, Error> {
self.index
.find(path)
.ok_or_else(|| Error::NoNode { path: path.clone() })
}
fn parent(&self, path: &Path, id: usize) -> Result<usize, Error> {
self.index.nodes[id]
.parent
.ok_or_else(|| Error::Unsupported {
path: path.clone(),
what: "remove or take the root",
})
}
fn is_empty_value(&self, id: usize) -> bool {
let node = &self.index.nodes[id];
match node.style {
Style::FlowSequence => node.children.is_empty(),
Style::Plain => matches!(self.source[node.value.clone()].trim(), "" | "~" | "null"),
Style::SingleQuoted
| Style::DoubleQuoted
| Style::Literal
| Style::Folded
| Style::Alias
| Style::BlockSequence
| Style::BlockMapping
| Style::FlowMapping => false,
}
}
fn colon(&self, path: &Path, id: usize) -> Result<(usize, usize), Error> {
let key = self.index.nodes[id]
.key
.as_ref()
.ok_or_else(|| Error::Unsupported {
path: path.clone(),
what: "fill an empty value that has no key",
})?;
let value = &self.index.nodes[id].value;
let limit = if value.start > key.range.end {
value.start
} else {
line_end(&self.source, key.range.end)
};
let after = &self.source[key.range.end..limit];
let mut line_offset = 0;
let mut found = None;
for line in after.split_inclusive(LF) {
let code = comment_start(line).map_or(line, |comment| &line[..comment]);
if let Some(colon) = code.find(':') {
found = Some(line_offset + colon);
break;
}
line_offset += line.len();
}
let offset = found.ok_or_else(|| Error::Unsupported {
path: path.clone(),
what: "write the value of a key with no ':' after it",
})?;
Ok((
key.range.end + offset + 1,
column(&self.source, key.range.start),
))
}
fn fill_empty(&mut self, path: &Path, id: usize, lines: &[String]) -> Result<(), Error> {
let (colon, key_column) = self.colon(path, id)?;
let value = self.index.nodes[id].value.clone();
let key_line_end = line_end(&self.source, colon);
let key_line_content =
key_line_end - usize::from(self.source[..key_line_end].ends_with(LF));
let (mut properties, mut comment) = self.key_line_parts(colon, Some(&value));
let end = if value.is_empty() || value.end <= key_line_content {
key_line_end
} else {
if value.start > key_line_content {
let value_line = line_start(&self.source, value.start);
if !self.source[key_line_end..value_line].trim().is_empty() {
return Err(Error::Unsupported {
path: path.clone(),
what: "fill an empty value with lines between it and its key",
});
}
properties = spaced(&[&properties, self.source[value_line..value.start].trim()]);
}
let end = line_end(&self.source, value.end);
comment = spaced(&[&comment, self.source[value.end..end].trim()]);
end
};
if let Some(tag) = properties.split_whitespace().find(|property| {
property.starts_with('!') && !matches!(*property, "!!seq" | "!<tag:yaml.org,2002:seq>")
}) {
return Err(Error::TagMismatch {
path: path.clone(),
tag: tag.to_owned(),
});
}
let end = if lines.last().is_some_and(|line| line.trim().is_empty()) {
blank_run_end(&self.source, end)
} else {
end
};
let head = spaced(&[&properties, &comment]);
let step = if self.compact { 0 } else { 2 };
let placed: Vec<String> = std::iter::once(if head.is_empty() {
head
} else {
format!(" {head}")
})
.chain(indent(lines, key_column + step))
.collect();
self.commit_splice(colon, end, &terminated(&join(&placed)))
}
fn key_line_parts(&self, colon: usize, value: Option<&Range<usize>>) -> (String, String) {
let end = line_end(&self.source, colon);
let mut rest = self.source[colon..end].trim_end_matches(LF).to_owned();
let line_content = colon + rest.len();
if let Some(value) = value.filter(|value| (colon..line_content).contains(&value.start)) {
rest.replace_range(value.start - colon..value.end.min(line_content) - colon, "");
}
let (properties, comment) = rest.split_at(comment_start(&rest).unwrap_or(rest.len()));
(properties.trim().to_owned(), comment.trim().to_owned())
}
fn remove_entry(&mut self, path: &Path, id: usize, parent: usize) -> Result<(), Error> {
let siblings = &self.index.nodes[parent].children;
if siblings.len() == 1 {
return Err(Error::Unsupported {
path: path.clone(),
what: "remove the only entry of a mapping",
});
}
let entry = self.index.entry_start(id);
if starts_line(&self.source, entry) {
let previous_keeps = siblings
.iter()
.position(|&sibling| sibling == id)
.and_then(|at| at.checked_sub(1))
.is_some_and(|at| self.ends_in_kept_lines(siblings[at]));
let from = self.index.owned_start(&self.source, id);
let to = line_end(&self.source, self.item_end(id));
let to = if previous_keeps {
blank_run_end(&self.source, to)
} else {
to
};
return self.commit_splice(from, to, "");
}
let Some(&[_, next]) = siblings.windows(2).find(|pair| pair[0] == id) else {
return Err(Error::Unsupported {
path: path.clone(),
what: "remove the last key of a mapping from its dash line",
});
};
let next_entry = self.index.entry_start(next);
let comments_start = self.index.owned_start(&self.source, next);
let comments_end = line_start(&self.source, next_entry);
let line = line_start(&self.source, entry);
let dash_column = column(&self.source, dash_before(&self.source, entry));
let comments: Vec<String> = self.source[comments_start..comments_end]
.lines()
.map(|comment| comment.trim_start().to_owned())
.collect();
let mut with: String = indent(&comments, dash_column)
.iter()
.map(|comment| terminated(comment))
.collect();
with.push_str(&self.source[line..entry]);
self.commit_splice(line, next_entry, &with)
}
fn cut(&self, path: &Path, id: usize, parent: usize) -> Result<Cut, Error> {
let source = &self.source;
let start = self.index.owned_start(source, id);
let end = line_end(source, self.item_end(id));
let text = dedent(&source[start..end], column(source, self.dash(id)));
let siblings = &self.index.nodes[parent].children;
if siblings.len() == 1 {
#[expect(
clippy::map_err_ignore,
reason = "a missing key is reported as the edit the document cannot do, which names the same path"
)]
let (colon, _) = self.colon(path, parent).map_err(|_| Error::Unsupported {
path: path.clone(),
what: "take the only item of a sequence with no key",
})?;
let (properties, comment) = self.key_line_parts(colon, None);
let with = format!(" {}{LF}", spaced(&[&properties, "[]", &comment]));
return Ok(Cut {
from: colon,
to: end,
with,
text,
});
}
let (mut from, mut to) = (start, end);
let previous_keeps = siblings
.iter()
.position(|&sibling| sibling == id)
.and_then(|at| at.checked_sub(1))
.is_some_and(|at| self.ends_in_kept_lines(siblings[at]));
if siblings.last() == Some(&id) && !previous_keeps {
while from > 0 {
let previous = line_start(source, from - 1);
if !source[previous..from].trim().is_empty() {
break;
}
from = previous;
}
} else {
to = blank_run_end(source, to);
}
Ok(Cut {
from,
to,
with: String::new(),
text,
})
}
fn remove_flow_item(&mut self, path: &Path, id: usize, parent: usize) -> Result<(), Error> {
let sequence = self.index.nodes[parent].value.clone();
if self.source[sequence.clone()].contains(LF) {
return Err(Error::Unsupported {
path: path.clone(),
what: "remove from a flow sequence that spans lines",
});
}
let children = &self.index.nodes[parent].children;
let at = children
.iter()
.position(|&child| child == id)
.unwrap_or_default();
let (from, to) = match (at.checked_sub(1), children.get(at + 1)) {
(_, Some(_)) => (
self.flow_item_start(parent, at),
self.flow_item_start(parent, at + 1),
),
(Some(previous), None) => (
self.index.nodes[children[previous]].value.end,
self.index.nodes[id].value.end,
),
(None, None) => (sequence.start + 1, sequence.end - 1),
};
self.commit_splice(from, to, "")
}
fn flow_item_start(&self, sequence: usize, k: usize) -> usize {
let node = &self.index.nodes[sequence];
let after = match k.checked_sub(1) {
None => node.value.start + 1,
Some(previous) => {
let end = self.index.nodes[node.children[previous]].value.end;
self.source[end..]
.find(',')
.map_or(end, |comma| end + comma + 1)
}
};
let rest = &self.source[after..];
after + rest.len() - rest.trim_start_matches(' ').len()
}
fn ends_in_kept_lines(&self, id: usize) -> bool {
let mut last = id;
while let Some(&child) = self.index.nodes[last].children.last() {
last = child;
}
self.keeps_trailing_lines(last)
}
fn item_end(&self, id: usize) -> usize {
let mut last = id;
while let Some(&child) = self.index.nodes[last].children.last() {
last = child;
}
self.kept_end(last).max(self.index.nodes[id].value.end)
}
fn keeps_trailing_lines(&self, id: usize) -> bool {
let node = &self.index.nodes[id];
matches!(node.style, Style::Literal | Style::Folded)
&& self.source[node.value.start..]
.split_whitespace()
.next()
.is_some_and(|header| header.contains('+'))
}
fn kept_end(&self, id: usize) -> usize {
let mut end = self.index.nodes[id].value.end;
if !self.keeps_trailing_lines(id) {
return end;
}
loop {
let next = line_end(&self.source, end);
if next >= self.source.len() {
break;
}
let line = &self.source[next..line_end(&self.source, next)];
if !line.trim().is_empty() {
break;
}
end = next + line.trim_end_matches(LF).len();
}
end
}
fn dash(&self, id: usize) -> usize {
dash_before(&self.source, self.index.nodes[id].value.start)
}
fn commit_splice(&mut self, from: usize, to: usize, with: &str) -> Result<(), Error> {
if self.source[from..to] == *with {
return Ok(());
}
if let Some(pending) = &mut self.pending {
pending.splices.push(Splice {
from,
to,
with: with.to_owned(),
});
return Ok(());
}
let edited = [&self.source[..from], with, &self.source[to..]].concat();
let index = Index::build(&edited).map_err(|error| Error::Invalid {
message: error.to_string(),
})?;
self.commit(edited, index);
Ok(())
}
fn apply(&mut self, mut splices: Vec<Splice>) -> Result<(), Error> {
if splices.is_empty() {
return Ok(());
}
splices.sort_by_key(|splice| (splice.from, splice.to));
let mut edited = String::with_capacity(self.source.len());
let mut at = 0;
for splice in &splices {
if splice.from < at {
return Err(Error::Unsupported {
path: Path::root(),
what: "apply overlapping edits in one batch",
});
}
edited.push_str(&self.source[at..splice.from]);
edited.push_str(&splice.with);
at = splice.to;
}
edited.push_str(&self.source[at..]);
let index = Index::build(&edited).map_err(|error| Error::Invalid {
message: error.to_string(),
})?;
self.commit(edited, index);
Ok(())
}
fn commit(&mut self, source: String, index: Index) {
self.compact = compact_lists(&source, &index);
self.source = source;
self.index = index;
}
}
impl fmt::Display for Document {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.source)
}
}
fn compact_lists(source: &str, index: &Index) -> bool {
index
.nodes
.iter()
.find(|node| node.style == Style::BlockSequence && node.key.is_some())
.and_then(|node| {
let key = node.key.as_ref()?;
let first = index.nodes.get(*node.children.first()?)?;
let dash = dash_before(source, first.value.start);
Some(column(source, dash) == column(source, key.range.start))
})
.unwrap_or(false)
}
fn blank_run_end(source: &str, mut at: usize) -> usize {
while at < source.len() && source[at..line_end(source, at)].trim().is_empty() {
at = line_end(source, at);
}
at
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::Document;
use crate::Error;
#[test]
fn an_edit_that_would_not_parse_leaves_the_text_unchanged() {
let source = indoc! {"
a: 1
b: 2
"};
let mut document = Document::parse(source).unwrap();
let broken = document.commit_splice(0, 4, "a: [1");
assert!(matches!(broken, Err(Error::Invalid { .. })));
assert_eq!(document.as_str(), source);
}
}