Skip to content

Commit

Permalink
Merge pull request #1048 from tweag/pretty-printer-doc-metadata
Browse files Browse the repository at this point in the history
Add multiline doc logic to the pretty printer
  • Loading branch information
vkleen authored Jan 17, 2023
2 parents 04daecc + 3733553 commit 9453ad6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ where
self.text(s)
}

fn multiline_string(&'a self, s: &str) -> DocBuilder<'a, Self, A> {
let delimiter = "%".repeat(min_interpolate_sign(s));
self.hardline()
.append(self.intersperse(s.lines().map(|d| self.text(d.to_owned())), self.hardline()))
.enclose(format!("m{delimiter}\""), format!("\"{delimiter}"))
}

fn metadata(&'a self, mv: &MetaValue, with_doc: bool) -> DocBuilder<'a, Self, A> {
if let Some(types) = &mv.types {
self.text(":")
Expand All @@ -71,20 +78,19 @@ where
}
.append(if with_doc {
mv.doc
.clone()
.as_ref()
.map(|doc| {
self.text("|")
.append(self.space())
.append(self.text("doc"))
.append(self.space())
.append(
self.hardline()
.append(self.intersperse(
doc.lines().map(|d| self.escaped_string(d)),
self.hardline().clone(),
))
.double_quotes(),
)
.append(self.hardline())
.append({
if doc.contains('\n') {
self.multiline_string(doc)
} else {
self.escaped_string(doc).double_quotes()
}
})
.append(self.line())
})
.unwrap_or_else(|| self.nil())
Expand Down
17 changes: 17 additions & 0 deletions tests/snapshot/inputs/pretty/multiline_doc.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
field | doc m%%"
Contract to enforce the value is a string that represents a boolean literal. Additionally casts "True" to "true"
and "False" to "false". This shouldn't interpolate: %{null}

For example:
```nickel
("True" | BoolLiteral) =>
"true"
("hello" | BoolLiteral) =>
error
(true | BoolLiteral) =>
error
```
"%%
= 1
}
21 changes: 21 additions & 0 deletions tests/snapshot/snapshots/snapshot__pretty_multiline_doc.ncl.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: tests/snapshot/main.rs
expression: snapshot
---
{
field | doc
m%%"
Contract to enforce the value is a string that represents a boolean literal. Additionally casts "True" to "true"
and "False" to "false". This shouldn't interpolate: %{null}

For example:
```nickel
("True" | BoolLiteral) =>
"true"
("hello" | BoolLiteral) =>
error
(true | BoolLiteral) =>
error
```"%%
= 1,
}

0 comments on commit 9453ad6

Please sign in to comment.