ni: now displaying attributes
[nit.git] / src / ni.nit
index 2d5791a..28a63a6 100644 (file)
@@ -82,23 +82,27 @@ class NitIndex
        end
 
        fun seek(entry: String) do
-               # quitting?
                if entry.is_empty then exit(0)
-
                var flag = false
+               # seek for modules
+               var mmatches = new List[MModule]
                for m in model.mmodules do
                        if m.name == entry then
                                flag = true
-                               module_fulldoc(m)
+                               mmatches.add(m)
                        end
                end
+               if not mmatches.is_empty then modules_fulldoc(mmatches)
+               # seek for classes
+               var cmatches = new List[MClass]
                for c in model.mclasses do
                        if c.name == entry then
-                               if not mbuilder.mclassdef2nclassdef[c.intro] isa AStdClassdef then continue
                                flag = true
-                               class_fulldoc(c)
+                               cmatches.add(c)
                        end
                end
+               if not cmatches.is_empty then classes_fulldoc(cmatches)
+               # seek for properties
                var matches = new List[MProperty]
                for p in model.mproperties do
                        if p.name == entry then
@@ -106,112 +110,174 @@ class NitIndex
                                matches.add(p)
                        end
                end
-               #if not matches.is_empty then doc_properties
-
+               if not matches.is_empty then props_fulldoc(matches)
+               # no matches
                if not flag then print "Nothing known about '{entry}'"
                if arguments.length == 1 then prompt
        end
 
-       private fun module_fulldoc(mmodule: MModule) do
-               var nmodule = mbuilder.mmodule2nmodule[mmodule]
+       private fun modules_fulldoc(mmodules: List[MModule]) do
                var pager = new Pager
-               pager.add("# module {mmodule.name}\n".bold)
-               pager.add("import {mmodule.in_importation.direct_greaters.join(", ")}")
-               pager.add_rule
-               pager.addn(nmodule.comment.green)
-               pager.add_rule
-
-               var cats = new HashMap[String, Collection[MClass]]
-               cats["introduced classes"] = mmodule.intro_mclasses
-               cats["refined classes"] = mmodule.redef_mclasses
-               cats["inherited classes"] = mmodule.imported_mclasses
-
-               for cat, list in cats do
-                       if not list.is_empty then
-                               pager.add("# {cat}\n".bold)
-                               for mclass in list do
-                                       var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
-                                       if not nclass.short_comment.is_empty then
-                                               pager.add("\t# {nclass.short_comment}")
-                                       end
-                                       if cat == "refined classes" then
-                                               pager.add("\tredef {mclass.short_doc}")
-                                       else
-                                               pager.add("\t{mclass.short_doc}")
-                                       end
-                                       if not mclass.intro_mmodule == mmodule then
-                                               pager.add("\t\tintroduced in {mmodule.full_name}::{mclass}".gray)
+               for mmodule in mmodules do
+                       var nmodule = mbuilder.mmodule2nmodule[mmodule]
+                       pager.add("# module {mmodule.namespace}\n".bold)
+                       if not mmodule.in_importation.direct_greaters.is_empty then
+                               pager.add("import ".bold + "{mmodule.in_importation.direct_greaters.join(", ")}\n")
+                       end
+                       if not mmodule.in_importation.direct_smallers.is_empty then
+                               pager.add("known clients: ".bold + "{mmodule.in_importation.direct_smallers.join(", ")}\n")
+                       end
+                       pager.add_rule
+                       pager.addn(nmodule.comment.green)
+                       pager.add_rule
+
+                       var cats = new HashMap[String, Collection[MClass]]
+                       cats["introduced classes"] = mmodule.intro_mclasses
+                       cats["refined classes"] = mmodule.redef_mclasses
+                       cats["inherited classes"] = mmodule.imported_mclasses
+
+                       for cat, list in cats do
+                               if not list.is_empty then
+                                       pager.add("# {cat}".bold)
+                                       for mclass in list do
+                                               var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
+                                               pager.add("")
+                                               if not nclass.short_comment.is_empty then
+                                                       pager.add("\t# {nclass.short_comment}")
+                                               end
+                                               if cat == "refined classes" then
+                                                       pager.add("\tredef {mclass.short_doc}")
+                                               else
+                                                       pager.add("\t{mclass.short_doc}")
+                                               end
+                                               if not mclass.intro_mmodule == mmodule then
+                                                       pager.add("\t\t" + "introduced in {mmodule.full_name}::{mclass}".gray)
+                                               end
+                                               for mclassdef in mclass.mclassdefs do
+                                                       if mclassdef != mclass.intro then
+                                                               pager.add("\t\t" + "refined in {mclassdef.namespace}".gray)
+                                                       end
+                                               end
                                        end
-                                       pager.add("")
                                end
                        end
+                       pager.add_rule
                end
-               pager.add_rule
                pager.render
        end
 
-       private fun class_fulldoc(mclass: MClass) do
-               var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
+       private fun classes_fulldoc(mclasses: List[MClass]) do
                var pager = new Pager
