nitx: add hierarchies commands
authorAlexandre Terrasa <alexandre@moz-code.org>
Sat, 30 May 2015 01:35:47 +0000 (21:35 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sat, 30 May 2015 01:37:44 +0000 (21:37 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/doc_phases/doc_console.nit
src/nitx.nit

index e14fa46..b6d08dc 100644 (file)
@@ -20,6 +20,7 @@ module doc_console
 
 import semantize
 import doc_extract
+import doc_poset
 import doc::console_templates
 
 # Nitx handles console I/O.
@@ -66,6 +67,11 @@ class Nitx
                print "\treturn: <Type>\t\tlookup methods returning the corresponding 'Type'"
                print "\tnew: <Type>\t\tlookup methods creating new instances of 'Type'"
                print "\tcall: <name>\t\tlookup methods calling 'name'"
+               print "\nHierarchy lookup:"
+               print "\tparents: <Class>\tlist direct parents of 'Class'"
+               print "\tancestors: <Class>\tlist all ancestors of 'Class'"
+               print "\tchildren: <Class>\tlist direct children of 'Class'"
+               print "\tdescendants: <Class>\tlist all descendants of 'Class'"
                print "\nCode lookup:"
                print "\tcode: <name>\t\tdisplay the source code associated to the 'name' entity"
                print "\n"
@@ -127,7 +133,14 @@ interface NitxQuery
                        return new CallQuery(query_string)
                else if query_string.has_prefix("code:") then
                        return new CodeQuery(query_string)
-
+               else if query_string.has_prefix("parents:") then
+                       return new ParentsQuery(query_string)
+               else if query_string.has_prefix("ancestors:") then
+                       return new AncestorsQuery(query_string)
+               else if query_string.has_prefix("children:") then
+                       return new ChildrenQuery(query_string)
+               else if query_string.has_prefix("descendants:") then
+                       return new DescendantsQuery(query_string)
                end
                return new CommentQuery("comment: {query_string}")
        end
@@ -359,6 +372,76 @@ class PageMatch
        end
 end
 
+# Search in class or module hierarchy of a `MEntity`.
+#
+# It actually searches for pages about the mentity and extracts the
+# pre-calculated hierarchies by the `doc_post` phase.
+abstract class HierarchiesQuery
+       super DocQuery
+
+       redef fun make_results(nitx, results) do
+               var page = new DocPage("hierarchy", "Hierarchy")
+               for result in results do
+                       if not result isa PageMatch then continue
+                       var rpage = result.page
+                       if not rpage isa MClassPage then continue
+                       page.root.add_child build_article(rpage)
+               end
+               return page
+       end
+
+       # Build an article containing the hierarchy list depending on subclasses.
+       private fun build_article(page: MClassPage): DocArticle is abstract
+end
+
+# List all parents of a `MClass`.
+class AncestorsQuery
+       super HierarchiesQuery
+
+       redef fun build_article(page) do
+               return new MEntitiesListArticle(
+                       "ancerstors",
+                       "Ancestors for {page.mentity.name}",
+                       page.ancestors.to_a)
+       end
+end
+
+# List direct parents of a `MClass`.
+class ParentsQuery
+       super HierarchiesQuery
+
+       redef fun build_article(page) do
+               return new MEntitiesListArticle(
+                       "parents",
+                       "Parents for {page.mentity.name}",
+                       page.parents.to_a)
+       end
+end
+
+# List direct children of a `MClass`.
+class ChildrenQuery
+       super HierarchiesQuery
+
+       redef fun build_article(page) do
+               return new MEntitiesListArticle(
+                       "children",
+                       "Children for {page.mentity.name}",
+                       page.children.to_a)
+       end
+end
+
+# List all descendants of a `MClass`.
+class DescendantsQuery
+       super HierarchiesQuery
+
+       redef fun build_article(page) do
+               return new MEntitiesListArticle(
+                       "descendants",
+                       "Descendants for {page.mentity.name}",
+                       page.children.to_a)
+       end
+end
+
 # A query to search source code from a file name.
 class CodeQuery
        super MetaQuery
index 23490a8..cf5ab95 100644 (file)
@@ -48,7 +48,8 @@ private class NitxPhase
                        new ExtractionPhase(toolcontext, doc),
                        new MakePagePhase(toolcontext, doc),
                        new ConcernsPhase(toolcontext, doc),
-                       new StructurePhase(toolcontext, doc): DocPhase]
+                       new StructurePhase(toolcontext, doc),
+                       new POSetPhase(toolcontext, doc): DocPhase]
 
                for phase in phases do
                        toolcontext.info("# {phase.class_name}", 1)