nitdoc: introduce POSetPhase
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 4 Feb 2015 20:06:25 +0000 (21:06 +0100)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 4 Feb 2015 20:17:38 +0000 (21:17 +0100)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/doc_pages.nit
src/doc/doc_phases/doc_phases.nit
src/doc/doc_phases/doc_poset.nit [new file with mode: 0644]
src/nitdoc.nit

index be6ad94..2a0f920 100644 (file)
@@ -629,12 +629,6 @@ class NitdocModule
 
        # inheritance section
        private fun tpl_inheritance(parent: TplSection) do
-               # Extract relevent modules
-               var imports = mmodule.in_importation.greaters
-               if imports.length > 10 then imports = mmodule.in_importation.direct_greaters
-               var clients = mmodule.in_importation.smallers
-               if clients.length > 10 then clients = mmodule.in_importation.direct_smallers
-
                # Display lists
                var section = new TplSection.with_title("dependencies", "Dependencies")
 
@@ -649,11 +643,6 @@ class NitdocModule
 
                # Imports
                var lst = new Array[MModule]
-               for dep in imports do
-                       if dep.is_fictive or dep.is_test_suite then continue
-                       if dep == mmodule then continue
-                       lst.add(dep)
-               end
                if not lst.is_empty then
                        name_sorter.sort lst
                        section.add_child tpl_list("imports", "Imports", lst)
@@ -661,11 +650,6 @@ class NitdocModule
 
                # Clients
                lst = new Array[MModule]
-               for dep in clients do
-                       if dep.is_fictive or dep.is_test_suite then continue
-                       if dep == mmodule then continue
-                       lst.add(dep)
-               end
                if not lst.is_empty then
                        name_sorter.sort lst
                        section.add_child tpl_list("clients", "Clients", lst)
@@ -684,18 +668,6 @@ class NitdocModule
 
        # Genrate dot hierarchy for class inheritance
        fun tpl_dot(mmodules: Collection[MModule]): nullable TplArticle do
-               var poset = new POSet[MModule]
-               for mmodule in mmodules do
-                       if mmodule.is_fictive or mmodule.is_test_suite then continue
-                       poset.add_node mmodule
-                       for omodule in mmodules do
-                               if omodule.is_fictive or omodule.is_test_suite then continue
-                               poset.add_node mmodule
-                               if mmodule.in_importation < omodule then
-                                       poset.add_edge(mmodule, omodule)
-                               end
-                       end
-               end
                # build graph
                var op = new RopeBuffer
                var name = "dep_module_{mmodule.nitdoc_id}"
@@ -787,36 +759,6 @@ class NitdocClass
        end
 
        private fun tpl_inheritance(parent: TplSection) do
-               # parents
-               var hparents = new HashSet[MClass]
-               for c in mclass.in_hierarchy(mainmodule).direct_greaters do
-                       if ctx.filter_mclass(c) then hparents.add c
-               end
-
-               # ancestors
-               var hancestors = new HashSet[MClass]
-               for c in mclass.in_hierarchy(mainmodule).greaters do
-                       if c == mclass then continue
-                       if not ctx.filter_mclass(c) then continue
-                       if hparents.has(c) then continue
-                       hancestors.add c
-               end
-
-               # children
-               var hchildren = new HashSet[MClass]
-               for c in mclass.in_hierarchy(mainmodule).direct_smallers do
-                       if ctx.filter_mclass(c) then hchildren.add c
-               end
-
-               # descendants
-               var hdescendants = new HashSet[MClass]
-               for c in mclass.in_hierarchy(mainmodule).smallers do
-                       if c == mclass then continue
-                       if not ctx.filter_mclass(c) then continue
-                       if hchildren.has(c) then continue
-                       hdescendants.add c
-               end
-
                # Display lists
                var section = new TplSection.with_title("inheritance", "Inheritance")
 
@@ -880,19 +822,6 @@ class NitdocClass
 
        # Generate dot hierarchy for classes
        fun tpl_dot(mclasses: Collection[MClass]): nullable TplArticle do
