fix: record formatting
This commit is contained in:
parent
9d14acbe0a
commit
809d5ea5c5
|
@ -319,7 +319,10 @@ impl<'comments> Formatter<'comments> {
|
||||||
..
|
..
|
||||||
} => name
|
} => name
|
||||||
.to_doc()
|
.to_doc()
|
||||||
.append(wrap_args(args.iter().map(|a| self.constant_call_arg(a))))
|
.append(wrap_args(
|
||||||
|
args.iter()
|
||||||
|
.map(|a| (self.constant_call_arg(a), a.label.is_some())),
|
||||||
|
))
|
||||||
.group(),
|
.group(),
|
||||||
|
|
||||||
Constant::Record {
|
Constant::Record {
|
||||||
|
@ -331,7 +334,10 @@ impl<'comments> Formatter<'comments> {
|
||||||
.to_doc()
|
.to_doc()
|
||||||
.append(".")
|
.append(".")
|
||||||
.append(name.as_str())
|
.append(name.as_str())
|
||||||
.append(wrap_args(args.iter().map(|a| self.constant_call_arg(a))))
|
.append(wrap_args(
|
||||||
|
args.iter()
|
||||||
|
.map(|a| (self.constant_call_arg(a), a.label.is_some())),
|
||||||
|
))
|
||||||
.group(),
|
.group(),
|
||||||
|
|
||||||
Constant::Var {
|
Constant::Var {
|
||||||
|
@ -430,7 +436,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn type_arguments<'a>(&mut self, args: &'a [Annotation]) -> Document<'a> {
|
fn type_arguments<'a>(&mut self, args: &'a [Annotation]) -> Document<'a> {
|
||||||
wrap_args(args.iter().map(|t| self.type_ast(t)))
|
wrap_args(args.iter().map(|t| (self.type_ast(t), false)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn type_alias<'a>(
|
pub fn type_alias<'a>(
|
||||||
|
@ -445,7 +451,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
let head = if args.is_empty() {
|
let head = if args.is_empty() {
|
||||||
head
|
head
|
||||||
} else {
|
} else {
|
||||||
head.append(wrap_args(args.iter().map(|e| e.to_doc())).group())
|
head.append(wrap_args(args.iter().map(|e| (e.to_doc(), false))).group())
|
||||||
};
|
};
|
||||||
|
|
||||||
head.append(" =")
|
head.append(" =")
|
||||||
|
@ -477,7 +483,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
let head = pub_(*public)
|
let head = pub_(*public)
|
||||||
.append("fn ")
|
.append("fn ")
|
||||||
.append(name)
|
.append(name)
|
||||||
.append(wrap_args(args.iter().map(|e| self.fn_arg(e))));
|
.append(wrap_args(args.iter().map(|e| (self.fn_arg(e), false))));
|
||||||
|
|
||||||
// Add return annotation
|
// Add return annotation
|
||||||
let head = match return_annotation {
|
let head = match return_annotation {
|
||||||
|
@ -508,7 +514,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
return_annotation: Option<&'a Annotation>,
|
return_annotation: Option<&'a Annotation>,
|
||||||
body: &'a UntypedExpr,
|
body: &'a UntypedExpr,
|
||||||
) -> Document<'a> {
|
) -> Document<'a> {
|
||||||
let args = wrap_args(args.iter().map(|e| self.fn_arg(e))).group();
|
let args = wrap_args(args.iter().map(|e| (self.fn_arg(e), false))).group();
|
||||||
let body = match body {
|
let body = match body {
|
||||||
UntypedExpr::When { .. } => self.expr(body).force_break(),
|
UntypedExpr::When { .. } => self.expr(body).force_break(),
|
||||||
_ => self.expr(body),
|
_ => self.expr(body),
|
||||||
|
@ -715,13 +721,16 @@ impl<'comments> Formatter<'comments> {
|
||||||
} else {
|
} else {
|
||||||
match args {
|
match args {
|
||||||
[arg] if is_breakable(&arg.value) => name
|
[arg] if is_breakable(&arg.value) => name
|
||||||
.append("(")
|
.append(if arg.label.is_some() { "{" } else { "(" })
|
||||||
.append(self.pattern_call_arg(arg))
|
.append(self.pattern_call_arg(arg))
|
||||||
.append(")")
|
.append(if arg.label.is_some() { "}" } else { ")" })
|
||||||
.group(),
|
.group(),
|
||||||
|
|
||||||
_ => name
|
_ => name
|
||||||
.append(wrap_args(args.iter().map(|a| self.pattern_call_arg(a))))
|
.append(wrap_args(
|
||||||
|
args.iter()
|
||||||
|
.map(|a| (self.pattern_call_arg(a), a.label.is_some())),
|
||||||
|
))
|
||||||
.group(),
|
.group(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -738,7 +747,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
|
|
||||||
_ => self
|
_ => self
|
||||||
.expr(fun)
|
.expr(fun)
|
||||||
.append(wrap_args(args.iter().map(|a| self.call_arg(a))))
|
.append(wrap_args(args.iter().map(|a| (self.call_arg(a), false))))
|
||||||
.group(),
|
.group(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -781,8 +790,8 @@ impl<'comments> Formatter<'comments> {
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
let constructor_doc = self.expr(constructor);
|
let constructor_doc = self.expr(constructor);
|
||||||
let spread_doc = "..".to_doc().append(self.expr(&spread.base));
|
let spread_doc = "..".to_doc().append(self.expr(&spread.base));
|
||||||
let arg_docs = args.iter().map(|a| self.record_update_arg(a));
|
let arg_docs = args.iter().map(|a| (self.record_update_arg(a), true));
|
||||||
let all_arg_docs = once(spread_doc).chain(arg_docs);
|
let all_arg_docs = once((spread_doc, true)).chain(arg_docs);
|
||||||
constructor_doc.append(wrap_args(all_arg_docs)).group()
|
constructor_doc.append(wrap_args(all_arg_docs)).group()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -868,11 +877,11 @@ impl<'comments> Formatter<'comments> {
|
||||||
} else if hole_in_first_position {
|
} else if hole_in_first_position {
|
||||||
// x |> fun(_, 2, 3)
|
// x |> fun(_, 2, 3)
|
||||||
self.expr(fun)
|
self.expr(fun)
|
||||||
.append(wrap_args(args.iter().skip(1).map(|a| self.call_arg(a))).group())
|
.append(wrap_args(args.iter().skip(1).map(|a| (self.call_arg(a), false))).group())
|
||||||
} else {
|
} else {
|
||||||
// x |> fun(1, _, 3)
|
// x |> fun(1, _, 3)
|
||||||
self.expr(fun)
|
self.expr(fun)
|
||||||
.append(wrap_args(args.iter().map(|a| self.call_arg(a))).group())
|
.append(wrap_args(args.iter().map(|a| (self.call_arg(a), false))).group())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -893,7 +902,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
|
|
||||||
_ => self
|
_ => self
|
||||||
.expr(fun)
|
.expr(fun)
|
||||||
.append(wrap_args(args.iter().map(|a| self.call_arg(a))).group()),
|
.append(wrap_args(args.iter().map(|a| (self.call_arg(a), false))).group()),
|
||||||
},
|
},
|
||||||
|
|
||||||
// The body of a capture being not a fn shouldn't be possible...
|
// The body of a capture being not a fn shouldn't be possible...
|
||||||
|
@ -950,9 +959,12 @@ impl<'comments> Formatter<'comments> {
|
||||||
None => self.type_ast(annotation),
|
None => self.type_ast(annotation),
|
||||||
};
|
};
|
||||||
|
|
||||||
commented(
|
(
|
||||||
self.doc_comments(location.start).append(arg).group(),
|
commented(
|
||||||
arg_comments,
|
self.doc_comments(location.start).append(arg).group(),
|
||||||
|
arg_comments,
|
||||||
|
),
|
||||||
|
label.is_some(),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)))
|
)))
|
||||||
|
@ -982,7 +994,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
name.to_doc()
|
name.to_doc()
|
||||||
} else {
|
} else {
|
||||||
name.to_doc()
|
name.to_doc()
|
||||||
.append(wrap_args(args.iter().map(|e| e.to_doc())))
|
.append(wrap_args(args.iter().map(|e| (e.to_doc(), false))))
|
||||||
.group()
|
.group()
|
||||||
})
|
})
|
||||||
.append(" {")
|
.append(" {")
|
||||||
|
@ -1021,7 +1033,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
name.to_doc()
|
name.to_doc()
|
||||||
} else {
|
} else {
|
||||||
name.to_doc()
|
name.to_doc()
|
||||||
.append(wrap_args(args.iter().map(|e| e.to_doc())))
|
.append(wrap_args(args.iter().map(|e| (e.to_doc(), false))))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1049,10 +1061,13 @@ impl<'comments> Formatter<'comments> {
|
||||||
printer: &mut tipo::pretty::Printer,
|
printer: &mut tipo::pretty::Printer,
|
||||||
) -> Document<'a> {
|
) -> Document<'a> {
|
||||||
wrap_args(args.iter().map(|arg| {
|
wrap_args(args.iter().map(|arg| {
|
||||||
arg.arg_name
|
(
|
||||||
.to_doc()
|
arg.arg_name
|
||||||
.append(": ".to_doc().append(printer.print(&arg.tipo)))
|
.to_doc()
|
||||||
.group()
|
.append(": ".to_doc().append(printer.print(&arg.tipo)))
|
||||||
|
.group(),
|
||||||
|
false,
|
||||||
|
)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1145,22 +1160,6 @@ impl<'comments> Formatter<'comments> {
|
||||||
.append(self.case_clause_value(&clause.then))
|
.append(self.case_clause_value(&clause.then))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn external_type<'a>(
|
|
||||||
&mut self,
|
|
||||||
public: bool,
|
|
||||||
name: &'a str,
|
|
||||||
args: &'a [String],
|
|
||||||
) -> Document<'a> {
|
|
||||||
pub_(public)
|
|
||||||
.append("external type ")
|
|
||||||
.append(name)
|
|
||||||
.append(if args.is_empty() {
|
|
||||||
nil()
|
|
||||||
} else {
|
|
||||||
wrap_args(args.iter().map(|e| e.to_doc()))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn list<'a>(
|
fn list<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
elements: &'a [UntypedExpr],
|
elements: &'a [UntypedExpr],
|
||||||
|
@ -1342,17 +1341,29 @@ impl<'a> Documentable<'a> for &'a BinOp {
|
||||||
|
|
||||||
pub fn wrap_args<'a, I>(args: I) -> Document<'a>
|
pub fn wrap_args<'a, I>(args: I) -> Document<'a>
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item = Document<'a>>,
|
I: IntoIterator<Item = (Document<'a>, bool)>,
|
||||||
{
|
{
|
||||||
let mut args = args.into_iter().peekable();
|
let mut args = args.into_iter().peekable();
|
||||||
if args.peek().is_none() {
|
|
||||||
|
let curly = if let Some((_, uses_curly)) = args.peek() {
|
||||||
|
*uses_curly
|
||||||
|
} else {
|
||||||
return "()".to_doc();
|
return "()".to_doc();
|
||||||
}
|
};
|
||||||
break_("(", "(")
|
|
||||||
|
let args = args.map(|a| a.0);
|
||||||
|
|
||||||
|
let ((open_broken, open_unbroken), close) = if curly {
|
||||||
|
((" {", " { "), "}")
|
||||||
|
} else {
|
||||||
|
(("(", "("), ")")
|
||||||
|
};
|
||||||
|
|
||||||
|
break_(open_broken, open_unbroken)
|
||||||
.append(join(args, break_(",", ", ")))
|
.append(join(args, break_(",", ", ")))
|
||||||
.nest(INDENT)
|
.nest(INDENT)
|
||||||
.append(break_(",", ""))
|
.append(break_(",", if curly { " " } else { "" }))
|
||||||
.append(")")
|
.append(close)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wrap_fields<'a, I>(args: I) -> Document<'a>
|
pub fn wrap_fields<'a, I>(args: I) -> Document<'a>
|
||||||
|
@ -1363,11 +1374,12 @@ where
|
||||||
if args.peek().is_none() {
|
if args.peek().is_none() {
|
||||||
return nil();
|
return nil();
|
||||||
}
|
}
|
||||||
break_(" ", " ")
|
|
||||||
.append(join(args, break_(",", ", ")))
|
line()
|
||||||
|
.append(join(args, ",".to_doc().append(line())))
|
||||||
.nest(INDENT)
|
.nest(INDENT)
|
||||||
.append(break_(",", ""))
|
.append(",")
|
||||||
.append(" ")
|
.append(line())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wrap_args_with_spread<'a, I>(args: I) -> Document<'a>
|
pub fn wrap_args_with_spread<'a, I>(args: I) -> Document<'a>
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
pub type ScriptContext { thing: String }
|
pub type ScriptContext {
|
||||||
|
thing: String,
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
pub type ScriptContext(purpose) { tx_info: TxInfo, script_purpose: purpose }
|
pub type ScriptContext(purpose) {
|
||||||
|
tx_info: TxInfo,
|
||||||
|
script_purpose: purpose,
|
||||||
|
}
|
||||||
|
|
||||||
pub type TxInfo { idk: Int }
|
pub type TxInfo {
|
||||||
|
idk: Int,
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
use sample/context
|
use sample/context
|
||||||
|
|
||||||
pub type Mint { currency_symbol: ByteArray, thing: Int, thing: Int, thing: Int }
|
pub type Mint {
|
||||||
|
currency_symbol: ByteArray,
|
||||||
|
thing: Int,
|
||||||
|
thing: Int,
|
||||||
|
thing: Int,
|
||||||
|
}
|
||||||
|
|
||||||
pub type ScriptContext =
|
pub type ScriptContext =
|
||||||
context.ScriptContext(Mint)
|
context.ScriptContext(Mint)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use sample/context
|
use sample/context
|
||||||
|
|
||||||
pub type Spend { idk: Int }
|
pub type Spend {
|
||||||
|
idk: Int,
|
||||||
|
}
|
||||||
|
|
||||||
pub type ScriptContext =
|
pub type ScriptContext =
|
||||||
context.ScriptContext(Spend)
|
context.ScriptContext(Spend)
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
use sample/mint
|
use sample/mint
|
||||||
use sample/spend
|
use sample/spend
|
||||||
|
|
||||||
pub type Datum { something: String }
|
pub type Datum {
|
||||||
|
something: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub type Redeemer {
|
pub type Redeemer {
|
||||||
Buy
|
Buy { id: Int }
|
||||||
Sell
|
Sell(Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spend(datum: Datum, rdmr: Redeemer, ctx: spend.ScriptContext) -> Bool {
|
pub fn spend(datum: Datum, rdmr: Redeemer, ctx: spend.ScriptContext) -> Bool {
|
||||||
|
|
Loading…
Reference in New Issue