Implement various visual improvements for the doc generator
- Display function's signature next to the function name
(instead of being repeated below the function documentation).
- Same for module constants
- Display record constructors in a more concise manner, with
constructors fields next to constructors.
- Display generic parameters, if any, next to the type
- Plus some minor color and icon rework.
This commit is contained in:
@@ -364,7 +364,6 @@ impl<'comments> Formatter<'comments> {
|
||||
} => name
|
||||
.to_doc()
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter()
|
||||
.map(|a| (self.constant_call_arg(a), a.label.is_some())),
|
||||
))
|
||||
@@ -380,7 +379,6 @@ impl<'comments> Formatter<'comments> {
|
||||
.append(".")
|
||||
.append(name.as_str())
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter()
|
||||
.map(|a| (self.constant_call_arg(a), a.label.is_some())),
|
||||
))
|
||||
@@ -397,7 +395,7 @@ impl<'comments> Formatter<'comments> {
|
||||
} => docvec![module, ".", name],
|
||||
|
||||
Constant::Tuple { elements, .. } => {
|
||||
wrap_args(false, elements.iter().map(|e| (self.const_expr(e), false))).group()
|
||||
wrap_args(elements.iter().map(|e| (self.const_expr(e), false))).group()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -467,17 +465,14 @@ impl<'comments> Formatter<'comments> {
|
||||
..
|
||||
} => "fn"
|
||||
.to_doc()
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter().map(|t| (self.annotation(t), false)),
|
||||
))
|
||||
.append(wrap_args(args.iter().map(|t| (self.annotation(t), false))))
|
||||
.group()
|
||||
.append(" ->")
|
||||
.append(break_("", " ").append(self.annotation(retrn)).nest(INDENT)),
|
||||
|
||||
Annotation::Var { name, .. } => name.to_doc(),
|
||||
Annotation::Tuple { elems, .. } => {
|
||||
wrap_args(false, elems.iter().map(|t| (self.annotation(t), false)))
|
||||
wrap_args(elems.iter().map(|t| (self.annotation(t), false)))
|
||||
}
|
||||
}
|
||||
.group()
|
||||
@@ -538,10 +533,7 @@ impl<'comments> Formatter<'comments> {
|
||||
.append(keyword)
|
||||
.append(" ")
|
||||
.append(name)
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter().map(|e| (self.fn_arg(e), false)),
|
||||
));
|
||||
.append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false))));
|
||||
|
||||
// Add return annotation
|
||||
let head = match return_annotation {
|
||||
@@ -572,7 +564,7 @@ impl<'comments> Formatter<'comments> {
|
||||
return_annotation: Option<&'a Annotation>,
|
||||
body: &'a UntypedExpr,
|
||||
) -> Document<'a> {
|
||||
let args = wrap_args(false, args.iter().map(|e| (self.fn_arg(e), false))).group();
|
||||
let args = wrap_args(args.iter().map(|e| (self.fn_arg(e), false))).group();
|
||||
let body = match body {
|
||||
UntypedExpr::When { .. } => self.expr(body).force_break(),
|
||||
_ => self.expr(body),
|
||||
@@ -764,7 +756,7 @@ impl<'comments> Formatter<'comments> {
|
||||
} => self.record_update(constructor, spread, args),
|
||||
|
||||
UntypedExpr::Tuple { elems, .. } => {
|
||||
wrap_args(false, elems.iter().map(|e| (self.wrap_expr(e), false))).group()
|
||||
wrap_args(elems.iter().map(|e| (self.wrap_expr(e), false))).group()
|
||||
}
|
||||
|
||||
UntypedExpr::TupleIndex { index, tuple, .. } => {
|
||||
@@ -841,7 +833,6 @@ impl<'comments> Formatter<'comments> {
|
||||
|
||||
_ => name
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter().map(|a| (self.pattern_call_arg(a), is_record)),
|
||||
))
|
||||
.group(),
|
||||
@@ -883,7 +874,6 @@ impl<'comments> Formatter<'comments> {
|
||||
_ => self
|
||||
.expr(fun)
|
||||
.append(wrap_args(
|
||||
false,
|
||||
args.iter()
|
||||
.map(|a| (self.call_arg(a, needs_curly), needs_curly)),
|
||||
))
|
||||
@@ -973,9 +963,7 @@ impl<'comments> Formatter<'comments> {
|
||||
let spread_doc = "..".to_doc().append(self.expr(&spread.base));
|
||||
let arg_docs = args.iter().map(|a| (self.record_update_arg(a), true));
|
||||
let all_arg_docs = once((spread_doc, true)).chain(arg_docs);
|
||||
constructor_doc
|
||||
.append(wrap_args(false, all_arg_docs))
|
||||
.group()
|
||||
constructor_doc.append(wrap_args(all_arg_docs)).group()
|
||||
}
|
||||
|
||||
pub fn bin_op<'a>(
|
||||
@@ -1061,7 +1049,6 @@ impl<'comments> Formatter<'comments> {
|
||||
// x |> fun(_, 2, 3)
|
||||
self.expr(fun).append(
|
||||
wrap_args(
|
||||
false,
|
||||
args.iter()
|
||||
.skip(1)
|
||||
.map(|a| (self.call_arg(a, false), false)),
|
||||
@@ -1070,9 +1057,8 @@ impl<'comments> Formatter<'comments> {
|
||||
)
|
||||
} else {
|
||||
// x |> fun(1, _, 3)
|
||||
self.expr(fun).append(
|
||||
wrap_args(false, args.iter().map(|a| (self.call_arg(a, false), false))).group(),
|
||||
)
|
||||
self.expr(fun)
|
||||
.append(wrap_args(args.iter().map(|a| (self.call_arg(a, false), false))).group())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1092,7 +1078,7 @@ impl<'comments> Formatter<'comments> {
|
||||
}
|
||||
|
||||
_ => self.expr(fun).append(
|
||||
wrap_args(false, args.iter().map(|a| (self.call_arg(a, false), false))).group(),
|
||||
wrap_args(args.iter().map(|a| (self.call_arg(a, false), false))).group(),
|
||||
),
|
||||
},
|
||||
|
||||
@@ -1136,34 +1122,29 @@ impl<'comments> Formatter<'comments> {
|
||||
constructor
|
||||
.name
|
||||
.to_doc()
|
||||
.append(wrap_args(
|
||||
false,
|
||||
constructor.arguments.iter().map(
|
||||
|RecordConstructorArg {
|
||||
label,
|
||||
annotation,
|
||||
location,
|
||||
..
|
||||
}| {
|
||||
let arg_comments = self.pop_comments(location.start);
|
||||
.append(wrap_args(constructor.arguments.iter().map(
|
||||
|RecordConstructorArg {
|
||||
label,
|
||||
annotation,
|
||||
location,
|
||||
..
|
||||
}| {
|
||||
let arg_comments = self.pop_comments(location.start);
|
||||
|
||||
let arg = match label {
|
||||
Some(l) => {
|
||||
l.to_doc().append(": ").append(self.annotation(annotation))
|
||||
}
|
||||
None => self.annotation(annotation),
|
||||
};
|
||||
let arg = match label {
|
||||
Some(l) => l.to_doc().append(": ").append(self.annotation(annotation)),
|
||||
None => self.annotation(annotation),
|
||||
};
|
||||
|
||||
(
|
||||
commented(
|
||||
self.doc_comments(location.start).append(arg).group(),
|
||||
arg_comments,
|
||||
),
|
||||
label.is_some(),
|
||||
)
|
||||
},
|
||||
),
|
||||
))
|
||||
(
|
||||
commented(
|
||||
self.doc_comments(location.start).append(arg).group(),
|
||||
arg_comments,
|
||||
),
|
||||
label.is_some(),
|
||||
)
|
||||
},
|
||||
)))
|
||||
.group()
|
||||
};
|
||||
|
||||
@@ -1290,7 +1271,24 @@ impl<'comments> Formatter<'comments> {
|
||||
&mut self,
|
||||
constructor: &'a RecordConstructor<A>,
|
||||
) -> Document<'a> {
|
||||
constructor.name.to_doc()
|
||||
if constructor.arguments.is_empty() {
|
||||
constructor.name.to_doc()
|
||||
} else {
|
||||
constructor
|
||||
.name
|
||||
.to_doc()
|
||||
.append(wrap_args(constructor.arguments.iter().map(|arg| {
|
||||
(
|
||||
(match &arg.label {
|
||||
Some(l) => l.to_doc().append(": "),
|
||||
None => "".to_doc(),
|
||||
})
|
||||
.append(self.annotation(&arg.annotation)),
|
||||
arg.label.is_some(),
|
||||
)
|
||||
})))
|
||||
.group()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn docs_fn_signature<'a>(
|
||||
@@ -1300,10 +1298,7 @@ impl<'comments> Formatter<'comments> {
|
||||
return_annotation: &'a Option<Annotation>,
|
||||
return_type: Arc<Type>,
|
||||
) -> Document<'a> {
|
||||
let head = name
|
||||
.to_doc()
|
||||
.append(self.docs_fn_args(false, args))
|
||||
.append(" -> ");
|
||||
let head = name.to_doc().append(self.docs_fn_args(args)).append(" -> ");
|
||||
|
||||
let tail = self.type_or_annotation(return_annotation, &return_type);
|
||||
|
||||
@@ -1317,7 +1312,7 @@ impl<'comments> Formatter<'comments> {
|
||||
{
|
||||
let head = name
|
||||
.to_doc()
|
||||
.append(self.docs_fn_args(true, args))
|
||||
.append(self.docs_fn_args(args).force_break())
|
||||
.append(" -> ");
|
||||
head.append(tail).group()
|
||||
} else {
|
||||
@@ -1326,16 +1321,8 @@ impl<'comments> Formatter<'comments> {
|
||||
}
|
||||
|
||||
// Will always print the types, even if they were implicit in the original source
|
||||
pub fn docs_fn_args<'a>(&mut self, multiline: bool, args: &'a [TypedArg]) -> Document<'a> {
|
||||
if multiline {
|
||||
line().nest(INDENT).append(wrap_args(
|
||||
true,
|
||||
args.iter()
|
||||
.map(|e| (self.docs_fn_arg(e).append(line()), false)),
|
||||
))
|
||||
} else {
|
||||
wrap_args(false, args.iter().map(|e| (self.docs_fn_arg(e), false)))
|
||||
}
|
||||
pub fn docs_fn_args<'a>(&mut self, args: &'a [TypedArg]) -> Document<'a> {
|
||||
wrap_args(args.iter().map(|e| (self.docs_fn_arg(e), false)))
|
||||
}
|
||||
|
||||
fn docs_fn_arg<'a>(&mut self, arg: &'a Arg<Arc<Type>>) -> Document<'a> {
|
||||
@@ -1492,7 +1479,7 @@ impl<'comments> Formatter<'comments> {
|
||||
Pattern::Discard { name, .. } => name.to_doc(),
|
||||
|
||||
Pattern::Tuple { elems, .. } => {
|
||||
wrap_args(false, elems.iter().map(|e| (self.pattern(e), false))).group()
|
||||
wrap_args(elems.iter().map(|e| (self.pattern(e), false))).group()
|
||||
}
|
||||
|
||||
Pattern::List { elements, tail, .. } => {
|
||||
@@ -1653,7 +1640,7 @@ impl<'a> Documentable<'a> for &'a BinOp {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wrap_args<'a, I>(multiline: bool, args: I) -> Document<'a>
|
||||
pub fn wrap_args<'a, I>(args: I) -> Document<'a>
|
||||
where
|
||||
I: IntoIterator<Item = (Document<'a>, bool)>,
|
||||
{
|
||||
@@ -1669,8 +1656,6 @@ where
|
||||
|
||||
let (open_broken, open_unbroken, close) = if curly {
|
||||
(" {", " { ", "}")
|
||||
} else if multiline {
|
||||
("( ", "( ", ")")
|
||||
} else {
|
||||
("(", "(", ")")
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user