mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
commit
6c0b6751ea
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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]
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 ""
|
||||
|
Loading…
Reference in New Issue
Block a user