-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: blank lines between things with docstrings (#954)
The cynic-parser pretty printing wasn't putting blank lines before fields & arguments that had docstrings, which made the output quite visually noisy. This implements logic to do this, resulting in much nicer output for this case.
- Loading branch information
Showing
5 changed files
with
8,431 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
cynic-parser/src/printing/type_system/argument_sequence.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::iter::Enumerate; | ||
|
||
use pretty::{DocAllocator, Pretty}; | ||
|
||
use super::{comma_or_newline, Allocator, InputValueDefinition, NodeDisplay}; | ||
|
||
/// A sequence of things with docstrings attached. | ||
/// | ||
/// This will print each entry with a prefix of: | ||
/// - first entry gets no prefix | ||
/// - when no docstring: just a single hardline prefix | ||
/// - when a docstring: two hardline prefix | ||
/// | ||
/// This is used for displaying fields (both input fields & output fields) | ||
/// but arguments should use ArgumentSequence | ||
pub struct ArgumentSequence<'a> { | ||
iterator: Enumerate<crate::type_system::iter::Iter<'a, InputValueDefinition<'a>>>, | ||
} | ||
|
||
impl<'a> ArgumentSequence<'a> { | ||
pub fn new(iterator: crate::type_system::iter::Iter<'a, InputValueDefinition<'a>>) -> Self { | ||
ArgumentSequence { | ||
iterator: iterator.enumerate(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Pretty<'a, Allocator<'a>> for ArgumentSequence<'a> { | ||
fn pretty(self, allocator: &'a Allocator<'a>) -> pretty::DocBuilder<'a, Allocator<'a>, ()> { | ||
let mut document = allocator.nil(); | ||
for (index, item) in self.iterator { | ||
if index != 0 { | ||
if item.description().is_some() { | ||
// We use a hardcoded `\n` for the first newline here because | ||
// pretty always adds an indent on line but we want this line blank | ||
document = document.append(allocator.text("\n").flat_alt(allocator.text(", "))); | ||
document = document.append(allocator.hardline()); | ||
} else { | ||
document = document.append(allocator.line().flat_alt(allocator.text(", "))); | ||
} | ||
} | ||
document = document.append(NodeDisplay(item)); | ||
} | ||
document | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::iter::Enumerate; | ||
|
||
use pretty::{DocAllocator, Pretty}; | ||
|
||
use crate::common::IdOperations; | ||
|
||
use super::{ | ||
iter::IdReader, Allocator, EnumValueDefinition, FieldDefinition, InputValueDefinition, | ||
NodeDisplay, TypeSystemId, | ||
}; | ||
|
||
/// A sequence of things with docstrings attached. | ||
/// | ||
/// This will print each entry with a prefix of: | ||
/// - first entry gets no prefix | ||
/// - when no docstring: just a single hardline prefix | ||
/// - when a docstring: two hardline prefix | ||
/// | ||
/// This is used for displaying fields (both input fields & output fields) | ||
/// but arguments should use ArgumentSequence | ||
pub struct FieldSequence<'a, T> | ||
where | ||
T: IdReader, | ||
{ | ||
iterator: Enumerate<crate::type_system::iter::Iter<'a, T>>, | ||
} | ||
|
||
impl<'a, T> FieldSequence<'a, T> | ||
where | ||
T: IdReader, | ||
T::Id: IdOperations, | ||
{ | ||
pub fn new(iterator: crate::type_system::iter::Iter<'a, T>) -> Self { | ||
FieldSequence { | ||
iterator: iterator.enumerate(), | ||
} | ||
} | ||
} | ||
|
||
trait DocstringedItem { | ||
fn has_docstring(&self) -> bool; | ||
} | ||
|
||
impl DocstringedItem for FieldDefinition<'_> { | ||
fn has_docstring(&self) -> bool { | ||
self.description().is_some() | ||
} | ||
} | ||
|
||
impl DocstringedItem for InputValueDefinition<'_> { | ||
fn has_docstring(&self) -> bool { | ||
self.description().is_some() | ||
} | ||
} | ||
|
||
impl DocstringedItem for EnumValueDefinition<'_> { | ||
fn has_docstring(&self) -> bool { | ||
self.description().is_some() | ||
} | ||
} | ||
|
||
impl<'a, T> Pretty<'a, Allocator<'a>> for FieldSequence<'a, T> | ||
where | ||
T: DocstringedItem + IdReader, | ||
T::Id: IdOperations, | ||
// This is a bit much :/ | ||
<<T as IdReader>::Id as TypeSystemId>::Reader<'a>: DocstringedItem, | ||
NodeDisplay<<<T as IdReader>::Id as TypeSystemId>::Reader<'a>>: Pretty<'a, Allocator<'a>>, | ||
{ | ||
fn pretty(self, allocator: &'a Allocator<'a>) -> pretty::DocBuilder<'a, Allocator<'a>, ()> { | ||
let mut document = allocator.nil(); | ||
for (index, item) in self.iterator { | ||
if index != 0 { | ||
if item.has_docstring() { | ||
// We use a hardcoded `\n` for the first newline here because | ||
// pretty always adds an indent but we want this line blank | ||
document = document | ||
.append(allocator.text("\n")) | ||
.append(allocator.hardline()); | ||
} else { | ||
document = document.append(allocator.hardline()); | ||
} | ||
} | ||
document = document.append(NodeDisplay(item)); | ||
} | ||
document | ||
} | ||
} |
Oops, something went wrong.