-               var poset = new POSet[MClass]
-
-               for mclass in mclasses do
-                       poset.add_node mclass
-                       for oclass in mclasses do
-                               if mclass == oclass then continue
-                               poset.add_node oclass
-                               if mclass.in_hierarchy(mainmodule) < oclass then
-                                       poset.add_edge(mclass, oclass)
-                               end
-                       end
-               end
-
                var op = new RopeBuffer
                var name = "dep_class_{mclass.nitdoc_id}"
                op.append("digraph \"{name.escape_to_dot}\" \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
index 02041b0..e9f5965 100644 (file)
@@ -18,3 +18,4 @@
 module doc_phases
 
 import doc_structure
+import doc_poset
diff --git a/src/doc/doc_phases/doc_poset.nit b/src/doc/doc_phases/doc_poset.nit
new file mode 100644 (file)
index 0000000..fd38e0f
--- /dev/null
@@ -0,0 +1,178 @@
+# 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.
+
+# Importation and inheritance POSet for pages.
+module doc_poset
+
+import doc_pages
+
+# This phase computes importation and inheritance POSet for pages.
+class POSetPhase
+       super DocPhase
+
+       # Populates the given DocModel.
+       redef fun apply do
+               for page in doc.pages do
+                       if page isa MEntityPage then page.build_poset(self, doc)
+               end
+       end
+end
+
+redef class MEntityPage
+
+       # The poset associated with this page.
+       #
+       # FIXME should be defined in subclasses
+       # as the POSet can contains other types than `SELF`
+       var poset = new POSet[MENTITY]
+
+       # Build the POSet for this page.
+       private fun build_poset(v: POSetPhase, doc: DocModel) do end
+end
+
+redef class MModulePage
+
+       # Imported modules that should appear in the documentation.
+       var imports = new HashSet[MModule]
+
+       # Clients modules that shjould appear in the documentation.
+       var clients = new HashSet[MModule]
+
+       redef fun build_poset(v, doc) do
+               # collect importation
+               for dep in mentity.in_importation.greaters do
+                       if dep == mentity then continue
+                       if not doc.mmodules.has(dep) then continue
+                       imports.add dep
+               end
+               # FIXME avoid diff
+               #if imports.length > 10 then
+               if mentity.in_importation.greaters.length > 10 then
+                       imports.clear
+                       for dep in mentity.in_importation.direct_greaters do
+                               if dep == mentity then continue
+                               if not doc.mmodules.has(dep) then continue
+                               imports.add dep
+                       end
+               end
+               # collect clients
+               for dep in mentity.in_importation.smallers do
+                       if dep == mentity then continue
+                       if not doc.mmodules.has(dep) then continue
+                       clients.add dep
+               end
+               if clients.length > 10 then
+                       clients.clear
+                       for dep in mentity.in_importation.direct_smallers do
+                               if dep == mentity then continue
+                               if not doc.mmodules.has(dep) then continue
+                               clients.add dep
+                       end
+               end
+               # make poset
+               var mmodules = new HashSet[MModule]
+               mmodules.add_all mentity.nested_mmodules
+               mmodules.add_all imports
+               if clients.length < 10 then mmodules.add_all clients
+               mmodules.add mentity
+               build_importation_poset(doc, mmodules)
+       end
+
+       # Build the POSet of importation from a list of `mmodules`.
+       private fun build_importation_poset(doc: DocModel, mmodules: Set[MModule]): POSet[MModule] do
+               for mmodule in mmodules do
+                       if not doc.mmodules.has(mmodule) then continue
+                       poset.add_node mmodule
+                       for omodule in mmodules do
+                               if not doc.mmodules.has(omodule) then continue
+                               poset.add_node mmodule
+                               if mmodule.in_importation < omodule then
+                                       poset.add_edge(mmodule, omodule)
+                               end
+                       end
+               end
+               return poset
+       end
+end
+
+redef class MClassPage
+
+       # Direct parents classes to document.
+       var parents = new HashSet[MClass]
+
+       # Transitive ancestors classes to document.
+       #
+       # Does not contain the direct ancestors.
+       # See `parents` for that.
+       var ancestors = new HashSet[MClass]
+
+       # Direct children classes to document.
+       var children = new HashSet[MClass]
+
+       # All descendants classes to document.
+       #
+       # Does not contain the direct children.
+       # See `children` for that.
+       var descendants = new HashSet[MClass]
+
+       redef fun build_poset(v, doc) do
+               poset.add_node mentity
+
+               var h = mentity.in_hierarchy(doc.mainmodule)
+               # parents
+               for mclass in h.direct_greaters do
+                       if doc.mclasses.has(mclass) then parents.add mclass
+               end
+               # ancestors
+               for mclass in h.greaters do
+                       if mclass == mentity then continue
+                       if not doc.mclasses.has(mclass) then continue
+                       if parents.has(mclass) then continue
+                       ancestors.add mclass
+               end
+               # children
+               for mclass in h.direct_smallers do
+                       if doc.mclasses.has(mclass) then children.add mclass
+               end
+               # descendants
+               for mclass in h.smallers do
+                       if mclass == mentity then continue
+                       if not doc.mclasses.has(mclass) then continue
+                       if children.has(mclass) then continue
+                       descendants.add mclass
+               end
+               # poset
+               var mclasses = new HashSet[MClass]
+               mclasses.add_all ancestors
+               mclasses.add_all parents
+               mclasses.add_all children
+               mclasses.add_all descendants
+               mclasses.add mentity
+               build_inheritance_poset(v, doc, mclasses)
+       end
+
+       private fun build_inheritance_poset(v: POSetPhase, doc: DocModel, mclasses: Set[MClass]): POSet[MClass] do
+               for mclass in mclasses do
+                       poset.add_node mclass
+                       for oclass in mclasses do
+                               if mclass == oclass then continue
+                               poset.add_node oclass
+                               if mclass.in_hierarchy(doc.mainmodule) < oclass then
+                                       poset.add_edge(mclass, oclass)
+                               end
+                       end
+               end
+               return poset
+       end
+end
index e0990e7..e1bd571 100644 (file)
@@ -35,6 +35,7 @@ private class Nitdoc
                var phases = [
                        new ExtractionPhase(toolcontext, doc),
                        new MakePagePhase(toolcontext, doc),
+                       new POSetPhase(toolcontext, doc),
                        new ConcernsPhase(toolcontext, doc),
                        new StructurePhase(toolcontext, doc): DocPhase]