mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
Format module params in imports
This commit is contained in:
parent
e5a09b5de6
commit
b36f88fd13
4 changed files with 155 additions and 30 deletions
|
@ -199,12 +199,7 @@ impl<'a> Formattable for ModuleImport<'a> {
|
||||||
|
|
||||||
!before_name.is_empty()
|
!before_name.is_empty()
|
||||||
|| name.is_multiline()
|
|| name.is_multiline()
|
||||||
|| match params {
|
|| params.is_multiline()
|
||||||
Some(ModuleImportParams { before, params }) => {
|
|
||||||
!before.is_empty() || is_collection_multiline(params)
|
|
||||||
}
|
|
||||||
None => false,
|
|
||||||
}
|
|
||||||
|| alias.is_multiline()
|
|| alias.is_multiline()
|
||||||
|| match exposed {
|
|| match exposed {
|
||||||
Some(a) => a.keyword.is_multiline() || is_collection_multiline(&a.item),
|
Some(a) => a.keyword.is_multiline() || is_collection_multiline(&a.item),
|
||||||
|
@ -230,39 +225,44 @@ impl<'a> Formattable for ModuleImport<'a> {
|
||||||
buf.indent(indent);
|
buf.indent(indent);
|
||||||
buf.push_str("import");
|
buf.push_str("import");
|
||||||
|
|
||||||
let indent = indent + INDENT;
|
let indent = if !before_name.is_empty()
|
||||||
|
|| (params.is_multiline() && exposed.is_some())
|
||||||
|
|| alias.is_multiline()
|
||||||
|
|| exposed.map_or(false, |e| e.keyword.is_multiline())
|
||||||
|
{
|
||||||
|
indent + INDENT
|
||||||
|
} else {
|
||||||
|
indent
|
||||||
|
};
|
||||||
|
|
||||||
fmt_default_spaces(buf, before_name, indent);
|
fmt_default_spaces(buf, before_name, indent);
|
||||||
|
|
||||||
buf.indent(indent);
|
|
||||||
name.format(buf, indent);
|
name.format(buf, indent);
|
||||||
|
params.format(buf, indent);
|
||||||
if let Some(params) = params {
|
alias.format(buf, indent);
|
||||||
// TODO: Format import params
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(alias) = alias {
|
|
||||||
alias.format(buf, indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(exposed) = exposed {
|
if let Some(exposed) = exposed {
|
||||||
exposed.keyword.format(buf, indent);
|
exposed.keyword.format(buf, indent);
|
||||||
|
fmt_collection(buf, indent, Braces::Square, exposed.item, Newlines::No);
|
||||||
let list_indent = if !before_name.is_empty()
|
|
||||||
|| alias.is_multiline()
|
|
||||||
|| exposed.keyword.is_multiline()
|
|
||||||
{
|
|
||||||
indent
|
|
||||||
} else {
|
|
||||||
// Align list with import keyword
|
|
||||||
indent - INDENT
|
|
||||||
};
|
|
||||||
|
|
||||||
fmt_collection(buf, list_indent, Braces::Square, exposed.item, Newlines::No);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> Formattable for ModuleImportParams<'a> {
|
||||||
|
fn is_multiline(&self) -> bool {
|
||||||
|
let ModuleImportParams { before, params } = self;
|
||||||
|
|
||||||
|
!before.is_empty() || is_collection_multiline(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_with_options(&self, buf: &mut Buf, _parens: Parens, newlines: Newlines, indent: u16) {
|
||||||
|
let ModuleImportParams { before, params } = self;
|
||||||
|
|
||||||
|
fmt_default_spaces(buf, before, indent);
|
||||||
|
fmt_collection(buf, indent, Braces::Curly, *params, newlines);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Formattable for IngestedFileImport<'a> {
|
impl<'a> Formattable for IngestedFileImport<'a> {
|
||||||
fn is_multiline(&self) -> bool {
|
fn is_multiline(&self) -> bool {
|
||||||
let Self {
|
let Self {
|
||||||
|
|
|
@ -1,2 +1,13 @@
|
||||||
import pf.Menu
|
import pf.Menu { echo, read }
|
||||||
import Menu
|
import Menu {
|
||||||
|
echo,
|
||||||
|
read,
|
||||||
|
}
|
||||||
|
import Menu {
|
||||||
|
echo,
|
||||||
|
read,
|
||||||
|
} as M
|
||||||
|
import Menu { echo, read } as M exposing [
|
||||||
|
main,
|
||||||
|
credits,
|
||||||
|
]
|
||||||
|
|
|
@ -2,21 +2,31 @@ Defs {
|
||||||
tags: [
|
tags: [
|
||||||
Index(2147483648),
|
Index(2147483648),
|
||||||
Index(2147483649),
|
Index(2147483649),
|
||||||
|
Index(2147483650),
|
||||||
|
Index(2147483651),
|
||||||
],
|
],
|
||||||
regions: [
|
regions: [
|
||||||
@0-29,
|
@0-29,
|
||||||
@30-60,
|
@30-60,
|
||||||
|
@61-96,
|
||||||
|
@97-157,
|
||||||
],
|
],
|
||||||
space_before: [
|
space_before: [
|
||||||
Slice(start = 0, length = 0),
|
Slice(start = 0, length = 0),
|
||||||
Slice(start = 0, length = 1),
|
Slice(start = 0, length = 1),
|
||||||
|
Slice(start = 1, length = 1),
|
||||||
|
Slice(start = 2, length = 1),
|
||||||
],
|
],
|
||||||
space_after: [
|
space_after: [
|
||||||
Slice(start = 0, length = 0),
|
Slice(start = 0, length = 0),
|
||||||
Slice(start = 1, length = 0),
|
Slice(start = 1, length = 0),
|
||||||
|
Slice(start = 2, length = 0),
|
||||||
|
Slice(start = 3, length = 0),
|
||||||
],
|
],
|
||||||
spaces: [
|
spaces: [
|
||||||
Newline,
|
Newline,
|
||||||
|
Newline,
|
||||||
|
Newline,
|
||||||
],
|
],
|
||||||
type_defs: [],
|
type_defs: [],
|
||||||
value_defs: [
|
value_defs: [
|
||||||
|
@ -79,5 +89,105 @@ Defs {
|
||||||
exposed: None,
|
exposed: None,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
ModuleImport(
|
||||||
|
ModuleImport {
|
||||||
|
before_name: [],
|
||||||
|
name: @68-72 ImportedModuleName {
|
||||||
|
package: None,
|
||||||
|
name: ModuleName(
|
||||||
|
"Menu",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
params: Some(
|
||||||
|
ModuleImportParams {
|
||||||
|
before: [],
|
||||||
|
params: [
|
||||||
|
@75-79 LabelOnly(
|
||||||
|
@75-79 "echo",
|
||||||
|
),
|
||||||
|
@85-90 SpaceBefore(
|
||||||
|
LabelOnly(
|
||||||
|
@85-89 "read",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
alias: Some(
|
||||||
|
KeywordItem {
|
||||||
|
keyword: Spaces {
|
||||||
|
before: [],
|
||||||
|
item: ImportAsKeyword,
|
||||||
|
after: [],
|
||||||
|
},
|
||||||
|
item: @95-96 ImportAlias(
|
||||||
|
"M",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
exposed: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ModuleImport(
|
||||||
|
ModuleImport {
|
||||||
|
before_name: [],
|
||||||
|
name: @104-108 ImportedModuleName {
|
||||||
|
package: None,
|
||||||
|
name: ModuleName(
|
||||||
|
"Menu",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
params: Some(
|
||||||
|
ModuleImportParams {
|
||||||
|
before: [],
|
||||||
|
params: [
|
||||||
|
@111-115 LabelOnly(
|
||||||
|
@111-115 "echo",
|
||||||
|
),
|
||||||
|
@117-122 LabelOnly(
|
||||||
|
@117-121 "read",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
alias: Some(
|
||||||
|
KeywordItem {
|
||||||
|
keyword: Spaces {
|
||||||
|
before: [],
|
||||||
|
item: ImportAsKeyword,
|
||||||
|
after: [],
|
||||||
|
},
|
||||||
|
item: @127-128 ImportAlias(
|
||||||
|
"M",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
exposed: Some(
|
||||||
|
KeywordItem {
|
||||||
|
keyword: Spaces {
|
||||||
|
before: [],
|
||||||
|
item: ImportExposingKeyword,
|
||||||
|
after: [],
|
||||||
|
},
|
||||||
|
item: [
|
||||||
|
@140-144 ExposedName(
|
||||||
|
"main",
|
||||||
|
),
|
||||||
|
@148-155 SpaceBefore(
|
||||||
|
ExposedName(
|
||||||
|
"credits",
|
||||||
|
),
|
||||||
|
[
|
||||||
|
Newline,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
import pf.Menu { echo, read }
|
import pf.Menu { echo, read }
|
||||||
import Menu { echo,
|
import Menu { echo,
|
||||||
read }
|
read }
|
||||||
|
import Menu { echo,
|
||||||
|
read } as M
|
||||||
|
import Menu { echo, read } as M exposing [ main,
|
||||||
|
credits ]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue