Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format lambda body like value declaration expressions #31

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 7 additions & 51 deletions crates/ditto-fmt/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
use super::{
expression::{gen_expression, gen_type_annotation},
has_comments::HasComments,
helpers::{group, space},
expression::{gen_body_expression, gen_type_annotation},
helpers::space,
name::{gen_name, gen_proper_name},
r#type::gen_type,
syntax::gen_parens_list1,
token::{gen_equals, gen_foreign_keyword, gen_pipe, gen_semicolon, gen_type_keyword},
};
use ditto_cst::{
Constructor, Declaration, Expression, ForeignValueDeclaration, Pipe, TypeDeclaration,
ValueDeclaration,
Constructor, Declaration, ForeignValueDeclaration, Pipe, TypeDeclaration, ValueDeclaration,
};
use dprint_core::formatting::{
condition_helpers, conditions, ir_helpers, ConditionResolver, ConditionResolverContext, Info,
PrintItems, Signal,
};
use std::rc::Rc;
use dprint_core::formatting::{ir_helpers, PrintItems, Signal};

pub fn gen_declaration(declaration: Declaration) -> PrintItems {
match declaration {
Expand All @@ -36,47 +30,9 @@ fn gen_value_declaration(decl: ValueDeclaration) -> PrintItems {
items.extend(space());
let equals_has_trailing_comment = decl.equals.0.has_trailing_comment();
items.extend(gen_equals(decl.equals));

let expression_start_info = Info::new("start");
let expression_end_info = Info::new("end");

let expression_has_leading_comments = decl.expression.has_leading_comments();
let expression_deserves_new_line_if_multi_lines =
matches!(decl.expression, Expression::If { .. });

let expression_should_be_on_new_line: ConditionResolver =
Rc::new(move |ctx: &mut ConditionResolverContext| -> Option<bool> {
if equals_has_trailing_comment || expression_has_leading_comments {
return Some(true);
}
if expression_deserves_new_line_if_multi_lines {
return condition_helpers::is_multiple_lines(
ctx,
&expression_start_info,
&expression_end_info,
);
}
// return Some(false);
None // NOTE I'm not sure what the implications are of None vs Some(false) ?
});

items.push_condition(conditions::if_true_or(
"valueDeclarationExpressionOnNewLine",
expression_should_be_on_new_line,
{
let mut items = PrintItems::new();
items.push_info(expression_start_info);
items.extend(group(gen_expression(decl.expression.clone()), true));
items.push_info(expression_end_info);
items
},
{
let mut items = PrintItems::new();
items.push_info(expression_start_info);
items.extend(group(gen_expression(decl.expression.clone()), false));
items.push_info(expression_end_info);
items
},
items.extend(gen_body_expression(
decl.expression,
equals_has_trailing_comment,
));

items.extend(gen_semicolon(decl.semicolon));
Expand Down
58 changes: 52 additions & 6 deletions crates/ditto-fmt/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,7 @@ pub fn gen_expression(expr: Expression) -> PrintItems {

let right_arrow_has_trailing_comment = right_arrow.0.has_trailing_comment();
items.extend(gen_right_arrow(right_arrow));

let body_has_leading_comments = body.has_leading_comments();
items.extend(group(
gen_expression(body),
right_arrow_has_trailing_comment || body_has_leading_comments,
));
items.extend(gen_body_expression(body, right_arrow_has_trailing_comment));
items
}
Expression::Call {
Expand All @@ -164,6 +159,50 @@ pub fn gen_expression(expr: Expression) -> PrintItems {
}
}

/// Generated a "body" expression, i.e. an expression on the right-hand-side
/// of an `=` or `->`.
pub fn gen_body_expression(expr: Expression, force_use_new_lines: bool) -> PrintItems {
let mut items = PrintItems::new();

let start_info = Info::new("start");
let end_info = Info::new("end");

let has_leading_comments = expr.has_leading_comments();
let deserves_new_line_if_multi_lines = matches!(expr, Expression::If { .. });

let expression_should_be_on_new_line: ConditionResolver =
Rc::new(move |ctx: &mut ConditionResolverContext| -> Option<bool> {
if force_use_new_lines || has_leading_comments {
return Some(true);
}
if deserves_new_line_if_multi_lines {
return condition_helpers::is_multiple_lines(ctx, &start_info, &end_info);
}
// return Some(false);
None // NOTE I'm not sure what the implications are of None vs Some(false) ?
});

items.push_condition(conditions::if_true_or(
"bodyExpressionOnNewLine",
expression_should_be_on_new_line,
{
let mut items = PrintItems::new();
items.push_info(start_info);
items.extend(group(gen_expression(expr.clone()), true));
items.push_info(end_info);
items
},
{
let mut items = PrintItems::new();
items.push_info(start_info);
items.extend(group(gen_expression(expr), false));
items.push_info(end_info);
items
},
));
items
}

pub fn gen_type_annotation(type_annotation: TypeAnnotation) -> PrintItems {
let mut items = PrintItems::new();
items.extend(gen_colon(type_annotation.0));
Expand Down Expand Up @@ -289,6 +328,13 @@ mod tests {
);
assert_fmt!("() -> [\n\t-- comment\n]");
assert_fmt!("() ->\n\t-- comment\n\t[5]");

assert_fmt!("() -> if true then yeh else nah");
assert_fmt!(
"() -> if loooooooooong then x else y",
"() ->\n\tif loooooooooong then\n\t\tx\n\telse\n\t\ty",
20
);
}

#[test]
Expand Down