Skip to main content
Professional code formatting system based on EmmyLuaCodeStyle, providing consistent code style across your entire project.

Overview

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.

Document Formatting

Format entire files with a single command

Selection Formatting

Format only selected code regions

Code Folding

Collapse code blocks for better navigation

Custom Regions

Define custom folding regions with comments

Document Formatting

Format entire Lua files to maintain consistent code style.
Shift + Alt + F (Windows/Linux) or Shift + Option + F (macOS)
-- Before formatting
local function test(x,y,z)
if x>0 then
return y+z
end
end

-- After formatting
local function test(x, y, z)
    if x > 0 then
        return y + z
    end
end
Features:
  • Fixes common formatting issues
  • Applies consistent indentation
  • Adjusts spacing around operators
  • Aligns table fields

Incremental Formatting

The formatter uses intelligent diff algorithms to minimize changes:
Only modified parts are reformatted, preserving unchanged regions:
-- Only the changed function is reformatted
local function unchanged()
    return true  -- This stays as-is
end

local function modified(x,y)  -- This gets reformatted
    return x+y
end
Configuration:
{
  "format": {
    "useDiff": true
  }
}
Controls when to use full-file replacement vs. line-based diff:
{
  "format": {
    "useDiff": true,
    "replaceAllThreshold": 50
  }
}
  • Below threshold: Line-based diff
  • Above threshold: Full file replacement
  • Default: 50 lines

Selection Formatting

Format only selected code regions.
  1. Select the code you want to format
  2. Right-click → Format Selection
  3. 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 * y
end

Formatting Options

Customize formatting behavior through configuration.

Basic Options

{
  "format": {
    "enable": true,
    "defaultConfig": {
      "indent_size": 4,
      "indent_style": "space",
      "quote_style": "double",
      "continuation_indent": 4,
      "max_line_length": 120
    }
  }
}
{
  "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()
    end
end

-- indent_size: 2, indent_style: "space"
function example()
  if condition then
    doSomething()
  end
end

Code Folding

Collapse code blocks for better navigation and readability.

Standard Folding

Functions

function example()
    -- Foldable region
    local x = 1
    return x
end

Control Flow

if condition then
    -- Foldable region
elseif other then
    -- Foldable region
end

Loops

for i = 1, 10 do
    -- Foldable region
end

while condition do
    -- Foldable region
end

Tables

local config = {
    -- Foldable region
    timeout = 30,
    retries = 3
}

Supported Folding Regions

-- Function declarations
function globalFunction()
    -- Foldable content
end

-- Local functions
local function localFunction()
    -- Foldable content
end

-- Anonymous functions
local callback = function()
    -- Foldable content
end

Custom Folding Regions

Define custom folding regions using special comments.
--region Custom Region Name
local config = {
    timeout = 30,
    retries = 3,
    enabled = true
}

local defaults = {
    mode = "production",
    verbose = false
}
--endregion
Features:
  • Group related code
  • Custom region names
  • Nestable regions
  • Better code organization

External Formatter Integration

Integrate external formatting tools for advanced formatting needs.
{
  "format": {
    "externalTool": {
      "command": "stylua",
      "args": ["--stdin-filepath", "${file}", "-"]
    }
  }
}
StyLua integration:
{
  "format": {
    "externalTool": {
      "command": "stylua",
      "args": ["--stdin-filepath", "${file}", "-"]
    }
  }
}

EmmyLuaCodeStyle

The built-in formatter is powered by EmmyLuaCodeStyle, which provides:

.editorconfig Support

Respects .editorconfig files in your project

.lua-format Support

Compatible with .lua-format configuration files

Custom Rules

Extensive customization options

Fast Performance

High-performance Rust implementation

Configuration File

Create a .editorconfig or .lua-format file in your project root:
# .editorconfig
[*.lua]
indent_style = space
indent_size = 4
quote_style = double
continuation_indent = 4
max_line_length = 120
For detailed formatting configuration options, refer to the EmmyLuaCodeStyle Documentation.

Best Practices

1

Enable Format on Save

Configure your editor to format on save for consistent style:
{
  "[lua]": {
    "editor.formatOnSave": true
  }
}
2

Use Project Configuration

Create a .editorconfig file in your project root to ensure team-wide consistency.
3

Test Format Rules

Test formatting rules on a sample file before applying to entire project.
4

Commit Formatting Changes Separately

When reformatting existing code, commit formatting changes separately from functional changes.
Formatting will not be applied to files with syntax errors. Fix syntax errors before formatting.

Build docs developers (and LLMs) love