Skip to content

Commit ff34bae

Browse files
committed
Added the ability to add newlines when formatting arguments
This is squashed by the PR #23 Author: Dylan Owen <[email protected]>
1 parent a45e58e commit ff34bae

7 files changed

+273
-71
lines changed

src/format.rs

+49-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ pub(crate) struct Formatter<'a> {
1818
#[derive(Debug, PartialEq, Clone)]
1919
pub struct Style {
2020
indent: u32,
21+
multiline_arguments: bool,
2122
}
2223

2324
impl Default for Style {
2425
fn default() -> Style {
2526
Style {
2627
indent: 2,
28+
multiline_arguments: false,
2729
}
2830
}
2931
}
@@ -34,6 +36,12 @@ impl Style {
3436
self.indent = indent;
3537
self
3638
}
39+
40+
/// Set whether to add new lines between arguments
41+
pub fn multiline_arguments(&mut self, multiline_arguments: bool) -> &mut Self {
42+
self.multiline_arguments = multiline_arguments;
43+
self
44+
}
3745
}
3846

3947
pub(crate) trait Displayable {
@@ -59,15 +67,44 @@ impl<'a> Formatter<'a> {
5967
self.buf.push('\n');
6068
}
6169

70+
pub fn start_argument_block(&mut self, open_char: char) {
71+
self.buf.push(open_char);
72+
if self.style.multiline_arguments {
73+
self.inc_indent();
74+
}
75+
}
76+
77+
pub fn end_argument_block(&mut self, close_char: char) {
78+
if self.style.multiline_arguments {
79+
self.endline();
80+
self.dec_indent();
81+
self.indent();
82+
}
83+
self.buf.push(close_char);
84+
}
85+
86+
pub fn start_argument(&mut self) {
87+
if self.style.multiline_arguments {
88+
self.endline();
89+
self.indent();
90+
}
91+
}
92+
93+
pub fn deliniate_argument(&mut self) {
94+
self.buf.push(',');
95+
if !self.style.multiline_arguments {
96+
self.buf.push(' ');
97+
}
98+
}
99+
62100
pub fn start_block(&mut self) {
63101
self.buf.push('{');
64102
self.endline();
65-
self.indent += self.style.indent;
103+
self.inc_indent();
66104
}
67105

68106
pub fn end_block(&mut self) {
69-
self.indent = self.indent.checked_sub(self.style.indent)
70-
.expect("negative indent");
107+
self.dec_indent();
71108
self.indent();
72109
self.buf.push('}');
73110
self.endline();
@@ -128,6 +165,15 @@ impl<'a> Formatter<'a> {
128165
self.buf.push_str(r#"""""#);
129166
}
130167
}
168+
169+
fn inc_indent(&mut self) {
170+
self.indent += self.style.indent;
171+
}
172+
173+
fn dec_indent(&mut self) {
174+
self.indent = self.indent.checked_sub(self.style.indent)
175+
.expect("negative indent");
176+
}
131177
}
132178

133179
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)

src/query/format.rs

+41-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::fmt;
22

3-
use crate::format::{Displayable, Formatter, Style, format_directives};
3+
use crate::format::{format_directives, Displayable, Formatter, Style};
44

55
use crate::query::ast::*;
66

77

