ni: cleaned options context
[nit.git] / src / ni.nit
index 9f4b09c..76a40b6 100644 (file)
@@ -36,9 +36,10 @@ class NitIndex
        init(toolcontext: ToolContext) do
                # We need a model to collect stufs
                self.toolcontext = toolcontext
+               self.toolcontext.option_context.options.clear
                self.arguments = toolcontext.option_context.rest
 
-               if arguments.length > 2 then
+               if arguments.is_empty or arguments.length > 2 then
                        print "usage: ni path/to/module.nit [expression]"
                        toolcontext.option_context.usage
                        exit(1)
@@ -84,33 +85,50 @@ class NitIndex
        fun seek(entry: String) do
                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
+               # seek return types
+               if entry.has_prefix("return:") then
+                       var ret = entry.split_with(":")[1].replace(" ", "")
+                       var matches = seek_returns(ret)
+                       if not matches.is_empty then
                                flag = true
-                               mmatches.add(m)
+                               props_fulldoc(matches)
                        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
+               else if entry.has_prefix("param:") then
+                       var param = entry.split_with(":")[1].replace(" ", "")
+                       var matches = seek_params(param)
+                       if not matches.is_empty then
                                flag = true
-                               cmatches.add(c)
+                               props_fulldoc(matches)
                        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
-                               flag = true
-                               matches.add(p)
+               else
+                       # seek for modules
+                       var mmatches = new List[MModule]
+                       for m in model.mmodules do
+                               if m.name == entry then
+                                       flag = true
+                                       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
+                                       flag = true
+                                       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
+                                       flag = true
+                                       matches.add(p)
+                               end
+                       end
+                       if not matches.is_empty then props_fulldoc(matches)
                end
-               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
@@ -225,21 +243,23 @@ class NitIndex
        private fun props_fulldoc(raw_mprops: List[MProperty]) do
                var pager = new Pager
                # group by module
-               var cats = new HashMap[MModule, Array[MProperty]]
+               var cats = new HashMap[MClass, Array[MProperty]]
                for mprop in raw_mprops do
-                       var mmodule = mprop.intro_mclassdef.mmodule
-                       if not cats.has_key(mmodule) then cats[mmodule] = new Array[MProperty]
-                       cats[mmodule].add(mprop)
+                       if not mbuilder.mpropdef2npropdef.has_key(mprop.intro) then continue
+                       if mprop isa MAttribute then continue
+                       var mclass = mprop.intro_mclassdef.mclass
+                       if not cats.has_key(mclass) then cats[mclass] = new Array[MProperty]
+                       cats[mclass].add(mprop)
                end
                #sort groups
-               var sorter = new ComparableSorter[MModule]
-               var sorted = new Array[MModule]
+               var sorter = new ComparableSorter[MClass]
+               var sorted = new Array[MClass]
                sorted.add_all(cats.keys)
                sorter.sort(sorted)
                # display
-               for mmodule in sorted do
-                       var mprops = cats[mmodule]
-                       pager.add("# {mmodule.namespace}".bold)
+               for mclass in sorted do
+                       var mprops = cats[mclass]
+                       pager.add("# {mclass.namespace}".bold)
                        var sorterp = new ComparableSorter[MProperty]
                        sorterp.sort(mprops)
                        for mprop in mprops do
@@ -256,6 +276,37 @@ class NitIndex
                pager.render
        end
 
+       private fun seek_returns(entry: String): List[MProperty] do
+               # TODO how to match with generic types?
+               var matches = new List[MProperty]
+               for mprop in model.mproperties do
+                       var intro = mprop.intro
+                       if intro isa MMethodDef then
+                               if intro.msignature.return_mtype != null and intro.msignature.return_mtype.to_s == entry then matches.add(mprop)
+                       else if intro isa MAttributeDef then
+                               if intro.static_mtype.to_s == entry then matches.add(mprop)
+                       end
+               end
+               return matches
+       end
+
+       private fun seek_params(entry: String): List[MProperty] do
+               # TODO how to match with generic types?
+               var matches = new List[MProperty]
+               for mprop in model.mproperties do
+                       var intro = mprop.intro
+                       if intro isa MMethodDef then
+                               var mparameters = intro.msignature.mparameters
+                               for mparameter in mparameters do
+                                       if mparameter.mtype.to_s == entry then matches.add(mprop)
+                               end
+                       else if intro isa MAttributeDef then
+                               if intro.static_mtype.to_s == entry then matches.add(mprop)
+                       end
+               end
+               return matches
+       end
+
        private fun method_fulldoc(pager: Pager, mprop: MMethod) do
                if mbuilder.mpropdef2npropdef.has_key(mprop.intro) then
                        var nprop = mbuilder.mpropdef2npropdef[mprop.intro]
@@ -408,6 +459,7 @@ redef class AAttrPropdef
 
        private fun read_accessor: String do
                var ret = "fun "
+               #FIXME bug with standard::stream::FDStream::fd
                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}"
@@ -543,8 +595,6 @@ toolcontext.process_options
 var ni = new NitIndex(toolcontext)
 ni.start
 
-# 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 seek with regexp