ropes: Fix `RopeBuffer.reset`.
[nit.git] / src / nitpretty.nit
index 99834a8..141ef1b 100644 (file)
 # `Licence comments` are attached to the top of the file
 # no blank line before, one after.
 #
-#     # This is a licence comment
+# ~~~nitish
+# # This is a licence comment
 #
-#     # Documentation for module `foo`
-#     module foo
+# # Documentation for module `foo`
+# module foo
+# ~~~
 #
 # `ADoc` are documentation comments attached to a `AModule`, `AClassdef`, `APropdef`.
 #
 # They are printed before the definition with a blank line before and no after
 # at the same indentation level than the definition.
 #
-#     # Documentation for module `foo`
-#     module foo
+# ~~~nitish
+# # Documentation for module `foo`
+# module foo
 #
-#     # Documentation for class `Bar`
-#     class Bar
-#          # Documentation for method `baz`
-#          fun baz do end
-#     end
+# # Documentation for class `Bar`
+# class Bar
+#      # Documentation for method `baz`
+#      fun baz do end
+# end
+# ~~~
 #
 # `Block comments` are comments composed of one or more line rattached to nothing.
 # They are displayed with one blank line before and after at current indent level.
 #
-#     <blank>
-#     # block
-#     # comment
-#     <blank>
+# ~~~nitish
+# <blank>
+# # block
+# # comment
+# <blank>
+# ~~~
 #
 # `Attached comments` are comments attached to a production.
 # They are printed as this.
 #
-#     fun foo do # attached comment
-#     end
+# ~~~nitish
+# fun foo do # attached comment
+# end
+# ~~~
 #
 # `nitpretty` automatically remove multiple blanks between comments:
 #
-#     # Licence
-#     # ...
-#     <blank>
-#     # Block comment
+# ~~~nitish
+# # Licence
+# # ...
+# <blank>
+# # Block comment
+# ~~~
 #
 # ### Inlining
 #
 # * There is a blank between each class definition
 # * There is no blank line at the end of the module
 #
-#     # Documentation for module `foo`
-#     module foo
+# ~~~nitish
+# # Documentation for module `foo`
+# module foo
 #
-#     import a
-#     # import b
-#     import c
+# import a
+# # import b
+# import c
 #
-#     # Documentation for class `Bar`
-#     class Bar end
+# # Documentation for class `Bar`
+# class Bar end
 #
-#     class Baz end # not a `ADoc` comment
+# class Baz end # not a `ADoc` comment
+# ~~~
 #
 #
 # ### Classes
 # * There is a blank between each block definition
 # * There no blank line at the end of the class definition
 #
-#     # Documentation for class `Bar`
-#     class Bar end
+# ~~~nitish
+# # Documentation for class `Bar`
+# class Bar end
 #
-#     class Baz
-#          super Bar
+# class Baz
+#     super Bar
 #
-#          fun a is abstract
-#          private fun b do end
+#      fun a is abstract
+#      private fun b do end
 #
-#          fun c do
-#               # ...
-#          end
-#     end
+#      fun c do
+#           # ...
+#      end
+# end
+# ~~~
 #
-# Generic types have no espace after or before brackets and are separated by a comma and a space:
+# Generic types have no space after or before brackets and are separated by a comma and a space:
 #
-#     class A[E: Type1, F: Type1] do end
+# ~~~nitish
+# class A[E: Type1, F: Type1] end
+# ~~~
 #
 # ### Blocks
 #
 # * Inlined productions have no blank lines between them
 # * Block productions have a blank before and after
 #
-#     var a = 10
-#     var b = 0
+# ~~~nitish
+# var a = 10
+# var b = 0
 #
-#     if a > b then
-#          # is positive
-#          print "positive"
-#     end
+# if a > b then
+#      # is positive
+#      print "positive"
+# end
 #
-#     print "end"
+# print "end"
+# ~~~
 #
 # ### Calls and Binary Ops
 #
 # Arguments are always printed separated with a comma and a space:
 #
-#     foo(a, b, c)
+# ~~~nitish
+# foo(a, b, c)
+# ~~~
 #
 # Binary ops are always printed wrapped with spaces:
 #
-#     var c = 1 + 2
+# ~~~nitish
+# var c = 1 + 2
+# ~~~
 #
 # Calls and binary ops can be splitted to fit the `max-size` constraint.
 # Breaking priority is given to arguments declaration after the comma.
 #
-#     return foo("aaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbb",
-#        "cccccccccccccccccccccccccc")
+# ~~~nitish
+# return foo("aaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbb",
+#     "cccccccccccccccccccccccccc")
+# ~~~
 #
 # Binary ops can also be broken to fit the `max-size` limit:
 #
-#     return "aaaaaaaaaaaaaaaaaaaaaaaaaa" + "bbbbbbbbbbbbbbbbbbbbbbbbbbb" +
-#        "cccccccccccccccccccccccccc"
-#
+# ~~~nitish
+# return "aaaaaaaaaaaaaaaaaaaaaaaaaa" + "bbbbbbbbbbbbbbbbbbbbbbbbbbb" +
+#     "cccccccccccccccccccccccccc"
+# ~~~
 module nitpretty
 
 import template
@@ -616,21 +641,21 @@ redef class AAnnotation
        redef fun accept_pretty_printer(v) do
                v.visit n_atid
                if not n_args.is_empty then
-                       v.visit n_opar
+                       if n_opar == null then
+                               v.adds
+                       else
+                               v.visit n_opar
+                       end
                        v.visit_list n_args
                        v.visit n_cpar
                end
        end
 end
 
-redef class ATypeAtArg
+redef class ATypeExpr
        redef fun accept_pretty_printer(v) do v.visit n_type
 end
 
