typing: add `ARangeExpr::init_callsite` and use it everywhere
[nit.git] / src / nitx.nit
index 8915412..d70c9e3 100644 (file)
@@ -17,7 +17,6 @@ module nitx
 
 import model_utils
 import modelize_property
-import frontend
 
 # Main class of the nit index tool
 # NitIndex build the model using the toolcontext argument
@@ -30,15 +29,26 @@ class NitIndex
        private var arguments: Array[String]
        private var renderer: PagerMatchesRenderer
 
+       # New constructor to use the pre-calculated model when interpreting a module
+       init with_infos(mbuilder: ModelBuilder, mmodule: MModule) do
+
+               self.model = mbuilder.model
+               self.mbuilder = mbuilder
+
+               self.mainmodule = mmodule
+               self.toolcontext = mbuilder.toolcontext
+               self.arguments = toolcontext.option_context.rest
+
+               renderer = new PagerMatchesRenderer(self)
+       end
+
        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.is_empty or arguments.length > 2 then
-                       print "usage: ni path/to/module.nit [expression]"
-                       toolcontext.option_context.usage
+               if arguments.length > 2 then
+                       print toolcontext.tooldescription
                        exit(1)
                end
 
@@ -107,7 +117,7 @@ class NitIndex
                        prompt
                        return
                end
-               if entry == ":q" then exit(0)
+               if entry == ":q" then return
 
                # Parse query string
                var query = parse_query(entry)
@@ -146,7 +156,7 @@ class NitIndex
                else
                        var category = parts[0]
                        var keyword = parts[1]
-                       if keyword.first == ' ' then keyword = keyword.substring_from(1)
+                       if keyword.chars.first == ' ' then keyword = keyword.substring_from(1)
                        return new IndexQueryPair(str, keyword, category)
                end
        end
@@ -252,7 +262,7 @@ end
 # Code Analysis
 
 redef class ToolContext
-       private var nitx_phase: NitxPhase = new NitxPhase(self, [typing_phase])
+       private var nitx_phase: NitxPhase = new NitxPhase(self, [modelize_property_phase])
 end
 
 # Compiler phase for nitx
@@ -357,36 +367,32 @@ end
 redef class MModule
        super IndexMatch
        # prototype of the module
-       #       module ownername::name
+       #       module name
        private fun prototype: String do return "module {name.bold}"
 
        # namespace of the module
-       #       ownername::name
+       #       project::name
        private fun namespace: String do
-               if public_owner == null then
+               if mgroup == null or mgroup.mproject.name == self.name then
                        return self.name
                else
-                       return "{public_owner.namespace}::{self.name}"
+                       return "{mgroup.mproject}::{self.name}"
                end
        end
 
        redef fun preview(index, pager) do
