doc/commands: extract `CmdCode` super class
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 22 Jan 2018 20:24:38 +0000 (15:24 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 22 Jan 2018 20:29:27 +0000 (15:29 -0500)
So we can factorize code related queries even if they are not related to a specific MEntity

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/api/api_model.nit
src/doc/commands/commands_html.nit
src/doc/commands/commands_http.nit
src/doc/commands/commands_json.nit
src/doc/commands/commands_model.nit
src/doc/commands/commands_parser.nit
src/doc/term/term.nit

index 9619892..e2b3473 100644 (file)
@@ -205,7 +205,7 @@ end
 class APIEntityCode
        super APICommand
 
-       redef fun command do return new CmdCode(config.view, config.modelbuilder)
+       redef fun command do return new CmdEntityCode(config.view, config.modelbuilder)
 end
 
 # Return the UML diagram for MEntity.
index ce5d89a..ee7a792 100644 (file)
@@ -104,9 +104,9 @@ redef class CmdComment
        end
 end
 
-redef class CmdCode
+redef class CmdEntityCode
        redef fun to_html do
-               var output = render
+               var output = render_code(node)
                if output == null then return ""
                return "<pre>{output.write_to_string}</pre>"
        end
index 83f2ef1..d35161b 100644 (file)
@@ -28,8 +28,6 @@ redef class DocCommand
 end
 
 redef class CmdEntity
-
-
        redef fun http_init(req) do
                var name = req.param("id")
                if name != null then name = name.from_percent_encoding
@@ -127,6 +125,18 @@ redef class CmdCode
        end
 end
 
+redef class CmdEntityCode
+       # FIXME avoid linearization conflict
+       redef fun http_init(req) do
+               var name = req.param("id")
+               if name != null then name = name.from_percent_encoding
+               mentity_name = name
+
+               format = req.string_arg("format") or else "raw"
+               return init_command
+       end
+end
+
 # CmdGraph
 
 redef class CmdGraph
index eb40af4..a4457f1 100644 (file)
@@ -73,14 +73,14 @@ redef class CmdComment
        end
 end
 
-redef class CmdCode
+redef class CmdEntityCode
        redef fun to_json do
                var obj = new JsonObject
                var node = self.node
                if node != null then
                        obj["location"] = node.location
                end
-               var output = render
+               var output = render_code(node)
                if output != null then
                        obj["code"] = output.write_to_string
                end
index 5f2dd48..c359546 100644 (file)
@@ -383,18 +383,15 @@ class WarningNoFeatures
        redef fun to_s do return "No features for `{mentity.full_name}`"
 end
 
-# Cmd that finds the source code related to an `mentity`
-class CmdCode
-       super CmdEntity
+# Abstract command that returns source-code pieces
+abstract class CmdCode
+       super DocCommand
 
-       autoinit(view, modelbuilder, mentity, mentity_name, format)
+       autoinit(view, modelbuilder, format)
 
        # ModelBuilder used to get AST nodes
        var modelbuilder: ModelBuilder
 
-       # AST node to return
-       var node: nullable ANode = null is optional, writable
-
        # Rendering format
        #
        # Set the output format for this piece of code.
@@ -406,6 +403,32 @@ class CmdCode
        # Another example is to render raw format to put into a HTML code tag.
        var format = "raw" is optional, writable
 
+       # Render `node` depending on the selected `format`
+       fun render_code(node: nullable ANode): nullable Writable do
+               if node == null then return null
+               if format == "html" then
+                       var hl = new HtmlightVisitor
+                       hl.highlight_node node
+                       return hl.html
+               else if format == "ansi" then
+                       var hl = new AnsiHighlightVisitor
+                       hl.highlight_node node
+                       return hl.result
+               end
+               return node.location.text
+       end
+end
+
+# Cmd that finds the source code related to an `mentity`
+class CmdEntityCode
+       super CmdEntity
+       super CmdCode
+
+       autoinit(view, modelbuilder, mentity, mentity_name, format)
+
+       # AST node to return
+       var node: nullable ANode = null is optional, writable
+
        # Same as `CmdEntity::init_mentity`
        #
        # Plus `WarningNoCode` if no code/AST node is found for `mentity`.
@@ -422,24 +445,6 @@ class CmdCode
                if node == null then return new WarningNoCode(mentity)
                return res
        end
-
-       # Render `node` depending on the selected `format`
-       fun render: nullable Writable do
-               var node = self.node
-               if node == null then return null
-               if format == "html" then
-                       var hl = new HtmlightVisitor
-                       hl.highlight_node node
-                       return hl.html
-               else if format == "ansi" then
-                       var hl = new AnsiHighlightVisitor
-                       hl.highlight_node node
-                       return hl.result
-               end
-               var mentity = self.mentity
-               if mentity == null then return null
-               return mentity.location.text
-       end
 end
 
 # No code for `mentity`
index 182ca53..d0711eb 100644 (file)
@@ -136,7 +136,7 @@ class CommandParser
        fun new_command(name: String): nullable DocCommand do
                # CmdEntity
                if name == "doc" then return new CmdComment(view)
-               if name == "code" then return new CmdCode(view, modelbuilder)
+               if name == "code" then return new CmdEntityCode(view, modelbuilder)
                if name == "lin" then return new CmdLinearization(view)
                if name == "defs" then return new CmdFeatures(view)
                if name == "parents" then return new CmdParents(view)
index 61d5206..a82d7b8 100644 (file)
@@ -207,7 +207,7 @@ redef class CmdFeatures
        end
 end
 
-redef class CmdCode
+redef class CmdEntityCode
 
        redef var format = "ansi" is optional
 
@@ -222,7 +222,7 @@ redef class CmdCode
                        print title
                end
                if no_color == null or not no_color then
-                       var ansi = render
+                       var ansi = render_code(node)
                        if ansi != null then
                                print "~~~"
                                print ansi.write_to_string