Why Use External Formatters?
Team Preference
Your team may already use a specific formatter
Custom Rules
External formatters may offer specific style rules you need
Migration
Easier migration from other Lua toolchains
Performance
Some external formatters may be faster for large files
Configuration Overview
External formatters are configured in.emmyrc.json using the reformat section:
.emmyrc.json
Configuration Fields
Configuration Fields
Configuration for full document formatting
Configuration for range (selection) formatting
Path to the formatter executable (must be in PATH or absolute path)
Command-line arguments passed to the formatter
Timeout in milliseconds for formatting operations
Use diff-based formatting for better performance on large files
Variable Substitution
Theargs array supports variable substitution for dynamic values:
Simple Variables
| Variable | Description | Example Value |
|---|---|---|
${file} | Full path of the current file | /path/to/script.lua |
${indent_size} | Indentation size (number of spaces) | 4 |
${start_offset} | Start byte offset of selection | 0 |
${end_offset} | End byte offset of selection | 100 |
${start_line} | Start line number | 1 |
${end_line} | End line number | 10 |
Conditional Variables
Conditional variables use ternary-like syntax:| Variable | Returns when true | Returns when false |
|---|---|---|
${use_tabs?Tabs:Spaces} | Tabs | Spaces |
${use_tabs?--use-tabs:--use-spaces} | --use-tabs | --use-spaces |
${insert_final_newline?--final-newline:} | --final-newline | Empty string |
StyLua Integration
StyLua is a popular opinionated Lua formatter.Installation
Configuration
.emmyrc.json
StyLua Configuration File
Createstylua.toml in your project root:
stylua.toml
StyLua Options
StyLua Options
column_width- Maximum line widthline_endings- Unix (LF) or Windows (CRLF)indent_type- Spaces or Tabsindent_width- Number of spaces per indentquote_style- AutoPreferDouble, AutoPreferSingle, ForceDouble, ForceSinglecall_parentheses- Always, NoSingleString, NoSingleTable, None
lua-format Integration
lua-format is another popular Lua formatter.Installation
Configuration
.emmyrc.json
lua-format Configuration File
Create.lua-format in your project root:
.lua-format
Custom Formatter Integration
You can integrate any formatter that:- Reads from stdin or accepts a file path
- Writes formatted output to stdout
- Supports command-line configuration
Example: Python-based Formatter
format_lua.py
.emmyrc.json
Format on Save
Configure your editor to format automatically:Troubleshooting
Formatter Not Found
Solutions:-
Ensure the formatter is installed and in PATH:
-
Use absolute path in configuration:
Formatting Timeout
Increase the timeout:Invalid Output
Ensure your formatter:- Outputs valid UTF-8
- Doesn’t include extra debug messages
- Returns non-zero exit code on errors
Inconsistent Formatting
If formatting differs between runs:- Check for non-deterministic formatter behavior
- Verify configuration files are committed to version control
- Ensure all team members use the same formatter version
Performance Considerations
.emmyrc.json
Benchmarking
Compare formatter performance:CI/CD Integration
GitHub Actions
.github/workflows/format.yml
Pre-commit Hook
.git/hooks/pre-commit
Migration Strategies
From Built-in to External Formatter
From External to Built-in Formatter
Simply remove theexternalTool configuration:
.emmyrc.json
Comparison Table
| Feature | Built-in (EmmyLuaCodeStyle) | StyLua | lua-format |
|---|---|---|---|
| Installation | Included | Separate install | Separate install |
| Speed | Fast | Very Fast | Moderate |
| Configuration | .editorconfig | stylua.toml | .lua-format |
| Opinionated | Moderate | High | Configurable |
| Range formatting | Yes | Yes | Limited |
| Lua 5.4 support | Yes | Yes | Yes |
| Active development | Yes | Yes | Moderate |
Best Practices
- Choose one formatter - Don’t mix formatters across the team
- Commit config files - Version control formatter configuration
- Document setup - Include formatter setup in README
- Automate formatting - Use pre-commit hooks or CI checks
- Format incrementally - Format new/changed code rather than reformatting everything at once
Next Steps
Code Style Guide
Establish team style conventions
Performance Tuning
Optimize formatter performance
