ni_nitdoc: Describe forml/virtual types
[nit.git] / src / ni_nitdoc.nit
index 29778d8..2b9ca45 100644 (file)
@@ -96,6 +96,7 @@ class Nitdoc
                        overview
                        fullindex
                        modules
+                       classes
                end
        end
 
@@ -116,6 +117,16 @@ class Nitdoc
                end
        end
 
+       fun classes do
+               for amodule in modelbuilder.nmodules do
+                       for mclass, aclassdef in amodule.mclass2nclassdef do
+                               mclass.amodule(modelbuilder.mmodule2nmodule)
+                               var classpage = new NitdocMClasses.with(mclass, aclassdef)
+                               classpage.save("{destinationdir.to_s}/{mclass.name}.html")
+                       end
+               end
+       end
+
 end
 
 class NitdocOverview
@@ -462,6 +473,7 @@ class NitdocModules
        redef fun body do
                super
                open("div").add_class("page")
+               menu
                add_content
                close("div")
                add("footer").text("Nit standard library. Version jenkins-component=stdlib-19.")
@@ -474,6 +486,7 @@ class NitdocModules
                add("div").add_class("subtitle").text("module {modulename}")
                module_comment
                classes
+               properties
                close("div")
        end
 
@@ -489,6 +502,48 @@ class NitdocModules
                close("div")
        end
 
+       fun menu do
+               var mmodule = amodule.mmodule 
+               open("div").add_class("menu")
+               open("nav")
+               add("h3").text("Module Hierarchy").attr("style","cursor: pointer;")
+               if mmodule.in_importation.direct_greaters.length > 0 then
+                       add_html("<h4>All dependencies</h4><ul>")
+                       for m in mmodule.in_importation.direct_greaters do
+                               if m == mmodule or mmodule == m.public_owner then continue
+                               open("li")
+                               add("a").attr("href", "{m.name}.html").text(m.name)
+                               close("li")
+                       end
+                       add_html("</ul>")
+               end     
+               if mmodule.in_importation.greaters.length > 0 then
+                       add_html("<h4>All clients</h4><ul>")
+                       for m in mmodule.in_importation.greaters do
+                               if m == mmodule then continue
+                               open("li")
+                               add("a").attr("href", "{m.name}.html").text(m.name)
+                               close("li")
+                       end
+                       add_html("</ul>")
+               end
+               close("nav")
+               if mmodule.in_nesting.direct_greaters.length > 0 then
+                       open("nav")
+                       add("h3").text("Nested Modules").attr("style","cursor: pointer;")
+                       open("ul")
+                       for m in mmodule.in_nesting.direct_greaters do
+                               open("li")
+                               add("a").attr("href", "{m.name}.html").text(m.name)
+                               close("li")
+                       end
+                       close("ul")
+                       
+                       close("nav")
+               end
+               close("div")
+       end
+
        fun classes do
                open("div").add_class("module")
                open("article").add_class("classes filterable")
@@ -511,8 +566,287 @@ class NitdocModules
                close("div")
        end
 
+       fun properties do
+               open("article").add_class("properties filterable")
+               add_html("<h2>Properties</h2>")
+               open("ul")
+               for method in amodule.mmodule.imported_methods do
+                       if method.visibility is none_visibility or method.visibility is intrude_visibility then continue
+                       open("li").add_class("intro")
+                       add("span").attr("title", "introduction").text("I")
+                       add_html("&nbsp;")
+                       add("a").attr("href", "{method.local_class.name}.html").attr("title", "").text("{method.name} ({method.local_class.name})")
+                       close("li")
+               end
+
+               for method in amodule.mmodule.redef_methods do
+                       if method.visibility is none_visibility or method.visibility is intrude_visibility then continue
+                       open("li").add_class("redef")
+                       add("span").attr("title", "redefinition").text("R")
+                       add_html("&nbsp;")
+                       add("a").attr("href", "{method.local_class.name}.html").attr("title", "").text("{method.name} ({method.local_class.name})")
+                       close("li")
+               end
+
+               close("ul")
+               close("article")
+       end
+
 end
 
