From fc43f5bf399fd6ecf7f9db2f6dfb89c6ec38a383 Mon Sep 17 00:00:00 2001 From: Viktor Kleen Date: Tue, 17 Jan 2023 14:17:03 +0000 Subject: [PATCH 1/2] Add multiline doc logic to the pretty printer --- src/pretty.rs | 30 ++++++++++++------- .../snapshot/inputs/pretty/multiline_doc.ncl | 18 +++++++++++ .../snapshot__pretty_multiline_doc.ncl.snap | 21 +++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 tests/snapshot/inputs/pretty/multiline_doc.ncl create mode 100644 tests/snapshot/snapshots/snapshot__pretty_multiline_doc.ncl.snap diff --git a/src/pretty.rs b/src/pretty.rs index 3753c607e2..1c82794de2 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -60,6 +60,17 @@ where self.text(s) } + fn multiline_string(&'a self, s: &str) -> DocBuilder<'a, Self, A> { + let percent_count = s.split(|c| c != '%').map(|v| v.len()).max().unwrap(); + let delimiter = "%".repeat(percent_count + 1); + self.hardline() + .append(self.intersperse( + s.lines().map(|d| self.text(d.to_owned())), + self.hardline().clone(), + )) + .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 +82,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..5f19030008 --- /dev/null +++ b/tests/snapshot/inputs/pretty/multiline_doc.ncl @@ -0,0 +1,18 @@ +{ + 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, +} From 37335538d76a100e57e975458bf4c426d0ecb1a5 Mon Sep 17 00:00:00 2001 From: Viktor Kleen Date: Tue, 17 Jan 2023 15:07:21 +0000 Subject: [PATCH 2/2] Apply suggestions from code review --- src/pretty.rs | 8 ++---- .../snapshot/inputs/pretty/multiline_doc.ncl | 27 +++++++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/pretty.rs b/src/pretty.rs index 1c82794de2..85d7e9e859 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -61,13 +61,9 @@ where } fn multiline_string(&'a self, s: &str) -> DocBuilder<'a, Self, A> { - let percent_count = s.split(|c| c != '%').map(|v| v.len()).max().unwrap(); - let delimiter = "%".repeat(percent_count + 1); + let delimiter = "%".repeat(min_interpolate_sign(s)); self.hardline() - .append(self.intersperse( - s.lines().map(|d| self.text(d.to_owned())), - self.hardline().clone(), - )) + .append(self.intersperse(s.lines().map(|d| self.text(d.to_owned())), self.hardline())) .enclose(format!("m{delimiter}\""), format!("\"{delimiter}")) } diff --git a/tests/snapshot/inputs/pretty/multiline_doc.ncl b/tests/snapshot/inputs/pretty/multiline_doc.ncl index 5f19030008..96728c70b6 100644 --- a/tests/snapshot/inputs/pretty/multiline_doc.ncl +++ b/tests/snapshot/inputs/pretty/multiline_doc.ncl @@ -1,18 +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} + 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 - ``` - "%% + For example: + ```nickel + ("True" | BoolLiteral) => + "true" + ("hello" | BoolLiteral) => + error + (true | BoolLiteral) => + error + ``` + "%% = 1 }