nitx: introduce code search
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 4 May 2015 15:04:56 +0000 (11:04 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 22 May 2015 01:01:01 +0000 (21:01 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/console_templates/console_model.nit
src/doc/doc_phases/doc_console.nit
src/nitx.nit

index 8de793c..dee8a74 100644 (file)
@@ -121,6 +121,26 @@ redef class MEntity
        #
        # See module `console`.
        fun cs_visibility_color(string: String): String do return string.green
+
+       # Source code associated to this MEntity.
+       #
+       # Uses `cs_location` to locate the source code.
+       fun cs_source_code: String do
+               # FIXME up location to mentity
+               var loc = new Location.from_string(cs_location)
+               var fr = new FileReader.open(loc.file.filename)
+               var content = new FlatBuffer
+               var i = 0
+               while not fr.eof do
+                       i += 1
+                       var line = fr.read_line
+                       if i < loc.line_start or i > loc.line_end then continue
+                       # FIXME add nitlight for console
+                       content.append "{line}\n"
+               end
+               fr.close
+               return content.write_to_string
+       end
 end
 
 redef class MProject
index 49cacef..dbd1d39 100644 (file)
@@ -64,6 +64,7 @@ class Nitx
                print "\treturn: <Type>\tlookup methods returning the corresponding 'Type'"
                print "\tnew: <Type>\tlookup methods creating new instances of 'Type'"
                print "\tcall: <name>\tlookup methods calling 'name'"
+               print "\tcode: <name>\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 ""
@@ -125,6 +126,9 @@ interface NitxQuery
                        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
@@ -356,6 +360,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.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
+
+
 # A query that contains a nitx command.
 #
 # These commands are prefixed with `:` and are used to control the execution of
@@ -474,6 +526,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
 
index 7d7e872..23490a8 100644 (file)
@@ -20,6 +20,7 @@
 # * Display documentation page from Nitdoc in console
 # * Find type usage in parameters, returns and news.
 # * Find usage of a specific property.
+# * Find source code related to class/property by its name.
 module nitx
 
 import modelbuilder