diff --git a/src/pretty.rs b/src/pretty.rs index 3753c607e2..85d7e9e859 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -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(":") @@ -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()) diff --git a/tests/snapshot/inputs/pretty/multiline_doc.ncl b/tests/snapshot/inputs/pretty/multiline_doc.ncl new file mode 100644 index 0000000000..96728c70b6 --- /dev/null +++ b/tests/snapshot/inputs/pretty/multiline_doc.ncl @@ -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 +} diff --git a/tests/snapshot/snapshots/snapshot__pretty_multiline_doc.ncl.snap b/tests/snapshot/snapshots/snapshot__pretty_multiline_doc.ncl.snap new file mode 100644 index 0000000000..34abf6ab79 --- /dev/null +++ b/tests/snapshot/snapshots/snapshot__pretty_multiline_doc.ncl.snap @@ -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, +}