本文档说明格式化器对外公开的配置分组、默认值以及各选项的预期影响。
luafmt 和路径感知的库 API 都支持向上查找最近的配置文件:
.luafmt.tomlluafmt.toml
当输入来自 stdin 且没有源文件路径时,luafmt 会从当前工作目录开始向上查找最近的配置文件。
显式传入配置文件时,支持:
- TOML
- JSON
- YAML
level:Lua 语法等级,支持Lua51、Lua52、Lua53、Lua54、Lua55、LuaJIT
默认值:
[syntax]
level = "Lua55"说明:
- 这个选项决定格式化前使用哪一种 Lua 语法进行解析。
- 如果配置文件里没有写
syntax.level,默认按Lua55解析。 - CLI 的
--level会覆盖配置文件里的syntax.level。
kind:Space或Tabwidth:缩进宽度
默认值:
[indent]
kind = "Space"
width = 4max_line_width:目标最大行宽max_blank_lines:保留的连续空行上限table_expand:Never、Always、Autocall_args_expand:Never、Always、Autofunc_params_expand:Never、Always、Auto
默认值:
[layout]
max_line_width = 120
max_blank_lines = 1
table_expand = "Auto"
call_args_expand = "Auto"
func_params_expand = "Auto"行为说明:
Auto表示允许格式化器在单行和多行候选之间进行比较。- 对于序列结构,格式化器在适用场景下会比较 fill、packed、aligned 和 one-per-line 等候选布局。
- 二元表达式链和语句表达式列表在总行数不变时,会优先选择更均衡的 packed 布局,以避免最后一行过短。
insert_final_newlinetrailing_comma:Never、Multiline、Alwaystrailing_table_separator:Inherit、Never、Multiline、Alwaysquote_style:Preserve、Double、Singlesingle_arg_call_parens:Preserve、Always、Omitsimple_lambda_single_line:Preserve、Always、Neverend_of_line:LF或CRLF
默认值:
[output]
insert_final_newline = true
trailing_comma = "Never"
trailing_table_separator = "Inherit"
quote_style = "Preserve"
single_arg_call_parens = "Preserve"
simple_lambda_single_line = "Preserve"
end_of_line = "LF"行为说明:
trailing_comma是通用序列的尾逗号策略。trailing_table_separator只覆盖 table 的尾部分隔符策略;设为Inherit时继承trailing_comma。quote_style只会在安全时重写普通短字符串;长字符串和其它字符串形式会保留原样。- 引号重写基于原始 token 文本判断是否存在未转义的目标引号,并只做保持语义不变所需的最小分隔符转义调整。
single_arg_call_parens = "Omit"只会对 Lua 允许的单字符串参数调用和单 table 参数调用去掉括号。simple_lambda_single_line = "Preserve"只会在输入本来就是单行简单 lambda 时保留单行。simple_lambda_single_line = "Always"会在满足条件且不超出行宽时,将简单 lambda 收回成function(...) return expr end。simple_lambda_single_line = "Never"会关闭简单单行 lambda 快路径,始终把闭包体格式化为多行。
space_before_call_parenspace_before_func_parenspace_inside_bracesspace_inside_parensspace_inside_bracketsspace_around_math_operatorspace_around_concat_operatorspace_around_assign_operator
这些选项只控制 token 级别的空格,不直接决定更高层的布局是否换行。
align_line_commentsalign_in_statementsalign_in_table_fieldsalign_in_call_argsalign_in_paramsalign_across_standalone_commentsalign_same_kind_onlyspace_after_comment_dashline_comment_min_spaces_beforeline_comment_min_column
默认值:
[comments]
align_line_comments = true
align_in_statements = false
align_in_table_fields = true
align_in_call_args = true
align_in_params = true
align_across_standalone_comments = false
align_same_kind_only = false
space_after_comment_dash = true
line_comment_min_spaces_before = 1
line_comment_min_column = 0行为说明:
- statement 尾随注释对齐默认关闭。
- table、调用参数、函数参数中的尾随注释对齐是输入驱动的;只有源代码已经体现出额外空格的对齐意图时,才会启用。
- standalone comment 默认会打断对齐分组。
- table 字段尾随注释只在连续子组内部对齐,不会拖动整个表体。
space_after_comment_dash只会在普通--comment这类“前缀后完全没有空格”的情况下补一个空格;已有多个空格的注释会保留原样。
align_tag_columnsalign_declaration_tagsalign_reference_tagsalign_multiline_alias_descriptionsspace_between_tag_columnsspace_after_description_dash
默认值:
[emmy_doc]
align_tag_columns = true
align_declaration_tags = true
align_reference_tags = true
align_multiline_alias_descriptions = true
space_between_tag_columns = false
space_after_description_dash = true当前已结构化处理的标签包括 @param、@field、@return、@class、@alias、@type、@generic、@overload。
align_multiline_alias_descriptions默认开启,用于把多行@alias块里--- | value # description的# description列对齐。space_between_tag_columns控制 EmmyLua tag 行里---和@之间是否保留空格,例如--- @enum MyEnum和---@enum MyEnum的区别。当前默认值是false,所以不额外配置时会输出---@tag。space_after_description_dash只影响普通 doc 描述行--- text/---text,不影响 tag 行前缀。
continuous_assign_statementtable_field
默认值:
[align]
continuous_assign_statement = false
table_field = true行为说明:
- 连续赋值对齐默认关闭。
- 表字段对齐默认开启,但只有当输入在
=后已经表现出额外空格的对齐意图时才会激活。
[layout]
max_line_width = 100
table_expand = "Auto"
call_args_expand = "Auto"
func_params_expand = "Auto"
[comments]
align_in_statements = false
align_in_table_fields = true
align_in_call_args = true
align_in_params = true
[align]
continuous_assign_statement = false
table_field = true[indent]
kind = "Space"
width = 4
[layout]
max_line_width = 120
max_blank_lines = 1
table_expand = "Auto"
call_args_expand = "Auto"
func_params_expand = "Auto"
[output]
insert_final_newline = true
trailing_comma = "Never"
trailing_table_separator = "Inherit"
quote_style = "Preserve"
single_arg_call_parens = "Preserve"
simple_lambda_single_line = "Preserve"
end_of_line = "LF"
[spacing]
space_before_call_paren = false
space_before_func_paren = false
space_inside_braces = true
space_inside_parens = false
space_inside_brackets = false
space_around_math_operator = true
space_around_concat_operator = true
space_around_assign_operator = true
[comments]
align_line_comments = true
align_in_statements = false
align_in_table_fields = true
align_in_call_args = true
align_in_params = true
align_across_standalone_comments = false
align_same_kind_only = false
space_after_comment_dash = true
line_comment_min_spaces_before = 1
line_comment_min_column = 0
[emmy_doc]
align_tag_columns = true
align_declaration_tags = true
align_reference_tags = true
align_multiline_alias_descriptions = true
tag_spacing = 1
space_after_description_dash = true
[align]
continuous_assign_statement = false
table_field = true