parent
1ca705005d
commit
1f15c2ca20
|
@ -133,14 +133,14 @@ impl<'comments> Formatter<'comments> {
|
||||||
let start = def.location().start;
|
let start = def.location().start;
|
||||||
|
|
||||||
match def {
|
match def {
|
||||||
Definition::Use { .. } => {
|
Definition::Use(import) => {
|
||||||
has_imports = true;
|
has_imports = true;
|
||||||
|
|
||||||
let comments = self.pop_comments(start);
|
let comments = self.pop_comments(start);
|
||||||
|
|
||||||
let def = self.definition(def);
|
let def = self.definition(def);
|
||||||
|
|
||||||
imports.push(commented(def, comments))
|
imports.push((import, commented(def, comments)))
|
||||||
}
|
}
|
||||||
|
|
||||||
_other => {
|
_other => {
|
||||||
|
@ -155,7 +155,15 @@ impl<'comments> Formatter<'comments> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let imports = join(imports.into_iter(), line());
|
let imports = join(
|
||||||
|
imports
|
||||||
|
.into_iter()
|
||||||
|
.sorted_by(|(import_a, _), (import_b, _)| {
|
||||||
|
Ord::cmp(&import_a.module, &import_b.module)
|
||||||
|
})
|
||||||
|
.map(|(_, doc)| doc),
|
||||||
|
line(),
|
||||||
|
);
|
||||||
|
|
||||||
let declarations = join(declarations.into_iter(), lines(2));
|
let declarations = join(declarations.into_iter(), lines(2));
|
||||||
|
|
||||||
|
@ -253,36 +261,7 @@ impl<'comments> Formatter<'comments> {
|
||||||
..
|
..
|
||||||
}) => self.data_type(*public, *opaque, name, parameters, constructors, location),
|
}) => self.data_type(*public, *opaque, name, parameters, constructors, location),
|
||||||
|
|
||||||
Definition::Use(Use {
|
Definition::Use(import) => self.import(import),
|
||||||
module,
|
|
||||||
as_name,
|
|
||||||
unqualified,
|
|
||||||
..
|
|
||||||
}) => "use "
|
|
||||||
.to_doc()
|
|
||||||
.append(Document::String(module.join("/")))
|
|
||||||
.append(if unqualified.is_empty() {
|
|
||||||
nil()
|
|
||||||
} else {
|
|
||||||
let unqualified = Itertools::intersperse(
|
|
||||||
unqualified
|
|
||||||
.iter()
|
|
||||||
.sorted_by(|a, b| a.name.cmp(&b.name))
|
|
||||||
.map(|e| e.to_doc()),
|
|
||||||
flex_break(",", ", "),
|
|
||||||
);
|
|
||||||
let unqualified = break_("", "")
|
|
||||||
.append(concat(unqualified))
|
|
||||||
.nest(INDENT)
|
|
||||||
.append(break_(",", ""))
|
|
||||||
.group();
|
|
||||||
".{".to_doc().append(unqualified).append("}")
|
|
||||||
})
|
|
||||||
.append(if let Some(name) = as_name {
|
|
||||||
docvec![" as ", name]
|
|
||||||
} else {
|
|
||||||
nil()
|
|
||||||
}),
|
|
||||||
|
|
||||||
Definition::ModuleConstant(ModuleConstant {
|
Definition::ModuleConstant(ModuleConstant {
|
||||||
public,
|
public,
|
||||||
|
@ -301,6 +280,42 @@ impl<'comments> Formatter<'comments> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn import<'a>(
|
||||||
|
&mut self,
|
||||||
|
Use {
|
||||||
|
module,
|
||||||
|
as_name,
|
||||||
|
unqualified,
|
||||||
|
..
|
||||||
|
}: &'a Use<()>,
|
||||||
|
) -> Document<'a> {
|
||||||
|
"use "
|
||||||
|
.to_doc()
|
||||||
|
.append(Document::String(module.join("/")))
|
||||||
|
.append(if unqualified.is_empty() {
|
||||||
|
nil()
|
||||||
|
} else {
|
||||||
|
let unqualified = Itertools::intersperse(
|
||||||
|
unqualified
|
||||||
|
.iter()
|
||||||
|
.sorted_by(|a, b| a.name.cmp(&b.name))
|
||||||
|
.map(|e| e.to_doc()),
|
||||||
|
flex_break(",", ", "),
|
||||||
|
);
|
||||||
|
let unqualified = break_("", "")
|
||||||
|
.append(concat(unqualified))
|
||||||
|
.nest(INDENT)
|
||||||
|
.append(break_(",", ""))
|
||||||
|
.group();
|
||||||
|
".{".to_doc().append(unqualified).append("}")
|
||||||
|
})
|
||||||
|
.append(if let Some(name) = as_name {
|
||||||
|
docvec![" as ", name]
|
||||||
|
} else {
|
||||||
|
nil()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn const_expr<'a, A, B>(&mut self, value: &'a Constant<A, B>) -> Document<'a> {
|
fn const_expr<'a, A, B>(&mut self, value: &'a Constant<A, B>) -> Document<'a> {
|
||||||
match value {
|
match value {
|
||||||
Constant::ByteArray { bytes, .. } => "#"
|
Constant::ByteArray { bytes, .. } => "#"
|
||||||
|
|
|
@ -5,14 +5,15 @@ use pretty_assertions::assert_eq;
|
||||||
fn assert_fmt(src: &str, expected: &str) {
|
fn assert_fmt(src: &str, expected: &str) {
|
||||||
let (module, extra) = parser::module(src, ModuleKind::Lib).unwrap();
|
let (module, extra) = parser::module(src, ModuleKind::Lib).unwrap();
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
format::pretty(&mut out, module.clone(), extra.clone(), src);
|
format::pretty(&mut out, module, extra, src);
|
||||||
|
|
||||||
// Output is what we expect
|
// Output is what we expect
|
||||||
assert_eq!(out, expected);
|
assert_eq!(out, expected);
|
||||||
|
|
||||||
// Formatting is idempotent
|
// Formatting is idempotent
|
||||||
|
let (module2, extra2) = parser::module(&out, ModuleKind::Lib).unwrap();
|
||||||
let mut out2 = String::new();
|
let mut out2 = String::new();
|
||||||
format::pretty(&mut out2, module, extra, &out);
|
format::pretty(&mut out2, module2, extra2, &out);
|
||||||
assert_eq!(out, out2);
|
assert_eq!(out, out2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,3 +172,28 @@ fn test_format_nested_when() {
|
||||||
|
|
||||||
assert_fmt(src, expected)
|
assert_fmt(src, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_imports() {
|
||||||
|
let src = indoc! {r#"
|
||||||
|
use aiken/list
|
||||||
|
// foo
|
||||||
|
use aiken/bytearray
|
||||||
|
use aiken/transaction/certificate
|
||||||
|
// bar
|
||||||
|
use aiken/transaction
|
||||||
|
use aiken/transaction/value
|
||||||
|
"#};
|
||||||
|
|
||||||
|
let expected = indoc! {r#"
|
||||||
|
// foo
|
||||||
|
use aiken/bytearray
|
||||||
|
use aiken/list
|
||||||
|
// bar
|
||||||
|
use aiken/transaction
|
||||||
|
use aiken/transaction/certificate
|
||||||
|
use aiken/transaction/value
|
||||||
|
"#};
|
||||||
|
|
||||||
|
assert_fmt(src, expected)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue