Skip to content

Commit

Permalink
feat(doc): Don't display the full path to types in the documentation
Browse files Browse the repository at this point in the history
`std.option.Option => Option`
  • Loading branch information
Marwes committed Mar 25, 2019
1 parent 494675f commit a669faa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 4 additions & 1 deletion base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,10 @@ pub trait TypeExt: Deref<Target = Type<<Self as TypeExt>::Id, Self>> + Clone + S
top(self).pretty(&Printer::new(arena, &()))
}

fn display<A>(&self, width: usize) -> TypeFormatter<Self::Id, Self, A> {
fn display<A>(&self, width: usize) -> TypeFormatter<Self::Id, Self, A>
where
Self::Id: AsRef<str>,
{
TypeFormatter::new(self).width(width)
}

Expand Down
28 changes: 21 additions & 7 deletions base/src/types/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,22 @@ where
width: usize,
typ: &'a T,
filter: &'a Fn(&I) -> Filter,
symbol_text: &'a Fn(&I) -> &str,
annotate_symbol: &'a Fn(&I) -> Option<A>,
_marker: PhantomData<I>,
}

impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
impl<'a, I, T, A> TypeFormatter<'a, I, T, A>
where
I: AsRef<str>,
{
pub fn new(typ: &'a T) -> Self {
TypeFormatter {
width: 80,
typ,
filter: &|_| Filter::Retain,
annotate_symbol: &|_| None,
symbol_text: &|s: &I| s.as_ref(),
_marker: PhantomData,
}
}
Expand All @@ -117,6 +122,11 @@ impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
self
}

pub fn symbol_text(mut self, symbol_text: &'a Fn(&I) -> &str) -> Self {
self.symbol_text = symbol_text;
self
}

pub fn pretty(&self, arena: &'a Arena<'a, A>) -> DocBuilder<'a, Arena<'a, A>, A>
where
T: Deref<Target = Type<I, T>> + HasSpan + Commented + 'a,
Expand All @@ -128,6 +138,7 @@ impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
arena,
source: &(),
filter: self.filter,
symbol_text: self.symbol_text,
annotate_symbol: self.annotate_symbol,
})
}
Expand All @@ -137,6 +148,7 @@ impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
arena,
source,
filter: self.filter,
symbol_text: self.symbol_text,
annotate_symbol: self.annotate_symbol,
}
}
Expand Down Expand Up @@ -166,15 +178,20 @@ pub struct Printer<'a, I: 'a, A: 'a> {
pub arena: &'a Arena<'a, A>,
pub source: &'a Source,
filter: &'a Fn(&I) -> Filter,
symbol_text: &'a Fn(&I) -> &str,
annotate_symbol: &'a Fn(&I) -> Option<A>,
}

impl<'a, I, A> Printer<'a, I, A> {
pub fn new(arena: &'a Arena<'a, A>, source: &'a Source) -> Printer<'a, I, A> {
pub fn new(arena: &'a Arena<'a, A>, source: &'a Source) -> Printer<'a, I, A>
where
I: AsRef<str>,
{
Printer {
arena,
source,
filter: &|_| Filter::Retain,
symbol_text: &|s: &I| s.as_ref(),
annotate_symbol: &|_| None,
}
}
Expand All @@ -183,11 +200,8 @@ impl<'a, I, A> Printer<'a, I, A> {
(self.filter)(field)
}

pub fn symbol(&self, symbol: &'a I) -> DocBuilder<'a, Arena<'a, A>, A>
where
I: AsRef<str>,
{
self.symbol_with(symbol, symbol.as_ref())
pub fn symbol(&self, symbol: &'a I) -> DocBuilder<'a, Arena<'a, A>, A> {
self.symbol_with(symbol, (self.symbol_text)(symbol))
}

pub fn symbol_with(&self, symbol: &'a I, text: &'a str) -> DocBuilder<'a, Arena<'a, A>, A> {
Expand Down
1 change: 1 addition & 0 deletions doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn print_type(current_module: &str, typ: &ArcType) -> String {
let mut doc = typ
.display(80)
.annotate_symbol(&annotate_symbol)
.symbol_text(&|s: &Symbol| s.declared_name())
.pretty(&arena);
match **typ {
Type::Record(_) => (),
Expand Down

0 comments on commit a669faa

Please sign in to comment.