Skip to content

Commit 4871a86

Browse files
committed
Added: Added more blocks to formatter
1 parent 30232b2 commit 4871a86

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

client/src/extension.ts

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,7 @@ export function activate(context: vscode.ExtensionContext) {
2525

2626
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('ini', {
2727
provideDocumentFormattingEdits: (document: vscode.TextDocument): vscode.TextEdit[] => {
28-
// return formatDocument(document);
29-
const edits: vscode.TextEdit[] = [];
30-
let indentlevel = 0;
31-
const indentSize = 2;
32-
33-
let singleBlockList = ["^\\b([Oo]bject)\\s+[a-zA-Z0-9_]", "^\\b([Oo]bject[Rr]eskin)\\s+[a-zA-Z0-9_]", "^\\b([Aa]dd[Mm]odule)$", "^\\b([Rr]eplace[Mm]odule)$", "^\\b([Dd]efault[Cc]ondition[Ss]tate)$", "^\\b([Uu]nit[Ss]pecific[Ss]ounds)$", "^\\b([Pp]rerequisites)$", "^\\b([Aa]rmor[Ss]et)$", "^\\b([Ww]eapon[Ss]et)$"];
34-
let multiblock = ["\\b([Dd]raw)\\s*=", "\\b([Cc]ondition[Ss]tate)\\s*=", "\\b([Tt]ransition[Ss]tate)\\s*=", "\\b([Bb]ody)\\s*=", "\\b([Bb]ehavior)\\s*=", "\\b([Cc]lient[Uu]pdate)\\s*=", "^\\b(Turret)$"];
35-
36-
for(let i = 0; i < document.lineCount; i++) {
37-
const lineText = document.lineAt(i).text;
38-
const trimmedLine = lineText.trim();
39-
40-
if(trimmedLine.endsWith("End") || trimmedLine.endsWith("end") || trimmedLine.endsWith("END")) {
41-
indentlevel = Math.max(0, indentlevel - 1);
42-
}
43-
44-
const indentedLine = " ".repeat(indentSize * indentlevel) + trimmedLine;
45-
const range = new vscode.Range(i, 0, i, lineText.length);
46-
edits.push(vscode.TextEdit.replace(range, indentedLine));
47-
48-
for(let block of singleBlockList) {
49-
if(checkLineWithRegex(trimmedLine, block)) {
50-
indentlevel++;
51-
}
52-
}
53-
54-
for(let block of multiblock) {
55-
if(checkLineWithRegex(trimmedLine, block)) {
56-
indentlevel++;
57-
}
58-
}
59-
60-
}
61-
62-
return edits;
28+
return formatDocument(document);
6329
}
6430
}));
6531

@@ -102,6 +68,58 @@ export function activate(context: vscode.ExtensionContext) {
10268
client.start();
10369
}
10470

