# Break too long string literals.
var opt_break_str = new OptionBool("Break too long string literals", "--break-strings")
+ # Force `do` on the same line as the method signature.
+ var opt_inline_do = new OptionBool("Force do keyword on the same line as the method signature",
+ "--inline-do")
+
# Check formatting instead of pretty printing.
#
# This option create a tempory pretty printed file then check if
# process options
var toolcontext = new ToolContext
-toolcontext.option_context.
- add_option(toolcontext.opt_dir, toolcontext.opt_output, toolcontext.opt_diff,
- toolcontext.opt_meld, toolcontext.opt_check, toolcontext.opt_break_str)
+var opts = toolcontext.option_context
+opts.add_option(toolcontext.opt_dir, toolcontext.opt_output)
+opts.add_option(toolcontext.opt_diff, toolcontext.opt_meld, toolcontext.opt_check)
+opts.add_option(toolcontext.opt_break_str, toolcontext.opt_inline_do)
toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
"Pretty print Nit code from Nit source files."
if toolcontext.opt_break_str.value then
v.break_strings = true
end
+if toolcontext.opt_inline_do.value then
+ v.inline_do = true
+end
for mmodule in mmodules do
if not mbuilder.mmodule2nmodule.has_key(mmodule) then
visit current_token
end
- while current_token isa TEol do skip
+ while current_token isa TEol do visit(current_token)
end
# The template under construction.
# Do we break string literals that are too long?
var break_strings = false is public writable
+
+ # Do we force `do` to be on the same line as the method signature?
+ var inline_do = false is public writable
end
# Base framework redefs
#
# Were annotations printed inline? If so, we need to print the block differently.
fun visit_block(v: PrettyPrinterVisitor, n_block: nullable AExpr, annot_inline: Bool) do
+ var can_inline = v.can_inline(n_block)
if n_block == null then return
- while not v.current_token isa TKwdo do v.skip
if n_annotations != null and not annot_inline then
v.addn
v.addt
- else
- v.adds
end
+ if v.inline_do then
+ while not v.current_token isa TKwdo do v.skip
+ end
+ var token = v.current_token
+ var do_inline = true
+ loop
+ if token isa TEol then
+ v.skip
+ if not v.can_inline(n_block) then
+ v.addn
+ v.addt
+ do_inline = false
+ end
+ end
+ token = v.current_token
+ if token isa TKwdo then break
+ end
+ if annot_inline and do_inline then v.adds
v.consume "do"
- if v.can_inline(n_block) then
+ if v.can_inline(n_block) and do_inline then
v.adds
if n_block isa ABlockExpr then
test_pretty/test_annot1.nit
--break-strings test_pretty/test_prop1.nit
--break-strings test_pretty/test_indent1.nit
+--inline-do test_pretty/test_prop1.nit
+--inline-do test_pretty/test_indent1.nit
class Foo
fun bar: Bool do return true
- fun foo(other: Foo): Foo do
+ fun foo(other: Foo): Foo
+ do
if other.bar then
return other
else
return nb
end
- fun gaz: Int do
+ fun gaz: Int
+ do
if bar then # 3
return 1
else
# comment 3
redef fun foo do return bar # comment
- redef fun bar do
+ redef fun bar
+ do
return 10 # comment 4
end
return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaa"
end
+
+ fun foo1(arr: Array[String], len: Int, ind: Int): String
+ do
+ return "Hello World!"
+ end
end
# end
\ No newline at end of file
class Foo
fun bar: Bool do return true
- fun foo(other: Foo): Foo do
+ fun foo(other: Foo): Foo
+ do
if other.bar then
return other
else
return nb
end
- fun gaz: Int do
+ fun gaz: Int
+ do
if bar then # 3
return 1
else
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# comment 1
+class A
+ type FOO: Discrete
+ private var foo: FOO # comment
+
+ # comment 2
+ var bar: Int = 10
+end
+
+class B
+ super A
+
+ redef type FOO: Int
+
+ # comment 3
+ redef fun foo do return bar # comment
+
+ redef fun bar do
+ return 10 # comment 4
+ end
+
+ fun baz do return # comment 5
+ protected fun baz2 do end
+
+ fun other: String do
+ return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ end
+
+ fun foo1(arr: Array[String], len: Int, ind: Int): String do
+ return "Hello World!"
+ end
+end
+
+# end
\ No newline at end of file
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class Foo
+ fun bar: Bool do return true
+
+ fun foo(other: Foo): Foo do
+ if other.bar then
+ return other
+ else
+ return self
+ end
+ end
+
+ fun baz: Int do
+ var nb = 0
+
+ while nb < 10 do
+ print nb
+ nb += 1
+ end # 1
+
+ return nb
+ end
+
+ fun gaz: Int do
+ if bar then # 3
+ return 1
+ else
+ return -1 # 4
+ end
+ end
+end
+
+class Test[E]
+ var heap: ArrayHeap[E]
+ init to(comparator: Comparator[E]) do heap = new ArrayHeap[E](comparator)
+
+ init from(comparator: Comparator[E], items: Collection[E]) do
+ heap = new ArrayHeap[E].from(comparator, items.to_a)
+ end
+
+ fun count(k: E): Int do
+ if heap.has(k) then
+ return 1
+ else
+ return 0
+ end
+ end
+
+ fun node_at_idx(i: Int, k: E) do
+ while heap != null do
+ if heap.is_empty or i == k then # FIXME prefilter because the compiler is not smart enought yet
+ break
+ end
+ end
+ end
+
+ fun save_those_nodes(nodes: Collection[Object]) do
+ for node in nodes do count(node)
+ end
+end
+
+fun foo do
+ if last_slash > 0 then
+ return substring(last_slash + 1, length)
+ else
+ return null
+ end
+end
+
+print "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tincidunt sapien et velit fringilla varius at non eros. Nunc ut ultricies metus, sit amet lacinia felis. Donec in facilisis neque, non laoreet nibh. Etiam nec purus eu orci congue iaculis eu quis lorem. Ut et blandit erat. Cras fermentum pellentesque ante, ut dapibus ipsum placerat sit amet. Vivamus pharetra, sem vitae consequat venenatis, diam risus placerat est, sed hendrerit purus justo vitae lectus. In id quam mattis, rutrum augue eu, vehicula ipsum. Nulla nec egestas turpis, nec ullamcorper odio. Pellentesque vitae arcu justo. Aliquam sed pharetra lacus."
+
+var lorem = "lorem"
+var ipsum = "ipsum" # for fun
+
+print "We also need to handle super strings: {lorem} {ipsum} dolor sit amet, consectetur adipiscing elit. Aliquam tincidunt sapien et velit fringilla varius at non eros. Nunc ut ultricies metus, sit amet lacinia felis. Donec in facilisis neque, non laoreet nibh. Etiam nec purus eu orci congue iaculis eu quis {lorem}. Ut et blandit erat. Cras fermentum pellentesque ante, ut dapibus {ipsum} placerat sit amet. Vivamus pharetra, sem vitae consequat venenatis, diam risus placerat est, sed hendrerit purus justo vitae lectus. In id quam mattis, rutrum augue eu, vehicula ipsum. Nulla nec egestas turpis, nec ullamcorper odio. Pellentesque vitae arcu justo. Aliquam sed pharetra lacus." # ending
+
+var title = "title"
+var links = new Array[String] # why not?
+
+var body = """
+<!DOCTYPE html>
+<head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
+ <title>{{{title}}}</title>
+</head>
+<body>
+ <div class="container">
+ <h1>{{{title}}}</h1>
+ <ul>
+ <li>{{{links.join("</li>\n\t\t\t<li>")}}}</li>
+ </ul>
+ </div>
+</body>
+</html>"""
\ No newline at end of file
# comment 3
redef fun foo do return bar # comment
- redef fun bar do
+ redef fun bar
+ do
return 10 # comment 4
end
fun other: String do
return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
end
+
+ fun foo1(arr: Array[String], len: Int, ind: Int): String
+ do
+ return "Hello World!"
+ end
end
# end
end # comment 5
protected fun baz2 do end
fun other: String do return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+
+ fun foo1(arr: Array[String], len: Int, ind: Int): String
+ do
+ return "Hello World!"
+ end
end
# end