Skip to content

Commit 57f486b

Browse files
author
xuhuanzy
committed
New Config: Enable automatic addition of "---"
1 parent d504733 commit 57f486b

4 files changed

+58
-33
lines changed

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,12 @@
280280
"type": "array",
281281
"default": [
282282
]
283-
}
283+
},
284+
"emmylua.autoInsertTripleDash": {
285+
"type": "boolean",
286+
"default": true,
287+
"description": "%config.autoInsertTripleDash.description%"
288+
}
284289
}
285290
},
286291
"configurationDefaults": {

package.nls.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"debug.launch.workingDir": "working directory",
1414
"debug.launch.arguments": "arguments pass to executable file",
1515
"debug.launch.blockOnExit": "block process when exit",
16-
"debug.launch.newWindow": "create new windows"
16+
"debug.launch.newWindow": "create new windows",
17+
"config.autoInsertTripleDash.description": "Automatically insert \"---\" when line breaking after a comment"
1718
}

package.nls.zh-cn.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"debug.launch.workingDir": "设置工作区",
1414
"debug.launch.arguments": "传递给可执行文件的参数",
1515
"debug.launch.blockOnExit": "进程结束时是否阻塞进程保持窗口开启",
16-
"debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程"
16+
"debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程",
17+
"config.autoInsertTripleDash.description": "在注解后换行时自动插入 \"---\""
1718
}

src/languageConfiguration.ts

+48-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,54 @@
11
import { LanguageConfiguration, IndentAction, IndentationRule } from "vscode";
2-
2+
import { workspace } from "vscode";
33
export class LuaLanguageConfiguration implements LanguageConfiguration {
4-
public onEnterRules = [
5-
{
6-
action: { indentAction: IndentAction.None, appendText: "---" },
7-
beforeText: /^---/,
8-
},
9-
{
10-
// 匹配 function 语句的行
11-
beforeText: /^\s*function\s+\w*\s*\(.*\)\s*$/,
12-
afterText: /^\s*end\s*$/,
13-
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
14-
},
15-
{
16-
// 匹配局部函数定义的行
17-
beforeText: /^\s*local\s+\w+\s*=\s*function\s*\(.*\)\s*$/,
18-
afterText: /^\s*end\s*$/,
19-
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
20-
},
21-
{
22-
// 匹配 xxx = function 语句的行
23-
beforeText: /^\s*.*\s*=\s*function\s*\(.*\)\s*$/,
24-
afterText: /^\s*end\s*$/,
25-
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
26-
},
27-
{
28-
// 匹配 local function 语句的行
29-
beforeText: /^\s*local\s+function\s+\w*\s*\(.*\)\s*$/,
30-
afterText: /^\s*end\s*$/,
31-
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
4+
public onEnterRules: any[];
5+
6+
constructor() {
7+
this.onEnterRules = [
8+
{
9+
// 匹配 function 语句的行
10+
beforeText: /^\s*function\s+\w*\s*\(.*\)\s*$/,
11+
afterText: /^\s*end\s*$/,
12+
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
13+
},
14+
{
15+
// 匹配局部函数定义的行
16+
beforeText: /^\s*local\s+\w+\s*=\s*function\s*\(.*\)\s*$/,
17+
afterText: /^\s*end\s*$/,
18+
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
19+
},
20+
{
21+
// 匹配 xxx = function 语句的行
22+
beforeText: /^\s*.*\s*=\s*function\s*\(.*\)\s*$/,
23+
afterText: /^\s*end\s*$/,
24+
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
25+
},
26+
{
27+
// 匹配 local function 语句的行
28+
beforeText: /^\s*local\s+function\s+\w*\s*\(.*\)\s*$/,
29+
afterText: /^\s*end\s*$/,
30+
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
31+
}
32+
];
33+
34+
// 读取配置决定是否添加三横线规则
35+
const config = workspace.getConfiguration('emmylua');
36+
const autoInsertTripleDash = config.get<boolean>('autoInsertTripleDash', true);
37+
// 第二个参数是默认值(当配置不存在时使用)
38+
if (autoInsertTripleDash) {
39+
this.onEnterRules = [
40+
{
41+
action: { indentAction: IndentAction.None, appendText: "---" },
42+
beforeText: /^---/,
43+
},
44+
...this.onEnterRules
45+
];
46+
} else {
47+
this.onEnterRules = [
48+
...this.onEnterRules
49+
];
3250
}
33-
];
51+
}
3452

3553
public indentationRules: IndentationRule = {
3654
// 匹配需要在下一行增加缩进的模式,例如函数定义、局部函数定义和 do 语句

0 commit comments

Comments
 (0)