From d944b06c1d2559a553a0cb1cd05f5b7233401eaa Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 21 Oct 2024 15:01:56 +0800 Subject: [PATCH] fix: fix indent of multi-line string (fix #2) --- pretty_graphql/src/printer.rs | 32 +++++++++---------- .../tests/fmt/kitchen-sink/kitchen-sink.snap | 2 +- .../tests/fmt/string/string.graphql | 7 ++++ pretty_graphql/tests/fmt/string/string.snap | 13 ++++++-- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/pretty_graphql/src/printer.rs b/pretty_graphql/src/printer.rs index f2e7b88..8b4ec21 100644 --- a/pretty_graphql/src/printer.rs +++ b/pretty_graphql/src/printer.rs @@ -1598,7 +1598,7 @@ impl DocGen for StringValue { .filter(|s| s.contains(['\n', '\r'])) { Doc::text("\"\"\"") - .concat(reflow_with_indent(s)) + .concat(reflow_with_indent(s).into_iter()) .append(Doc::text("\"\"\"")) } else { Doc::text(s) @@ -2416,7 +2416,7 @@ fn reflow(text: &str, docs: &mut Vec>) { } } -fn reflow_with_indent(s: &str) -> impl Iterator> + '_ { +fn reflow_with_indent(s: &str) -> Vec> { let indent = s .lines() .skip(if s.starts_with([' ', '\t']) { 0 } else { 1 }) @@ -2429,25 +2429,25 @@ fn reflow_with_indent(s: &str) -> impl Iterator> + '_ { }) .min() .unwrap_or_default(); - s.split('\n').enumerate().flat_map(move |(i, s)| { - let s = s.strip_suffix('\r').unwrap_or(s); - let s = if s.starts_with([' ', '\t']) { + let mut docs = Vec::with_capacity(2); + let mut lines = s.split('\n').enumerate().peekable(); + while let Some((i, line)) = lines.next() { + let s = line.strip_suffix('\r').unwrap_or(line); + let s = if s.starts_with([' ', '\t']) && i > 0 { s.get(indent..).unwrap_or(s) } else { s }; - [ - if i == 0 { - Doc::nil() - } else if s.trim().is_empty() { - Doc::empty_line() + if i > 0 { + if s.trim().is_empty() && lines.peek().is_some() { + docs.push(Doc::empty_line()); } else { - Doc::hard_line() - }, - Doc::text(s.to_owned()), - ] - .into_iter() - }) + docs.push(Doc::hard_line()); + } + } + docs.push(Doc::text(s.to_owned())); + } + docs } fn should_ignore(node: &SyntaxNode, ctx: &Ctx) -> bool { diff --git a/pretty_graphql/tests/fmt/kitchen-sink/kitchen-sink.snap b/pretty_graphql/tests/fmt/kitchen-sink/kitchen-sink.snap index 531a563..84e1caa 100644 --- a/pretty_graphql/tests/fmt/kitchen-sink/kitchen-sink.snap +++ b/pretty_graphql/tests/fmt/kitchen-sink/kitchen-sink.snap @@ -56,7 +56,7 @@ fragment frag on Friend { key: "value" block: """ block string uses \""" - """ + """ } ) } diff --git a/pretty_graphql/tests/fmt/string/string.graphql b/pretty_graphql/tests/fmt/string/string.graphql index d6b8ceb..0bd9b75 100644 --- a/pretty_graphql/tests/fmt/string/string.graphql +++ b/pretty_graphql/tests/fmt/string/string.graphql @@ -58,3 +58,10 @@ one: string { foo(input: {multiline: """ """}) { id } } + +directive @constraint( + """ + min length + """ + minLength: Int +) on INPUT_FIELD_DEFINITION diff --git a/pretty_graphql/tests/fmt/string/string.snap b/pretty_graphql/tests/fmt/string/string.snap index bc78419..fc49db0 100644 --- a/pretty_graphql/tests/fmt/string/string.snap +++ b/pretty_graphql/tests/fmt/string/string.snap @@ -30,7 +30,7 @@ type Foo { """ This is a description of the `one` field. -""" + """ one: Type } @@ -49,14 +49,14 @@ type Foo { enum Enum { """ Description of `one` -""" + """ one } input Input { """ Description of `one` -""" + """ one: string } @@ -77,3 +77,10 @@ input Input { id } } + +directive @constraint( + """ + min length + """ + minLength: Int +) on INPUT_FIELD_DEFINITION