nitpretty: does not force `do` inlining on APropdefs anymore
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 13 Jan 2015 16:52:40 +0000 (17:52 +0100)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 13 Jan 2015 16:52:40 +0000 (17:52 +0100)
But also let the user chose with --inline-do

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitpretty.nit
src/pretty.nit
tests/nitpretty.args
tests/sav/nitpretty_args25.res
tests/sav/nitpretty_args28.res
tests/sav/nitpretty_args29.res
tests/sav/nitpretty_args30.res [new file with mode: 0644]
tests/sav/nitpretty_args31.res [new file with mode: 0644]
tests/sav/nitpretty_args7.res
tests/test_pretty/test_prop1.nit

index 4d4e613..545eb58 100644 (file)
@@ -35,6 +35,10 @@ redef class ToolContext
        # 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
@@ -55,9 +59,10 @@ end
 # 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."
@@ -87,6 +92,9 @@ var v = new PrettyPrinterVisitor
 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
index 5c4bb96..bc556ad 100644 (file)
@@ -183,7 +183,7 @@ class PrettyPrinterVisitor
                        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.
@@ -246,6 +246,9 @@ class PrettyPrinterVisitor
 
        # 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
@@ -792,17 +795,33 @@ redef class APropdef
        #
        # 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
index 3af0759..fa592d3 100644 (file)
@@ -27,3 +27,5 @@ test_pretty/test_prims1.nit
 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
index 99d53dd..e8ba342 100644 (file)
@@ -15,7 +15,8 @@
 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
@@ -34,7 +35,8 @@ class Foo
                return nb
        end
 
-       fun gaz: Int do
+       fun gaz: Int
+       do
                if bar then # 3
                        return 1
                else
index e678bbb..83c9064 100644 (file)
@@ -29,7 +29,8 @@ class B
        # comment 3
        redef fun foo do return bar # comment
 
-       redef fun bar do
+       redef fun bar
+       do
                return 10 # comment 4
        end
 
@@ -40,6 +41,11 @@ class B
                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
index b8d17b3..8f24177 100644 (file)
@@ -15,7 +15,8 @@
 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
@@ -34,7 +35,8 @@ class Foo
                return nb
        end
 
-       fun gaz: Int do
+       fun gaz: Int
+       do
                if bar then # 3
                        return 1
                else
diff --git a/tests/sav/nitpretty_args30.res b/tests/sav/nitpretty_args30.res
new file mode 100644 (file)
index 0000000..4820e6d
--- /dev/null
@@ -0,0 +1,48 @@
+# 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
diff --git a/tests/sav/nitpretty_args31.res b/tests/sav/nitpretty_args31.res
new file mode 100644 (file)
index 0000000..3364250
--- /dev/null
@@ -0,0 +1,109 @@
+# 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
index 4a7d663..ea248bc 100644 (file)
@@ -29,7 +29,8 @@ class B
        # comment 3
        redef fun foo do return bar # comment
 
-       redef fun bar do
+       redef fun bar
+       do
                return 10 # comment 4
        end
 
@@ -39,6 +40,11 @@ class B
        fun other: String do
                return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        end
+
+       fun foo1(arr: Array[String], len: Int, ind: Int): String
+       do
+               return "Hello World!"
+       end
 end
 
 # end
index 0b46c27..3b692a5 100644 (file)
@@ -35,6 +35,11 @@ class B
        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