Merge: mdoc_or_fallback: Refactor
authorJean Privat <jean@pryen.org>
Mon, 27 Mar 2017 13:13:56 +0000 (09:13 -0400)
committerJean Privat <jean@pryen.org>
Mon, 27 Mar 2017 13:13:56 +0000 (09:13 -0400)
Centralize its definition and update its documentation.

Pull-Request: #2231
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/console_templates/console_model.nit
src/doc/html_templates/model_html.nit
src/highlight.nit
src/model/mdoc.nit
src/model/model.nit

index 96b1a40..cacac49 100644 (file)
@@ -208,7 +208,6 @@ redef class MModule
 end
 
 redef class MClass
-       redef fun mdoc_or_fallback do return intro.mdoc
        redef fun cs_icon do return intro.cs_icon
 
        # Format: `Foo[E]`
@@ -258,7 +257,6 @@ redef class MClass
 end
 
 redef class MClassDef
-       redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
        redef fun cs_icon do return "C"
 
        # Depends if `self` is an intro or not.
@@ -339,7 +337,6 @@ redef class MClassDef
 end
 
 redef class MProperty
-       redef fun mdoc_or_fallback do return intro.mdoc
        redef fun cs_modifiers do return intro.cs_modifiers
        redef fun cs_declaration do return intro.cs_declaration
        redef fun cs_icon do return intro.cs_icon
@@ -373,8 +370,6 @@ redef class MProperty
 end
 
 redef class MPropDef
-       redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
-
        # Depends if `self` is an intro or not.
        #
        # * If intro contains the visibility and kind.
index 0310a9c..20ae4ed 100644 (file)
@@ -155,8 +155,6 @@ redef class MModule
 end
 
 redef class MClass
-       redef fun mdoc_or_fallback do return intro.mdoc
-
        # Format: `Foo[E]`
        redef var html_name is lazy do
                var tpl = new Template
@@ -201,8 +199,6 @@ redef class MClass
 end
 
 redef class MClassDef
-       redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
-
        # Depends if `self` is an intro or not.
        #
        # * If intro contains the visibility and kind.
@@ -303,7 +299,6 @@ redef class MClassDef
 end
 
 redef class MProperty
-       redef fun mdoc_or_fallback do return intro.mdoc
        redef fun html_modifiers do return intro.html_modifiers
        redef fun html_declaration do return intro.html_declaration
 
@@ -327,8 +322,6 @@ redef class MProperty
 end
 
 redef class MPropDef
-       redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
-
        # Depends if `self` is an intro or not.
        #
        # * If intro contains the visibility and kind.
index 8dd1637..cf7c598 100644 (file)
@@ -425,6 +425,13 @@ redef class MEntity
                end
                return (new HTMLTag("a")).attr("href", href).text(text)
        end
+
+       # Append an entry for the doc in the given infobox
+       private fun add_doc_to_infobox(res: HInfoBox)
+       do
+               var mdoc = mdoc_or_fallback
+               if mdoc != null then mdoc.fill_infobox(res)
+       end
 end
 
 redef class MModule
@@ -433,8 +440,7 @@ redef class MModule
                var res = new HInfoBox(v, "module {name}")
                res.href = v.hrefto(self)
                res.new_field("module").add(linkto(v))
-               var mdoc = self.mdoc
-               if mdoc != null then mdoc.fill_infobox(res)
+               add_doc_to_infobox(res)
                if in_importation.greaters.length > 1 then
                        var c = res.new_dropdown("imports", "{in_importation.greaters.length-1} modules")
                        for x in in_importation.greaters do
@@ -459,9 +465,7 @@ redef class MClassDef
                        res.new_field("redef class").text(mclass.name)
                        res.new_field("intro").add mclass.intro.linkto_text(v, "in {mclass.intro_mmodule.to_s}")
                end
-               var mdoc = self.mdoc
-               if mdoc == null then mdoc = mclass.intro.mdoc
-               if mdoc != null then mdoc.fill_infobox(res)
+               add_doc_to_infobox(res)
 
                var in_hierarchy = self.in_hierarchy
                if in_hierarchy == null then return res
@@ -515,9 +519,7 @@ redef class MPropDef
                else
                        res.new_field("intro").add mproperty.intro.linkto_text(v, "in {mproperty.intro.mclassdef}")
                end
-               var mdoc = self.mdoc
-               if mdoc == null then mdoc = mproperty.intro.mdoc
-               if mdoc != null then mdoc.fill_infobox(res)
+               add_doc_to_infobox(res)
                if mproperty.mpropdefs.length > 1 then
                        var c = res.new_dropdown("redef", "redefinitions")
                        for x in mproperty.mpropdefs do
