Parse and format import package shorthand

The original proposal [1] suggested dropping the package shorthand,
but we later decided to keep it [2] to improve UX.

[1] https://docs.google.com/document/d/1E_77fO-44BtoBtXoVeWyGh1xN2KRTWTu8q6i25RNNx0/edit?usp=sharing
[2] 385104011
This commit is contained in:
Agus Zubiaga 2023-12-06 21:39:00 -03:00
parent c56091ee3e
commit 65ce811587
No known key found for this signature in database
15 changed files with 349 additions and 115 deletions

View file

@ -4,10 +4,11 @@ use crate::pattern::fmt_pattern;
use crate::spaces::{fmt_default_newline, fmt_default_spaces, fmt_spaces, INDENT};
use crate::Buf;
use roc_parse::ast::{
AbilityMember, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, Pattern, Spaced,
Spaces, StrLiteral, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
AbilityMember, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, ImportAlias,
ImportedModuleName, Pattern, Spaced, Spaces, StrLiteral, TypeAnnotation, TypeDef, TypeHeader,
ValueDef,
};
use roc_parse::header::{ExposedName, ModuleName};
use roc_parse::header::ExposedName;
use roc_region::all::Loc;
/// A Located formattable value is also formattable
@ -185,6 +186,48 @@ impl<'a> Formattable for TypeHeader<'a> {
}
}
impl<'a> Formattable for ImportedModuleName<'a> {
fn is_multiline(&self) -> bool {
// No newlines in module name itself.
false
}
fn format_with_options(
&self,
buf: &mut Buf,
_parens: Parens,
_newlines: Newlines,
indent: u16,
) {
buf.indent(indent);
if let Some(package_shorthand) = self.package {
buf.push_str(package_shorthand);
buf.push_str(".");
}
buf.push_str(self.name);
}
}
impl<'a> Formattable for ImportAlias<'a> {
fn is_multiline(&self) -> bool {
// No newlines in alias itself.
false
}
fn format_with_options(
&self,
buf: &mut Buf,
_parens: Parens,
_newlines: Newlines,
indent: u16,
) {
buf.indent(indent);
buf.push_str(self.as_str());
}
}
impl<'a> Formattable for ValueDef<'a> {
fn is_multiline(&self) -> bool {
use roc_parse::ast::ValueDef::*;
@ -256,7 +299,7 @@ impl<'a> Formattable for ValueDef<'a> {
} => {
buf.indent(indent);
buf.push_str("import");
fmt_import_body(buf, &name, &alias, &exposed, indent + INDENT);
fmt_import_body(buf, name, alias, exposed, indent + INDENT);
}
}
}
@ -264,19 +307,18 @@ impl<'a> Formattable for ValueDef<'a> {
fn fmt_import_body<'a>(
buf: &mut Buf,
name: &'a Loc<Spaced<'a, ModuleName>>,
alias: &'a Option<Loc<Spaced<'a, ModuleName>>>,
name: &'a Loc<Spaced<'a, ImportedModuleName<'a>>>,
alias: &'a Option<Loc<Spaced<'a, ImportAlias<'a>>>>,
exposed: &'a Option<(
&[CommentOrNewline<'a>],
Collection<'a, Loc<Spaced<'a, ExposedName<'a>>>>,
)>,
indent: u16,
) -> () {
) {
let name_spaces = name.extract_spaces();
fmt_default_spaces(buf, name_spaces.before, indent);
buf.indent(indent);
buf.push_str(name.value.item().as_str());
name.value.item().format(buf, indent);
fmt_default_spaces(buf, name_spaces.after, indent);
@ -291,9 +333,7 @@ fn fmt_import_body<'a>(
buf.push_str("as");
fmt_default_spaces(buf, alias_spaces.before, indent + INDENT);
buf.indent(indent + INDENT);
buf.push_str(alias_name.value.item().as_str());
alias_name.value.item().format(buf, indent + INDENT);
fmt_default_spaces(buf, alias_spaces.after, indent);

View file

@ -4,9 +4,9 @@ use roc_module::called_via::{BinOp, UnaryOp};
use roc_parse::{
ast::{
AbilityImpls, AbilityMember, AssignedField, Collection, CommentOrNewline, Defs, Expr,
Header, Implements, ImplementsAbilities, ImplementsAbility, ImplementsClause, Module,
Pattern, PatternAs, RecordBuilderField, Spaced, Spaces, StrLiteral, StrSegment, Tag,
TypeAnnotation, TypeDef, TypeHeader, ValueDef, WhenBranch,
Header, Implements, ImplementsAbilities, ImplementsAbility, ImplementsClause, ImportAlias,
ImportedModuleName, Module, Pattern, PatternAs, RecordBuilderField, Spaced, Spaces,
StrLiteral, StrSegment, Tag, TypeAnnotation, TypeDef, TypeHeader, ValueDef, WhenBranch,
},
header::{
AppHeader, ExposedName, HostedHeader, ImportsEntry, InterfaceHeader, KeywordItem,
@ -571,7 +571,7 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
exposed,
} => ModuleImport {
name: name.remove_spaces(arena),
alias: alias.map(|alias_name| alias_name.remove_spaces(arena)),
alias: alias.remove_spaces(arena),
exposed: if let Some((spaces, exposed)) = exposed {
Some((spaces, exposed.remove_spaces(arena)))
} else {
@ -582,6 +582,21 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
}
}
impl<'a> RemoveSpaces<'a> for ImportedModuleName<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self {
ImportedModuleName {
package: self.package.remove_spaces(arena),
name: self.name.remove_spaces(arena),
}
}
}
impl<'a> RemoveSpaces<'a> for ImportAlias<'a> {
fn remove_spaces(&self, _arena: &'a Bump) -> Self {
*self
}
}
impl<'a> RemoveSpaces<'a> for Implements<'a> {
fn remove_spaces(&self, _arena: &'a Bump) -> Self {
Implements::Implements