Skip to content

Commit d52d634

Browse files
committed
add external tool range format document
1 parent cb668b5 commit d52d634

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed

docs/external_format/external_formatter_options_CN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ emmyLua_ls支持使用外部格式化工具来格式化 Lua 代码。通过配
66

77
`.emmyrc.json` 文件中,你可以配置外部格式化工具:
88

9+
文档格式化配置:
910
```json
1011
{
1112
"format" : {
@@ -20,8 +21,31 @@ emmyLua_ls支持使用外部格式化工具来格式化 Lua 代码。通过配
2021
}
2122
}
2223
}
24+
25+
```
26+
范围格式化配置:
27+
```json
28+
{
29+
"format" : {
30+
"externalToolRangeFormat": {
31+
"program": "stylua",
32+
"args": [
33+
"-",
34+
"--stdin-filepath",
35+
"${file}",
36+
"--indent-width=${indent_size}",
37+
"--indent-type",
38+
"${use_tabs?Tabs:Spaces}",
39+
"--range-start=${start_offset}",
40+
"--range-end=${end_offset}"
41+
],
42+
"timeout": 5000
43+
}
44+
}
45+
}
2346
```
2447

48+
2549
## 配置项说明
2650

2751
- **program**: 外部格式化工具的可执行文件路径
@@ -38,6 +62,10 @@ emmyLua_ls支持使用外部格式化工具来格式化 Lua 代码。通过配
3862
|------|------|--------|
3963
| `${file}` | 当前文件的完整路径 | `/path/to/script.lua` |
4064
| `${indent_size}` | 缩进大小(空格数) | `4` |
65+
| `${start_offset}` | 选定范围的起始UTF8偏移量 | `0` |
66+
| `${end_offset}` | 选定范围的结束UTF8偏移量 | `100` |
67+
| `${start_line}` | 当前文件的起始行 | `1` |
68+
| `${end_line}` | 当前文件的结束行 | `10` |
4169

4270
### 条件变量
4371

@@ -78,6 +106,20 @@ emmyLua_ls支持使用外部格式化工具来格式化 Lua 代码。通过配
78106
"--indent-type",
79107
"${use_tabs?Tabs:Spaces}"
80108
]
109+
},
110+
"externalToolRangeFormat": {
111+
"program": "stylua",
112+
"args": [
113+
"-",
114+
"--stdin-filepath",
115+
"${file}",
116+
"--indent-width=${indent_size}",
117+
"--indent-type",
118+
"${use_tabs?Tabs:Spaces}",
119+
"--range-start=${start_offset}",
120+
"--range-end=${end_offset}"
121+
],
122+
"timeout": 5000
81123
}
82124
}
83125
}

docs/external_format/external_formatter_options_EN.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# External Formatter Options
1+
# External Formatter Tool Options
22

3-
[中文版本](./external_formatter_options_CN.md)
3+
[中文版](external_formatter_options_CN.md)
44

5-
EmmyLua_ls supports using external formatting tools to format Lua code. By configuring the `.emmyrc.json` file, you can integrate any command-line code formatting tool.
5+
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.
66

77
## Configuration Format
88

99
In the `.emmyrc.json` file, you can configure external formatting tools:
1010

11+
Document formatting configuration:
1112
```json
1213
{
1314
"format" : {
@@ -22,33 +23,60 @@ In the `.emmyrc.json` file, you can configure external formatting tools:
2223
}
2324
}
2425
}
26+
27+
```
28+
Range formatting configuration:
29+
```json
30+
{
31+
"format" : {
32+
"externalToolRangeFormat": {
33+
"program": "stylua",
34+
"args": [
35+
"-",
36+
"--stdin-filepath",
37+
"${file}",
38+
"--indent-width=${indent_size}",
39+
"--indent-type",
40+
"${use_tabs?Tabs:Spaces}",
41+
"--range-start=${start_offset}",
42+
"--range-end=${end_offset}"
43+
],
44+
"timeout": 5000
45+
}
46+
}
47+
}
2548
```
2649

50+
2751
## Configuration Options
2852

2953
- **program**: Path to the external formatting tool executable
30-
- **args**: List of arguments to pass to the formatting tool
31-
- **timeout**: Timeout for the formatting operation in milliseconds (default: 5000ms)
54+
- **args**: List of arguments passed to the formatting tool
55+
- **timeout**: Timeout for formatting operations (milliseconds), default is 5000ms
3256

