Code formatting guides

Reformat a Lua table and verify the long string next to it

·

A Lua file can contain code whose indentation improves readability and text whose indentation is part of its value. The formatter example puts both forms next to each other so you can compare them.

Lua Formatter

Compare the Lua table and its long string

Choose Lua 5.3 and 2 spaces. Load the example and choose Format Lua. The stops table receives spaces around its field assignments. The two spaces before the return statement are indentation.

Compare the two lines of note with the output. The first line ends after North Gate and the second begins with two literal spaces. Those two spaces belong to the string value.

The formatted assignment with the original multiline value
local note = [=[North Gate
  Check the lamp]=]

Lua 5.3 reference manualStyLua settings and output verification

Check a Lua 5.3 operator under Lua 5.1

Replace the example with the one-line expression below. Lua 5.3 formats it as return 7 & 3. Choose Lua 5.1 and try again to see a parser error for the same operator.

Use the language version of the target application. Successful formatting shows the source fits the selected parser. Runtime libraries and application behavior are separate checks.

A bitwise operator defined in Lua 5.3
return 7&3

Lua 5.3 reference manual

Inspect formatted.lua in your project editor

Download the formatted file and compare it with your original. Confirm the quote delimiters, long-bracket string body and table fields in the diff. Use the project's Lua parser or Lua compiler syntax check before adding a changed file.

Lua 5.3 reference manualStyLua settings and output verification

Common questions

Can Lua strings use multiple equals signs in long brackets?

Yes. The opening and closing long brackets must contain the same number of equals signs. Choose a bracket level that does not appear as a closing delimiter within the string.

Why can the formatter change quote style?

StyLua can choose quote delimiters and escapes that preserve the value. Its verification mode compares the parsed output with the parsed input.

Sources

Put it into practice.

Related guides