EmmyLua’s formatting system provides automatic code formatting with extensive customization options. It supports both document-level and selection-level formatting, with intelligent handling of Lua syntax and EmmyDoc annotations.
Format entire Lua files to maintain consistent code style.
Format Document
Format on Save
Format on Type
Shift + Alt + F (Windows/Linux) or Shift + Option + F (macOS)
-- Before formattinglocal function test(x,y,z)if x>0 thenreturn y+zendend-- After formattinglocal function test(x, y, z) if x > 0 then return y + z endend
Features:
Fixes common formatting issues
Applies consistent indentation
Adjusts spacing around operators
Aligns table fields
Enable automatic formatting when saving files:VS Code settings.json:
The formatter uses intelligent diff algorithms to minimize changes:
Diff-based Formatting
Only modified parts are reformatted, preserving unchanged regions:
-- Only the changed function is reformattedlocal function unchanged() return true -- This stays as-isendlocal function modified(x,y) -- This gets reformatted return x+yend
Configuration:
{ "format": { "useDiff": true }}
Replace All Threshold
Controls when to use full-file replacement vs. line-based diff:
Or use Ctrl + K, Ctrl + F (Windows/Linux) or Cmd + K, Cmd + F (macOS)
local function example() -- Select this messy code local x=1+2 local y=3+4 return x*y -- After formatting: -- local x = 1 + 2 -- local y = 3 + 4 -- return x * yend
Selection formatting maintains the same style rules as document formatting:
Automatically adjusts indentation levels based on context:
function outer() -- Paste poorly indented code herefunction inner()local x=1return xendend-- Format selection to fix indentationfunction outer() function inner() local x = 1 return x endend
{ "indent_size": 4, // Number of spaces per indent "indent_style": "space", // "space" or "tab" "continuation_indent": 4 // Indent for continued lines}
Examples:
-- indent_size: 4, indent_style: "space"function example() if condition then doSomething() endend-- indent_size: 2, indent_style: "space"function example() if condition then doSomething() endend
{ "max_line_length": 120}
Controls automatic line breaking:
-- Before (exceeds 120 characters)local result = someVeryLongFunctionName(param1, param2, param3, param4, param5)-- Afterlocal result = someVeryLongFunctionName( param1, param2, param3, param4, param5)
-- Function declarationsfunction globalFunction() -- Foldable contentend-- Local functionslocal function localFunction() -- Foldable contentend-- Anonymous functionslocal callback = function() -- Foldable contentend
-- If statementsif condition then -- Foldableelseif other then -- Foldableelse -- Foldableend-- For loopsfor i = 1, 10 do -- Foldableend-- While loopswhile condition do -- Foldableend-- Repeat loopsrepeat -- Foldableuntil condition
--[[ Foldable multi-line comment]]-- Consecutive single-line comments-- are grouped and-- become foldable-- as a block--- EmmyDoc comments---@class Example---@field value number