Overview
EmmyLua Analyzer delegates formatting to external command-line tools rather than implementing its own formatter. This design allows you to use any formatter that:- Accepts input via stdin
- Outputs formatted code to stdout
- Supports command-line arguments
The most popular choice for Lua formatting is StyLua, which is used in the examples below.
Format Configuration
Document Formatting
Configuration for formatting entire documents.Properties:
program: Path to the external formatter executableargs: Array of command-line argumentstimeout: Timeout in milliseconds (default: 5000)
Range Formatting
Configuration for formatting selected ranges. Requires formatter support for range formatting.
Diff Mode
Use diff algorithm for formatting. When enabled, only the changed portions are sent to the editor, which can be more efficient for large files.
Variable Substitution
Theargs array supports variable substitution, allowing you to pass dynamic values from the editor to the formatter.
Simple Variables
Full path of the current file.Example:
/path/to/project/src/main.luaIndentation size (number of spaces).Example:
4Start byte offset of the selected range (for range formatting).Example:
0End byte offset of the selected range (for range formatting).Example:
1250Start line number of the selection.Example:
1End line number of the selection.Example:
50Conditional Variables
Conditional variables use the format${variable?true_value:false_value}.
Returns different values based on whether tabs are used for indentation.Example:
${use_tabs?Tabs:Spaces} → Tabs or SpacesReturns value if final newline should be inserted, empty string otherwise.Example:
${insert_final_newline?--final-newline} → --final-newline or emptyReturns value if non-standard symbols are allowed, empty string otherwise.Example:
${non_standard_symbol?--allow-non-standard} → --allow-non-standard or emptyIf a conditional variable results in an empty string, that argument is omitted from the command.
Formatter Examples
StyLua (Recommended)
StyLua is the most popular Lua formatter with excellent support for Lua 5.1-5.4 and LuaJIT.Basic StyLua Configuration
Basic StyLua Configuration
StyLua with Editor Settings
StyLua with Editor Settings
StyLua with Range Formatting
StyLua with Range Formatting
StyLua with Configuration File
StyLua with Configuration File
.stylua.toml in your project root:EmmyLuaCodeStyle
EmmyLuaCodeStyle is another formatter specifically designed for EmmyLua-annotated code.EmmyLuaCodeStyle Configuration
EmmyLuaCodeStyle Configuration
lua-format
Alternative formatter with different style options.lua-format Configuration
lua-format Configuration
Custom Formatter Script
You can wrap your formatter in a custom script for complex scenarios.Custom Script Example
Custom Script Example
custom-format.sh:Formatter Installation
Installing StyLua
- Cargo
- Homebrew (macOS)
- npm
- GitHub Releases
Installing EmmyLuaCodeStyle
- GitHub Releases
- Build from Source
Download from EmmyLuaCodeStyle Releases
Formatting Workflow
When you trigger code formatting:Language Server Reads Configuration
EmmyLua Analyzer reads the
format.externalTool or format.externalToolRangeFormat configuration.Variables are Substituted
Variables in the
args array are replaced with actual values from the editor.Code is Piped to stdin
The current file content (or selected range) is sent to the formatter’s stdin.
Troubleshooting
Formatter not found
Formatter not found
Error:
Failed to execute formatter: program not foundSolution:- Ensure the formatter is installed and available in your PATH
- Use absolute path to the formatter executable:
Formatting timeout
Formatting timeout
Error:
Formatting operation timed outSolution:- Increase the timeout value:
Invalid formatted output
Invalid formatted output
Error:
Formatter returned invalid UTF-8Solution:- Ensure your formatter outputs UTF-8 encoded text
- Check that the formatter is configured correctly
- Test the formatter command manually:
Formatter returns non-zero exit code
Formatter returns non-zero exit code
Error:
Formatter exited with code 1Solution:- Check formatter logs for syntax errors in your code
- Verify that all required formatter arguments are provided
- Test the command manually to see error messages
Range formatting not working
Range formatting not working
Error: Range formatting fails or formats entire fileSolution:
- Ensure your formatter supports range formatting
- Check that
${start_offset}and${end_offset}variables are correctly passed - Some formatters may not support precise range formatting
Format on Save
Format on save is configured in your editor, not in.emmyrc.json.
- VS Code
- Neovim
- Sublime Text
In
settings.json:Best Practices
Use a formatter config file
Instead of passing all options via command-line, use a formatter-specific config file (like
.stylua.toml) and commit it to your repository.Keep timeout reasonable
Set timeout to 5-10 seconds. If formatting takes longer, investigate why (large file? slow formatter?).
Test formatter separately
Test your formatter command manually before adding it to
.emmyrc.json to ensure it works correctly.Team consistency
Share your
.emmyrc.json and formatter config files with your team to ensure consistent formatting.