User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions

Ganaram inukshuk (talk | contribs)
Ganaram inukshuk (talk | contribs)
No edit summary
Line 1: Line 1:
Parts of it are adapted to follow editing on the wiki.
The following style guide is a provisional guide adapted from the LuaRocks style guide: https://github.com/luarocks/lua-style-guide
== Lua style ==
 
Parts of it are adapted for use on the wiki, with details on specific conventions. If a specific convention is missing, fall back to that described in the Luarocks style guide.


The following style guide is a provisional guide adapted from the LuaRocks style guide: https://github.com/luarocks/lua-style-guide
== Preliminaries ==


=== Indentation and formatting ===
=== Indentation and formatting ===
Use the default as specified by the in-browser Lua editor.
The in-browser Lua editor uses tabs for indenting, which equates to 4 spaces.


=== Documentation ===
=== Declaring and naming variables ===
''to be determined''


=== Variable names ===
Variable names should be short and descriptive, written <code>snake_case</code>. Exceptions to this include counter variables, like with <code>i</code> in for loops. Boolean variables are prefixed with <code>is_</code>. Constants are named using <code>TRAIN_CASE</code>.
Same as LuaRocks style guide.


=== Tables ===
All variable declarations should start with <code>local</code>.
Same as LuaRocks style guide.


=== Strings ===
=== Strings ===
Same as LuaRocks style guide.


Do not escape double-quotes when a string can be enclosed in single-quotes instead. Only escape double-quotes when a string contains both single and double quotes.
Strings are enclosed in double quotes <code>" "</code>. If a string contains double quotes, do not escape them while enclosing the string in double quotes, as this reduces readability; instead, enclose the string in single quotes <code>' '</code>. Escape characters are only necessary if the string contains both single and double quotes.
 
'''Preferred'''
<syntaxhighlight lang="lua">local sentence = "That's his cheese quesadilla!"
local quotation = "And he said to me, \"One man's trash is another man's treasure.\""
</syntaxhighlight>
 
'''Avoid'''
<syntaxhighlight lang="lua">local formatting = "padding: 0.25em 0.5em; border: 1px solid white;\" colspan="2\" | "
</syntaxhighlight>


=== Line lengths ===
=== Line lengths ===
''to be determined''


=== Function declaration syntax ===
The 80-column limit is to be obeyed wherever possible for both comments and code, except for cases where strings, inline comments, or function calls are too long.
Same as LuaRocks style guide
 
<syntaxhighlight lang="lua">
local function navbox_title()
if not title then return "" end
local has_navbar = name ~= nil
 
return "|-\n" ..
'! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;' ..
'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | ' ..
'<span style="display: inline-block; float: left; text-align: left; font-weight: normal; font-style: normal; min-width: 4em; padding: 0px; margin: 0px;">' ..
(has_navbar and navbar(name, "mini", "") or "") .. '</span>' ..
'<span style="font-size: 1.05em;">' .. title .. '</span>' ..
(is_collapsible and '' or '<span style="display: inline-block; float: right; font-size: 0.8em; width: 5em;">&nbsp;</span>')
end
</syntaxhighlight>
 
=== Naming, declaring, and calling functions ===


=== Function calls ===
As with variables, functions are named using <code>snake_case</code>.
Same as LuaRocks style guide.


==== Use of wrapper functions ====
''tbd''
Allowed.


=== Table attributes ===
=== Table attributes ===