# format lua code

### Related tags

Miscellaneous format emmylua luaparse

# EmmyLuaCodeStyle

## 格式化行为介绍

### 基本语句

```local t=123 --local
aaa,bbbb = 1123, 2342```

```local t = 123 --local
aaa, bbbb = 1123, 2342```

### 连续赋值语句

```local ttt  = 123 --first
cd        = 345 --second```

```local ttt  = 123 --first
cd         = 345 --second```

### 表的排版

```local t = {1,2,3}
local c = {
aaa, bbb, eee
}
local d = {
aa  =123,
bbbb = 4353,
eee  = 131231,
}```

```local t = { 1, 2, 3 }
local c = {
aaa, bbb, eee
}
local d = {
aa   = 123,
bbbb = 4353,
eee  = 131231,
}```

### 长表达式列表

```local aaa = 13131, bbbb,
ccc, 123131, ddddd,
eeee, fff

return aaa, bbb, dddd
or cccc

if aaa and bbb
or ccc() then

end
```

### 包含语句块的语句

```do return end
function f(x,y) return x<y  end```

```do return end
function f(x, y) return x < y end```

```do
if a then
return
end

local c = 123
end

print("hello world")```

### 函数定义参数与函数调用参数

```function ffff(a, b, c,
callback)

end```

```print(aaa, bbbb, eeee, ffff,
ggggg, hhhhh,
lllll)```

`require "code_format"`

### 注释语句与内联注释

```--[[
这是个注释
]]

--[email protected] f number
--[email protected] number
function ffff(f)
end```

```local t = 123 --fffff
local c = {
aaa = 123, --[[ffff]] dddd,
ccc = 456
}
```

## 格式化选项

### use_tab

```--格式化前
function ff()
local t = 123
local c = 456
end

--格式化后
function ff()
local t = 123
local c = 456
end```

### indent

```--格式化前
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```

### continuation_indent

```-- 格式化前
local fff = function(x,y)
end

local ccc = aa + bbb + ccc /
ddd + eee

if aaa == 123
or bbb == 456 then
end

-- 值为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
```

### align_call_args

```-- 格式化前
helloWorld(aaa,bbb,ccc,
eee,ddd)

-- 格式化后
-- align_call_args 为true
helloWorld(aaa, bbb, ccc,
eee, ddd)

-- align_call_args 为false
helloWorld(aaa, bbb, ccc,
eee, ddd)
```

### keep_one_space_between_call_args_and_bracket

```-- 格式化前
helloWorld(aaa,bbb,ccc,
eee,ddd)

print(123,456)

-- 该选项为true时
helloWorld( aaa, bbb, ccc,
eee, ddd )

print( 123, 456 )
```

### align_function_define_params

```-- 格式化前
function fffff(aaa,bbb,ccc)
end

function ddddd(eeee,ffff,
ggg,hhh)
end

-- 该选项为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```

### keep_one_space_between_table_and_bracket

```-- 格式化前
local t = {1,2,3}
local cc ={
4,5,6
}

-- 该选项为true时
local t = { 1, 2, 3 }
local cc = {
4, 5, 6
}
-- 该选项为false时
local t = {1, 2, 3}
local cc = {
4, 5, 6
}```

### align_table_field_to_first_field

```-- 格式化前
local t = {
aaa,bbbb,ccc,
ddd,eee,fff
}

local t2 = {aaa,bbbb,
ccc,eee
}

-- 该选项为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
}```

### continuous_assign_statement_align_to_equal_sign

```-- 格式化前
local t  = 123
local cccc = 456
cccc = 321
-- 这是个注释
eeeeeeeee = 654 -- 这也是个注释

--不连续了
local c =132

-- 选项为true时
local t    = 123
local cccc = 456
cccc       = 321
-- 这是个注释
eeeeeeeee  = 654 -- 这也是个注释

--不连续了
local c = 132

-- 选项为false时
local t = 123
local cccc = 456
cccc = 321
-- 这是个注释
eeeeeeeee = 654 -- 这也是个注释

--不连续了
local c = 132```