+# Nit Standard Library
+class NitdocMClasses
+       super NitdocPage
+
+       var mclass: MClass
+       var aclassdef: AClassdef
+       var stdclassdef: nullable AStdClassdef
+       var public_owner: nullable MModule
+
+       init with(mclass: MClass, aclassdef: AClassdef) do
+               self.mclass = mclass
+               self.aclassdef = aclassdef
+               if aclassdef isa AStdClassdef then self.stdclassdef = aclassdef
+               self.public_owner = mclass.intro_mmodule.public_owner
+               opt_nodot = false
+               destinationdir = ""
+       end
+
+       redef fun head do
+               super
+               add("title").text("{self.mclass.name} class | Nit Standard Library")
+       end
+
+       redef fun header do
+               open("header")
+               open("nav").add_class("main")
+               open("ul")
+               open("li")
+               add_html("<a href=\"index.html\">Overview</a>")
+               close("li")
+               open("li")
+               if public_owner is null then
+                       add_html("<a href=\"{mclass.intro_mmodule.name}.html\">{mclass.intro_mmodule.name}</a>")
+               else
+                       add_html("<a href=\"{public_owner.name}.html\">{public_owner.name}</a>")
+               end
+               close("li")
+               add("li").add_class("current").text(mclass.name)
+               open("li")
+               add_html("<a href=\"full-index.html\" >Full Index</a>")
+               close("li")
+               open("li").attr("id", "liGitHub")
+               open("a").add_class("btn").attr("id", "logGitHub")
+               add("img").attr("id", "imgGitHub").attr("src", "resources/icons/github-icon.png")
+               close("a")
+               open("div").add_class("popover bottom")
+               add("div").add_class("arrow").text(" ")
+               open("div").add_class("githubTitle")
+               add("h3").text("Github Sign In")
+               close("div")
+               open("div")
+               add("label").attr("id", "lbloginGit").text("Username")
+               add("input").attr("id", "loginGit").attr("name", "login").attr("type", "text")
+               open("label").attr("id", "logginMessage").text("Hello ")
+               open("a").attr("id", "githubAccount")
+               add("strong").attr("id", "nickName").text(" ")
+               close("a")
+               close("label")
+               close("div")
+               open("div")
+               add("label").attr("id", "lbpasswordGit").text("Password")
+               add("input").attr("id", "passwordGit").attr("name", "password").attr("type", "password")
+               open("div").attr("id", "listBranches")
+               add("label").attr("id", "lbBranches").text("Branch")
+               add("select").add_class("dropdown").attr("id", "dropBranches").attr("name", "dropBranches").attr("tabindex", "1").text(" ")
+               close("div")
+               close("div")
+               open("div")
+               add("label").attr("id", "lbrepositoryGit").text("Repository")
+               add("input").attr("id", "repositoryGit").attr("name", "repository").attr("type", "text")
+               close("div")
+               open("div")
+               add("label").attr("id", "lbbranchGit").text("Branch")
+               add("input").attr("id", "branchGit").attr("name", "branch").attr("type", "text")
+               close("div")
+               open("div")
+               add("a").attr("id", "signIn").text("Sign In")
+               close("div")
+               close("div")
+               close("li")
+               close("ul")
+               close("nav")
+               close("header")
+       end
+
+       redef fun body do
+               super
+               open("div").add_class("page")
+               add_content
+               close("div")
+               add("footer").text("Nit standard library. Version jenkins-component=stdlib-19.")
+       end
+
+       # Insert all tags in content part
+       fun add_content do
+               open("div").add_class("menu")
+               properties_column
+               inheritance_column
+               close("div")
+               open("div").add_class("content")
+               content
+               close("div")
+       end
+
+       fun properties_column do
+               open("nav").add_class("properties filterable")
+               add("h3").text("Properties")
+
+               if mclass.virtual_types.length > 0 then
+                       add("h4").text("Virtual Types")
+                       open("ul")
+                       for prop in mclass.virtual_types do
+                               add_html("<li class=\"redef\"><span title=\"Redefined\">R</span><a href=\"{prop.link_anchor}\">{prop.name}</a></li>")
+                       end
+                       close("ul")
+               end
+               if mclass.constructors.length > 0 then
+                       add("h4").text("Constructors")
+                       open("ul")
+                       for prop in mclass.constructors do
+                               add_html("<li class=\"intro\"><span title=\"Introduced\">I</span><a href=\"{prop.link_anchor}\">{prop.name}</a></li>")
+                       end
+                       close("ul")
+               end
+               add("h4").text("Methods")
+               open("ul")
+               if mclass.intro_methods.length > 0 then
+                       for prop in mclass.intro_methods do
+                               if prop.visibility is public_visibility or prop.visibility is protected_visibility then add_html("<li class=\"intro\"><span title=\"Introduced\">I</span><a href=\"{prop.link_anchor}\">{prop.name}</a></li>")
+                       end
+               end
+               if mclass.inherited_methods.length > 0 then
+                       for prop in mclass.inherited_methods do
+                               if prop.visibility is public_visibility or prop.visibility is protected_visibility then add_html("<li class=\"inherit\"><span title=\"Inherited\">H</span><a href=\"{prop.link_anchor}\">{prop.name}</a></li>")
+                       end
+               end
+               if mclass.redef_methods.length > 0 then
+                       for prop in mclass.redef_methods do
+                               if prop.visibility is public_visibility or prop.visibility is protected_visibility then add_html("<li class=\"redef\"><span title=\"Refined\">R</span><a href=\"{prop.link_anchor}\">{prop.name}</a></li>")
+                       end
+               end
+               close("ul")
+               close("nav")
+       end
+
+       fun inheritance_column do
+               open("nav")
+               add("h3").text("Inheritance")
+               if mclass.parents.length > 0 then
+                       add("h4").text("Superclasses")
+                       open("ul")
+                       for sup in mclass.parents do add_html("<li><a href=\"{sup.name}.html\">{sup.name}</a></li>")
+                       close("ul")
+               end
+
+               if mclass.descendants.length is 0 then
+                       add("h4").text("No Known Subclasses")
+               else if mclass.descendants.length <= 100 then
+                       add("h4").text("Subclasses")
+                       open("ul")
+                       for sub in mclass.descendants do add_html("<li><a href=\"{sub.name}\">{sub.name}</a></li>")
+                       close("ul")
+               else if mclass.children.length <= 100 then
+                       add("h4").text("Direct Subclasses Only")
+                       open("ul")
+                       for sub in mclass.children do add_html("<li><a href=\"{sub.name}\">{sub.name}</a></li>")
+                       close("ul")
+               else
+                       add("h4").text("Too much Subclasses to list")
+               end
+               close("nav")
+       end
+
+       fun content do
+               var subtitle = ""
+               var lmmodule = new List[MModule]
+               # Insert the subtitle part
+               add("h1").text(mclass.name)
+               open("div").add_class("subtitle")
+               if mclass.visibility is none_visibility then subtitle += "private "
+               subtitle += "{mclass.kind} <a href=\"{mclass.public_owner.name}.html\">{mclass.public_owner.name}</a>::{mclass.name}"
+               add_html(subtitle)
+               close("div")
+               add_html("<div style=\"float: right;\"><a id=\"lblDiffCommit\"></a></div>")
+               # We add the class description
+               open("section").add_class("description")
+               if not stdclassdef is null and not stdclassdef.comment.is_empty then add_html("<pre class=\"text_label\" title=\"122\" name=\"\" tag=\"{mclass.mclassdefs.first.location.to_s}\" type=\"2\">{stdclassdef.comment} </pre><textarea id=\"fileContent\" class=\"edit\" cols=\"76\" rows=\"1\" style=\"display: none;\"></textarea><a id=\"cancelBtn\" style=\"display: none;\">Cancel</a><a id=\"commitBtn\" style=\"display: none;\">Commit</a><pre id=\"preSave\" class=\"text_label\" type=\"2\"></pre>")
+               close("section")
+               open("section").add_class("concerns")
+               add("h2").add_class("section-header").text("Concerns")
+               open("ul")
+               for owner, childs in mclass.concerns do
+                       open("li")
+                       add_html("<a href=\"#MOD_{owner.name}\">{owner.name}</a>: {owner.amodule.short_comment}")
+                       if not childs is null then
+                               open("ul")
+                               for child in childs.as(not null) do add_html("<li><a href=\"#MOD_{child.name}\">{child.name}</a>: {child.amodule.short_comment} </li>")
+                               close("ul")
+                       end
+                       close("li")
+               end
+               close("ul")
+               close("section")
+               # Insert virtual types if there is almost one
+               if mclass.virtual_types.length > 0 or (stdclassdef != null and stdclassdef.n_formaldefs.length > 0) then
+                       open("section").add_class("types")
+                       add("h2").text("Formal and Virtual Types")
+                       if mclass.virtual_types.length > 0 then for prop in mclass.virtual_types do description(prop)
+                       if stdclassdef.n_formaldefs.length > 0 then
+                               for prop in stdclassdef.n_formaldefs do
+                                       open("article").attr("id", "FT_Object_{prop.collect_text}")
+                                       open("h3").add_class("signature").text("{prop.collect_text}: nullable ")
+                                       add_html("<a title=\"The root of the class hierarchy.\" href=\"Object.html\">Object</a>")
+                                       close("h3")
+                                       add_html("<div class=\"info\">formal generic type</div>")
+                                       close("article")
+                               end
+                       end
+                       close("section")
+               end
+       end
+
+       # Insert description tags for 'prop'
+       fun description(prop: MProperty) do
+               open("article").add_class("fun public {if prop.is_redef then "redef" else ""}").attr("id", "{prop.anchor}")
+               var sign = prop.name
+               if prop.apropdef != null then sign += prop.apropdef.signature
+               add_html("<h3 class=\"signature\">{sign}</h3>")
+               add_html("<div class=\"info\">{if prop.is_redef then "redef" else ""} fun {prop.intro_mclassdef.namespace(mclass)}::{prop.name}</div><div style=\"float: right;\"><a id=\"lblDiffCommit\"></a></div>")
+
+               open("div").add_class("description")
+               if prop.apropdef is null or prop.apropdef.comment == "" then
+                       add_html("<a class=\"newComment\" title=\"32\" tag=\"\">New Comment</a>")
+               else
+                       add_html("<pre class=\"text_label\" title=\"\" name=\"\" tag=\"\" type=\"1\">{prop.apropdef.comment}</pre>")
+               end
+               add_html("<textarea id=\"fileContent\" class=\"edit\" cols=\"76\" rows=\"1\" style=\"display: none;\"></textarea><a id=\"cancelBtn\" style=\"display: none;\">Cancel</a><a id=\"commitBtn\" style=\"display: none;\">Commit</a><pre id=\"preSave\" class=\"text_label\" type=\"2\"></pre>")
+               open("p")
+               if prop.local_class != mclass then add_html("inherited from {prop.local_class.intro_mmodule.name} ")
+               #TODO display show code if doc github
+               add_html("defined by the module <a href=\"{prop.intro_mclassdef.mmodule.name}.html\">{prop.intro_mclassdef.mmodule.name}</a> (<a href=\"\">show code</a>).")
+
+               for parent in mclass.parents do
+                       if prop isa MMethod then if parent.constructors.has(prop) then add_html(" Previously defined by: <a href=\"{parent.intro_mmodule.name}.html\">{parent.intro_mmodule.name}</a> for <a href=\"{parent.name}.html\">{parent.name}</a>.")
+               end
+               close("p")
+               close("div")
+
+               close("article")
+       end
+
+end    
+
 class NitdocPage
        super HTMLPage
        
