Skip to content

Commit

Permalink
support comments in args
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Mar 18, 2024
1 parent 0281b74 commit 125fab8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
20 changes: 16 additions & 4 deletions graphqxl_synthesizer/src/synth_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::synth_description::DescriptionSynth;
use crate::synth_directive::DirectiveSynth;
use crate::synth_identifier::IdentifierSynth;
use crate::synth_value_data::ValueDataSynth;
use crate::synth_value_type::ValueTypeSynth;
use crate::synths::{
ChainSynth, MultilineListSynth, OneLineListSynth, StringSynth, Synth, SynthContext,
ChainSynth, MultilineListSynth, OneLineListSynth, PairSynth, StringSynth, Synth, SynthContext,
};
use graphqxl_parser::{Argument, ArgumentDefaultValue};

pub(crate) struct ArgumentsSynth(pub(crate) Vec<Argument>);

impl Synth for ArgumentsSynth {
fn synth(&self, context: &mut SynthContext) -> bool {
let inner_synths = self
let mut at_least_one_description = false;
let inner_synths: Vec<Box<dyn Synth>> = self
.0
.iter()
.map(|argument| {
Expand All @@ -31,11 +33,21 @@ impl Synth for ArgumentsSynth {
v.push(Box::new(StringSynth::from(" ")));
v.push(Box::new(DirectiveSynth(directive.clone())));
}
ChainSynth(v)

if !argument.description.is_empty() {
at_least_one_description = true;
Box::new(PairSynth {
first: DescriptionSynth::text(&argument.description),
last: ChainSynth(v),
line_jump_sep: true,
})
} else {
Box::new(ChainSynth(v)) as Box<dyn Synth>
}
})
.collect();

if self.0.len() > context.config.max_one_line_args {
if self.0.len() > context.config.max_one_line_args || at_least_one_description {
MultilineListSynth::no_suffix(("(", inner_synths, ")")).synth(context);
} else {
OneLineListSynth::comma(("(", inner_synths, ")")).synth(context);
Expand Down
6 changes: 6 additions & 0 deletions graphqxl_synthesizer/src/synths/synth_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub(crate) trait Synth {
}
}

impl Synth for Box<dyn Synth> {
fn synth(&self, context: &mut SynthContext) -> bool {
self.as_ref().synth(context)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 14 additions & 0 deletions src/test/comments-in-args.graphqxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Query {
"Comment on foo"
foo(
"Comment on bar"
bar: String
baz: Int
"""
multiline comment
bar_baz
"""
bar_baz: Boolean
): [String!]!
}
15 changes: 15 additions & 0 deletions src/test/comments-in-args.graphqxl.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Query {
"Comment on foo"
foo(
"Comment on bar"
bar: String
baz: Int
"""
multiline comment

bar_baz
"""
bar_baz: Boolean
): [String!]!
}

0 comments on commit 125fab8

Please sign in to comment.