diff --git a/package.json b/package.json index a954d3b..d945eb6 100644 --- a/package.json +++ b/package.json @@ -280,7 +280,12 @@ "type": "array", "default": [ ] - } + }, + "emmylua.autoInsertTripleDash": { + "type": "boolean", + "default": true, + "description": "%config.autoInsertTripleDash.description%" + } } }, "configurationDefaults": { diff --git a/package.nls.json b/package.nls.json index 30b652b..3ef942f 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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" } diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 8c2773a..1e61da4 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -13,5 +13,6 @@ "debug.launch.workingDir": "设置工作区", "debug.launch.arguments": "传递给可执行文件的参数", "debug.launch.blockOnExit": "进程结束时是否阻塞进程保持窗口开启", - "debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程" + "debug.launch.newWindow": "emmylua会创建一个新窗口展示这个进程", + "config.autoInsertTripleDash.description": "在注解后换行时自动插入 \"---\"" } diff --git a/src/languageConfiguration.ts b/src/languageConfiguration.ts index af22aaf..272d52c 100644 --- a/src/languageConfiguration.ts +++ b/src/languageConfiguration.ts @@ -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('autoInsertTripleDash', true); + // 第二个参数是默认值(当配置不存在时使用) + if (autoInsertTripleDash) { + this.onEnterRules = [ + { + action: { indentAction: IndentAction.None, appendText: "---" }, + beforeText: /^---/, + }, + ...this.onEnterRules + ]; + } else { + this.onEnterRules = [ + ...this.onEnterRules + ]; } - ]; + } public indentationRules: IndentationRule = { // 匹配需要在下一行增加缩进的模式,例如函数定义、局部函数定义和 do 语句