-               pager.add("# {mclass.intro_mmodule.public_owner.name}::{mclass.name}\n".bold)
-               pager.add("{mclass.short_doc} ")
-               pager.add_rule
-               pager.addn(nclass.comment.green)
-               pager.add_rule
-               if not mclass.parameter_types.is_empty then
-                       pager.add("# formal types\n".bold)
-                       for ft, bound in mclass.parameter_types do
-                               pager.add("\t{ft.to_s.green}: {bound}")
-                               pager.add("")
+               for mclass in mclasses do
+                       var nclass = mbuilder.mclassdef2nclassdef[mclass.intro].as(AStdClassdef)
+
+                       pager.add("# {mclass.namespace}\n".bold)
+                       pager.add("{mclass.short_doc}")
+                       pager.add_rule
+                       pager.addn(nclass.comment.green)
+                       pager.add_rule
+                       if not mclass.parameter_types.is_empty then
+                               pager.add("# formal types".bold)
+                               for ft, bound in mclass.parameter_types do
+                                       pager.add("")
+                                       pager.add("\t{ft.to_s.green}: {bound}")
+                               end
                        end
-               end
-               if not mclass.virtual_types.is_empty then
-                       pager.add("# virtual types\n".bold)
-                       for vt in mclass.virtual_types do
-                               if vt.visibility.to_s == "public" then pager.add("\t{vt.to_s.green}: {vt.intro.bound.to_s}")
-                               if vt.visibility.to_s == "private" then pager.add("\t{vt.to_s.red}: {vt.intro.bound.to_s}")
-                               if vt.visibility.to_s == "protected" then pager.add("\t{vt.to_s.yellow}: {vt.intro.bound.to_s}")
-                               if vt.intro_mclassdef != mclass.intro then
-                                       pager.add("\t\tintroduced in {vt.intro_mclassdef.namespace}::{vt}".gray)
+                       if not mclass.virtual_types.is_empty then
+                               pager.add("# virtual types".bold)
+                               for vt in mclass.virtual_types do
+                                       pager.add("")
+                                       vt_fulldoc(pager, vt)
                                end
-                               pager.add("")
                        end
-               end
-               pager.add_rule
-
-               var cats = new HashMap[String, Collection[MMethod]]
-               cats["constructors"] = mclass.constructors
-               cats["introduced methods"] = mclass.intro_methods
-               cats["refined methods"] = mclass.redef_methods
-               cats["inherited methods"] = mclass.inherited_methods
-
-               for cat, list in cats do
-                       if not list.is_empty then
-                               pager.add("# {cat}\n".bold)
-                               for mmethod in list do
-                                       #TODO verifier cast
-                                       var nmethod = mbuilder.mpropdef2npropdef[mmethod.intro].as(AMethPropdef)
-                                       if not nmethod.short_comment.is_empty then
-                                               pager.add("\t# {nmethod.short_comment}")
-                                       end
-                                       if cat == "refined methods" then
-                                               pager.add("\tredef {nmethod}")
-                                       else
-                                               pager.add("\t{nmethod}")
-                                       end
-                                       if not mmethod.intro_mclassdef == mclass.intro then
-                                               pager.add("\t\tintroduced in {mmethod.intro_mclassdef.namespace}::{mmethod}".gray)
+                       pager.add_rule
+
+                       var cats = new HashMap[String, Collection[MMethod]]
+                       cats["constructors"] = mclass.constructors
+                       cats["introduced methods"] = mclass.intro_methods
+                       cats["refined methods"] = mclass.redef_methods
+                       cats["inherited methods"] = mclass.inherited_methods
+
+                       for cat, list in cats do
+                               if not list.is_empty then
+                                       pager.add("\n# {cat}".bold)
+                                       for mprop in list do
+                                               pager.add("")
+                                               method_fulldoc(pager, mprop)
                                        end
+                               end
+                       end
+                       pager.add_rule
+               end
+               pager.render
+       end
+
+       private fun props_fulldoc(raw_mprops: List[MProperty]) do
+               var pager = new Pager
+               # group by module
+               var cats = new HashMap[MModule, List[MProperty]]
+               for mprop in raw_mprops do
+                       var mmodule = mprop.intro_mclassdef.mmodule
+                       if not cats.has_key(mmodule) then cats[mmodule] = new List[MProperty]
+                       cats[mmodule].add(mprop)
+               end
+               # display
+               for mmodule, mprops in cats do
+                       pager.add("# {mmodule.namespace}".bold)
+                       for mprop in mprops do
+                               if mprop isa MMethod and mbuilder.mpropdef2npropdef.has_key(mprop.intro) then
+                                       pager.add("")
+                                       method_fulldoc(pager, mprop)
+                               else if mprop isa MVirtualTypeProp then
                                        pager.add("")
+                                       vt_fulldoc(pager, mprop)
                                end
                        end
+                       pager.add_rule
                end
                pager.render
        end