### continuous_assign_table_field_align_to_equal_sign

```-- 格式化前
local t = {
bbb =456,
ddd =789
}

-- 选项为true时
local t = {
bbb     = 456,
ddd     = 789
}
-- 选项为false时
local t = {
bbb = 456,
ddd = 789
}```

### 语句的行布局

``````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
``````

• minLine:\${n}

表示和下一个语句间距至少n行

• keepLine

表示和下一行保持原本的行间距

• keepLine:\${n}

表示和下一行保持n行间距

## 代码风格诊断

• #### [enhancement] can we support the following functions?

Hello, Can we add support for the following functions? I use them for C/C++ on vscode. It will be pretty handy for me if they are available in Lua also.

• SpaceBeforeFunctionOpenParenthesis
• SpaceBeforeOpenSquareBracket
• SpaceBeforeLambdaOpenParenthesis
• SpaceWithinExpression

SpaceBeforeFunctionOpenParenthesis

Before Formatting:

``````function ff()
end
``````

After Formatting:

``````function ff ()
end
``````

SpaceBeforeOpenSquareBracket

Before Formatting:

``````details["names"] = "ashok"
``````

After Formatting:

``````details ["names"] = "ashok"
``````

SpaceBeforeLambdaOpenParenthesis

Before Formatting:

``````local details ={}
``````

After Formatting:

``````local details = {}

``````

SpaceWithinExpression

Before Formatting:

``````i?"true":"false";
i>0

``````

After Formatting:

``````i ? "true" : "false";
i > 0
``````

Thank You, ashok.

enhancement good first issue feature request
opened by achidipothu 29
• #### 多行字符串不对齐，看起来很难受

``````local sql = fmt("select a, b, c " ..
"from table " ..
"where a > 10 and b < 10")
``````

期待的格式：

``````local sql = fmt("select a, b, c " ..
"from table " ..
"where a > 10 and b < 10")
``````
enhancement discuss feature request
opened by sanpoo 19
• #### Help removing empty continuous lines?

I'm using sumneko_lua through neovim and testing the new formatting capabilities out using `.editorconfig` to see if I can't replace stylua as my default formatter but still keep a similar style.

In my config file I have the line `max_continuous_line_distance = 1`; My assumption is that this would remove empty lines between statements and would format the examples below, but it doesn't appear to do anything...

How can I achieve this result? It successfully fixes indentation and providing diagnostics so the rest of the config is clearly working.

``````print('hello, world')

--- Modules ---

require('lsp.handlers')
require('lsp.null-ls')

require('lsp.installer').setup({

capabilities = capabilities,
language_servers = language_servers,

on_attach = on_attach,
})

local example1 = 'foo'
local example2 = 'bar'
``````
``````print('hello, world')

--- Modules ---

require('lsp.handlers')
require('lsp.null-ls')
require('lsp.installer').setup({
capabilities = capabilities,
language_servers = language_servers,
on_attach = on_attach,
})

local example1 = 'foo'
local example2 = 'bar'
``````
question
opened by daephx 13
• #### Use `insertSpaces` in `formattingOptions` instead of `indent_style` in `.editorconfig`

See https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#formattingOptions

Since LSP specifies that this field will be included in the formatted request, you should use the LSP field first. You should only use the settings in `.editorconfig` if formatting is not requested via LSP.

enhancement discuss diagnostic
opened by sumneko 13
• #### 格式化时等号对齐应该使用最小长度

``````local aaaaaa = 1
local b  = 1
local c        = 1
``````

现在会被格式化为

``````local aaaaaa   = 1
local b        = 1
local c        = 1
``````

但用户的期望一般是

``````local aaaaaa = 1
local b      = 1
local c      = 1
``````
enhancement discuss
opened by sumneko 13
• #### Add support for inner spaces within parentheses and square brackets.

First of all, thank you for this project!

Also fixed one failing test before adding my changes (commits are logically split). See the test files for the expected outcomes of new formatting options.

