EmmyLua_ls supports using external formatting tools to format Lua code. By configuring the .emmyrc.json file, you can integrate any code formatting tool that supports command line interface.
In the .emmyrc.json file, you can configure external formatting tools:
Document formatting configuration:
{
"format" : {
"externalTool": {
"program": "stylua",
"args": [
"-",
"--stdin-filepath",
"${file}",
],
"timeout": 5000
}
}
}
Range formatting configuration:
{
"format" : {
"externalToolRangeFormat": {
"program": "stylua",
"args": [
"-",
"--stdin-filepath",
"${file}",
"--indent-width=${indent_size}",
"--indent-type",
"${use_tabs?Tabs:Spaces}",
"--range-start=${start_offset}",
"--range-end=${end_offset}"
],
"timeout": 5000
}
}
}- program: Path to the external formatting tool executable
- args: List of arguments passed to the formatting tool
- timeout: Timeout for formatting operations (milliseconds), default is 5000ms
In the args parameters, you can use the following variables that will be replaced with actual values at runtime:
| 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 the selected range | 0 |
${end_offset} |
End byte offset of the selected range | 100 |
${start_line} |
Start line of the current file | 1 |
${end_line} |
End line of the current file | 10 |
Conditional variables use the format ${variable?true_value:false_value}, returning different values based on conditions:
| Variable | Description | Returns when true | Returns when false |
|---|---|---|---|
${use_tabs?--use-tabs:--use-spaces} |
Whether to use tab indentation | --use-tabs |
--use-spaces |
${insert_final_newline?--final-newline:} |
Whether to insert newline at end of file | --final-newline |
Empty string |
${non_standard_symbol?--allow-non-standard} |
Whether to allow non-standard symbols | --allow-non-standard |
Empty string |
${variable}- Simple variable substitution${variable?value}- Conditional variable, returns value when condition is true, otherwise returns empty string${variable?true_value:false_value}- Conditional variable, returns different values based on condition
- If a conditional variable results in an empty string, that argument will not be passed to the external tool
- Variable names are case-sensitive
- Unknown variables will remain unchanged
{
"format" : {
"externalTool": {
"program": "stylua",
"args": [
"-",
"--stdin-filepath",
"${file}",
"--indent-width=${indent_size}",
"--indent-type",
"${use_tabs?Tabs:Spaces}"
]
},
"externalToolRangeFormat": {
"program": "stylua",
"args": [
"-",
"--stdin-filepath",
"${file}",
"--indent-width=${indent_size}",
"--indent-type",
"${use_tabs?Tabs:Spaces}",
"--range-start=${start_offset}",
"--range-end=${end_offset}"
],
"timeout": 5000
}
}
}- When the user triggers code formatting, EmmyLua analyzer reads the configured external tool settings
- Parses variables in
argsand replaces them with actual values - Starts the external formatting tool and passes the current code to it via stdin
- Waits for the external tool to complete processing and reads the formatted code
- If formatting is successful, applies the result to the editor
- If the external tool doesn't exist or cannot be executed, an error log will be recorded
- If the formatting process times out, the process will be terminated and a timeout error will be recorded
- If the external tool returns a non-zero exit code, error information will be recorded
- If the output is not valid UTF-8 text, an encoding error will be recorded