Skip to content

Commit

Permalink
fix: fix indent of multi-line string (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Oct 21, 2024
1 parent 9e9b36c commit d944b06
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
32 changes: 16 additions & 16 deletions pretty_graphql/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -2416,7 +2416,7 @@ fn reflow(text: &str, docs: &mut Vec<Doc<'static>>) {
}
}

fn reflow_with_indent(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
fn reflow_with_indent(s: &str) -> Vec<Doc<'static>> {
let indent = s
.lines()
.skip(if s.starts_with([' ', '\t']) { 0 } else { 1 })
Expand All @@ -2429,25 +2429,25 @@ fn reflow_with_indent(s: &str) -> impl Iterator<Item = Doc<'static>> + '_ {
})
.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 {
Expand Down
2 changes: 1 addition & 1 deletion pretty_graphql/tests/fmt/kitchen-sink/kitchen-sink.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fragment frag on Friend {
key: "value"
block: """
block string uses \"""
"""
"""
}
)
}
Expand Down
7 changes: 7 additions & 0 deletions pretty_graphql/tests/fmt/string/string.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ one: string
{
foo(input: {multiline: """ """}) { id }
}

directive @constraint(
"""
min length
"""
minLength: Int
) on INPUT_FIELD_DEFINITION
13 changes: 10 additions & 3 deletions pretty_graphql/tests/fmt/string/string.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Foo {
"""
This is a description
of the `one` field.
"""
"""
one: Type
}

Expand All @@ -49,14 +49,14 @@ type Foo {
enum Enum {
"""
Description of `one`
"""
"""
one
}

input Input {
"""
Description of `one`
"""
"""
one: string
}

Expand All @@ -77,3 +77,10 @@ input Input {
id
}
}

directive @constraint(
"""
min length
"""
minLength: Int
) on INPUT_FIELD_DEFINITION

0 comments on commit d944b06

Please sign in to comment.