-redef class AExprAtArg
-       redef fun accept_pretty_printer(v) do v.visit n_expr
-end
-
 # Modules
 
 redef class AModule
@@ -928,8 +953,7 @@ redef class AAttrPropdef
                super
                v.visit n_kwvar
                v.adds
-               if n_id != null then v.visit n_id
-               if n_id2 != null then v.visit n_id2
+               v.visit n_id2
 
                if n_type != null then
                        v.consume ":"
@@ -937,16 +961,6 @@ redef class AAttrPropdef
                        v.visit n_type
                end
 
-               if n_readable != null then
-                       v.adds
-                       v.visit n_readable
-               end
-
-               if n_writable != null then
-                       v.adds
-                       v.visit n_writable
-               end
-
                if n_expr != null then
                        v.adds
                        v.consume "="
@@ -987,6 +1001,10 @@ end
 
 redef class AMethPropdef
        redef fun accept_pretty_printer(v) do
+               #  TODO: Handle extern annotations
+
+               var before = v.indent
+               var can_inline = v.can_inline(self)
                super
                if n_kwinit != null then v.visit n_kwinit
                if n_kwmeth != null then v.visit n_kwmeth
@@ -1004,94 +1022,13 @@ redef class AMethPropdef
                else
                        v.adds
                end
-       end
-
-       # Can be inlined if:
-       # * block is empty or can be inlined
-       # * contains no comments
-       redef fun is_inlinable do
-               if not super then return false
-               if n_annotations != null and not n_annotations.is_inlinable then return false
-               if n_block != null and not n_block.is_inlinable then return false
-               if not collect_comments.is_empty then return false
-               return true
-       end
-end
-
-redef class AMainMethPropdef
-       redef fun accept_pretty_printer(v) do
-               v.visit n_block
-               v.addn
-       end
-end
-
-redef class ADeferredMethPropdef
-       redef fun accept_pretty_printer(v) do
-               super
-               if n_annotations == null then
-                       while not v.current_token isa TKwis do v.skip
-                       v.consume "is"
-                       v.adds
-                       while not v.current_token isa TKwabstract do v.skip
-                       v.consume "abstract"
-               end
-               v.finish_line
-               v.addn
-       end
-end
-
-redef class AExternPropdef
-       redef fun accept_pretty_printer(v) do
-               super
-               while v.current_token isa TEol do v.skip
 
-               if v.current_token isa TKwis then
-                       v.consume "is"
-                       v.adds
-               end
-
-               if v.current_token isa TKwextern then
-                       v.consume "extern"
-                       v.adds
+               if n_extern_calls != null or n_extern_code_block != null then
+                       if n_annotations != null then v.adds
+                       if n_extern_calls != null then v.visit n_extern_calls
+                       if n_extern_code_block != null then v.visit n_extern_code_block
                end
 
-               if n_extern != null then v.visit n_extern
-               if n_extern_calls != null then v.visit n_extern_calls
-               if n_extern_code_block != null then v.visit n_extern_code_block
-               v.finish_line
-               v.addn
-       end
-
-       redef fun is_inlinable do
-               if not super then return false
-               if n_block != null and not n_block.is_inlinable then return false
-               if n_extern_calls != null and not n_extern_calls.is_inlinable then return false
-               if n_extern_code_block != null and not n_extern_code_block.is_inlinable then return false
-               return true
-       end
-
-       redef fun must_be_inline do
-               if super then return true
-               return n_extern != null
-       end
-end
-
-redef class AInternMethPropdef
-       redef fun accept_pretty_printer(v) do
-               super
-               v.consume "is"
-               v.adds
-               v.consume "intern"
-               v.finish_line
-               v.addn
-       end
-end
-
-redef class AConcreteMethPropdef
-       redef fun accept_pretty_printer(v) do
-               var before = v.indent
-               var can_inline = v.can_inline(self)
-               super
                var n_block = self.n_block
 
                if n_block != null then
@@ -1149,6 +1086,26 @@ redef class AConcreteMethPropdef
                v.addn
                assert v.indent == before
        end
+
+       # Can be inlined if:
+       # * block is empty or can be inlined
+       # * contains no comments
+       redef fun is_inlinable do
+               if not super then return false
+               if n_annotations != null and not n_annotations.is_inlinable then return false
+               if n_block != null and not n_block.is_inlinable then return false
+               if n_extern_calls != null and not n_extern_calls.is_inlinable then return false
+               if n_extern_code_block != null and not n_extern_code_block.is_inlinable then return false
+               if not collect_comments.is_empty then return false
+               return true
+       end
+end
+
+redef class AMainMethPropdef
+       redef fun accept_pretty_printer(v) do
+               v.visit n_block
+               v.addn
+       end
 end
 
 redef class ASignature
@@ -1903,34 +1860,6 @@ redef class AAttrReassignExpr
        end
 end
 
-redef class AAble
-       redef fun accept_pretty_printer(v) do
-               if n_kwredef != null then
-                       v.visit n_kwredef
-                       v.adds
-               end
-
-               if not n_visibility isa APublicVisibility then
-                       v.visit n_visibility
-                       v.adds
-               end
-       end
-end
-
-redef class AReadAble
-       redef fun accept_pretty_printer(v) do
-               super
-               v.visit n_kwreadable
-       end
-end
-
-redef class AWriteAble
-       redef fun accept_pretty_printer(v) do
-               super
-               v.visit n_kwwritable
-       end
-end
-
 # Exprs
 
 redef class AVardeclExpr
@@ -2344,7 +2273,6 @@ end
 
 redef class ASuperstringExpr
        redef fun accept_pretty_printer(v) do
-               var force_inline = self.force_inline
                for n_expr in n_exprs do v.visit n_expr
        end