+# 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.search_mentities(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("Code Results")
+ for res in results do
+ page.add new CodeQueryArticle(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
+
+