X-Git-Url: http://nitlanguage.org diff --git a/src/doc/doc_phases/doc_console.nit b/src/doc/doc_phases/doc_console.nit index 0d1509c..4c0c193 100644 --- a/src/doc/doc_phases/doc_console.nit +++ b/src/doc/doc_phases/doc_console.nit @@ -19,6 +19,7 @@ module doc_console import semantize +import doc_extract import doc::console_templates # Nitx handles console I/O. @@ -63,6 +64,8 @@ class Nitx print "\tparam: \tlookup methods using the corresponding 'Type' as parameter" print "\treturn: \tlookup methods returning the corresponding 'Type'" print "\tnew: \tlookup methods creating new instances of 'Type'" + print "\tcall: \tlookup methods calling 'name'" + print "\tcode: \tdisplay the source code associated to the 'name' entity" print "\t:h\t\tdisplay this help message" print "\t:q\t\tquit interactive mode" print "" @@ -122,6 +125,11 @@ interface NitxQuery return new ReturnQuery(query_string) else if query_string.has_prefix("new:") then return new NewQuery(query_string) + else if query_string.has_prefix("call:") then + return new CallQuery(query_string) + else if query_string.has_prefix("code:") then + return new CodeQuery(query_string) + end return new CommentQuery("comment: {query_string}") end @@ -131,8 +139,8 @@ interface NitxQuery # Pretty prints the results for the console. fun make_results(nitx: Nitx, results: Array[NitxMatch]): DocPage do - var page = new DocPage("Results") - page.root.add_child(new QueryResultArticle(self, results)) + var page = new DocPage("results", "Results") + page.root.add_child(new QueryResultArticle("results.article", "Results", self, results)) return page end @@ -196,7 +204,7 @@ class CommentQuery redef fun perform(nitx, doc) do var name = args.first var res = new Array[NitxMatch] - for mentity in doc.search_mentities(name) do + for mentity in doc.mentities_by_name(name) do res.add new MEntityMatch(self, mentity) end return res @@ -207,8 +215,8 @@ class CommentQuery if len == 1 then var res = results.first.as(MEntityMatch) var mentity = res.mentity - var page = new DocPage("Results") - var article = new DefinitionArticle(mentity) + var page = new DocPage("results", "Results") + var article = new DefinitionArticle("results.article", "Results", mentity) article.cs_title = mentity.name article.cs_subtitle = mentity.cs_declaration page.root.add_child article @@ -282,6 +290,27 @@ class NewQuery end end +# A query to search methods calling a specific `MProperty`. +class CallQuery + super MetaQuery + + redef fun perform(nitx, doc) do + var res = new Array[NitxMatch] + var mprop_name = args.first + for mpropdef in doc.mpropdefs do + var visitor = new MPropertyCallVisitor + var npropdef = nitx.ctx.modelbuilder.mpropdef2node(mpropdef) + if npropdef == null then continue + visitor.enter_visit(npropdef) + for mprop in visitor.calls do + if mprop.name != mprop_name then continue + res.add new MEntityMatch(self, mpropdef) + end + end + return res + end +end + # A query to search a Nitdoc documentation page by its name. class DocQuery super MetaQuery @@ -289,7 +318,7 @@ class DocQuery redef fun perform(nitx, doc) do var res = new Array[NitxMatch] var name = args.first - for page in doc.pages do + 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 @@ -332,6 +361,54 @@ class PageMatch end end +# A query to search source code from a file name. +class CodeQuery + super MetaQuery + + # FIXME refactor this! + redef fun perform(nitx, doc) do + var res = new Array[NitxMatch] + var name = args.first + # if name is an existing sourcefile, opens it + if name.file_exists then + var fr = new FileReader.open(name) + var content = fr.read_all + fr.close + res.add new CodeMatch(self, name, content) + 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) + end + return res + end + + redef fun make_results(nitx, results) do + var page = new DocPage("results", "Code Results") + for res in results do + page.add new CodeQueryArticle("results.article", "Results", self, res.as(CodeMatch)) + end + return page + end +end + +# A match between a piece of code and a string. +class CodeMatch + super NitxMatch + + # Location of the code match. + var location: String + + # Piece of code matched. + var content: String + + 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 @@ -359,32 +436,6 @@ end ## exploration -redef class DocModel - - # Lists all MEntities in the model. - private var mentities: Collection[MEntity] is lazy do - var res = new HashSet[MEntity] - res.add_all mprojects - res.add_all mgroups - res.add_all mmodules - res.add_all mclasses - res.add_all mclassdefs - res.add_all mproperties - res.add_all mpropdefs - return res - end - - # Search MEntities that match `name` by their name or namespace. - private fun search_mentities(name: String): Array[MEntity] do - var res = new Array[MEntity] - for mentity in mentities do - if mentity.name != name and mentity.cs_namespace != name then continue - res.add mentity - end - return res - end -end - # Visitor looking for initialized `MType` (new T). # # See `NewQuery`. @@ -405,6 +456,21 @@ private class TypeInitVisitor end end +# Visitor looking for calls to a `MProperty` (new T). +# +# See `CallQuery`. +private class MPropertyCallVisitor + super Visitor + + var calls = new HashSet[MProperty] + redef fun visit(node) + do + node.visit_all(self) + if not node isa ASendExpr then return + calls.add node.callsite.mproperty + end +end + # display # A `DocArticle` that displays query results. @@ -435,6 +501,24 @@ private class QueryResultArticle end end +# An article that displays a piece of code. +private class CodeQueryArticle + super DocArticle + + # The query linked to the result to display. + var query: NitxQuery + + # The result to display. + var result: CodeMatch + + redef fun render_body do + addn "" + addn "in {result.location}".gray.bold + addn "" + add result.content + end +end + # A Pager is used to display data into a unix `less` container. private class Pager