+
+       private fun method_fulldoc(pager: Pager, mprop: MMethod) do
+               if mbuilder.mpropdef2npropdef.has_key(mprop.intro) then
+                       var nprop = mbuilder.mpropdef2npropdef[mprop.intro]
+                       if not nprop.short_comment.is_empty then
+                               pager.add("\t# {nprop.short_comment}")
+                       end
+                       if nprop isa AAttrPropdef then
+                               pager.add("\t{nprop.read_accessor}")
+                               pager.add("\t{nprop.write_accessor}")
+                       else if nprop isa AMethPropdef then
+                               pager.add("\t{nprop}")
+                       end
+                       pager.add("\t\t" + "introduced in {mprop.intro_mclassdef.namespace}".gray)
+                       for mpropdef in mprop.mpropdefs do
+                               if mpropdef != mprop.intro then
+                                       pager.add("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray)
+                               end
+                       end
+               end
+       end
+
+       private fun vt_fulldoc(pager: Pager, vt: MVirtualTypeProp) do
+               pager.add("\t{vt.short_doc}")
+               pager.add("\t\t" + "introduced in {vt.intro_mclassdef.namespace}::{vt}".gray)
+               for mpropdef in vt.mpropdefs do
+                       if mpropdef != vt.intro then
+                               pager.add("\t\t" + "refined in {mpropdef.mclassdef.namespace}".gray)
+                       end
+               end
+       end
 end
 
 # Printing facilities
 
+redef class MModule
+       private fun namespace: String do
+               return full_name
+       end
+end
+
 redef class MClass
 
        redef fun to_s: String do
@@ -231,9 +297,15 @@ redef class MClass
                if visibility.to_s == "public" then ret = "{ret}{to_s.green}"
                if visibility.to_s == "private" then ret = "{ret}{to_s.red}"
                if visibility.to_s == "protected" then ret = "{ret}{to_s.yellow}"
-               ret = "{ret} super {parents.join(", ")}"
+               if not parents.is_empty then
+                       ret = "{ret} super {parents.join(", ")}"
+               end
                return ret
        end
+
+       private fun namespace: String do
+               return "{intro_mmodule.public_owner.name}::{name}"
+       end
 end
 
 redef class MClassDef
@@ -242,6 +314,16 @@ redef class MClassDef
        end
 end
 
+redef class MVirtualTypeProp
+       private fun short_doc: String do
+               var ret = ""
+               if visibility.to_s == "public" then ret = "{to_s.green}: {intro.bound.to_s}"
+               if visibility.to_s == "private" then ret = "\t{to_s.red}: {intro.bound.to_s}"
+               if visibility.to_s == "protected" then ret = "\t{to_s.yellow}: {intro.bound.to_s}"
+               return ret
+       end
+end
+
 redef class AModule
        private fun comment: String do
                var ret = ""
@@ -277,8 +359,51 @@ redef class AStdClassdef
        end
 end
 
+redef class APropdef
+       private fun short_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
+
+       private fun read_accessor: String do
+               var ret = "fun "
+               var name = mreadpropdef.mproperty.name
+               if mpropdef.mproperty.visibility.to_s == "public" then ret = "{ret}{name.green}"
+               if mpropdef.mproperty.visibility.to_s == "private" then ret = "{ret}{name.red}"
+               if mpropdef.mproperty.visibility.to_s == "protected" then ret = "{ret}{name.yellow}"
+               ret = "{ret}: {n_type.to_s}"
+               if n_kwredef != null then ret = "redef {ret}"
+               return ret
+       end
+
+       private fun write_accessor: String do
+               var ret = "fun "
+               var name = "{mreadpropdef.mproperty.name}="
+               if n_readable != null and n_readable.n_visibility != null then
+                       if n_readable.n_visibility isa APublicVisibility then ret = "{ret}{name.green}"
+                       if n_readable.n_visibility isa APrivateVisibility then ret = "{ret}{name.red}"
+                       if n_readable.n_visibility isa AProtectedVisibility then ret = "{ret}{name.yellow}"
+               else
+                       ret = "{ret}{name.red}"
+               end
+               ret = "{ret}({mreadpropdef.mproperty.name}: {n_type.to_s})"
+               if n_kwredef != null then ret = "redef {ret}"
+               return ret
+       end
+end
+
 redef class AMethPropdef
-       private fun short_comment: String do
+       redef fun short_comment do
                var ret = ""
                if n_doc != null then
                        var txt = n_doc.n_comment.first.text
@@ -387,8 +512,10 @@ toolcontext.process_options
 var ni = new NitIndex(toolcontext)
 ni.start
 
-# TODO seek methods
-# TODO lister les methods qui retournent un certain type
-# TODO lister les methods qui utilisent un certain type
-# TODO lister les sous-types connus d'une type
-# TODO sorter par ordre alphabétique
+# TODO seek methods by return type :<type>
+# TODO seek methods by param type: (<type>)
+# TODO seek subclasses and super classes <.<class> >.<class>
+# TODO seek subclasses and super types <:<type> >:<type>
+# TODO sort by alphabetic order
+# TODO seek with regexp
+# TODO standardize namespaces with private option