Skip to content

Commit

Permalink
feat(doc): Make all types link to their definition
Browse files Browse the repository at this point in the history
Closes #552
  • Loading branch information
Marwes committed Jul 6, 2018
1 parent d227c26 commit 34fff8d
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 51 deletions.
20 changes: 8 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ impl<Id> ArcType<Id> {
top(self).pretty(&Printer::new(arena, &()))
}

pub fn display(&self, width: usize) -> TypeFormatter<Id, Self> {
pub fn display<A>(&self, width: usize) -> TypeFormatter<Id, Self, A> {
TypeFormatter::new(self).width(width)
}
}
Expand Down Expand Up @@ -1907,8 +1907,8 @@ where
// This should not be displayed normally as it should only exist in `ExtendRow`
// which handles `EmptyRow` explicitly
Type::EmptyRow => arena.text("EmptyRow"),
Type::Ident(ref id) => arena.text(id.as_ref()),
Type::Alias(ref alias) => arena.text(alias.name.as_ref()),
Type::Ident(ref id) => printer.symbol(id),
Type::Alias(ref alias) => printer.symbol(&alias.name),
};
match **typ {
Type::App(..) | Type::ExtendRow { .. } | Type::Variant(..) | Type::Function(..) => doc,
Expand Down
35 changes: 29 additions & 6 deletions base/src/types/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,32 @@ impl From<bool> for Filter {
}
}

pub struct TypeFormatter<'a, I, T>
pub struct TypeFormatter<'a, I, T, A>
where
I: 'a,
T: 'a,
A: 'a,
{
width: usize,
typ: &'a T,
filter: &'a Fn(&I) -> Filter,
annotate_symbol: &'a Fn(&I) -> Option<A>,
_marker: PhantomData<I>,
}

impl<'a, I, T> TypeFormatter<'a, I, T> {
impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
pub fn new(typ: &'a T) -> Self {
TypeFormatter {
width: 80,
typ: typ,
filter: &|_| Filter::Retain,
annotate_symbol: &|_| None,
_marker: PhantomData,
}
}
}

impl<'a, I, T> TypeFormatter<'a, I, T> {
impl<'a, I, T, A> TypeFormatter<'a, I, T, A> {
pub fn width(mut self, width: usize) -> Self {
self.width = width;
self
Expand All @@ -109,7 +112,12 @@ impl<'a, I, T> TypeFormatter<'a, I, T> {
self
}

pub fn pretty<A>(&self, arena: &'a Arena<'a, A>) -> DocBuilder<'a, Arena<'a, A>, A>
pub fn annotate_symbol(mut self, annotate_symbol: &'a Fn(&I) -> Option<A>) -> Self {
self.annotate_symbol = annotate_symbol;
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,
I: AsRef<str>,
Expand All @@ -120,19 +128,21 @@ impl<'a, I, T> TypeFormatter<'a, I, T> {
arena,
source: &(),
filter: self.filter,
annotate_symbol: self.annotate_symbol,
})
}

pub fn build<A>(&self, arena: &'a Arena<'a, A>, source: &'a Source) -> Printer<'a, I, A> {
pub fn build(&self, arena: &'a Arena<'a, A>, source: &'a Source) -> Printer<'a, I, A> {
Printer {
arena,
source,
filter: self.filter,
annotate_symbol: self.annotate_symbol,
}
}
}

impl<'a, I, T> fmt::Display for TypeFormatter<'a, I, T>
impl<'a, I, T> fmt::Display for TypeFormatter<'a, I, T, ()>
where
T: Deref<Target = Type<I, T>> + HasSpan + Commented + 'a,
I: AsRef<str>,
Expand All @@ -156,6 +166,7 @@ pub struct Printer<'a, I: 'a, A: 'a> {
pub arena: &'a Arena<'a, A>,
pub source: &'a Source,
filter: &'a Fn(&I) -> Filter,
annotate_symbol: &'a Fn(&I) -> Option<A>,
}

impl<'a, I, A> Printer<'a, I, A> {
Expand All @@ -164,13 +175,25 @@ impl<'a, I, A> Printer<'a, I, A> {
arena,
source,
filter: &|_| Filter::Retain,
annotate_symbol: &|_| None,
}
}

pub fn filter(&self, field: &I) -> Filter {
(self.filter)(field)
}

pub fn symbol(&self, symbol: &'a I) -> DocBuilder<'a, Arena<'a, A>, A>
where
I: AsRef<str>,
{
let doc = self.arena.text(symbol.as_ref());
match (self.annotate_symbol)(symbol) {
Some(ann) => doc.annotate(ann),
None => doc,
}
}

pub fn space_before(&self, pos: BytePos) -> DocBuilder<'a, Arena<'a, A>, A> {
let (doc, comments) = self.comments_before_(pos);
if let Doc::Nil = doc.1 {
Expand Down
2 changes: 1 addition & 1 deletion base/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn break_record() {
),
],
);
let arena = Arena::new();
let arena = Arena::<()>::new();
let source = &();
let printer = pretty_print::Printer::new(&arena, source);
let typ = arena
Expand Down
2 changes: 1 addition & 1 deletion doc/src/doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2>Modules</h2>
{{#each modules}}
<tr>
<td>
<a href="{{symbol_link name}}">{{name}}</a>
<a href="{{module_link name}}">{{name}}</a>
</td>
<td valign="middle">
{{markdown_first_paragraph comment}}
Expand Down
6 changes: 3 additions & 3 deletions doc/src/doc/module.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<ul class="nav flex-column">
{{#each sibling_modules}}
<li class="nav-item">
<a class="nav-link active" href="{{symbol_link this}}">{{this}}</a>
<a class="nav-link active" href="{{module_link this}}">{{this}}</a>
</li>
{{/each}}
</ul>
Expand All @@ -36,7 +36,7 @@ <h2 class="pb-3 mb-4 border-bottom"><a class="anchor field" id="Types" href="#Ty
{{#each record.types}}

<h4>
<pre>type <a class="anchor field" id="type.{{name}}" href="#type.{{name}}">{{name}}</a>{{#each args}} {{name}}{{/each}} = {{type~}}
<pre>type <a class="anchor field" id="type.{{name}}" href="#type.{{name}}">{{name}}</a>{{#each args}} {{name}}{{/each}} = {{{type~}}}
</pre>
</h4>
<div class="docblock">{{markdown comment}}</div>
Expand All @@ -52,7 +52,7 @@ <h4>
<pre>let <a class="anchor field" id="value.{{name}}" href="#value.{{name}}">{{name}}</a>
{{~#each args~}}
{{~#if implicit}} ?{{name}}{{else}} {{name}}{{~/if~}}
{{~/each}} : {{type~}}
{{~/each}} : {{{type~}}}
</pre>
</h4>
<div class="docblock">{{markdown comment}}</div>
Expand Down
Loading

0 comments on commit 34fff8d

Please sign in to comment.