enhancement discuss
opened by obszczymucha 10
• #### If statements in multiple lines

Currently the formatter would do it like this:

``````if s_whatevercheck and
s_whatevercheck2 and
s_whatevercheck3 then
s_whatevervar = false
end
``````

instead I would prefer sth like this:

``````if s_whatevercheck and
s_whatevercheck2 and
s_whatevercheck3 then
s_whatevervar = false
end
``````

I just want to see where the if statement ends in a better way. Let me know what you think or if you have a better suggestion.

question
opened by FlashHit 8
• #### 格式化选定内容/粘贴时格式化的问题

``````TEST [[
[email protected] Class

[email protected] k Class
for <?k?> in pairs(t) do
end
]]
[[
parameter k: Class
]]
``````

选中倒数第二行的 `parameter`，选择 `格式化选定内容` 后，会导致倒数第三行格式改变，变为：

``````TEST [[
[email protected] Class

[email protected] k Class
for <?k?> in pairs(t) do
end
]]
[[
parameter k: Class
]]
``````
enhancement feature request
opened by sumneko 7
• #### Default editorconfig

Hi, I use neovim and the sumneko LSP server, and I started using the new code formatting feature. BTW, thanks!!! great job :) Even though it works great if I have a `.editorconfig` file in my project root, I wondered if it's possible to set a default configuration, so I don't need to set it in every project, but only when I want to change something. Thanks!

enhancement good first issue
opened by szaffarano 7
• #### [QUESTION] .editorconfig option to put table closing paranthesis to new line.

I have the following table:

``````Data = {
howToBreakFormation = BreakImmediately,
faceTargetHorizontal = 1,
faceTargetVertical = 0,
RandomActions =
{
{
Type = PickNewTarget,
Weighting = 1, },
{
Type = NoAction,
Weighting = 9, },
},
BeingAttackedActions = {},
FiringActions = {}, }
``````

What I want is to put closing bracket for each table to new line. Is there a way to force formatter to make it look like this:

``````Data = {
howToBreakFormation = BreakImmediately,
faceTargetHorizontal = 1,
faceTargetVertical = 0,
RandomActions =
{
{
Type = PickNewTarget,
Weighting = 1,
},
{
Type = NoAction,
Weighting = 9,
},
},
BeingAttackedActions = {},
FiringActions = {},
}
``````
enhancement feature request
opened by Real-Gecko 5
• #### 'auto' is not a valid value in the editorconfig spec for end_of_line

https://github.com/CppCXY/EmmyLuaCodeStyle/blob/ec2cd0e2cca3b1fa2e08c8bc8d146afd1bdfba19/lua.template.editorconfig#L19

Ran into this issue when testing out neovim nightly build which now ships with editorconfig support built-in: https://github.com/neovim/neovim/issues/21669

enhancement discuss
opened by craigmac 4
• #### 1.0.0 FEATURE LISTS

1.0.0 version will complete

• [ ] BUG #79
• [ ] FEATURES #78
• [ ] FEATURES #77
• [x] FEATURES #74
• [x] FEATURES #72
• [x] FEATURES #69
• [x] FEATURES #64
• [ ] Implement align_continuous_similar_array_table move to 1.1.0
• [ ] Implement align_continuous_similar_call_args move to 1.1.0

refactor

• [x] spell-check
• [ ] name-style-check
• [x] code-style-check
• [ ] type-format
• [ ] command-line-tool
• [ ] code test
enhancement discuss
opened by CppCXY 0
• #### Only apply if_condition_align_with_each_other when there are conditions to align

When `if_condition_align_with_each_other` is enabled but there are no conditions to align, you just end up with two spaces after the if statement, which looks a bit odd.

``````if  foo then
exp
elseif bar then
exp
end
``````

I think it should only apply if an `and` or `or` is detected beneath the `if`/`elseif`.

bug
opened by ddickstein 1
• #### Automatic splitting / joining lines respecting max length

