From 86704432f167ce9fea0a461d4de0e26bcd394c87 Mon Sep 17 00:00:00 2001 From: gabotechs Date: Mon, 18 Mar 2024 19:34:45 +0100 Subject: [PATCH] support comments in args --- graphqxl_synthesizer/src/synth_arguments.rs | 20 +++++++++++++++---- .../src/synths/synth_context.rs | 6 ++++++ src/test/comments-in-args.graphqxl | 14 +++++++++++++ src/test/comments-in-args.graphqxl.result | 15 ++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/test/comments-in-args.graphqxl create mode 100644 src/test/comments-in-args.graphqxl.result diff --git a/graphqxl_synthesizer/src/synth_arguments.rs b/graphqxl_synthesizer/src/synth_arguments.rs index ef355d7..845f74b 100644 --- a/graphqxl_synthesizer/src/synth_arguments.rs +++ b/graphqxl_synthesizer/src/synth_arguments.rs @@ -1,9 +1,10 @@ +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}; @@ -11,7 +12,8 @@ pub(crate) struct ArgumentsSynth(pub(crate) Vec); 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> = self .0 .iter() .map(|argument| { @@ -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 + } }) .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); diff --git a/graphqxl_synthesizer/src/synths/synth_context.rs b/graphqxl_synthesizer/src/synths/synth_context.rs index ddf5119..b952eec 100644 --- a/graphqxl_synthesizer/src/synths/synth_context.rs +++ b/graphqxl_synthesizer/src/synths/synth_context.rs @@ -94,6 +94,12 @@ pub(crate) trait Synth { } } +impl Synth for Box { + fn synth(&self, context: &mut SynthContext) -> bool { + self.as_ref().synth(context) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/test/comments-in-args.graphqxl b/src/test/comments-in-args.graphqxl new file mode 100644 index 0000000..c0c1f72 --- /dev/null +++ b/src/test/comments-in-args.graphqxl @@ -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!]! +} diff --git a/src/test/comments-in-args.graphqxl.result b/src/test/comments-in-args.graphqxl.result new file mode 100644 index 0000000..5963c27 --- /dev/null +++ b/src/test/comments-in-args.graphqxl.result @@ -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!]! +} +