nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
nitiwiki: extends the example with an external highlighter
[nit.git]
/
src
/
pretty.nit
diff --git
a/src/pretty.nit
b/src/pretty.nit
index
b4a14b3
..
d55e248
100644
(file)
--- a/
src/pretty.nit
+++ b/
src/pretty.nit
@@
-121,10
+121,11
@@
class PrettyPrinterVisitor
# Is the node inlinable and can fit on the line.
fun can_inline(n: nullable ANode): Bool do
if n == null then return true
# 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.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
# check block is inlinable
return n.is_inlinable
end
@@
-219,7
+220,8
@@
class PrettyPrinterVisitor
var tab_size = 8
# Max line size.
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
# Length of the current line.
var current_length = 0
@@
-283,6
+285,9
@@
class PrettyPrinterVisitor
# Do we force the deletion of empty lines?
var skip_empty = false is public writable
# 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
end
# Base framework redefs
@@
-1058,7
+1063,7
@@
redef class AExternCalls
v.adds
v.visit_list n_extern_calls
else
v.adds
v.visit_list n_extern_calls
else
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
v.indent += 1
v.addt
v.indent -= 1
@@
-1158,7
+1163,7
@@
redef class TExternCodeSegment
v.add "`\{"
if not lines.first.trim.is_empty then
v.add "`\{"
if not lines.first.trim.is_empty then
- v.addn
+ v.forcen
lines.first.l_trim
v.indent += 1
v.addt
lines.first.l_trim
v.indent += 1
v.addt
@@
-1235,7
+1240,7
@@
redef class AIfExpr
v.adds
else
v.visit n_expr
v.adds
else
v.visit n_expr
- v.addn
+ v.forcen
v.addt
end
v.addt
end
@@
-1287,7
+1292,10
@@
redef class AIfExpr
end
v.consume_comments
end
v.consume_comments
- if has_else(v) then
+
+ # 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.indent -= 1
v.addt
@@
-1574,11
+1582,11
@@
redef class ACallExpr
v.visit_recv n_expr
if not n_expr isa AImplicitSelfExpr and not can_inline then
v.visit_recv n_expr
if not n_expr isa AImplicitSelfExpr and not can_inline then
- v.addn
+ v.forcen
v.addt
end
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
if not n_args.n_exprs.is_empty then
if is_stmt and n_args.n_exprs.length == 1 then
@@
-1599,7
+1607,7
@@
end
redef class ACallAssignExpr
redef fun accept_pretty_printer(v) do
v.visit_recv n_expr
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 "("
if not n_args.n_exprs.is_empty then
v.consume "("
@@
-1617,7
+1625,7
@@
end
redef class ACallReassignExpr
redef fun accept_pretty_printer(v) do
v.visit_recv n_expr
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 "("
if not n_args.n_exprs.is_empty then
v.consume "("
@@
-1719,17
+1727,17
@@
redef class ANewExpr
v.adds
v.visit n_type
v.adds
v.visit n_type
- if n_id != null then
+ if n_qid != null then
v.consume "."
if not can_inline then
v.consume "."
if not can_inline then
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
end
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
end
v.visit_args n_args.n_exprs
@@
-1841,7
+1849,7
@@
redef class AAssertExpr
else
v.addt
v.visit n_else
else
v.addt
v.visit n_else
- v.addn
+ v.forcen
v.indent -= 1
v.addt
v.add "end"
v.indent -= 1
v.addt
v.add "end"
@@
-1969,7
+1977,7
@@
private class ABinOpHelper
v.adds
v.visit bin_expr2
else
v.adds
v.visit bin_expr2
else
- v.addn
+ v.forcen
v.indent += 1
v.addt
v.indent -= 1
v.indent += 1
v.addt
v.indent -= 1
@@
-2036,16
+2044,9
@@
end
# Syntax
# 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
redef fun accept_pretty_printer(v) do
- v.consume "-"
+ v.visit n_op
v.visit n_expr
end
end
v.visit n_expr
end
end
@@
-2115,7
+2116,7
@@
redef class AStringFormExpr
while i < text.length do
v.add text[i].to_s
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
v.add "\" +"
if was_inline then
v.forcen