|
1 | 1 | import { LanguageConfiguration, IndentAction, IndentationRule } from "vscode";
|
2 |
| - |
| 2 | +import { workspace } from "vscode"; |
3 | 3 | 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 | + ]; |
32 | 50 | }
|
33 |
| - ]; |
| 51 | + } |
34 | 52 |
|
35 | 53 | public indentationRules: IndentationRule = {
|
36 | 54 | // 匹配需要在下一行增加缩进的模式,例如函数定义、局部函数定义和 do 语句
|
|
0 commit comments