Format module header with params

This commit is contained in:
Agus Zubiaga 2024-05-01 23:41:00 -03:00
parent 760ffaf68f
commit 370013b960
No known key found for this signature in database
10 changed files with 126 additions and 7 deletions

View File

@ -1,3 +1,5 @@
use std::cmp::max;
use crate::annotation::{is_collection_multiline, Formattable, Newlines, Parens};
use crate::collection::{fmt_collection, Braces};
use crate::expr::fmt_str_literal;
@ -177,7 +179,20 @@ pub fn fmt_module_header<'a>(buf: &mut Buf, header: &'a ModuleHeader<'a>) {
buf.indent(0);
buf.push_str("module");
let indent = fmt_spaces_with_outdent(buf, header.after_keyword, INDENT);
let mut indent = fmt_spaces_with_outdent(buf, header.after_keyword, 0);
if let Some(params) = &header.params {
if is_collection_multiline(&params.params) {
indent = INDENT;
}
fmt_collection(buf, indent, Braces::Curly, params.params, Newlines::Yes);
indent = fmt_spaces_with_outdent(buf, params.before_arrow, indent);
buf.push_str("->");
indent = fmt_spaces_with_outdent(buf, params.after_arrow, indent);
}
fmt_exposes(buf, header.exposes, indent);
}
@ -202,18 +217,19 @@ pub fn fmt_app_header<'a>(buf: &mut Buf, header: &'a AppHeader<'a>) {
buf.indent(0);
buf.push_str("app");
let indent = fmt_spaces_with_outdent(buf, header.before_provides, INDENT);
let indent = fmt_spaces_with_outdent(buf, header.before_provides, 0);
fmt_exposes(buf, header.provides, indent);
let indent = fmt_spaces_with_outdent(buf, header.before_packages, INDENT);
let indent = fmt_spaces_with_outdent(buf, header.before_packages, indent);
fmt_packages(buf, header.packages.value, indent);
}
pub fn fmt_spaces_with_outdent(buf: &mut Buf, spaces: &[CommentOrNewline], indent: u16) -> u16 {
if spaces.iter().all(|c| c.is_newline()) {
buf.spaces(1);
0
indent
} else {
let indent = max(INDENT, indent + INDENT);
fmt_default_spaces(buf, spaces, indent);
indent
}
@ -223,10 +239,10 @@ pub fn fmt_package_header<'a>(buf: &mut Buf, header: &'a PackageHeader<'a>) {
buf.indent(0);
buf.push_str("package");
let indent = fmt_spaces_with_outdent(buf, header.before_exposes, INDENT);
let indent = fmt_spaces_with_outdent(buf, header.before_exposes, 0);
fmt_exposes(buf, header.exposes, indent);
let indent = fmt_spaces_with_outdent(buf, header.before_packages, INDENT);
let indent = fmt_spaces_with_outdent(buf, header.before_packages, indent);
fmt_packages(buf, header.packages.value, indent);
}

View File

@ -0,0 +1,9 @@
module
{
echo,
# comment before param
read,
} -> [
mainMenu,
credits,
]

View File

@ -0,0 +1,44 @@
Module {
comments: [],
header: Module(
ModuleHeader {
after_keyword: [],
params: Some(
ModuleParams {
params: [
@8-12 Identifier {
ident: "echo",
},
@39-43 SpaceBefore(
Identifier {
ident: "read",
},
[
Newline,
LineComment(
" comment before param",
),
],
),
],
before_arrow: [],
after_arrow: [],
},
),
exposes: [
@50-58 ExposedName(
"mainMenu",
),
@64-71 SpaceBefore(
ExposedName(
"credits",
),
[
Newline,
],
),
],
interface_imports: None,
},
),
}

View File

@ -0,0 +1,4 @@
module {echo,
# comment before param
read } -> [mainMenu,
credits ]

View File

@ -1 +1 @@
module [menu]
module { echo, read } -> [menu]

View File

@ -0,0 +1,4 @@
module { echo, read } -> [
mainMenu,
credits,
]

View File

@ -0,0 +1,36 @@
Module {
comments: [],
header: Module(
ModuleHeader {
after_keyword: [],
params: Some(
ModuleParams {
params: [
@8-12 Identifier {
ident: "echo",
},
@14-18 Identifier {
ident: "read",
},
],
before_arrow: [],
after_arrow: [],
},
),
exposes: [
@25-33 ExposedName(
"mainMenu",
),
@39-46 SpaceBefore(
ExposedName(
"credits",
),
[
Newline,
],
),
],
interface_imports: None,
},
),
}

View File

@ -0,0 +1,2 @@
module {echo, read } -> [mainMenu,
credits ]

View File

@ -354,6 +354,8 @@ mod test_snapshots {
pass/module_multiline_exposes.header,
pass/module_with_newline.header,
pass/module_with_params.header,
pass/module_with_params_and_multiline_exposes.header,
pass/module_with_multiline_params_and_exposes.header,
pass/multi_backpassing.expr,
pass/multi_backpassing_in_def.moduledefs,
pass/multi_backpassing_with_apply.expr,

View File

@ -0,0 +1,2 @@
module {echo, read } -> [mainMenu,
credits ]