71+
function formatDocument(document: vscode.TextDocument): vscode.TextEdit[] {
72+
const edits: vscode.TextEdit[] = [];
73+
let indentlevel = 0;
74+
const indentSize = 2;
75+
76+
let ObjectsRegex = ["^\\b([Oo]bject)\\s+[a-zA-Z0-9_]", "^\\b([Oo]bject[Rr]eskin)\\s+[a-zA-Z0-9_]", "^\\b([Aa]dd[Mm]odule)$", "^\\b([Rr]eplace[Mm]odule)$", "^\\b([Dd]efault[Cc]ondition[Ss]tate)$", "^\\b([Uu]nit[Ss]pecific[Ss]ounds)$", "^\\b([Pp]rerequisites)$", "^\\b([Aa]rmor[Ss]et)$", "^\\b([Ww]eapon[Ss]et)$", "^\\b([Dd]raw)\\s*=", "^\\b([Cc]ondition[Ss]tate)\\s*=", "^\\b([Tt]ransition[Ss]tate)\\s*=", "^\\b([Bb]ody)\\s*=", "^\\b([Bb]ehavior)\\s*=", "^\\b([Cc]lient[Uu]pdate)\\s*=", "^\\b(Turret)$"];
77+
let SimpleClassesRegex = ["^\\b([Mm]apped[Ii]mage)\\s+[a-zA-Z0-9_]", "^\\b([Pp]article[Ss]ystem)\\s+[a-zA-Z0-9_]", "^\\b([Ll]ocomotor)\\s+[a-zA-Z0-9_]", "^\\b([Aa]udio[Ee]vent)\\s+[a-zA-Z0-9_]", "^\\b([Dd]ialog[Ee]vent)\\s+[a-zA-Z0-9_]", "^\\b([Aa]rmor)\\s+[a-zA-Z0-9_]", "^\\b([Cc]ommand[Ss]et)\\s+[a-zA-Z0-9_]", "^\\b([Cc]ommand[Bb]utton)\\s+[a-zA-Z0-9_]", "^\\b([Ww]eapon)\\s+[a-zA-Z0-9_]", "^\\b([Dd]amage[Ff][Xx])\\s+[A-Za-z0-9_]", "^\\b([Uu]pgrade)\\s+[a-zA-Z0-9_]", "^\\b([Pp]layer[Tt]emplate)\\s+[a-zA-Z0-9_]", "^\\b(Rank)\\s+[1-8]$", "^\\b([Ii]n[Gg]ame[Uu][Ii])$", "^\\b(A10StrikeRadiusCursor)$", "^\\b(AmbushRadiusCursor)$", "^\\b(ClusterMinesRadiusCursor)$", "^\\b(AnthraxBombRadiusCursor)$"];
78+
let OCLRegex = ["^\\b([Oo]bject[Cc]reation[Ll]ist)\\s+[a-zA-Z0-9_]", "^\\b([Cc]reate[Oo]bject)$", "^\\b([Cc]reate[Dd]ebris)$", "^\\b([Aa]pply[Rr]andom[Ff]orce)$", "^\\b([Dd]eliver[Pp]ayload)$", "^\\b([Dd]elivery[Dd]ecal)$", "^\\b([Ff]ire[ww]eapon)$", "^\\b([Aa]ttack)$"];
79+
let FXlistRegex = ["^\\b([Ff][Xx][Ll]ist)\\s+[a-zA-Z0-9_]", "^\\b([Pp]article[Ss]ystem)$", "^\\b([Ss]ound)$", "^\\b([Tt]errain[Ss]corch)$", "^\\b([Tt]racer)$", "^\\b([Ll]ight[Pp]ulse)$", "^\\b([Vv]iew[Ss]hake)$", "^\\b([Ff][Xx][Ll]ist[Aa]t[Bb]one[Pp]os)$"];
80+
81+
let EndRegex = ["^\\b([Ee]nd)$", "^\\b(END)$"]
82+
83+
for(let i = 0; i < document.lineCount; i++) {
84+
const lineText = document.lineAt(i).text;
85+
const trimmedLine = lineText.trim();
86+
87+
for(let block of EndRegex) {
88+
if(checkLineWithRegex(trimmedLine, block)) {
89+
indentlevel = Math.max(0, indentlevel - 1);
90+
}
91+
}
92+
93+
const indentedLine = " ".repeat(indentSize * indentlevel) + trimmedLine;
94+
const range = new vscode.Range(i, 0, i, lineText.length);
95+
edits.push(vscode.TextEdit.replace(range, indentedLine));
96+
97+
98+
for(let block of ObjectsRegex) {
99+
if(checkLineWithRegex(trimmedLine, block)) {
100+
indentlevel++;
101+
}
102+
}
103+
for(let block of SimpleClassesRegex) {
104+
if(checkLineWithRegex(trimmedLine, block)) {
105+
indentlevel++;
106+
}
107+
}
108+
for(let block of FXlistRegex) {
109+
if(checkLineWithRegex(trimmedLine, block)) {
110+
indentlevel++;
111+
}
112+
}
113+
for(let block of OCLRegex) {
114+
if(checkLineWithRegex(trimmedLine, block)) {
115+
indentlevel++;
116+
}
117+
}
118+
}
119+
120+
return edits;
121+
}
122+
105123
function checkLineWithRegex(line: string, regex: string) {
106124
let Regex = new RegExp(regex, "g");
107125
return Regex.test(line);

0 commit comments

Comments
 (0)