+# A query to search signatures using a specific `MType` as parameter.
+redef class ParamCommand
+ redef fun perform(nitx, doc) do
+ var res = new Array[NitxMatch]
+ var mtype_name = args.first
+ for mproperty in doc.mproperties do
+ if not mproperty isa MMethod then continue
+ var msignature = mproperty.intro.msignature
+ if msignature != null then
+ for mparam in msignature.mparameters do
+ if mparam.mtype.name == mtype_name then
+ res.add new MEntityMatch(self, mproperty)
+ end
+ end
+ end
+ end
+ return res
+ end
+end
+
+# A query to search signatures using a specific `MType` as return.
+redef class ReturnCommand
+ redef fun perform(nitx, doc) do
+ var res = new Array[NitxMatch]
+ var mtype_name = args.first
+ for mproperty in doc.mproperties do
+ if not mproperty isa MMethod then continue
+ var msignature = mproperty.intro.msignature
+ if msignature != null then
+ var mreturn = msignature.return_mtype
+ if mreturn != null and mreturn.name == mtype_name then
+ res.add new MEntityMatch(self, mproperty)
+ end
+ end
+ end
+ return res
+ end
+end
+
+# A query to search methods creating new instances of a specific `MType`.
+redef class NewCommand
+ redef fun perform(nitx, doc) do
+ var res = new Array[NitxMatch]
+ var mtype_name = args.first
+ for mpropdef in doc.mpropdefs do
+ var visitor = new TypeInitVisitor(mtype_name)
+ var npropdef = nitx.ctx.modelbuilder.mpropdef2node(mpropdef)
+ if npropdef == null then continue
+ visitor.enter_visit(npropdef)
+ for i in visitor.inits do
+ res.add new MEntityMatch(self, mpropdef)
+ end
+ end
+ return res
+ end
+end
+
+# A query to search methods calling a specific `MProperty`.
+redef class CallCommand
+ 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.
+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
+
+ # `DocPage` matched.
+ var page: DocPage
+
+ redef fun make_list_item do
+ var page = self.page
+ if page isa MEntityPage then
+ return page.mentity.cs_list_item
+ end
+ return " * {page.title}"
+ end
+end
+
+# Search in class or module hierarchy of a `MEntity`.
+#
+# It actually searches for pages about the mentity and extracts the
+# pre-calculated hierarchies by the `doc_post` phase.
+abstract class HierarchiesQuery
+ super DocCommand
+
+ redef fun make_results(nitx, results) do
+ var page = new DocPage("hierarchy", "Hierarchy")
+ for result in results do
+ if not result isa PageMatch then continue
+ var rpage = result.page
+ if not rpage isa MClassPage then continue
+ page.root.add_child build_article(rpage)
+ end
+ return page
+ end
+
+ # Build an article containing the hierarchy list depending on subclasses.
+ private fun build_article(page: MClassPage): DocArticle is abstract
+end
+
+# List all parents of a `MClass`.
+class AncestorsQuery
+ super HierarchiesQuery
+
+ redef fun build_article(page) do
+ return new MEntitiesListArticle(
+ "ancerstors",
+ "Ancestors for {page.mentity.name}",
+ page.ancestors.to_a)
+ end
+end
+
+# List direct parents of a `MClass`.
+class ParentsQuery
+ super HierarchiesQuery
+
+ redef fun build_article(page) do
+ return new MEntitiesListArticle(
+ "parents",
+ "Parents for {page.mentity.name}",
+ page.parents.to_a)
+ end
+end
+
+# List direct children of a `MClass`.
+class ChildrenQuery
+ super HierarchiesQuery
+
+ redef fun build_article(page) do
+ return new MEntitiesListArticle(
+ "children",
+ "Children for {page.mentity.name}",
+ page.children.to_a)
+ end
+end
+
+# List all descendants of a `MClass`.
+class DescendantsQuery
+ super HierarchiesQuery
+
+ redef fun build_article(page) do
+ return new MEntitiesListArticle(
+ "descendants",
+ "Descendants for {page.mentity.name}",
+ page.children.to_a)
+ end
+end
+
+# A query to search source code from a file name.
+redef class CodeCommand
+ # 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", "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
+
+