doc/commands: parser stores commands usage and help
[nit.git] / src / doc / doc_phases / doc_console.nit
index 070b86b..6a5d045 100644 (file)
@@ -22,6 +22,7 @@ import semantize
 import doc_commands
 import doc_poset
 import doc::console_templates
+import model::model_index
 
 # Nitx handles console I/O.
 #
@@ -87,43 +88,80 @@ class Nitx
                prompt
        end
 
+       # Parser used to process doc commands
+       var parser: DocCommandParser is lazy do
+               var parser = new DocCommandParser
+               parser.allowed_commands = ["doc", "comment", "list", "param", "return",
+                       "new", "call", "code"]
+               return parser
+       end
+
        # Processes the query string and performs it.
        fun do_query(str: String) do
-               var query = new DocCommand(str)
-               if query isa NitxCommand then
-                       query.execute(self)
+               if str == ":q" then
+                       exit 0
+               else if str == ":h" then
+                       help
                        return
                end
+               var query = parser.parse(str)
+               if query == null then
+                       query = new CommentCommand(str)
+                       query.arg = str
+               end
                var res = query.perform(self, doc)
-               var page = query.make_results(self, res)
+               var suggest = null
+               if res.is_empty then
+                       suggest = query.suggest(self, doc)
+               end
+               var page = query.make_results(self, res, suggest)
                print page.write_to_string
        end
 end
 
-redef interface DocCommand
-
-       redef new(query_string) do
-               if query_string == ":q" then
-                       return new NitxQuit
-               else if query_string == ":h" then
-                       return new NitxHelp
-               end
-               var cmd = super(query_string)
-               if cmd isa UnknownCommand then
-                       return new CommentCommand("comment: {query_string}")
-               end
-               return cmd
-       end
+redef class DocCommand
 
        # Looks up the `doc` model and returns possible matches.
        fun perform(nitx: Nitx, doc: DocModel): Array[NitxMatch] is abstract
 
+       # Looks up the `doc` model and returns possible suggestions.
+       fun suggest(nitx: Nitx, doc: DocModel): nullable Array[MEntity] do
+               return find_suggestions(doc, arg)
+       end
+
        # Pretty prints the results for the console.
-       fun make_results(nitx: Nitx, results: Array[NitxMatch]): DocPage do
+       fun make_results(nitx: Nitx, results: Array[NitxMatch], suggest: nullable Array[MEntity]): DocPage do
                var page = new DocPage("results", "Results")
-               page.root.add_child(new QueryResultArticle("results", "Results", self, results))
+               page.root.add_child(new QueryResultArticle("results", "Results", self, results, suggest))
                return page
        end
+
+       # Lookup mentities based on a `query` string.
+       #
+       # 1- lookup by first name (returns always one value)
+       # 2- lookup by name (can return conflicts)
+       fun find_mentities(doc: DocModel, query: String): Array[MEntityMatch] do
+               var res = new Array[MEntityMatch]
+
+               # First lookup by full_name
+               var mentity = doc.mentity_by_full_name(query)
+               if mentity != null then
+                       res.add new MEntityMatch(self, mentity)
+                       return res
+               end
+
+               # If no results, lookup by name
+               for m in doc.mentities_by_name(query) do
+                       res.add new MEntityMatch(self, m)
+               end
+
+               return res
+       end
+
+       # Suggest some mentities based on a `query` string.
+       fun find_suggestions(doc: DocModel, query: String): Array[MEntity] do
+               return doc.find(query, 3)
+       end
 end
 
 # Something that matches a `DocCommand`.
@@ -147,16 +185,9 @@ class MEntityMatch
 end
 
 redef class CommentCommand
-       redef fun perform(nitx, doc) do
-               var name = args.first
-               var res = new Array[NitxMatch]
-               for mentity in doc.mentities_by_name(name) do
-                       res.add new MEntityMatch(self, mentity)
-               end
-               return res
-       end
+       redef fun perform(nitx, doc) do return find_mentities(doc, arg)
 
-       redef fun make_results(nitx, results) do
+       redef fun make_results(nitx, results, suggest) do
                var len = results.length
                if len == 1 then
                        var res = results.first.as(MEntityMatch)
@@ -177,7 +208,7 @@ end
 redef class ParamCommand
        redef fun perform(nitx, doc) do
                var res = new Array[NitxMatch]
-               var mtype_name = args.first
+               var mtype_name = arg
                for mproperty in doc.mproperties do
                        if not mproperty isa MMethod then continue
                        var msignature = mproperty.intro.msignature
@@ -197,7 +228,7 @@ end
 redef class ReturnCommand
        redef fun perform(nitx, doc) do
                var res = new Array[NitxMatch]
-               var mtype_name = args.first
+               var mtype_name = arg
                for mproperty in doc.mproperties do
                        if not mproperty isa MMethod then continue
                        var msignature = mproperty.intro.msignature
@@ -216,7 +247,7 @@ end
 redef class NewCommand
        redef fun perform(nitx, doc) do
                var res = new Array[NitxMatch]
-               var mtype_name = args.first
+               var mtype_name = arg
                for mpropdef in doc.mpropdefs do
                        var visitor = new TypeInitVisitor(mtype_name)
                        var npropdef = nitx.ctx.modelbuilder.mpropdef2node(mpropdef)
