Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "LDT/User Area/Documentation Language"

< LDT
(Identifiers)
(Samples)
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This documentation language has been developed as part of Koneki, its main goal is to '''describe the API supplied by a file'''. It is strongly inspired by LDoc. Information given with this language is parsed by LDT and supply advanced features such as code completion and documentation view. Before diving into syntax, it is time to enumerate the underlying concepts, knowing them will enable you to write documentation more efficiently.
+
This documentation language has been developed as part of LDT, its main goal is to '''describe the API supplied by a file'''. It is strongly inspired by LDoc. Information given with this language is parsed by LDT and supply advanced features such as code completion and documentation view. Before diving into syntax, it is time to enumerate the underlying concepts, knowing them will enable you to write documentation more efficiently.
  
 
= Concepts =
 
= Concepts =
Line 29: Line 29:
 
== Global ==
 
== Global ==
  
When a module is required, it could modify the global environment. To handle those cases, the predefined type ''global'' is available. It is a reserved type which will be available everywhere, enabling you to attach field and function to it.
+
When a module is required, it could modify the global environment. To handle those cases, the predefined type ''global'' is available. It is a reserved type which will be available everywhere, enabling you to attach fields and functions to it.
This is a way to express the creation of new global variables when module is loaded.
+
This is a way to express the creation of new global variables when a module is loaded.
 +
 
 +
{{message|'''global variable auto-completion behavior'''
 +
To be aware of where a global variable should be accessible is a real challenge for tooling. Actually, there are two possibilities :
 +
*the global variable is preloaded by the interpreter (VM) at launch : in this case, it's easy, the global variable is always accessible.
 +
*the global is created dynamically during runtime : in this case, it's very difficult to know where the global variable must be available in autocompletion.
 +
 
 +
The first case (preloaded global var) is managed by the use of a global.lua file in [[Koneki/LDT/User_Area/Execution_Environment_file_format#Execution_Environment | execution environment]] which must contains all preloaded global variables. (Those global vars will always be available in autocompletion)
 +
 
 +
The other case, LDT lets you choose between (customizable in ''Preference/Lua''):
 +
* '''All''' (default): display all global variables available in project sourcepath/buildpath. (with the risk to reference globals where they are not effectively available at runtime)
 +
* '''None''': do not propose any global of this kind.
 +
|image=Idea.png|bgcolor=#def3fe|bdcolor=#c5d7e0}}
 +
 
 +
== List ==
 +
 
 +
A '''list''' is a sequence of values stored in a table under numeric indexes.
 +
 
 +
== Map ==
 +
 
 +
A '''map''' is a set of  key/value pairs stored in a table.
  
 
== Type references ==
 
== Type references ==
Line 58: Line 78:
 
* <code>modulename#typename</code> will refer to type ''typename'' defined in module ''modulename''.
 
* <code>modulename#typename</code> will refer to type ''typename'' defined in module ''modulename''.
 
* <code>modulename#modulename</code> will refer to type ''modulename'' returned in module ''modulename''.
 
* <code>modulename#modulename</code> will refer to type ''modulename'' returned in module ''modulename''.
 +
 +
== Structure types ==
 +
 +
Enables to describe list and map structures.
 +
 +
* <code>#list<#valuetype></code> where ''#valuetype'' is the reference to the type contained in the list.
 +
* <code>#map<#keyvalue,#valuetype></code> where ''#valuetype'' is the reference to the type contained in the map and ''#keyvalue'' the reference of the type used to index map values.
  
 
= Comments =
 
= Comments =
  
It is the ''only source'' of information of our documentation language. So far, there is no inference from code. So all aspect of the API has to be described explicitly.
+
You could describe explicitly all file API just with our documentation language.
 +
But LDT is able to guess quite a lot of this information from code. (see [[#Samples|samples]])
  
 
== Special comments ==
 
== Special comments ==
Line 73: Line 101:
 
'''Note''':  All leading <code>-</code>'s in special comments are trimmed.
 
'''Note''':  All leading <code>-</code>'s in special comments are trimmed.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Short description.
 
-- Short description.
 
-- Long _markdown_ description
 
-- Long _markdown_ description
</pre></code>
+
</source>
 
<center><small>Special comment with trimmed first line </small></center>
 
<center><small>Special comment with trimmed first line </small></center>
 
'''Note''': <code>_markdown_</code> is supposed to be interpreted by [http://en.wikipedia.org/wiki/Markdown Markdown].
 
'''Note''': <code>_markdown_</code> is supposed to be interpreted by [http://en.wikipedia.org/wiki/Markdown Markdown].
  
<code><pre>
+
<source lang="lua">
 
---
 
---
 
-- Short description.
 
-- Short description.
 
-- Long description
 
-- Long description
</pre></code>
+
</source>
 
<center><small>Special comment composed of short comments format</small></center>
 
<center><small>Special comment composed of short comments format</small></center>
  
<code><pre>
+
<source lang="lua">
 
--[[-
 
--[[-
 
  Short description.
 
  Short description.
 
  Long description
 
  Long description
 
]]
 
]]
</pre></code>
+
</source>
 
<center><small> Special comment with long comment format</small></center>
 
<center><small> Special comment with long comment format</small></center>
  
<code><pre>--- Short description. Long description</pre></code>
+
<source lang="lua">--- Short description. Long description</source>
 
<center><small> Special comment in one line</small></center>
 
<center><small> Special comment in one line</small></center>
  
 
== Type comment block ==
 
== Type comment block ==
  
A [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type|type]] comment block is a ''special comment'' with a type declaration with <code>@type</code> key word followed by desired type name.
+
A [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type|type]] comment block is a ''special comment'' with a type declaration with <code>@type</code> key word followed by desired type name.<br/>
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Type short description.
 
-- Type short description.
Line 109: Line 137:
 
--
 
--
 
-- @type typename
 
-- @type typename
</pre></code>
+
</source>
 
<center><small>Sample of type declaration</small></center>
 
<center><small>Sample of type declaration</small></center>
 +
 +
'''Since 1.2''': <code>@list</code> and <code>@map</code> tags. The '''list''' and '''map''' tags allow to describe if the type is used as a [[Koneki/LDT/Technical_Documentation/Documentation_Language#List|list]] or a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Map|map]] of elements.
 +
 +
{|  border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
 +
<source lang="lua">
 +
--------------------------------------------------------------------------------
 +
-- Type short description.
 +
-- Type long description
 +
--
 +
-- @type typename
 +
-- @list <#valuetypename>
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
--------------------------------------------------------------------------------
 +
-- Type short description.
 +
-- Type long description
 +
--
 +
-- @type typename
 +
-- @map <#keytype,#valuetype>
 +
</source>
 +
|-
 +
|<center><small>Sample of type declaration used as a list.</small></center>
 +
|<center><small>Sample of type declaration used as a map.</small></center>
 +
|}
  
 
== Module comment block ==
 
== Module comment block ==
  
Denoted by <code>@module</code> keyword and followed by a module name, a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Module|module]] is the type described in a file. This is why there should be only one module declaration per file. Here is the simplest way to declare a module.  
+
Denoted by <code>@module</code> keyword and followed by a module name, a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Module|module]] is the type described in a file. This is why there should be only one module declaration per file. Here is the simplest way to declare a module.<br/>
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Module short description.
 
-- Module short description.
Line 122: Line 177:
 
--
 
--
 
-- @module modulename
 
-- @module modulename
</pre></code>
+
</source>
 
<center><small>Sample of module declaration.</small></center>
 
<center><small>Sample of module declaration.</small></center>
  
 
When a module is declared, a type with its name is automatically created and returned. So, the following is equivalent to first module sample.
 
When a module is declared, a type with its name is automatically created and returned. So, the following is equivalent to first module sample.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Module short description.
 
-- Module short description.
Line 136: Line 191:
  
 
---@type modulename
 
---@type modulename
</pre></code>
+
</source>
 
<center><small>Sample of verbose module declaration</small></center>
 
<center><small>Sample of verbose module declaration</small></center>
 
'''Note''': We used <code>#modulename</code> to refer to a declared type, if you want to know more about it refer to [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference section]].
 
'''Note''': We used <code>#modulename</code> to refer to a declared type, if you want to know more about it refer to [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference section]].
Line 142: Line 197:
 
In previous sample, you may wonder in which case it could be of any use to return manually a type for a module. It is useful when you want to return a type different from the one automatically created.
 
In previous sample, you may wonder in which case it could be of any use to return manually a type for a module. It is useful when you want to return a type different from the one automatically created.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Module short description.
 
-- Module short description.
Line 149: Line 204:
 
-- @module modulename
 
-- @module modulename
 
-- @return #string
 
-- @return #string
</pre></code>
+
</source>
 
<center><small>Sample of module returning custom type</small></center>
 
<center><small>Sample of module returning custom type</small></center>
 
'''Note''': We used <code>#string</code> to refer to a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Primitive_references|primitive type]].
 
'''Note''': We used <code>#string</code> to refer to a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Primitive_references|primitive type]].
 +
 +
'''Since 1.2''': <code>@list</code> and <code>@map</code> tags. The '''list''' and '''map''' tags allow to describe if the module is used as a [[Koneki/LDT/Technical_Documentation/Documentation_Language#List|list]] or a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Map|map]] of elements.
 +
 +
{|  border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
<source lang="lua">
 +
--------------------------------------------------------------------------------
 +
-- Module short description.
 +
-- Module long description
 +
--
 +
-- @module modulename
 +
-- @list <#valuetype>
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
--------------------------------------------------------------------------------
 +
-- Module short description.
 +
-- Module long description
 +
--
 +
-- @module modulename
 +
-- @map <#keytype,#valuetype>
 +
</source>
 +
|-
 +
|<center><small>Sample of module declaration used as a list.</small></center>
 +
|<center><small>Sample of module declaration used as a map.</small></center>
 +
|}
  
 
== Field comment block ==
 
== Field comment block ==
Line 159: Line 240:
 
There are two ways of defining a field, in its parent block type or in a separate documentation block where you have to mention field parent type.
 
There are two ways of defining a field, in its parent block type or in a separate documentation block where you have to mention field parent type.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Type short description.
 
-- Type short description.
Line 166: Line 247:
 
-- @type typename
 
-- @type typename
 
-- @field #string fieldname Field description
 
-- @field #string fieldname Field description
</pre></code>
+
</source>
 
<center><small>Sample of field declaration in parent type block</small></center>
 
<center><small>Sample of field declaration in parent type block</small></center>
  
<code><pre>
+
<source lang="lua">
 
---
 
---
 
-- Type short description.
 
-- Type short description.
Line 180: Line 261:
 
--
 
--
 
-- @field [parent=#typename] #string fieldname  
 
-- @field [parent=#typename] #string fieldname  
</pre></code>
+
</source>
 
<center><small>Sample of field declaration in dedicated block</small></center>
 
<center><small>Sample of field declaration in dedicated block</small></center>
  
Line 186: Line 267:
  
 
The function comment block has to be attached to a type. Its keyword is <code>@function</code>. A [[Koneki/LDT/Technical_Documentation/Documentation_Language#Function|function]] can have several parameters denoted by keyword <code>@param</code>, they can be optionally typed with a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]] and have an optional descriptions. Several <code>@return</code> cases are also possible, but ''LDT inference -which is used for code assistance- only handles the first one''. As Lua functions allow to return several values at once, it is possible to ''define several returned values'' per <code>@return</code> markup. Returned values can be optionally typed using a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]].<br/>
 
The function comment block has to be attached to a type. Its keyword is <code>@function</code>. A [[Koneki/LDT/Technical_Documentation/Documentation_Language#Function|function]] can have several parameters denoted by keyword <code>@param</code>, they can be optionally typed with a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]] and have an optional descriptions. Several <code>@return</code> cases are also possible, but ''LDT inference -which is used for code assistance- only handles the first one''. As Lua functions allow to return several values at once, it is possible to ''define several returned values'' per <code>@return</code> markup. Returned values can be optionally typed using a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]].<br/>
'''Note''': If the first <code>@param</code> is called ''self'', LDT will show completion proposal without this parameter but using <code>:</code> invocation operator.
+
'''Note''': If the first <code>@param</code> is called ''self'', LDT will show completion proposal without this parameter but using <code>:</code> invocation operator.<br/>
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Function short description.
 
-- Function short description.
Line 199: Line 280:
 
-- @return #nil, #string Traditional nil and error message
 
-- @return #nil, #string Traditional nil and error message
 
-- @return Untyped return description
 
-- @return Untyped return description
</pre></code>
+
</source>
 
<center><small>Sample of function declaration</small></center>
 
<center><small>Sample of function declaration</small></center>
  
 
'''Note''': It is also possible to document function for types which name contains <code>"."</code>.
 
'''Note''': It is also possible to document function for types which name contains <code>"."</code>.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Function short description.
 
-- Function short description.
Line 210: Line 291:
 
--
 
--
 
-- @function [parent=#typeprefix.typename] functionname
 
-- @function [parent=#typeprefix.typename] functionname
</pre></code>
+
</source>
 
<center><small>Sample of function declaration related to a type which name contains <code>"."</code></small></center>
 
<center><small>Sample of function declaration related to a type which name contains <code>"."</code></small></center>
 +
 +
'''Since 1.2''': <code>@callof</code> tag. In Lua a table can be call using the metatable keyword '''__call'''. To set a type callable just add <code>@callof #typename</code> in the function behind the '''__call''' metamethod. The first param should be ''self'' and of the type given to ''callof''.
 +
 +
<source lang="lua">
 +
--------------------------------------------------------------------------------
 +
-- Function short description.
 +
-- Function long description
 +
--
 +
-- @callof #typename
 +
-- @param #typename self
 +
-- @param #paramtype paramname
 +
</source>
 +
<center><small>Sample of function declaration related </small></center>
  
 
== Global comment block ==
 
== Global comment block ==
Line 217: Line 311:
 
To declare a function or a field with a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]] as ''[[Koneki/LDT/Technical_Documentation/Documentation_Language#Global|global]]'', you just have to attach it to the <code>global</code> type.
 
To declare a function or a field with a [[Koneki/LDT/Technical_Documentation/Documentation_Language#Type_references|type reference]] as ''[[Koneki/LDT/Technical_Documentation/Documentation_Language#Global|global]]'', you just have to attach it to the <code>global</code> type.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Field long description
 
-- Field long description
Line 228: Line 322:
 
-- @function [parent=#global] functionname
 
-- @function [parent=#global] functionname
 
-- @param self
 
-- @param self
</pre></code>
+
</source>
 
<center><small>Sample of global field and function</small></center>
 
<center><small>Sample of global field and function</small></center>
  
= Sample mixing code and comments =
+
= Samples =
<code><pre>
+
== Simple module sample ==
 +
{|  border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
<source lang="lua">
 
---
 
---
 
-- Module short description.
 
-- Module short description.
Line 251: Line 348:
 
--
 
--
 
-- @function [parent=#modulename] functionname
 
-- @function [parent=#modulename] functionname
-- @param self Parameter description
+
-- @param #number n Parameter description
 
-- @return #number Typed return description
 
-- @return #number Typed return description
 
-- @return #nil, #string Traditional nil and error message
 
-- @return #nil, #string Traditional nil and error message
function M.functionname(self)
+
function M.functionname(n)
    return 0
+
  if n then return n else return nil, "error" end
 
end
 
end
 
return M
 
return M
</pre></code>
+
</source>
<center><small>Sample of documented module</small></center>
+
|valign="top"|
 +
<source lang="lua">
 +
---
 +
-- Module short description.
 +
-- Module _long description_
 +
local M = {}
 +
 
 +
---
 +
-- Field description
 +
M.fieldname = 'field value'
 +
 
 +
---
 +
-- Function short description.
 +
-- Function long description
 +
-- @param #number n Parameter description
 +
-- @return #number Typed return description
 +
-- @return #nil, #string Traditional nil and error message
 +
function M.functionname(n)
 +
  if n then return n else return nil, "error" end
 +
end
 +
return M
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
---
 +
-- Module short description.
 +
-- Module _long description_
 +
--
 +
-- @module modulename
 +
 
 +
---
 +
-- Field description
 +
--
 +
-- @field [parent=#modulename] #string fieldname
 +
 
 +
---
 +
-- Function short description.
 +
-- Function long description
 +
--
 +
-- @function [parent=#modulename] functionname
 +
-- @param #number n Parameter description
 +
-- @return #number Typed return description
 +
-- @return #nil, #string Traditional nil and error message
 +
return nil
 +
</source>
 +
|-
 +
|<center><small>Full Documentation  sample</small></center>
 +
|<center><small>Minimal Documentation</small></center>
 +
|<center><small>Documentation Only</small></center>
 +
|}
 +
 
 +
== Object-oriented sample ==
 +
{| border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
<source lang="lua">
 +
--- A module that allow to manage geometry shapes
 +
-- @module geometry
 +
local M = {}
 +
 
 +
--- A rectangle
 +
-- @type rectangle
 +
-- @field #number x
 +
-- @field #number y
 +
-- @field #number width
 +
-- @field #number height
 +
local R = {x=0, y=0, width=100, height=100, }
 +
 
 +
--- Move the rectangle
 +
-- @function [parent=#rectangle] move
 +
-- @param self
 +
-- @param x
 +
-- @param y
 +
function R.move(self,x,y)
 +
  self.x = self.x + x
 +
  self.y = self.y + y
 +
end
 +
 
 +
--- Create a new rectangle
 +
-- @function [parent=#geometry] newRectangle
 +
-- @param x
 +
-- @param y
 +
-- @param width
 +
-- @param height
 +
-- @return #rectangle the created rectangle
 +
function M.newRectangle(x,y,width,height)
 +
  local newrectangle = {x=x,y=y,width=width,height=height}
 +
 +
  -- Set to new rectangle the properties of a rectangle
 +
  setmetatable(newrectangle, {__index = R})
 +
  return newrectangle
 +
end
 +
 
 +
return M
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
--- A module that allow to manage geometry shapes
 +
local M = {}
 +
 
 +
--- A rectangle
 +
-- @type rectangle
 +
local R = {x=0, y=0, width=100, height=100, }
 +
 
 +
--- Move the rectangle
 +
function R.move(self,x,y)
 +
  self.x = self.x + x
 +
  self.y = self.y + y
 +
end
 +
 
 +
--- Create a new rectangle
 +
-- @return #rectangle the created rectangle
 +
function M.newRectangle(x,y,width,height)
 +
  local newrectangle = {x=x,y=y,width=width,height=height}
 +
 +
  -- set to new rectangle the properties of a rectangle
 +
  setmetatable(newrectangle, {__index = R})
 +
  return newrectangle
 +
end
 +
 
 +
return M
 +
</source>
 +
|-
 +
|<center><small>Full Documentation Sample</small></center>
 +
|<center><small>Minimal Documentation</small></center>
 +
|}
 +
 
 +
== Callof sample ==
 +
 
 +
The ''@callof'' tag is only available since the '''v1.2'''
 +
 
 +
{|  border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
<source lang="lua">
 +
--- A module that allow to manage circle shapes
 +
-- @module circleModule
 +
local M = {}
 +
 +
--- A circle
 +
-- @type circle
 +
local C = {radius=0, x=0, y=100}
 +
 +
--- Move the circle
 +
-- @function [parent=#circle] move
 +
function C.move(self,x,y)
 +
  self.x = self.x + x
 +
  self.y = self.y + y
 +
end
 +
 +
--- Create a new circle
 +
-- @callof #circleModule
 +
-- @return #circle the created cicle
 +
local function new(self,radius,x,y)
 +
  local newcircle = {radius=radius,x=x,y=y}
 +
 +
  -- Set to new circle the properties of a circle
 +
  setmetatable(newcircle, {__index = C})
 +
  return newcircle
 +
end
 +
 
 +
setmetatable(M,{__call=new})
 +
 +
return M
 +
 
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
local circlemodule = require "circleModule"
 +
 
 +
local firstcircle = circlemodule(10,0,0)
 +
local secondcircle = circlemodule(50,10,0)
 +
 
 +
firstcircle.radius = 20
 +
</source>
 +
 
 +
|-
 +
|<center><small>Circle module sample</small></center>
 +
|<center><small>Usage of the circle module</small></center>
 +
|}
 +
 
 +
== List sample ==
 +
 
 +
The ''@callof'' tag is only available since the '''v1.2'''
 +
 
 +
{|  border="1" cellspacing="0" valign="top"
 +
|valign="top"|
 +
<source lang="lua">
 +
---
 +
-- @module addressbook
 +
-- @list <#contact>
 +
local M = {}
 +
 
 +
--- @type contact
 +
-- @field #string name
 +
-- @field #number phone
 +
 
 +
function M:add(name,phonenumber)
 +
table.insert(self,{name = name, phone = phonenumber})
 +
end
 +
 
 +
---
 +
-- @return #contact
 +
-- @return nil, errormsg
 +
function M:find(name)
 +
for _, contact in pairs(self) do
 +
if (contact.name == name) then
 +
return contact
 +
end
 +
end
 +
return nil, string.format("No contact found under the name %s", name)
 +
end
 +
 
 +
return M
 +
 
 +
</source>
 +
|valign="top"|
 +
<source lang="lua">
 +
---
 +
-- @module library
 +
-- @map <#string,#book>
 +
local M = {}
 +
 
 +
--- @type book
 +
-- @field #string author
 +
-- @field #number pages Number of pages
 +
 
 +
function M:add(title,author, pages)
 +
self[title] = {author=author, pages=pages}
 +
end
 +
 
 +
function M:findauthor(title)
 +
return self[title].author
 +
end
 +
 
 +
return M
 +
</source>
 +
|-
 +
|<center><small>Sample using a list</small></center>
 +
|<center><small>Sample using a map</small></center>
 +
|}
  
== Short references ==
+
= Short references =
  
 
It is way to reference a types and their fields ''in a textual description''. You just have to surround a type reference with <code>@{}</code>. You can reference a types and their fields, functions are handled as a specific type of fields.
 
It is way to reference a types and their fields ''in a textual description''. You just have to surround a type reference with <code>@{}</code>. You can reference a types and their fields, functions are handled as a specific type of fields.
Line 274: Line 609:
 
'''Note''': So far, there are no short references for ''globals''.
 
'''Note''': So far, there are no short references for ''globals''.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Short description. Long description with a reference to @{io#io.flush}.
 
-- Short description. Long description with a reference to @{io#io.flush}.
 
--
 
--
 
-- @function [parent=#typename] functionname
 
-- @function [parent=#typename] functionname
</pre></code>
+
</source>
 
<center><small>Sample of type reference in a description</small></center>
 
<center><small>Sample of type reference in a description</small></center>
  
Line 295: Line 630:
  
 
= Limitations =
 
= Limitations =
 
 
  
 
== Markdown ==
 
== Markdown ==
  
 
Markdown allows reusable element. As each description is parsed separately, you cannot reuse an element from another description.
 
Markdown allows reusable element. As each description is parsed separately, you cannot reuse an element from another description.
 
== Inference ==
 
 
So far, all information about the documented module has to be explicitly described. There is no inference enabling us to extract documentation-valuable information from the code ... yet.
 
  
 
== Parsing ==
 
== Parsing ==
Line 316: Line 645:
 
When you have a comment block related to a concept, you can give one or several samples of how it should be used by using the <code>@usage</code> keyword.
 
When you have a comment block related to a concept, you can give one or several samples of how it should be used by using the <code>@usage</code> keyword.
  
<code><pre>
+
<source lang="lua">
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
 
-- Module short description.
 
-- Module short description.
Line 324: Line 653:
 
-- @usage local modulename = require 'modulename'
 
-- @usage local modulename = require 'modulename'
 
-- @usage require('modulename')
 
-- @usage require('modulename')
</pre></code>
+
</source>
 
<center><small>Sample of module declaration with usage markup.</small></center>
 
<center><small>Sample of module declaration with usage markup.</small></center>

Revision as of 12:24, 10 April 2014

This documentation language has been developed as part of LDT, its main goal is to describe the API supplied by a file. It is strongly inspired by LDoc. Information given with this language is parsed by LDT and supply advanced features such as code completion and documentation view. Before diving into syntax, it is time to enumerate the underlying concepts, knowing them will enable you to write documentation more efficiently.

Concepts

Our documentation language introduce some concepts which may not be described explicitly in Lua, but help to defined the contract between the library provider and the user.

Type

This is the most important one. A compound type is a set of values, accessible through fields. It is not a primitive type like string, number or nil. Theses fields can point to functions, primitive values or other compound types.

It is the way to explicitly ensure the structure of a Lua table.

Module

It is requireable entity - you can use the require function on it-
This concept allow you to express :

  • which type will be return by the require function for your module,
  • which new global variables will be available after your module was loaded.

Most of the time people refer to the returned type instance as module.

Field

It is always a field of a type. This is the way to ensure the presence of a value in a table which implement a type.

Function

It is a special kind of field which can have parameters and return values, just as Lua functions. Both parameters and returned values can be typed. This concept allow to express a contract about which parameters suit a function and expected outputs.

Global

When a module is required, it could modify the global environment. To handle those cases, the predefined type global is available. It is a reserved type which will be available everywhere, enabling you to attach fields and functions to it. This is a way to express the creation of new global variables when a module is loaded.

Idea.png
global variable auto-completion behavior

To be aware of where a global variable should be accessible is a real challenge for tooling. Actually, there are two possibilities :

  • the global variable is preloaded by the interpreter (VM) at launch : in this case, it's easy, the global variable is always accessible.
  • the global is created dynamically during runtime : in this case, it's very difficult to know where the global variable must be available in autocompletion.

The first case (preloaded global var) is managed by the use of a global.lua file in execution environment which must contains all preloaded global variables. (Those global vars will always be available in autocompletion)

The other case, LDT lets you choose between (customizable in Preference/Lua):

  • All (default): display all global variables available in project sourcepath/buildpath. (with the risk to reference globals where they are not effectively available at runtime)
  • None: do not propose any global of this kind.


List

A list is a sequence of values stored in a table under numeric indexes.

Map

A map is a set of key/value pairs stored in a table.

Type references

It is often needed to refer to a type. There is a notation for this. It is based on types. So if you want to type a field, a function parameter or returned value, they are several kinds of references available.

Primitive references

Refer to Lua primitive types, it is the type prefixed with #.

  • #boolean
  • #nil
  • #number
  • #string
  • #table

Internal references

Enables to refer to types defined in current module, it is type name prefixed with #.

  • #typename will refer to type typename defined in current module.

External references

Enables to refer to a type defined in another module, it is targeted module name followed by internal reference which could be used in targeted module.

  • modulename#typename will refer to type typename defined in module modulename.
  • modulename#modulename will refer to type modulename returned in module modulename.

Structure types

Enables to describe list and map structures.

  • #list<#valuetype> where #valuetype is the reference to the type contained in the list.
  • #map<#keyvalue,#valuetype> where #valuetype is the reference to the type contained in the map and #keyvalue the reference of the type used to index map values.

Comments

You could describe explicitly all file API just with our documentation language. But LDT is able to guess quite a lot of this information from code. (see samples)

Special comments

First of all, only special comments are parsed as part of Lua documentation. To mark your comment as special, just start it with ---.

Special comments can contain a short and an long description. The short description start at the beginning of the comment and continue till . or ?. The long description is the text coming after. By the way, Markdown is supported in all descriptions.

Special comments are handled only if they describe a concept.

Note: All leading -'s in special comments are trimmed.

--------------------------------------------------------------------------------
-- Short description.
-- Long _markdown_ description
Special comment with trimmed first line

Note: _markdown_ is supposed to be interpreted by Markdown.

---
-- Short description.
-- Long description
Special comment composed of short comments format
--[[-
 Short description.
 Long description
]]
Special comment with long comment format
--- Short description. Long description
Special comment in one line

Type comment block

A type comment block is a special comment with a type declaration with @type key word followed by desired type name.

--------------------------------------------------------------------------------
-- Type short description.
-- Type long description
--
-- @type typename
Sample of type declaration

Since 1.2: @list and @map tags. The list and map tags allow to describe if the type is used as a list or a map of elements.

--------------------------------------------------------------------------------
-- Type short description.
-- Type long description
--
-- @type typename
-- @list <#valuetypename>
--------------------------------------------------------------------------------
-- Type short description.
-- Type long description
--
-- @type typename
-- @map <#keytype,#valuetype>
Sample of type declaration used as a list.
Sample of type declaration used as a map.

Module comment block

Denoted by @module keyword and followed by a module name, a module is the type described in a file. This is why there should be only one module declaration per file. Here is the simplest way to declare a module.

--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
Sample of module declaration.

When a module is declared, a type with its name is automatically created and returned. So, the following is equivalent to first module sample.

--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
-- @return #modulename
 
---@type modulename
Sample of verbose module declaration

Note: We used #modulename to refer to a declared type, if you want to know more about it refer to type reference section.

In previous sample, you may wonder in which case it could be of any use to return manually a type for a module. It is useful when you want to return a type different from the one automatically created.

--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
-- @return #string
Sample of module returning custom type

Note: We used #string to refer to a primitive type.

Since 1.2: @list and @map tags. The list and map tags allow to describe if the module is used as a list or a map of elements.

--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
-- @list <#valuetype>
--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
-- @map <#keytype,#valuetype>
Sample of module declaration used as a list.
Sample of module declaration used as a map.

Field comment block

The field block represents a field of a type. It is possible to declare one with @field keyword followed by optional type reference, field name and optional description.

There are two ways of defining a field, in its parent block type or in a separate documentation block where you have to mention field parent type.

--------------------------------------------------------------------------------
-- Type short description.
-- Type long description
--
-- @type typename
-- @field #string fieldname Field description
Sample of field declaration in parent type block
---
-- Type short description.
-- Type long description
--
-- @type typename
 
---
-- Field description
--
-- @field [parent=#typename] #string fieldname
Sample of field declaration in dedicated block

Function comment block

The function comment block has to be attached to a type. Its keyword is @function. A function can have several parameters denoted by keyword @param, they can be optionally typed with a type reference and have an optional descriptions. Several @return cases are also possible, but LDT inference -which is used for code assistance- only handles the first one. As Lua functions allow to return several values at once, it is possible to define several returned values per @return markup. Returned values can be optionally typed using a type reference.
Note: If the first @param is called self, LDT will show completion proposal without this parameter but using : invocation operator.

--------------------------------------------------------------------------------
-- Function short description.
-- Function long description
--
-- @function [parent=#typename] functionname
-- @param self Parameter description
-- @param #string parametername Parameter description
-- @return #number Typed return description
-- @return #nil, #string Traditional nil and error message
-- @return Untyped return description
Sample of function declaration

Note: It is also possible to document function for types which name contains ".".

--------------------------------------------------------------------------------
-- Function short description.
-- Function long description
--
-- @function [parent=#typeprefix.typename] functionname
Sample of function declaration related to a type which name contains "."

Since 1.2: @callof tag. In Lua a table can be call using the metatable keyword __call. To set a type callable just add @callof #typename in the function behind the __call metamethod. The first param should be self and of the type given to callof.

--------------------------------------------------------------------------------
-- Function short description.
-- Function long description
--
-- @callof #typename
-- @param #typename self
-- @param #paramtype paramname
Sample of function declaration related

Global comment block

To declare a function or a field with a type reference as global, you just have to attach it to the global type.

--------------------------------------------------------------------------------
-- Field long description
--
-- @field [parent=#global] #string fieldname
 
--------------------------------------------------------------------------------
-- Function short description. Function long description
--
-- @function [parent=#global] functionname
-- @param self
Sample of global field and function

Samples

Simple module sample

---
-- Module short description.
-- Module _long description_
--
-- @module modulename
local M = {}
 
---
-- Field description
--
-- @field [parent=#modulename] #string fieldname 
M.fieldname = 'field value'
 
---
-- Function short description.
-- Function long description
--
-- @function [parent=#modulename] functionname
-- @param #number n Parameter description
-- @return #number Typed return description
-- @return #nil, #string Traditional nil and error message
function M.functionname(n)
  if n then return n else return nil, "error" end
end
return M
---
-- Module short description.
-- Module _long description_
local M = {}
 
---
-- Field description
M.fieldname = 'field value'
 
---
-- Function short description.
-- Function long description
-- @param #number n Parameter description
-- @return #number Typed return description
-- @return #nil, #string Traditional nil and error message
function M.functionname(n)
  if n then return n else return nil, "error" end
end
return M
---
-- Module short description.
-- Module _long description_
--
-- @module modulename
 
---
-- Field description
--
-- @field [parent=#modulename] #string fieldname 
 
---
-- Function short description.
-- Function long description
--
-- @function [parent=#modulename] functionname
-- @param #number n Parameter description
-- @return #number Typed return description
-- @return #nil, #string Traditional nil and error message
return nil
Full Documentation sample
Minimal Documentation
Documentation Only

Object-oriented sample

--- A module that allow to manage geometry shapes
-- @module geometry
local M = {}
 
--- A rectangle 
-- @type rectangle
-- @field #number x 
-- @field #number y 
-- @field #number width
-- @field #number height
local R = {x=0, y=0, width=100, height=100, }
 
--- Move the rectangle
-- @function [parent=#rectangle] move
-- @param self
-- @param x
-- @param y
function R.move(self,x,y)
  self.x = self.x + x
  self.y = self.y + y
end
 
--- Create a new rectangle
-- @function [parent=#geometry] newRectangle
-- @param x
-- @param y
-- @param width
-- @param height
-- @return #rectangle the created rectangle
function M.newRectangle(x,y,width,height)
  local newrectangle = {x=x,y=y,width=width,height=height}
 
  -- Set to new rectangle the properties of a rectangle
  setmetatable(newrectangle, {__index = R})
  return newrectangle
end
 
return M
--- A module that allow to manage geometry shapes
local M = {}
 
--- A rectangle 
-- @type rectangle
local R = {x=0, y=0, width=100, height=100, }
 
--- Move the rectangle
function R.move(self,x,y)
  self.x = self.x + x
  self.y = self.y + y
end
 
--- Create a new rectangle
-- @return #rectangle the created rectangle
function M.newRectangle(x,y,width,height)
  local newrectangle = {x=x,y=y,width=width,height=height}
 
  -- set to new rectangle the properties of a rectangle
  setmetatable(newrectangle, {__index = R})
  return newrectangle
end
 
return M
Full Documentation Sample
Minimal Documentation

Callof sample

The @callof tag is only available since the v1.2

--- A module that allow to manage circle shapes
-- @module circleModule
local M = {}
 
--- A circle
-- @type circle
local C = {radius=0, x=0, y=100}
 
--- Move the circle
-- @function [parent=#circle] move
function C.move(self,x,y)
  self.x = self.x + x
  self.y = self.y + y
end
 
--- Create a new circle
-- @callof #circleModule
-- @return #circle the created cicle
local function new(self,radius,x,y)
  local newcircle = {radius=radius,x=x,y=y}
 
  -- Set to new circle the properties of a circle
  setmetatable(newcircle, {__index = C})
  return newcircle
end
 
setmetatable(M,{__call=new})
 
return M
local circlemodule = require "circleModule"
 
local firstcircle = circlemodule(10,0,0)
local secondcircle = circlemodule(50,10,0)
 
firstcircle.radius = 20
Circle module sample
Usage of the circle module

List sample

The @callof tag is only available since the v1.2

---
-- @module addressbook
-- @list <#contact> 
local M = {}
 
--- @type contact
-- @field #string name
-- @field #number phone
 
function M:add(name,phonenumber)
	table.insert(self,{name = name, phone = phonenumber})
end
 
---
-- @return #contact
-- @return nil, errormsg
function M:find(name)
	for _, contact in pairs(self) do
		if (contact.name == name) then
			return contact
		end
	end
	return nil, string.format("No contact found under the name %s", name)
end
 
return M
---
-- @module library
-- @map <#string,#book> 
local M = {}
 
--- @type book
-- @field #string author
-- @field #number pages Number of pages
 
function M:add(title,author, pages)
	self[title] = {author=author, pages=pages}
end
 
function M:findauthor(title)
	return self[title].author
end
 
return M
Sample using a list
Sample using a map

Short references

It is way to reference a types and their fields in a textual description. You just have to surround a type reference with @{}. You can reference a types and their fields, functions are handled as a specific type of fields.

  • Reference to types
    • @{#typename} will refer to type typename defined in current module.
    • @{modulename} will refer to module named modulename.
    • @{modulename#typename} will refer to type typename defined in module modulename.
  • Reference to fields
    • @{#typename.fieldname} will refer to fieldname which could be a function or field attached to type typename defined in current module.
    • @{modulename#typename.fieldname} will refer to fieldname which could be a function or field attached to type typename defined in modulename module.

Note: So far, there are no short references for globals.

--------------------------------------------------------------------------------
-- Short description. Long description with a reference to @{io#io.flush}.
--
-- @function [parent=#typename] functionname
Sample of type reference in a description

Ambiguity

It is possible to use dots in type names, but then it becomes hard to differentiate type name from field name. Let's explained the default behavior:

  • Everything before # is module name
  • Everything between # and last dot is type name.
  • Everything after last dot is field name

So in @{module.name#type.name.fieldname} will refer to field fieldname of type named type.name from module module.name. Well, but what happens when we simply want to reference a type name containing dots? It is possible to surround type name with parenthesis to remove ambiguity.

  • @{modulenamed#(type.name).fieldname} will refer to field named fieldname from type named type.name defined in module named modulename.
  • @{modulenamed#(type.name)} will refer to type named type.name from module named modulename
  • @{#(type.name)} will refer to type named type.name.

Limitations

Markdown

Markdown allows reusable element. As each description is parsed separately, you cannot reuse an element from another description.

Parsing

We use Metalua to parse comments, and it can't parse only comments. So if you write a documentation only file, ensure it contains a least a valid statement. Most of the time we use, return nil at end of file.

Tips

Usage

When you have a comment block related to a concept, you can give one or several samples of how it should be used by using the @usage keyword.

--------------------------------------------------------------------------------
-- Module short description.
-- Module long description
--
-- @module modulename
-- @usage local modulename = require 'modulename'
-- @usage require('modulename')
Sample of module declaration with usage markup.

Back to the top