8-
impl<'a, T: Text<'a>> Document<'a, T>
8+
impl<'a, T: Text<'a>> Document<'a, T>
99
where T: Text<'a>,
1010
{
1111
/// Format a document according to style
@@ -23,7 +23,7 @@ fn to_string<T: Displayable>(v: &T) -> String {
2323
formatter.into_string()
2424
}
2525

26-
impl<'a, T: Text<'a>> Displayable for Document<'a, T>
26+
impl<'a, T: Text<'a>> Displayable for Document<'a, T>
2727
where T: Text<'a>,
2828
{
2929
fn display(&self, f: &mut Formatter) {
@@ -33,7 +33,7 @@ impl<'a, T: Text<'a>> Displayable for Document<'a, T>
3333
}
3434
}
3535

36-
impl<'a, T: Text<'a>> Displayable for Definition<'a, T>
36+
impl<'a, T: Text<'a>> Displayable for Definition<'a, T>
3737
where T: Text<'a>,
3838
{
3939
fn display(&self, f: &mut Formatter) {
@@ -44,7 +44,7 @@ impl<'a, T: Text<'a>> Displayable for Definition<'a, T>
4444
}
4545
}
4646

47-
impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T>
47+
impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T>
4848
where T: Text<'a>,
4949
{
5050
fn display(&self, f: &mut Formatter) {
@@ -57,7 +57,7 @@ impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T>
5757
}
5858
}
5959

60-
impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T>
60+
impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T>
6161
where T: Text<'a>,
6262
{
6363
fn display(&self, f: &mut Formatter) {
@@ -77,7 +77,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T>
7777
}
7878
}
7979

80-
impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T>
80+
impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T>
8181
where T: Text<'a>,
8282
{
8383
fn display(&self, f: &mut Formatter) {
@@ -91,7 +91,7 @@ impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T>
9191
}
9292
}
9393

94-
impl<'a, T: Text<'a>> Displayable for Selection<'a, T>
94+
impl<'a, T: Text<'a>> Displayable for Selection<'a, T>
9595
where T: Text<'a>,
9696
{
9797
fn display(&self, f: &mut Formatter) {
@@ -103,25 +103,27 @@ impl<'a, T: Text<'a>> Displayable for Selection<'a, T>
103103
}
104104
}
105105

106-
fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter)
106+
fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter)
107107
where T: Text<'a>,
108108
{
109109
if !arguments.is_empty() {
110-
f.write("(");
111-
f.write(arguments[0].0.as_ref());
110+
f.start_argument_block('(');
111+
f.start_argument();
112+
f.write(&arguments[0].0.as_ref());
112113
f.write(": ");
113114
arguments[0].1.display(f);
114115
for arg in &arguments[1..] {
115-
f.write(", ");
116-
f.write(arg.0.as_ref());
116+
f.deliniate_argument();
117+
f.start_argument();
118+
f.write(&arg.0.as_ref());
117119
f.write(": ");
118120
arg.1.display(f);
119121
}
120-
f.write(")");
122+
f.end_argument_block(')');
121123
}
122124
}
123125

124-
impl<'a, T: Text<'a>> Displayable for Field<'a, T>
126+
impl<'a, T: Text<'a>> Displayable for Field<'a, T>
125127
where T: Text<'a>,
126128
{
127129
fn display(&self, f: &mut Formatter) {
@@ -146,7 +148,7 @@ impl<'a, T: Text<'a>> Displayable for Field<'a, T>
146148
}
147149
}
148150

149-
impl<'a, T: Text<'a>> Displayable for Query<'a, T>
151+
impl<'a, T: Text<'a>> Displayable for Query<'a, T>
150152
where T: Text<'a>,
151153
{
152154
fn display(&self, f: &mut Formatter) {
@@ -176,7 +178,7 @@ impl<'a, T: Text<'a>> Displayable for Query<'a, T>
176178
}
177179
}
178180

179-
impl<'a, T: Text<'a>> Displayable for Mutation<'a, T>
181+
impl<'a, T: Text<'a>> Displayable for Mutation<'a, T>
180182
where T: Text<'a>,
181183
{
182184
fn display(&self, f: &mut Formatter) {
@@ -206,7 +208,7 @@ impl<'a, T: Text<'a>> Displayable for Mutation<'a, T>
206208
}
207209
}
208210

209-
impl<'a, T: Text<'a>> Displayable for Subscription<'a, T>
211+
impl<'a, T: Text<'a>> Displayable for Subscription<'a, T>
210212
where T: Text<'a>,
211213
{
212214
fn display(&self, f: &mut Formatter) {
@@ -234,7 +236,7 @@ impl<'a, T: Text<'a>> Displayable for Subscription<'a, T>
234236
}
235237
}
236238

237-
impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T>
239+
impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T>
238240
where T: Text<'a>,
239241
{
240242
fn display(&self, f: &mut Formatter) {
@@ -249,7 +251,7 @@ impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T>
249251
}
250252
}
251253