It would be great if there were an option to automatically split and join lines to respect a max length where the line is split when the max length is exceeded and joined when it can all be fit on a single line without exceeding max length. How to split expressions is of course quite opinionated, so this would probably need to be controlled by multiple options. For example, one might want to support:

``````local x = {
foo,
bar,
baz,
}
``````

and

``````local x =
{ foo, bar, baz }
``````

but never

``````local x =
{ foo, bar,
baz }
``````

I'm not sure how many options are needed here to satisfy all the different kinds of layouts people might want to allow and disallow (e.g., whether `baz` in the first example there should always / never end in a comma, whether to use trailing or leading commas, etc.) and I haven't thought too much about other kinds of expressions.

feature request
opened by ddickstein 2
• #### [Feature request] Fluent configuration of whitespace insertion

Currently we have `table_append_expression_no_space` option that allows `+` operator not to be wrapped with spaces in statements like `t[#t+1]`. It would be nice to have more configurable space insertion For example, don't insert spaces around math operators `i^2+1` or exponentiation operator in particular `i^2 + 1`, don't insert spaces in expression in square brackets `t[i+1]`, don't insert spaces in numeric for ranges `for i = 1,7,2` and between multiple variables and/or values in general

``````a,b = 1,2
for k,v in next,t do print(k,v) end
``````

Can any of these please be done? I usually use your language server to format lua code that after is pasted into another application, and since that application has not much space for code I try to write as compact as possible.

enhancement good first issue feature request
opened by ArmoredPony 1
• #### [Feat] keep_line_after_comment_statement

As an extension from #34, formatting rules don't apply between commented lines. With the previously mentioned settings:

``````keep_line_after_do_statement = maxLine:1
keep_line_after_expression_statement = maxLine:1
keep_line_after_for_statement = maxLine:1
keep_line_after_function_define_statement = maxLine:1
keep_line_after_if_statement = maxLine:1
keep_line_after_local_or_assign_statement = maxLine:1
keep_line_after_repeat_statement = maxLine:1
keep_line_after_while_statement = maxLine:1
``````

Given a format of:

``````-- Make Y act like other capitals
map('n', 'Y', 'y\$')

-- Black hole Register <Delete>
-- Prevent <Delete> from yanking - Breaks default functionality
-- map('v', '<del>', '"_d')

-- Remove highlight after search with ESCAPE
map('n', '<esc>', ':nohlsearch<cr><esc>')
``````

Should you uncomment the line `map('v', '<del>', '"_d')`, this will result in the desired output:

``````-- Make Y act like other capitals
map('n', 'Y', 'y\$')

-- Black hole Register <Delete>
-- Prevent <Delete> from yanking - Breaks default functionality
map('v', '<del>', '"_d')

-- Remove highlight after search with ESCAPE
map('n', '<esc>', ':nohlsearch<cr><esc>')
``````

However, with that line commented out, no formatting rules are applied. Provided a rule like `keep_line_after_comment_statement=maxLine:1`, these could be removed.

``````+ -- Make Y act like other capitals
+ map('n', 'Y', 'y\$')
+
+ -- Black hole Register <Delete>
+ -- Prevent <Delete> from yanking - Breaks default functionality
+ -- map('v', '<del>', '"_d')
+
-
-
+ -- Remove highlight after search with ESCAPE
+ map('n', '<esc>', ':nohlsearch<cr><esc>')
``````
opened by daephx 1
##### Releases(0.18.0)
###### (Simple String Format) is an syntax of format and a library for parse this.

SSFMT (Simple String Format) is an syntax of format and a library for parse this. SSFMT != {fmt} SSFMT is NOT an API/library for parse {fmt} syntax !

2 Jan 30, 2022
###### A dependency free, embeddable debugger for Lua in a single file (.lua or .c)

debugger.lua A simple, embedabble debugger for Lua 5.x, and LuaJIT 2.x. debugger.lua is a simple, single file, pure Lua debugger that is easy to integ

