src/hightlight: hightlight and do not crash on MNotNullType
[nit.git] / src / highlight.nit
index a93fec9..39a042a 100644 (file)
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Highliting of Nit AST
+# Highlighting of Nit AST
 module highlight
 
 import frontend
@@ -27,19 +27,29 @@ class HighlightVisitor
 
        # Is the HTML include a nested `<span class"{type_of_node}">` element for each `ANode` of the AST?
        # Used to have a really huge and verbose HTML (mainly for debug)
-       var with_ast writable = false
+       var with_ast = false is writable
+
+       # Prefixes used in generated IDs for line `<span>` elements.
+       # Useful if more than one highlighted code is present in the same HTML document.
+       #
+       # If set to the empty string, id for lines are disabled.
+       #
+       # Is `"L"` by default.
+       var line_id_prefix = "L" is writable
 
        # The first line to generate, null if start at the first line
-       var first_line: nullable Int writable = null
+       var first_line: nullable Int = null is writable
 
        # The last line to generate, null if finish at the last line
-       var last_line: nullable Int writable = null
+       var last_line: nullable Int = null is writable
 
        init
        do
                html.add_class("nitcode")
        end
 
+       # The entry-point of the highlighting.
+       # Will fill `html` with the generated HTML content.
        fun enter_visit(n: ANode)
        do
                n.parentize_tokens
@@ -52,7 +62,6 @@ class HighlightVisitor
        do
                var stack2 = new Array[HTMLTag]
                var stack = new Array[Prod]
-               var closes = new Array[Prod]
                var line = 0
                var c: nullable Token = first_token
                var hv = new HighlightVisitor
@@ -83,7 +92,8 @@ class HighlightVisitor
 
                                # Add a div for the whole line
                                var tag = new HTMLTag("span")
-                               tag.attrs["id"] = "L{cline}"
+                               var p = line_id_prefix
+                               if p != "" then tag.attrs["id"] = "{p}{cline}"
                                tag.classes.add "line"
                                stack2.add(html)
                                html.add tag
@@ -166,9 +176,9 @@ class HighlightVisitor
                return """
 .nitcode a { color: inherit; cursor:pointer; }
 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
-pre.nitcode .foldable { display: block } /* for block productions*/
-pre.nitcode .line{ display: block } /* for lines */
-pre.nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
+.nitcode .foldable { display: block } /* for block productions*/
+.nitcode .line{ display: block } /* for lines */
+.nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
 /* lexical raw tokens. independent of usage or semantic: */
 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
@@ -321,6 +331,7 @@ redef class MModule
                return res
        end
 
+       # The module HTML page
        fun href: String
        do
                return name + ".html"
@@ -350,6 +361,8 @@ redef class MClassDef
                if mdoc == null then mdoc = mclass.intro.mdoc
                if mdoc != null then mdoc.fill_infobox(res)
 
+               if in_hierarchy == null then return res
+
                if in_hierarchy.greaters.length > 1 then
                        var c = res.new_dropdown("hier", "super-classes")
                        for x in in_hierarchy.greaters do
@@ -376,6 +389,7 @@ redef class MClassDef
                return res
        end
 
+       # The class HTML page (an anchor in the module page)
        fun href: String
        do
                return mmodule.href + "#" + to_s
@@ -396,11 +410,11 @@ redef class MPropDef
                var res = new HInfoBox(v, to_s)
                res.href = href
                if self isa MMethodDef then
-                       res.new_field("fun").append(mproperty.name).add msignature.linkto
+                       if msignature != null then res.new_field("fun").append(mproperty.name).add msignature.linkto
                else if self isa MAttributeDef then
-                       res.new_field("fun").append(mproperty.name).add static_mtype.linkto
+                       if static_mtype != null then res.new_field("fun").append(mproperty.name).add static_mtype.linkto
                else if self isa MVirtualTypeDef then
-                       res.new_field("add").append(mproperty.name).add bound.linkto
+                       if bound != null then res.new_field("add").append(mproperty.name).add bound.linkto
                else
                        res.new_field("wat?").append(mproperty.name)
                end
@@ -422,6 +436,7 @@ redef class MPropDef
                return res
        end
 
+       # The property HTML page (an anchor in the module page)
        fun href: String
        do
                return self.mclassdef.mmodule.href + "#" + self.to_s
@@ -473,13 +488,11 @@ redef class MParameterType
        redef fun infobox(v)
        do
                var res = new HInfoBox(v, to_s)
-               var name = mclass.intro.parameter_names[rank]
                res.new_field("parameter type").append("{name} from class ").add mclass.intro.linkto
                return res
        end
        redef fun linkto
        do
-               var name = mclass.intro.parameter_names[rank]
                return (new HTMLTag("span")).text(name)
        end
 end
@@ -497,6 +510,33 @@ redef class MNullableType
        end
 end
 
+redef class MNotNullType
+       redef fun infobox(v)
+       do
+               return mtype.infobox(v)
+       end
+       redef fun linkto
+       do
+               var res = new HTMLTag("span")
+               res.append("not null ").add(mtype.linkto)
+               return res
+       end
+end
+
+redef class MNullType
+       redef fun infobox(v)
+       do
+               var res = new HInfoBox(v, to_s)
+               return res
+       end
+       redef fun linkto
+       do
+               var res = new HTMLTag("span")
+               res.append("null")
+               return res
+       end
+end
+
 redef class MSignature
        redef fun linkto
        do
@@ -857,8 +897,8 @@ redef class AType
        do
                var mt = mtype
                if mt == null then return null
-               mt = mt.as_notnullable
-               if mt isa MVirtualType or mt isa MParameterType then
+               mt = mt.undecorate
+               if mt isa MFormalType then
                        res.add_class("nc_vt")
                end
                return mt.infobox(v)
@@ -934,4 +974,3 @@ redef class AExpr
                return t.infobox(v)
        end
 end
-