Skip to content

Commit

Permalink
refactor(fmt): drop itertools
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 5, 2024
1 parent 2e9e57e commit 34312cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pretty_yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ keywords = ["yaml", "formatter"]
exclude = ["/tests"]

[dependencies]
itertools = "0.12"
rowan = "0.15"
serde = { version = "1.0", features = ["derive"], optional = true }
tiny_pretty = "0.1"
Expand Down
22 changes: 14 additions & 8 deletions pretty_yaml/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ impl DocGen for BlockScalar {
.children_with_tokens()
.any(|element| element.kind() == SyntaxKind::INDENT_INDICATOR)
{
return Doc::list(reflow(token.text()).collect());
let mut docs = Vec::with_capacity(2);
reflow(token.text(), &mut docs);
return Doc::list(docs);
}
let space_len = text.find(|c: char| !c.is_ascii_whitespace()).map(
|first_contentful| {
Expand Down Expand Up @@ -238,7 +240,7 @@ impl DocGen for Document {
match element {
SyntaxElement::Node(node) => {
if should_ignore(&node, ctx) {
docs.extend(reflow(&node.to_string()));
reflow(&node.to_string(), &mut docs);
} else {
match node.kind() {
SyntaxKind::BLOCK => {
Expand Down Expand Up @@ -1000,7 +1002,7 @@ where
match element {
SyntaxElement::Node(node) => {
if should_ignore(&node, ctx) {
docs.extend(reflow(&node.to_string()));
reflow(&node.to_string(), &mut docs);
} else if let Some(item) = Item::cast(node) {
docs.push(item.doc(ctx));
}
Expand Down Expand Up @@ -1210,11 +1212,15 @@ fn intersperse_lines(docs: &mut Vec<Doc<'static>>, mut lines: impl Iterator<Item
}
}

fn reflow(text: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
itertools::intersperse(
text.lines().map(|s| Doc::text(s.to_owned())),
Doc::empty_line(),
)
fn reflow(text: &str, docs: &mut Vec<Doc<'static>>) {
let mut lines = text.lines();
if let Some(line) = lines.next() {
docs.push(Doc::text(line.to_owned()));
}
for line in lines {
docs.push(Doc::empty_line());
docs.push(Doc::text(line.to_owned()));
}
}

fn should_ignore(node: &SyntaxNode, ctx: &Ctx) -> bool {
Expand Down

0 comments on commit 34312cd

Please sign in to comment.