-               if index.mbuilder.mmodule2nmodule.has_key(self) then
-                       var node = index.mbuilder.mmodule2nmodule[self]
-                       if node.n_moduledecl != null and not node.n_moduledecl.n_doc == null and not node.n_moduledecl.n_doc.short_comment.is_empty then
-                               pager.add(node.n_moduledecl.n_doc.short_comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       pager.add(mdoc.short_comment.green)
                end
                pager.add(prototype)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
        end
 
        redef fun content(index, pager) do
-               if index.mbuilder.mmodule2nmodule.has_key(self) then
-                       var node = index.mbuilder.mmodule2nmodule[self]
-                       if node.n_moduledecl != null and not node.n_moduledecl.n_doc == null and not node.n_moduledecl.n_doc.comment.is_empty then
-                               for comment in node.n_moduledecl.n_doc.comment do pager.add(comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       for comment in mdoc.content do pager.add(comment.green)
                end
                pager.add(prototype)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
@@ -395,7 +401,7 @@ redef class MModule
                # imported modules
                var imports = new Array[MModule]
                for mmodule in in_importation.direct_greaters.to_a do
-                       if not in_nesting.direct_greaters.has(mmodule) then imports.add(mmodule)
+                       imports.add(mmodule)
                end
                if not imports.is_empty then
                        sorter.sort(imports)
@@ -408,19 +414,6 @@ redef class MModule
                        end
                        pager.indent = pager.indent - 1
                end
-               # nested modules
-               var nested = in_nesting.direct_greaters.to_a
-               if not nested.is_empty then
-                       sorter.sort(nested)
-                       pager.add("")
-                       pager.add("== nested modules".bold)
-                       pager.indent = pager.indent + 1
-                       for mmodule in nested do
-                               pager.add("")
-                               mmodule.preview(index, pager)
-                       end
-                       pager.indent = pager.indent - 1
-               end
                # mclassdefs
                var csorter = new MClassDefNameSorter
                var intros = new Array[MClassDef]
@@ -499,11 +492,9 @@ redef class MClass
 
        redef fun content(index, pager) do
                # intro comment
-               if index.mbuilder.mclassdef2nclassdef.has_key(intro) then
-                       var node = index.mbuilder.mclassdef2nclassdef[intro]
-                       if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.comment.is_empty then
-                               for comment in node.n_doc.comment do pager.add(comment.green)
-                       end
+               var mdoc = intro.mdoc
+               if mdoc != null then
+                       for comment in mdoc.content do pager.add(comment.green)
                end
                pager.add(intro.to_console)
                pager.add("{intro.namespace}".bold.gray + " (lines {intro.location.lines})".gray)
@@ -584,22 +575,18 @@ redef class MClassDef
        end
 
        redef fun preview(index, pager) do
-               if index.mbuilder.mclassdef2nclassdef.has_key(self) then
-                       var node = index.mbuilder.mclassdef2nclassdef[self]
-                       if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.short_comment.is_empty then
-                               pager.add(node.n_doc.short_comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       pager.add(mdoc.short_comment.green)
                end
                pager.add(to_console)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
        end
 
        redef fun content(index, pager) do
-               if index.mbuilder.mclassdef2nclassdef.has_key(self) then
-                       var node = index.mbuilder.mclassdef2nclassdef[self]
-                       if node isa AStdClassdef and not node.n_doc == null and not node.n_doc.comment.is_empty then
-                               for comment in node.n_doc.comment do pager.add(comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       for comment in mdoc.content do pager.add(comment.green)
                end
                pager.add(to_console)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
@@ -685,22 +672,18 @@ redef class MPropDef
        end
 
        redef fun preview(index, pager) do
-               if index.mbuilder.mpropdef2npropdef.has_key(self) then
-                       var nprop = index.mbuilder.mpropdef2npropdef[self]
-                       if not nprop.n_doc == null and not nprop.n_doc.short_comment.is_empty then
-                               pager.add(nprop.n_doc.short_comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       pager.add(mdoc.short_comment.green)
                end
                pager.add(to_console)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
        end
 
        redef fun content(index, pager) do
-               if index.mbuilder.mpropdef2npropdef.has_key(self) then
-                       var nprop = index.mbuilder.mpropdef2npropdef[self]
-                       if not nprop.n_doc == null and not nprop.n_doc.comment.is_empty then
-                               for comment in nprop.n_doc.comment do pager.add(comment.green)
-                       end
+               var mdoc = self.mdoc
+               if mdoc != null then
+                       for comment in mdoc.content do pager.add(comment.green)
                end
                pager.add(to_console)
                pager.add("{namespace}".bold.gray + " (lines {location.lines})".gray)
@@ -798,17 +781,9 @@ redef class MVirtualType
        redef fun to_console do return mproperty.name
 end
 
-redef class ADoc
-       private fun comment: List[String] do
-               var res = new List[String]
-               for t in n_comment do
-                       res.add(t.text.replace("\n", ""))
-               end
-               return res
-       end
-
+redef class MDoc
        private fun short_comment: String do
-               return n_comment.first.text.replace("\n", "")
+               return content.first
        end
 end
 
@@ -863,7 +838,7 @@ redef class String
        private fun escape: String
        do
                var b = new Buffer
-               for c in self do
+               for c in self.chars do
                        if c == '\n' then
                                b.append("\\n")
                        else if c == '\0' then
@@ -892,7 +867,8 @@ end
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
-toolcontext.process_options
+toolcontext.tooldescription = "Usage: nitx [OPTION]... <file.nit> [query]\nDisplays specific pieces of API information from Nit source files."
+toolcontext.process_options(args)
 
 # Here we launch the nit index
 var ni = new NitIndex(toolcontext)