Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / nitx.nit
index c1ce189..7162e43 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# `nitx`, is a command tool that displays useful informations about the code.
+# `nitx`, a command tool that displays useful data about Nit code
 #
 # Features:
 #
-# * Display comment from name/namespace
+# * Display documentation from name/namespace
+# * Find type usage in parameters, returns and news.
+# * Find usage (calls) of a specific property.
+# * Find source code related to class/property by its name.
+# * Display inheritance lists
 module nitx
 
-import modelbuilder
-import doc::doc_phases::doc_console
+import frontend
+import doc::term
+import prompt
 
 redef class ToolContext
 
-       # Nittx generation phase.
-       var docx: Phase = new NitxPhase(self, null)
-
        # Used to shortcut the prompt and display directly the result in console.
-       var opt_query = new OptionString("Nitx query to perform", "-q", "--query")
+       var opt_command = new OptionString("Nitx command to perform", "-c", "--command")
+
+       # Compute and use the catalog
+       var opt_catalog = new OptionBool("Use catalog", "--catalog")
 
-       init do option_context.add_option opt_query
+       init do option_context.add_option(opt_command, opt_catalog)
 end
 
-# Nitx phase explores the model and prepares the console rendering.
-private class NitxPhase
-       super Phase
-       redef fun process_mainmodule(mainmodule, mmodules)
-       do
-               var doc = new DocModel(mainmodule.model, mainmodule)
-
-               var phases = [
-                       new ExtractionPhase(toolcontext, doc),
-                       new MakePagePhase(toolcontext, doc),
-                       new ConcernsPhase(toolcontext, doc),
-                       new StructurePhase(toolcontext, doc): DocPhase]
-
-               for phase in phases do
-                       toolcontext.info("# {phase.class_name}", 1)
-                       phase.apply
+# Nitx handles console queries
+#
+# Using `prompt`, the command line can be turned on an interactive tool.
+class Nitx
+
+       # Model that contains the informations to display
+       var model: Model
+
+       # Mainmodule for class linearization
+       var mainmodule: MModule
+
+       # ModelBuilder to access AST nodes
+       var modelbuilder: ModelBuilder
+
+       # Catalog if any
+       var catalog: nullable Catalog = null is optional
+
+       # Do not use colors in the output
+       var no_color = false is optional
+
+       # Displays the welcome message and start prompt.
+       fun start do
+               welcome
+               prompt
+       end
+
+       # Displays the welcome message and the list of loaded modules.
+       fun welcome do
+               print "Welcome in the Nit Index!"
+               print ""
+               print "Loaded packages:\n"
+               var cmd = new CmdModelEntities(model, kind = "packages")
+               cmd.init_command
+               for mpackage in cmd.results.as(not null) do
+                       print " * {mpackage.full_name}"
+               end
+               help
+       end
+
+       # Displays the list of available queries.
+       fun help do
+               # TODO automatize that
+               print "\nCommands:\n"
+               for usage, doc in parser.commands_usage do
+                       var l = usage.length / 8
+                       print "\t{usage}{"\t" * (3 - l)}{doc}"
                end
+               print "\n"
+               print "\t:h\t\t\tdisplay this help message"
+               print "\t:q\t\t\tquit interactive mode"
+               print ""
+       end
+
+       # Prompts the user for a query.
+       fun prompt do
+               var line = sys.prompt(">> ", true)
+               if line != null then
+                       do_command(line)
+               else
+                       # EOF
+                       exit 0
+               end
+               prompt
+       end
 
-               # start nitx
-               var nitx = new Nitx(toolcontext, doc)
-               var q = toolcontext.opt_query.value
-               if q != null then # shortcut prompt
-                       print ""
-                       nitx.do_query(q)
+       # Parser used to process doc commands
+       var parser = new CommandParser(model, mainmodule, modelbuilder, catalog) is lazy
+
+       # Processes the query string and performs it.
+       fun do_command(str: String) do
+               if str == ":q" then
+                       exit 0
+               else if str == ":h" then
+                       help
                        return
                end
-               nitx.start
+               parser.execute(str, no_color)
+       end
+end
+
+redef class Catalog
+       # Build the catalog for Nitx
+       private fun build_catalog(model: Model, filter: nullable ModelFilter) do
+               # Scan all modules of collected packages
+               for p in model.collect_mpackages(filter) do
+                       var g = p.root
+                       assert g != null
+                       modelbuilder.scan_group(g)
+               end
+               # Build the catalog
+               for mpackage in model.collect_mpackages(filter) do
+                       package_page(mpackage)
+                       git_info(mpackage)
+                       mpackage_stats(mpackage)
+               end
        end
 end
 
@@ -67,7 +140,7 @@ end
 var toolcontext = new ToolContext
 var tpl = new Template
 tpl.add "Usage: nitx [OPTION]... <file.nit>... [query]\n"
-tpl.add "Displays specific pieces of API information from Nit source files."
+tpl.add "Displays pieces of API information from Nit source files."
 toolcontext.tooldescription = tpl.write_to_string
 
 # process options
@@ -83,3 +156,21 @@ var mmodules = mbuilder.parse_full(arguments)
 if mmodules.is_empty then return
 mbuilder.run_phases
 toolcontext.run_global_phases(mmodules)
+var mainmodule = toolcontext.make_main_module(mmodules)
+
+# build views
+var catalog = null
+if toolcontext.opt_catalog.value then
+       catalog = new Catalog(mbuilder)
+       catalog.build_catalog(model)
+end
+
+# start nitx
+var nitx = new Nitx(model, mainmodule, mbuilder, catalog, toolcontext.opt_no_color.value)
+var q = toolcontext.opt_command.value
+if q != null then # shortcut prompt
+       print ""
+       nitx.do_command(q)
+       return
+end
+nitx.start