doc/commands: introduce CmdEntityLink command
[nit.git] / src / doc / commands / commands_model.nit
index 5f2dd48..a119d45 100644 (file)
@@ -93,6 +93,34 @@ class WarningNoMDoc
        redef fun to_s do return "No documentation for `{mentity.full_name}`."
 end
 
+# Get the link to a MEntity API documentation
+class CmdEntityLink
+       super CmdEntity
+
+       # The link text to display
+       var text: nullable String = null is optional, writable
+
+       # The link title to display when the link is hovered
+       var title: nullable String = null is optional, writable
+
+       redef fun init_command do
+               var res = super
+               if not res isa CmdSuccess then return res
+               var mentity = self.mentity.as(not null)
+
+               if text == null then
+                       text = mentity.name
+               end
+               if title == null then
+                       var mdoc = mentity.mdoc_or_fallback
+                       if mdoc != null then
+                               title = mdoc.synopsis
+                       end
+               end
+               return res
+       end
+end
+
 # MEntity ancestors command
 #
 # Retrieve all the ancestors (direct and indirect) of a MEntity.
@@ -383,18 +411,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 +431,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 +473,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`