Usually, in order to maintain a uniform code style, careful configuration is required in the project. On the other hand, the default formatting options may not meet everyone's aesthetics.
This project provides configuration and formatting based on editorconfig. For complete configuration, please refer to the lua.template.editorconfig that comes with this project.
This option specifies the invisible characters used when indenting, the optional value is space/tab
The formatting effect is as follows:
--use space
function ff()
local t = 123
local c = 456
end
--use tab
function ff()
local t = 123
local c = 456
end
This option indicates the number of columns for a single indentation, the default value is 4, only valid when indent_style is space
The effects of various common options are as follows:
-- default
function ff()
local t = 123
local c = 456
end
-- indent = 2
function ff()
local t = 123
local c = 456
end
-- indent = 3
function ff()
local t = 123
local c = 456
end
-- indent = 4
function ff()
local t = 123
local c = 456
end
This option is only valid when indent_style is tab The effect of this option is usually related to the editor and will not be shown here.
This option indicates whether all string literals expressed in quotation marks are unified into single quotation marks or double or remain the same. This option has three optional values none/single/double
When quote_style = single or double, if there is an unescaped target quote type in the string, it will not be formatted.
This option means that if the parameter in the call expression has only a single-string literal constant or a table expression, consider keeping or removing the parentheses. The optional value of this option is keep/remove/remove_table_only/remove_string_only The default value is keep.
f("wfwefw")({ e1231313 })("1e1231313131")
after formatting
--keep
f("wfwefw")({ e1231313 })("1e1231313131")
--remove
f "wfwefw" { e1231313 } "1e1231313131"
--remove_table_only
f("wfwefw") { e1231313 } ("1e1231313131")
--remove_string_only
f "wfwefw" ({ e1231313 }) "1e1231313131"
If the value is unambiguous_remove_string_only, the parentheses of the string in the call expression will be removed when there is no ambiguity, for example:
local t = require("aaaa").bbb
local d = require("cccc")
will be formatted as:
local t = require("aaaa").bbb
local d = require "cccc"
This option indicates the number of subsequent indented columns when a long expression or expression list is expressed in a new line. The default value of this option is 4, and 8 is common
Before formatting:
--
local fff = function(x,y)
end
local ccc = aa + bbb + ccc /
ddd + eee
if aaa == 123
or bbb == 456 then
end
After formatting:
-- = 4
local fff = function(x, y)
end
local ccc = aa + bbb + ccc /
ddd + eee
if aaa == 123
or bbb == 456 then
end
-- = 8
local fff = function(x, y)
end
local ccc = aa + bbb + ccc /
ddd + eee
if aaa == 123
or bbb == 456 then
end
This parameter represents a list of local or right expressions of an assignment statement. If there is more than one line, it will be aligned to the first expression, for example:
local t = aaa,bbb,ccc,
ddd,eee,fff
will be formatted as
local t = aaa,bbb,ccc,
ddd,eee,fff
When this option is in effect, the continuation_indent_size option has no effect on local and assignment statements. When a cross-line expression appears in the expression list, such as function and table defined across lines, this option is invalid.
This parameter indicates whether the calling parameter is aligned to the first parameter, the default value is false
Before formatting:
helloWorld(aaa,bbb,ccc,
eee,ddd)
after formatting:
-- align_call_args = true
helloWorld(aaa, bbb, ccc,
eee, ddd)
-- align_call_args = false
helloWorld(aaa, bbb, ccc,
eee, ddd)
The behavior defined by this option has no effect when a cross-line expression appears in the call parameter list
This option indicates whether the parameter list of the function definition statement is aligned to the position of the first parameter, the option is true by default, and the formatting effect is as follows:
Before formatting:
function fffff(aaa,bbb,ccc)
end
function ddddd(eeee,ffff,
ggg,hhh)
end
after formatting:
-- true
function fffff(aaa, bbb, ccc)
end
function ddddd(eeee, ffff,
ggg, hhh)
end
-- false
function fffff(aaa, bbb, ccc)
end
function ddddd(eeee, ffff,
ggg, hhh)
end
This option indicates whether there is a space between the table entry and the table brackets. This option is true by default.
Before formatting:
local t = {1,2,3}
local cc ={
4,5,6
}
after formatting:
-- true
local t = { 1, 2, 3 }
local cc = {
4, 5, 6
}
-- false
local t = {1, 2, 3}
local cc = {
4, 5, 6
}
This option indicates whether the table entry is aligned to the first entry. The default value of the option is true.
Before formatting:
local t = {
aaa,bbbb,ccc,
ddd,eee,fff
}
local t2 = {aaa,bbbb,
ccc,eee
}
after formatting:
-- = true
local t = {
aaa, bbbb, ccc,
ddd, eee, fff
}
local t2 = { aaa, bbbb,
ccc, eee
}
-- false
local t = {
aaa, bbbb, ccc,
ddd, eee, fff
}
local t2 = { aaa, bbbb,
ccc, eee
}
This option indicates that a space should be maintained between the name definition in the name list of the local statement and its attribute.
Before formatting:
local t<const> = 1
After formatting:
local t <const> = 1
This option indicates the definition of continuous lines, and its value determines the same continuous when the spacing between lines is less than or equal to how much.
This option means that any line of a continuous assignment statement that is aligned to the equal sign using the principle of least alignment if the equal sign is greater than one space apart from the left symbol, and the default value for this option is true.
Before formatting:
local t = 123
local cccc = 456
cccc = 321
-- this is a comment
eeeeeeeee = 654 -- this is a comment2
-- no continuous
local c =132
after formatting:
-- true
local t = 123
local cccc = 456
cccc = 321
-- this is a comment
eeeeeeeee = 654 -- this is a comment2
-- no continuous
local c = 132
-- false
local t = 123
local cccc = 456
cccc = 321
-- this is a comment
eeeeeeeee = 654 -- this is a comment2
-- no continuous
local c = 132
This option indicates whether the items in the table are allowed to be aligned to the equal sign. The alignment rules are similar to the above options. The default value of the option is true
Before formatting:
local t = {
aadddd = 123,
bbb =456,
ddd =789
}
after formatting:
-- true
local t = {
aadddd = 123,
bbb = 456,
ddd = 789
}
-- false
local t = {
aadddd = 123,
bbb = 456,
ddd = 789
}
This option means that the labels are not indented.
Before formatting:
function fff()
for i=1,2,3 do
if true then
goto continue
end
::continue::
end
end
After formatting:
function fff()
for i=1,2,3 do
if true then
goto continue
end
::continue::
end
end
This option means no indentation inside the do statement block.
Before formatting:
function fff()
do
for i=1,2,3 do
if true then
goto continue
end
::continue::
end
end
end
After formatting:
function fff()
do
for i=1,2,3 do
if true then
goto continue
end
::continue::
end
end
end
This option indicates that the conditional expression of the if statement does not need to be indented if it spans lines.
Before formatting:
if a
and b then
end
After formatting:
if a
and b then
end
This option represents an expression of the form t[#t+1], which will not be formatted as t[#t + 1]
This option means that the conditional expressions of the if statement will be aligned with each other. This option is valid if and only if there is an elseif statement, and when this option is valid, the conditional expressions of the if statement must be without indentation. Alignment with each other refers to typesetting like a switch case:
if aa.isDDDD()
and bb == fwfwfw
or hi == 123 then
print(1313)
elseif cc == 123
or dd == 13131 and ddd == 123 then
local ccc = 123
end
after formatting:
if aa.isDDDD()
and bb == fwfwfw
or hi == 123 then
print(1313)
elseif cc == 123
or dd == 13131 and ddd == 123 then
local ccc = 123
end
This option represents the symbol at the end of the line, the default is crlf, and it can also be lf
The algorithm also supports setting the line spacing between different sentences. The sentences that can be set include if, while, repeat, for, do, and local/assign sentences.
The names and default values of these options are:
keep_line_after_if_statement = minLine:1
keep_line_after_do_statement = minLine:1
keep_line_after_while_statement = minLine:1
keep_line_after_repeat_statement = minLine:1
keep_line_after_for_statement = minLine:1
keep_line_after_local_or_assign_statement = keepLine
keep_line_after_function_define_statement = keepLine:1
keep_line_after_expression_statement = keepLine
All options support three value expressions:
-
minLine:${n}
Indicates that there is at least n lines between the next sentence
-
keepLine
Means to maintain the original line spacing with the next line
-
keepLine:${n}
Means to maintain n line spacing with the next line
-
maxLine:${n}
Indicates and the next line at most n lines