@@ -613,6 +947,198 @@ redef class MProperty
                return classdef.mclass
        end
 
+       fun class_text: String do
+               return local_class.name
+       end
+
+       fun link_anchor: String do
+               return "{class_text}.html#{anchor}"
+       end
+
+       fun anchor: String do
+               return "PROP_{c_name}"
+       end
+
+end
+
+redef class MClass
+
+       # Associate all MMethods to each MModule concerns
+       fun all_methods: HashMap[MModule, Set[MMethod]] do
+               var hm = new HashMap[MModule, Set[MMethod]]
+               for mmodule, childs in concerns do
+                       if not hm.has_key(mmodule) then hm[mmodule] = new HashSet[MMethod]
+                       for prop in intro_methods do
+                               if mmodule == prop.intro_mclassdef.mmodule then
+                                       prop.is_redef = false
+                                       hm[mmodule].add(prop)
+                               end
+                       end
+                       for prop in redef_methods do
+                               if mmodule == prop.intro_mclassdef.mmodule then
+                                       prop.is_redef = true
+                                       hm[mmodule].add(prop)
+                               end
+                       end
+
+                       if childs != null then
+                               for child in childs do
+                                       if not hm.has_key(child) then hm[child] = new HashSet[MMethod]
+                                       for prop in intro_methods do
+                                               if child == prop.intro_mclassdef.mmodule then
+                                                       prop.is_redef = false
+                                                       hm[child].add(prop)
+                                               end
+                                       end
+                                       for prop in redef_methods do
+                                               if child == prop.intro_mclassdef.mmodule then
+                                                       prop.is_redef = true
+                                                       hm[child].add(prop)
+                                               end
+                                       end
+                               end
+                       end
+               end
+               return hm
+       end
+
+       fun public_owner: MModule do
+               var owner = intro_mmodule
+               if owner.public_owner is null then
+                       return owner
+               else
+                       return owner.public_owner.as(not null)
+               end
+       end
+       
+       # Associate Amodule to all MModule concern by 'self'
+       fun amodule(amodules: HashMap[MModule, AModule]) do
+               for owner, childs in concerns do
+                       if childs != null then for child in childs do child.amodule = amodules[child]
+                       owner.amodule = amodules[owner]
+               end
+       end
+
+end
+
+redef class AStdClassdef
+       private fun comment: String do
+               var ret = ""
+               if n_doc != null then
+                       for t in n_doc.n_comment do
+                               var txt = t.text.replace("# ", "")
+                               txt = txt.replace("#", "")
+                               ret += "{txt}"
+                       end
+               end
+               return ret
+       end
+
+       private fun short_comment: String do
+               var ret = ""
+               if n_doc != null then
+                       var txt = n_doc.n_comment.first.text
+                       txt = txt.replace("# ", "")
+                       txt = txt.replace("\n", "")
+                       ret += txt
+               end
+               return ret
+       end
+end
+
+redef class ASignature
+       redef fun to_s do
+               #TODO closures
+               var ret = ""
+               if not n_params.is_empty then
+                       ret = "{ret}({n_params.join(", ")})"
+               end
+               if n_type != null and n_type.to_s != "" then ret += " {n_type.to_s}"
+               return ret
+       end
+end
+
+redef class AParam
+       redef fun to_s do
+               var ret = "{n_id.text}"
+               if n_type != null then
+                       ret = "{ret}: {n_type.to_s}"
+                       if n_dotdotdot != null then ret = "{ret}..."
+               end
+               return ret
+       end
+end
+
+redef class AType
+       redef fun to_s do
+               var ret = "<a href=\"{n_id.text}.html\">{n_id.text}</a>"
+               if n_kwnullable != null then ret = "nullable {ret}"
+               if not n_types.is_empty then ret = "{ret}[{n_types.join(", ")}]"
+               return ret
+       end
+end
+
+redef class APropdef
+       private fun short_comment: String is abstract
+       private fun signature: String is abstract
+       private fun comment: String is abstract
+end
+
+redef class AAttrPropdef
+       redef fun short_comment do
+               var ret = ""
+               if n_doc != null then
+                       var txt = n_doc.n_comment.first.text
+                       txt = txt.replace("# ", "")
+                       txt = txt.replace("\n", "")
+                       ret += txt
+               end
+               return ret
+       end
+end
+
+redef class AMethPropdef
+       redef fun short_comment do
+               var ret = ""
+               if n_doc != null then
+                       var txt = n_doc.n_comment.first.text
+                       txt = txt.replace("# ", "")
+                       txt = txt.replace("\n", "")
+                       ret += txt
+               end
+               return ret
+       end
+
+       redef fun signature: String do
+               var sign = ""
+               if n_signature != null then sign = " {n_signature.to_s}"
+               return sign
+       end
+
+       redef private fun comment: String do
+               var ret = ""
+               if n_doc != null then
+                       for t in n_doc.n_comment do
+                               var txt = t.text.replace("# ", "")
+                               txt = txt.replace("#", "")
+                               ret += "{txt}"
+                       end
+               end
+               return ret
+       end
+end
+
+redef class MClassDef
+       private fun namespace(mclass: MClass): String do
+
+               if mmodule.public_owner is null then
+                       return "{mmodule.full_name}::{mclass.name}"
+               else if mclass is self.mclass then
+                       return "{mmodule.public_owner.name}::{mclass.name}"
+               else
+                       return "{mmodule.public_owner.name}::<a href=\"{mclass.name}.html\">{mclass.name}</a>"
+               end
+       end
 end
 
 # Create a tool context to handle options and paths