Skip to content

Commit

Permalink
Merge pull request #200 from xuhuanzy/dev
Browse files Browse the repository at this point in the history
New Config: Enable automatic addition of "---"
  • Loading branch information
CppCXY authored Feb 9, 2025
2 parents d504733 + 57f486b commit 000e984
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 33 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@
"type": "array",
"default": [
]
}
},
"emmylua.autoInsertTripleDash": {
"type": "boolean",
"default": true,
"description": "%config.autoInsertTripleDash.description%"
}
}
},
"configurationDefaults": {
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"debug.launch.workingDir": "working directory",
"debug.launch.arguments": "arguments pass to executable file",
"debug.launch.blockOnExit": "block process when exit",
"debug.launch.newWindow": "create new windows"
"debug.launch.newWindow": "create new windows",
"config.autoInsertTripleDash.description": "Automatically insert \"---\" when line breaking after a comment"
}
3 changes: 2 additions & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"debug.launch.workingDir": "设置工作区",
"debug.launch.arguments": "传递给可执行文件的参数",
"debug.launch.blockOnExit": "进程结束时是否阻塞进程保持窗口开启",
"debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程"
"debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程",
"config.autoInsertTripleDash.description": "在注解后换行时自动插入 \"---\""
}
78 changes: 48 additions & 30 deletions src/languageConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
import { LanguageConfiguration, IndentAction, IndentationRule } from "vscode";

import { workspace } from "vscode";
export class LuaLanguageConfiguration implements LanguageConfiguration {
public onEnterRules = [
{
action: { indentAction: IndentAction.None, appendText: "---" },
beforeText: /^---/,
},
{
// 匹配 function 语句的行
beforeText: /^\s*function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配局部函数定义的行
beforeText: /^\s*local\s+\w+\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配 xxx = function 语句的行
beforeText: /^\s*.*\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配 local function 语句的行
beforeText: /^\s*local\s+function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
public onEnterRules: any[];

constructor() {
this.onEnterRules = [
{
// 匹配 function 语句的行
beforeText: /^\s*function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配局部函数定义的行
beforeText: /^\s*local\s+\w+\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配 xxx = function 语句的行
beforeText: /^\s*.*\s*=\s*function\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
},
{
// 匹配 local function 语句的行
beforeText: /^\s*local\s+function\s+\w*\s*\(.*\)\s*$/,
afterText: /^\s*end\s*$/,
action: { indentAction: IndentAction.IndentOutdent, appendText: "\t" }
}
];

// 读取配置决定是否添加三横线规则
const config = workspace.getConfiguration('emmylua');
const autoInsertTripleDash = config.get<boolean>('autoInsertTripleDash', true);
// 第二个参数是默认值(当配置不存在时使用)
if (autoInsertTripleDash) {
this.onEnterRules = [
{
action: { indentAction: IndentAction.None, appendText: "---" },
beforeText: /^---/,
},
...this.onEnterRules
];
} else {
this.onEnterRules = [
...this.onEnterRules
];
}
];
}

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

0 comments on commit 000e984

Please sign in to comment.