mirror of
https://github.com/roc-lang/roc.git
synced 2024-10-26 10:17:51 +03:00
Format module header with params
This commit is contained in:
parent
760ffaf68f
commit
370013b960
@ -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(¶ms.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);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
module
|
||||
{
|
||||
echo,
|
||||
# comment before param
|
||||
read,
|
||||
} -> [
|
||||
mainMenu,
|
||||
credits,
|
||||
]
|
@ -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,
|
||||
},
|
||||
),
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
module {echo,
|
||||
# comment before param
|
||||
read } -> [mainMenu,
|
||||
credits ]
|
@ -1 +1 @@
|
||||
module [menu]
|
||||
module { echo, read } -> [menu]
|
||||
|
@ -0,0 +1,4 @@
|
||||
module { echo, read } -> [
|
||||
mainMenu,
|
||||
credits,
|
||||
]
|
@ -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,
|
||||
},
|
||||
),
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
module {echo, read } -> [mainMenu,
|
||||
credits ]
|
@ -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,
|
||||
|
2
module_with_params_and_multiline_exposes.header.roc
Normal file
2
module_with_params_and_multiline_exposes.header.roc
Normal file
@ -0,0 +1,2 @@
|
||||
module {echo, read } -> [mainMenu,
|
||||
credits ]
|
Loading…
Reference in New Issue
Block a user