252-
impl<'a, T: Text<'a>> Displayable for Type<'a, T>
254+
impl<'a, T: Text<'a>> Displayable for Type<'a, T>
253255
where T: Text<'a>,
254256
{
255257
fn display(&self, f: &mut Formatter) {
@@ -268,12 +270,15 @@ impl<'a, T: Text<'a>> Displayable for Type<'a, T>
268270
}
269271
}
270272

271-
impl<'a, T: Text<'a>> Displayable for Value<'a, T>
273+
impl<'a, T: Text<'a>> Displayable for Value<'a, T>
272274
where T: Text<'a>,
273275
{
274276
fn display(&self, f: &mut Formatter) {
275277
match *self {
276-
Value::Variable(ref name) => { f.write("$"); f.write(name.as_ref()); },
278+
Value::Variable(ref name) => {
279+
f.write("$");
280+
f.write(name.as_ref());
281+
}
277282
Value::Int(ref num) => f.write(&format!("{}", num.0)),
278283
Value::Float(val) => f.write(&format!("{}", val)),
279284
Value::String(ref val) => f.write_quoted(val),
@@ -282,36 +287,39 @@ impl<'a, T: Text<'a>> Displayable for Value<'a, T>
282287
Value::Null => f.write("null"),
283288
Value::Enum(ref name) => f.write(name.as_ref()),
284289
Value::List(ref items) => {
285-
f.write("[");
290+
f.start_argument_block('[');
286291
if !items.is_empty() {
292+
f.start_argument();
287293
items[0].display(f);
288294
for item in &items[1..] {
289-
f.write(", ");
295+
f.deliniate_argument();
296+
f.start_argument();
290297
item.display(f);
291298
}
292299
}
293-
f.write("]");
300+
f.end_argument_block(']');
294301
}
295302
Value::Object(ref items) => {
296-
f.write("{");
303+
f.start_argument_block('{');
297304
let mut first = true;
298305
for (name, value) in items.iter() {
299306
if first {
300307
first = false;
301308
} else {
302-
f.write(", ");
309+
f.deliniate_argument();
303310
}
311+
f.start_argument();
304312
f.write(name.as_ref());
305313
f.write(": ");
306314
value.display(f);
307315
}
308-
f.write("}");
316+
f.end_argument_block('}');
309317
}
310318
}
311319
}
312320
}
313321

314-
impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T>
322+
impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T>
315323
where T: Text<'a>,
316324
{
317325
fn display(&self, f: &mut Formatter) {
@@ -331,7 +339,7 @@ impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T>
331339
}
332340
}
333341

334-
impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T>
342+
impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T>
335343
where T: Text<'a>,
336344
{
337345
fn display(&self, f: &mut Formatter) {
@@ -344,7 +352,7 @@ impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T>
344352
}
345353
}
346354

347-
impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T>
355+
impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T>
348356
where T: Text<'a>,
349357
{
350358
fn display(&self, f: &mut Formatter) {
@@ -356,7 +364,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T>
356364
}
357365
}
358366

359-
impl<'a, T: Text<'a>> Displayable for Directive<'a, T>
367+
impl<'a, T: Text<'a>> Displayable for Directive<'a, T>
360368
where T: Text<'a>,
361369
{
362370
fn display(&self, f: &mut Formatter) {
@@ -366,9 +374,8 @@ impl<'a, T: Text<'a>> Displayable for Directive<'a, T>
366374
}
367375
}
368376

369-
370377
impl_display!(
371-
'a
378+
'a
372379
Document,
373380
Definition,
374381
OperationDefinition,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
node @dir(
3+
a: 1,
4+
b: "2",
5+
c: true,
6+
d: false,
7+
e: null
8+
)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
node(
3+
id: 1
4+
)
5+
node(
6+
id: 1,
7+
one: 3
8+
)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
node(
3+
id: [
4+
5,
5+
6,
6+
7
7+
]
8+
)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
node(
3+
id: 1,
4+
obj: {
5+
key1: 123,
6+
key2: 456
7+
}
8+
)
9+
}

0 commit comments

Comments
 (0)