3357
## Variable Substitution
3458

35-
In the `args` parameter, you can use the following variables, which will be replaced with actual values at runtime:
59+
In the `args` parameters, you can use the following variables that will be replaced with actual values at runtime:
3660

3761
### Simple Variables
3862

3963
| Variable | Description | Example Value |
4064
|----------|-------------|---------------|
41-
| `${file}` | Full path to the current file | `/path/to/script.lua` |
65+
| `${file}` | Full path of the current file | `/path/to/script.lua` |
4266
| `${indent_size}` | Indentation size (number of spaces) | `4` |
67+
| `${start_offset}` | Start UTF8 offset of the selected range | `0` |
68+
| `${end_offset}` | End UTF8 offset of the selected range | `100` |
69+
| `${start_line}` | Start line of the current file | `1` |
70+
| `${end_line}` | End line of the current file | `10` |
4371

4472
### Conditional Variables
4573

46-
Conditional variables use the format `${variable?true_value:false_value}` to return different values based on conditions:
74+
Conditional variables use the format `${variable?true_value:false_value}`, returning different values based on conditions:
4775

48-
| Variable | Description | When true | When false |
49-
|----------|-------------|-----------|------------|
76+
| Variable | Description | Returns when true | Returns when false |
77+
|----------|-------------|-------------------|-------------------|
5078
| `${use_tabs?--use-tabs:--use-spaces}` | Whether to use tab indentation | `--use-tabs` | `--use-spaces` |
51-
| `${insert_final_newline?--final-newline:}` | Whether to insert a newline at the end of file | `--final-newline` | Empty string |
79+
| `${insert_final_newline?--final-newline:}` | Whether to insert newline at end of file | `--final-newline` | Empty string |
5280
| `${non_standard_symbol?--allow-non-standard}` | Whether to allow non-standard symbols | `--allow-non-standard` | Empty string |
5381

5482
## Variable Syntax
@@ -60,7 +88,7 @@ Conditional variables use the format `${variable?true_value:false_value}` to ret
6088

6189
### Special Handling
6290
- If a conditional variable results in an empty string, that argument will not be passed to the external tool
63-
- Variable names are case sensitive
91+
- Variable names are case-sensitive
6492
- Unknown variables will remain unchanged
6593

6694
## Configuration Examples
@@ -80,22 +108,36 @@ Conditional variables use the format `${variable?true_value:false_value}` to ret
80108
"--indent-type",
81109
"${use_tabs?Tabs:Spaces}"
82110
]
111+
},
112+
"externalToolRangeFormat": {
113+
"program": "stylua",
114+
"args": [
115+
"-",
116+
"--stdin-filepath",
117+
"${file}",
118+
"--indent-width=${indent_size}",
119+
"--indent-type",
120+
"${use_tabs?Tabs:Spaces}",
121+
"--range-start=${start_offset}",
122+
"--range-end=${end_offset}"
123+
],
124+
"timeout": 5000
83125
}
84126
}
85127
}
86128
```
87129

88130
## Workflow
89131

90-
1. When the user triggers code formatting, the EmmyLua analyzer reads the configured external tool settings
91-
2. Parse the variables in `args` and replace them with actual values
92-
3. Start the external formatting tool and pass the current code to it through stdin
93-
4. Wait for the external tool to complete processing and read the formatted code
94-
5. If formatting is successful, apply the result to the editor
132+
1. When the user triggers code formatting, EmmyLua analyzer reads the configured external tool settings
133+
2. Parses variables in `args` and replaces them with actual values
134+
3. Starts the external formatting tool and passes the current code to it via stdin
135+
4. Waits for the external tool to complete processing and reads the formatted code
136+
5. If formatting is successful, applies the result to the editor
95137

96138
## Error Handling
97139

98140
- If the external tool doesn't exist or cannot be executed, an error log will be recorded
99-
- If the formatting process times out, the process will be terminated and a timeout error will be logged
100-
- If the external tool returns a non-zero exit code, the error information will be logged
101-
- If the output is not valid UTF-8 text, an encoding error will be logged
141+
- If the formatting process times out, the process will be terminated and a timeout error will be recorded
142+
- If the external tool returns a non-zero exit code, error information will be recorded
143+
- If the output is not valid UTF-8 text, an encoding error will be recorded

0 commit comments

Comments
 (0)