# 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 page from Nitdoc in console
+# * Display documentation from name/namespace
# * Find type usage in parameters, returns and news.
-# * Find usage of a specific property.
+# * 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
+
+ # ModelView that contains the informations to display
+ var view: ModelView
+
+ # 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(view, 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
+
+ # Parser used to process doc commands
+ var parser = new CommandParser(view, modelbuilder, catalog) is lazy
- # 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)
+ # 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(view: ModelView) do
+ # Compute the poset
+ for p in view.mpackages do
+ var g = p.root
+ assert g != null
+ modelbuilder.scan_group(g)
+
+ deps.add_node(p)
+ for gg in p.mgroups do for m in gg.mmodules do
+ for im in m.in_importation.direct_greaters do
+ var ip = im.mpackage
+ if ip == null or ip == p then continue
+ deps.add_edge(p, ip)
+ end
+ end
+ end
+ # Build the catalog
+ for mpackage in view.mpackages do
+ package_page(mpackage)
+ git_info(mpackage)
+ mpackage_stats(mpackage)
+ end
end
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
if mmodules.is_empty then return
mbuilder.run_phases
toolcontext.run_global_phases(mmodules)
+var mainmodule = toolcontext.make_main_module(mmodules)
+
+# build views
+var view = new ModelView(model, mainmodule)
+var catalog = null
+if toolcontext.opt_catalog.value then
+ catalog = new Catalog(mbuilder)
+ catalog.build_catalog(view)
+end
+
+# start nitx
+var nitx = new Nitx(view, 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