Merge pull request #2674 from rtfeldman/roc_fmt_fixes

Roc fmt fixes
This commit is contained in:
Anton-4 2022-03-08 14:36:39 +01:00 committed by GitHub
commit 6c0b6751ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 42 deletions

View File

@ -35,7 +35,7 @@ pub enum Parens {
/// we also want to show newlines. By default the formatter
/// takes care of inserting newlines, but sometimes the user's
/// newlines are taken into account.
#[derive(PartialEq, Eq, Clone, Copy)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Newlines {
No,
Yes,

View File

@ -143,11 +143,17 @@ impl<'a> Formattable for Expr<'a> {
} else {
buf.indent(indent);
buf.push('(');
let next_indent = if starts_with_newline(sub_expr) {
indent + INDENT
} else {
indent
};
sub_expr.format_with_options(
buf,
Parens::NotNeeded,
Newlines::Yes,
indent + INDENT,
next_indent,
);
buf.indent(indent);
buf.push(')');
@ -304,6 +310,30 @@ impl<'a> Formattable for Expr<'a> {
}
}
fn starts_with_newline(expr: &Expr) -> bool {
use roc_parse::ast::Expr::*;
match expr {
SpaceBefore(_, comment_or_newline) => {
if !comment_or_newline.is_empty() {
// safe because we check the length before
comment_or_newline.get(0).unwrap().is_newline()
} else {
false
}
}
SpaceAfter(_, comment_or_newline) => {
if !(**comment_or_newline).is_empty() {
// safe because we check the length before
comment_or_newline.get(0).unwrap().is_newline()
} else {
false
}
}
_ => false,
}
}
fn format_str_segment<'a, 'buf>(seg: &StrSegment<'a>, buf: &mut Buf<'buf>, indent: u16) {
use StrSegment::*;
@ -440,7 +470,13 @@ fn fmt_bin_ops<'a, 'buf>(
buf.spaces(1);
}
loc_right_side.format_with_options(buf, apply_needs_parens, Newlines::Yes, indent);
let next_indent = if is_multiline {
indent + INDENT
} else {
indent
};
loc_right_side.format_with_options(buf, apply_needs_parens, Newlines::Yes, next_indent);
}
fn empty_line_before_expr<'a>(expr: &'a Expr<'a>) -> bool {

View File

@ -2582,6 +2582,30 @@ mod test_fmt {
));
}
#[test]
fn apply_lambda() {
expr_formats_same(indoc!(
r#"
List.map
xs
(\i ->
i + length)
"#
));
}
#[test]
fn pipline_apply_lambda() {
expr_formats_same(indoc!(
r#"
shout
|> List.map
xs
(\i -> i)
"#
));
}
// MODULES
#[test]

View File

@ -9,13 +9,13 @@ forever = \task ->
looper = \{ } ->
task
|> Effect.map
\res ->
when res is
Ok _ ->
Step {}
\res ->
when res is
Ok _ ->
Step {}
Err e ->
Done (Err e)
Err e ->
Done (Err e)
Effect.loop {} looper
@ -24,16 +24,16 @@ loop = \state, step ->
looper = \current ->
step current
|> Effect.map
\res ->
when res is
Ok (Step newState) ->
Step newState
\res ->
when res is
Ok (Step newState) ->
Step newState
Ok (Done result) ->
Done (Ok result)
Ok (Done result) ->
Done (Ok result)
Err e ->
Done (Err e)
Err e ->
Done (Err e)
Effect.loop state looper

View File

@ -9,16 +9,16 @@ loop = \state, step ->
looper = \current ->
step current
|> Effect.map
\res ->
when res is
Ok (Step newState) ->
Step newState
\res ->
when res is
Ok (Step newState) ->
Step newState
Ok (Done result) ->
Done (Ok result)
Ok (Done result) ->
Done (Ok result)
Err e ->
Done (Err e)
Err e ->
Done (Err e)
Effect.loop state looper

View File

@ -9,13 +9,13 @@ forever = \task ->
looper = \{ } ->
task
|> Effect.map
\res ->
when res is
Ok _ ->
Step {}
\res ->
when res is
Ok _ ->
Step {}
Err e ->
Done (Err e)
Err e ->
Done (Err e)
Effect.loop {} looper
@ -24,16 +24,16 @@ loop = \state, step ->
looper = \current ->
step current
|> Effect.map
\res ->
when res is
Ok (Step newState) ->
Step newState
\res ->
when res is
Ok (Step newState) ->
Step newState
Ok (Done result) ->
Done (Ok result)
Ok (Done result) ->
Done (Ok result)
Err e ->
Done (Err e)
Err e ->
Done (Err e)
Effect.loop state looper

View File

@ -24,10 +24,10 @@ echo = \shout ->
|> Str.toUtf8
|> List.mapWithIndex
(\_, i ->
length = (List.len (Str.toUtf8 shout) - i)
phrase = (List.split (Str.toUtf8 shout) length).before
length = (List.len (Str.toUtf8 shout) - i)
phrase = (List.split (Str.toUtf8 shout) length).before
List.concat (silence (if i == 0 then 2 * length else length)) phrase)
List.concat (silence (if i == 0 then 2 * length else length)) phrase)
|> List.join
|> Str.fromUtf8
|> Result.withDefault ""