Module: Jade::Formatter::Lambda
Constant Summary
collapse
- INLINE_BODY =
Atoms (and a few near-atoms) that read fine on a single line next to the lambda head. Everything else forces the multi-line ‘{ body }` shape.
[
AST::Literal, AST::CharLiteral, AST::VariableReference,
AST::ConstructorReference, AST::FunctionCall, AST::RecordAccess,
AST::MemberAccess, AST::InfixApplication, AST::RecordLiteral,
AST::List, AST::Tuple, AST::Grouping, AST::RecordUpdate,
AST::RecordUpdateSugar, AST::RecordAccessSugar,
].freeze
Instance Method Summary
collapse
Methods included from Helper
and_indent, dispatch_for, format_delimited, format_exposing, format_leading_comments, format_node, format_pattern, format_trailing_comment, format_type, format_type_atom, too_long?
Instance Method Details
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/jade/formatter/lambda.rb', line 18
def format(node, indent:, source:)
node => AST::Lambda(params:, body:)
head = format_head(params)
if inline_body?(body)
"#{head} { #{format_node(body.expressions.first, source:)} }"
.then(&and_indent(indent))
else
[
"#{head} {".then(&and_indent(indent)),
format_node(body, indent: indent + 1, source:),
"}".then(&and_indent(indent)),
].join("\n")
end
end
|
35
36
37
38
39
40
41
42
|
# File 'lib/jade/formatter/lambda.rb', line 35
def format_head(params)
return "->" if params.empty?
params
.map { format_pattern(it) }
.join(', ')
.then { "(#{it}) ->" }
end
|
#inline_body?(body) ⇒ Boolean
44
45
46
47
|
# File 'lib/jade/formatter/lambda.rb', line 44
def inline_body?(body)
body.expressions.length == 1 &&
INLINE_BODY.any? { body.expressions.first.is_a?(it) }
end
|