600 Dec 31, 2022
###### The Lua development repository, as seen by the Lua team. Mirrored irregularly

The Lua development repository, as seen by the Lua team. Mirrored irregularly

6.4k Jan 5, 2023
###### Standardise code formating for cmake projects with git and clang-format

git-cmake-format This project aims to provide a quick and easy way to integrate clang-format into your CMake project hosted in a git repository, it co

50 Dec 28, 2022
###### Code examples used in my Lua talk

LuaTalk Summary Introduction Slide 1 : Introduction slide Slide 2 : Github repository Part 1 : Presentation Slide 3 : Quick Lua Presentation Part 2 :

3 Jun 9, 2022
###### 💉 Source code for creating Lua executor

FiveM Lua Executor This is the ultimate great source code for building the best injectable Exec on FiveM. I'm not going to tell you how to create a pr

57 Jan 3, 2023
###### lua binding for Software implementation in C of the FIPS 198 Keyed-Hash Message Authentication Code HMAC

lua-hmac Compute the SHA-224, SHA-256, SHA-384, and SHA-512 message digests and the Hash-based Message Authentication Code (HMAC). this module is Lua

3 Dec 3, 2022
###### Converts file formats supported by AdPlug to VGM format.

adlib2vgm Converts file formats supported by AdPlug to VGM format. Notice This tool is originally designed to work with the RetroWave OPL3 Sound Card.

16 Nov 25, 2022
###### (R) Efficient methods and operators for the sparse matrix classes in 'Matrix' (esp. CSR format or "RsparseMatrix")

MatrixExtra MatrixExtra is an R package which extends the sparse matrix and sparse vector types in the Matrix package, particularly the CSR or Rsparse

15 Aug 29, 2022
###### A free, open-source compressor for the ZX0 format

salvador -- a fast, near-optimal compressor for the ZX0 format salvador is a command-line tool and a library that compresses bitstreams in the ZX0 for

35 Dec 26, 2022
###### Immersive IMM Format and Player

Introduction Immersive media (IMM) is an API-neutral runtime immersive media delivery format. IMM provides an efficient, extensible, interoperable for

42 Dec 19, 2022
###### A format converter for surface mesh intergrated with muli tools.

MeshConverter A format converter for surface mesh intergrated with small tools for fast surface mesh modification in CFD application scenarios. Build

4 Jul 22, 2022
###### Quite OK Image (QOI) format encoder/decoder

This project implements encoding and decoding the "Quite OK Image" (QOI) format in the Ć programming language. Ć can be automatically translated to pu

50 Dec 25, 2022
###### The Leap Motion cross-format, cross-platform declarative serialization library

Introduction to LeapSerial LeapSerial is a cross-format, declarative, serialization and deserialization library written and maintained by Leap Motion.

15 Jan 17, 2022
###### IconVG is a compact, binary format for simple vector graphics: icons, logos, glyphs and emoji.

IconVG IconVG is a compact, binary format for simple vector graphics: icons, logos, glyphs and emoji. WARNING: THIS FORMAT IS EXPERIMENTAL AND SUBJECT

631 Jan 5, 2023
###### meters.lv2 is a collection of audio-level meters with GUI in LV2 plugin format

meters.lv2 - Audio Level Meters meters.lv2 is a collection of audio-level meters with GUI in LV2 plugin format. It includes needle style meters (mono

144 Oct 26, 2022
###### A library to handle Apple Property List format in binary or XML

libplist A small portable C library to handle Apple Property List files in binary or XML format. Features The project provides an interface to read an

433 Dec 26, 2022
###### Scripts to help create QUIC version test vectors in RFC 9001 format.

quic-test-vector Scripts to help create QUIC version test vectors in RFC 9001 format. Just type 'make all' to build everything. There are two tools he

2 Jan 21, 2022
###### C++ implementation for a binary data storage format.

bsmlib- A C++ library for loading and writing binary data to and from files. bsmlib provides functions for loading, modifying, and saving BSM (Binary

4 Oct 9, 2022