@@ -535,9 +537,7 @@ redef class MClassType
                var res = new HInfoBox(v, to_s)
                res.href = v.hrefto(self)
                res.new_field("class").add mclass.intro.linkto(v)
-               var mdoc = mclass.mdoc
-               if mdoc == null then mdoc = mclass.intro.mdoc
-               if mdoc != null then mdoc.fill_infobox(res)
+               add_doc_to_infobox(res)
                return res
        end
        redef fun linkto(v)
@@ -551,9 +551,7 @@ redef class MVirtualType
                var res = new HInfoBox(v, to_s)
                res.href = v.hrefto(mproperty)
                var p = mproperty
-               var pd = p.intro
-               res.new_field("virtual type").add pd.linkto(v)
-               var mdoc = pd.mdoc
+               add_doc_to_infobox(res)
                if mdoc != null then mdoc.fill_infobox(res)
                return res
        end
@@ -649,9 +647,7 @@ redef class CallSite
                else
                        res.new_field("intro").add mproperty.intro.linkto_text(v, "in {mproperty.intro.mclassdef}")
                end
-               var mdoc = mpropdef.mdoc
-               if mdoc == null then mdoc = mproperty.intro.mdoc
-               if mdoc != null then mdoc.fill_infobox(res)
+               add_doc_to_infobox(res)
 
                return res
        end
index 2035b06..408e572 100644 (file)
@@ -39,9 +39,13 @@ redef class MEntity
 
        # The documentation associated to the entity or their main nested entity.
        #
-       # MPackage fall-back to their root MGroup
-       # MGroup fall-back to their default_mmodule
-       # Other entities do not fall-back
+       # * `MPackage`s fall back to their root `MGroup`.
+       # * `MGroup`s fall back to their `default_mmodule`.
+       # * `MClass`es, `MClassDef`s, `MProperty`s and `MPropDef`s fall-back to
+       #   their introducing definition.
+       # * `MClassType`s fall back to their wrapped `MClass`.
+       # * `MVirtualType`s fall back to their wrapped `MProperty`.
+       # * Other entities do not fall back.
        #
        # One may use `MDoc::original_mentity` to retrieve the original
        # source of the documentation.
index 2243a88..cfb5778 100644 (file)
@@ -566,7 +566,12 @@ class MClass
        # Is `self` and abstract class?
        var is_abstract: Bool is lazy do return kind == abstract_kind
 
-       redef fun mdoc_or_fallback do return intro.mdoc_or_fallback
+       redef fun mdoc_or_fallback
+       do
+               # Don’t use `intro.mdoc_or_fallback` because it would create an infinite
+               # recursion.
+               return intro.mdoc
+       end
 end
 
 
@@ -725,6 +730,8 @@ class MClassDef
 
        # All property introductions and redefinitions (not inheritance) in `self` by its associated property.
        var mpropdefs_by_property = new HashMap[MProperty, MPropDef]
+
+       redef fun mdoc_or_fallback do return mdoc or else mclass.mdoc_or_fallback
 end
 
 # A global static type
@@ -1295,6 +1302,7 @@ class MClassType
        private var collect_mclasses_cache = new HashMap[MModule, Set[MClass]]
        private var collect_mtypes_cache = new HashMap[MModule, Set[MClassType]]
 
+       redef fun mdoc_or_fallback do return mclass.mdoc_or_fallback
 end
 
 # A type based on a generic class.
@@ -1525,6 +1533,8 @@ class MVirtualType
        redef fun full_name do return self.mproperty.full_name
 
        redef fun c_name do return self.mproperty.c_name
+
+       redef fun mdoc_or_fallback do return mproperty.mdoc_or_fallback
 end
 
 # The type associated to a formal parameter generic type of a class
@@ -2049,7 +2059,12 @@ abstract class MProperty
 
        redef var location
 
-       redef fun mdoc_or_fallback do return intro.mdoc_or_fallback
+       redef fun mdoc_or_fallback
+       do
+               # Don’t use `intro.mdoc_or_fallback` because it would create an infinite
+               # recursion.
+               return intro.mdoc
+       end
 
        # The canonical name of the property.
        #
@@ -2471,6 +2486,8 @@ abstract class MPropDef
                assert has_next_property: i.is_ok
                return i.item
        end
+
+       redef fun mdoc_or_fallback do return mdoc or else mproperty.mdoc_or_fallback
 end
 
 # A local definition of a method