@@ -234,7 +265,7 @@ end
 redef class CallCommand
        redef fun perform(nitx, doc) do
                var res = new Array[NitxMatch]
-               var mprop_name = args.first
+               var mprop_name = arg
                for mpropdef in doc.mpropdefs do
                        var visitor = new MPropertyCallVisitor
                        var npropdef = nitx.ctx.modelbuilder.mpropdef2node(mpropdef)
@@ -249,38 +280,6 @@ redef class CallCommand
        end
 end
 
-# A query to search a Nitdoc documentation page by its name.
-redef class ArticleCommand
-       redef fun perform(nitx, doc) do
-               var res = new Array[NitxMatch]
-               var name = args.first
-               for page in doc.pages.values do
-                       if name == "*" then # FIXME dev only
-                               res.add new PageMatch(self, page)
-                       else if page.title == name then
-                               res.add new PageMatch(self, page)
-                       else if page isa MEntityPage and page.mentity.cs_namespace == name then
-                               res.add new PageMatch(self, page)
-                       end
-               end
-               return res
-       end
-
-       redef fun make_results(nitx, results) do
-               var len = results.length
-               # FIXME how to render the pager for one worded namespaces like "core"?
-               if len == 1 then
-                       var page = results.first.as(PageMatch).page
-                       var pager = new Pager
-                       pager.add page.write_to_string
-                       pager.render
-                       return page
-               else
-                       return super
-               end
-       end
-end
-
 # A match between a `DocPage` and a `MEntity`.
 class PageMatch
        super NitxMatch
@@ -304,7 +303,7 @@ end
 abstract class HierarchiesQuery
        super DocCommand
 
-       redef fun make_results(nitx, results) do
+       redef fun make_results(nitx, results, suggest) do
                var page = new DocPage("hierarchy", "Hierarchy")
                for result in results do
                        if not result isa PageMatch then continue
@@ -372,7 +371,7 @@ redef class CodeCommand
        # FIXME refactor this!
        redef fun perform(nitx, doc) do
                var res = new Array[NitxMatch]
-               var name = args.first
+               var name = arg
                # if name is an existing sourcefile, opens it
                if name.file_exists then
                        var fr = new FileReader.open(name)
@@ -382,15 +381,15 @@ redef class CodeCommand
                        return res
                end
                # else, lookup the model by name
-               for mentity in doc.mentities_by_name(name) do
-                       if mentity isa MClass then continue
-                       if mentity isa MProperty then continue
-                       res.add new CodeMatch(self, mentity.cs_location, mentity.cs_source_code)
+               for match in find_mentities(doc, name) do
+                       if match.mentity isa MClass then continue
+                       if match.mentity isa MProperty then continue
+                       res.add new CodeMatch(self, match.mentity.cs_location, match.mentity.cs_source_code)
                end
                return res
        end
 
-       redef fun make_results(nitx, results) do
+       redef fun make_results(nitx, results, suggest) do
                var page = new DocPage("results", "Code Results")
                for res in results do
                        page.add new CodeQueryArticle("results", "Results", self, res.as(CodeMatch))
@@ -412,32 +411,6 @@ class CodeMatch
        redef fun make_list_item do return "* {location}"
 end
 
-
-# A query that contains a nitx command.
-#
-# These commands are prefixed with `:` and are used to control the execution of
-# `nitx` like displaying the help or quiting.
-interface NitxCommand
-       super DocCommand
-
-       # Executes the command.
-       fun execute(nitx: Nitx) is abstract
-end
-
-# Exits nitx.
-class NitxQuit
-       super NitxCommand
-
-       redef fun execute(nitx) do exit 0
-end
-
-# Displays the help message.
-class NitxHelp
-       super NitxCommand
-
-       redef fun execute(nitx) do nitx.help
-end
-
 ## exploration
 
 # Visitor looking for initialized `MType` (new T).
@@ -487,10 +460,25 @@ private class QueryResultArticle
        # Results to display.
        var results: Array[NitxMatch]
 
+       # Optional suggestion when no matches where found
+       var suggest: nullable Array[MEntity] = null is optional
+
        redef fun render_title do
                var len = results.length
                if len == 0 then
-                       add "No result found for '{query.string}'..."
+                       addn "No result found for '{query.string}'..."
+                       var suggest = self.suggest
+                       if suggest != null and suggest.not_empty then
+                               add "\nDid you mean "
+                               var i = 0
+                               for s in suggest do
+                                       add "`{s.full_name}`"
+                                       if i == suggest.length - 2 then add ", "
+                                       if i == suggest.length - 1 then add " or "
+                                       i += 1
+                               end
+                               add "?"
+                       end
                else
                        add "# {len} result(s) for '{query.string}'".green.bold
                end
@@ -551,7 +539,7 @@ private class Pager
                        else if c == '`' then
                                b.append("'")
                        else if c.code_point < 32 then
-                               b.append("\\{c.code_point.to_base(8, false)}")
+                               b.append("\\{c.code_point.to_base(8)}")
                        else
                                b.add(c)
                        end