# Is the node inlinable and can fit on the line.
fun can_inline(n: nullable ANode): Bool do
if n == null then return true
+ if no_inline and n.location.line_start != n.location.line_end then return false
if n.must_be_inline then return true
if n.must_be_block then return false
# check length
- if n.collect_length + current_length > max_size then return false
+ if max_size > 0 and n.collect_length + current_length > max_size then return false
# check block is inlinable
return n.is_inlinable
end
end
end
+ # Consume comments and end of lines if any
+ fun consume_comments do
+ while current_token isa TEol or current_token isa TComment do visit current_token
+ end
+
# Visit `current_token`.
fun consume(token: String) do
+ consume_comments
if current_token.text == token then else current_token.debug("Got `{current_token.text}`; expected `{token}`.")
visit current_token
end
var tab_size = 8
# Max line size.
- var max_size = 80
+ # 0 (or negative) to disable.
+ var max_size = 80 is writable
# Length of the current line.
var current_length = 0
# Do we force the deletion of empty lines?
var skip_empty = false is public writable
+
+ # Disable automatic inlining.
+ var no_inline = false is writable
end
# Base framework redefs
for e in self do
var e_can_inline = v.can_inline(e)
+ if v.current_token isa TComma then v.skip
+
if e != first then
if not e_can_inline then
v.add ","
v.adds
v.visit_list n_extern_calls
else
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
v.add "`\{"
if not lines.first.trim.is_empty then
- v.addn
+ v.forcen
lines.first.l_trim
v.indent += 1
v.addt
v.adds
else
v.visit n_expr
- v.addn
+ v.forcen
v.addt
end
end
end
- if has_else(v) then
- while not v.current_token isa TKwelse do
- v.consume v.current_token.text
- end
+ v.consume_comments
+
+ # FIXME: for some unknown reasons, has_else can be true even if
+ # there is no `else` keyword but a `end` instead.
+ if has_else(v) and v.current_token isa TKwelse then
v.indent -= 1
v.addt
v.visit n_kwfor
v.adds
+ for n_group in n_groups do
+ v.visit n_group
+ if n_group != n_groups.last then v.add ", "
+
+ end
+
+ v.adds
+ v.visit n_kwdo
+ if can_inline then visit_loop_inline v else visit_loop_block v
+ end
+end
+
+redef class AForGroup
+ redef fun accept_pretty_printer(v) do
for n_id in n_ids do
v.visit n_id
if n_id != n_ids.last then v.add ", "
v.consume "in"
v.adds
v.visit n_expr
- v.adds
- v.visit n_kwdo
- if can_inline then visit_loop_inline v else visit_loop_block v
end
end
v.visit_recv n_expr
if not n_expr isa AImplicitSelfExpr and not can_inline then
- v.addn
+ v.forcen
v.addt
end
- v.visit n_id
+ v.visit n_qid
if not n_args.n_exprs.is_empty then
if is_stmt and n_args.n_exprs.length == 1 then
redef class ACallAssignExpr
redef fun accept_pretty_printer(v) do
v.visit_recv n_expr
- v.visit n_id
+ v.visit n_qid
if not n_args.n_exprs.is_empty then
v.consume "("
redef class ACallReassignExpr
redef fun accept_pretty_printer(v) do
v.visit_recv n_expr
- v.visit n_id
+ v.visit n_qid
if not n_args.n_exprs.is_empty then
v.consume "("
v.adds
v.visit n_type
- if n_id != null then
+ if n_qid != null then
v.consume "."
if not can_inline then
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
end
- v.visit n_id
+ v.visit n_qid
end
v.visit_args n_args.n_exprs
else
v.addt
v.visit n_else
- v.addn
+ v.forcen
v.indent -= 1
v.addt
v.add "end"
v.adds
v.visit bin_expr2
else
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
# Syntax
-redef class AUplusExpr
- redef fun accept_pretty_printer(v) do
- v.consume "+"
- v.visit n_expr
- end
-end
-
-redef class AUminusExpr
+redef class AUnaryopExpr
redef fun accept_pretty_printer(v) do
- v.consume "-"
+ v.visit n_op
v.visit n_expr
end
end
redef fun accept_pretty_printer(v) do
v.consume "["
v.visit_list n_exprs
+ if n_type != null then
+ v.consume ":"
+ v.adds
+ v.visit n_type
+ end
v.consume "]"
end
end
while i < text.length do
v.add text[i].to_s
- if v.current_length >= v.max_size and i <= text.length - 3 then
+ if v.max_size > 0 and v.current_length >= v.max_size and i <= text.length - 3 then
v.add "\" +"
if was_inline then
v.forcen