Skip to content

Commit ef95504

Browse files
authored
Merge pull request #25 from ViTeXFTW/issue/24
fix: TransitionKeys are now precompiled before running diagnostics.
2 parents b306cf9 + e592235 commit ef95504

File tree

5 files changed

+99
-18
lines changed

5 files changed

+99
-18
lines changed

client/src/extension.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function activate(context: vscode.ExtensionContext) {
3232
// context.subscriptions.push(vscode.commands.registerCommand(command, formatDocument));
3333

3434
let languageServerRunning = ZSconfig.get<boolean>('serverStartupSetting', false); // Default to 2 if not set
35+
let displayAlphaWarning = ZSconfig.get<boolean>('displayAlphaWarning', true);
36+
let precompileTransitionKeys = ZSconfig.get<boolean>('precompileTransitionKeys', false);
3537

3638
context.subscriptions.push(vscode.commands.registerCommand('ZeroSyntax.stopLanguageServer', () => {
3739
if (languageServerRunning) {
@@ -72,10 +74,12 @@ export function activate(context: vscode.ExtensionContext) {
7274
// Notify the server about file changes to '.clientrc files contained in the workspace
7375
fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
7476
},
75-
// initializationOptions: {
77+
initializationOptions: {
78+
precompileTransitionKeys: precompileTransitionKeys,
79+
forceAddModule: forceAddModule
7680
// SemanticTokenTypes,
7781
// SemanticTokenModifiers
78-
// },
82+
},
7983
// middleware: {
8084
// didChange: (event, next) => {
8185
// const { contentChanges, document } = event;
@@ -104,7 +108,14 @@ export function activate(context: vscode.ExtensionContext) {
104108
client.start();
105109
}
106110

107-
vscode.window.showWarningMessage('Zero Syntax: This is an alpha version!\nPlease report bugs to https://github.com/ViTeXFTW/ZeroSyntax-Server/issues');
111+
if (displayAlphaWarning) {
112+
// Add buttons to close or turn off the warning
113+
vscode.window.showWarningMessage('Zero Syntax: This is an alpha version!\nPlease report bugs to https://github.com/ViTeXFTW/ZeroSyntax-Server/issues', 'Turn off', 'Close').then((selection) => {
114+
if (selection === 'Turn off') {
115+
ZSconfig.update('displayAlphaWarning', false, vscode.ConfigurationTarget.Global);
116+
}
117+
});
118+
}
108119
}
109120

110121
vscode.workspace.onDidChangeConfiguration((e) => {

package.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,28 @@
8282
"default": true,
8383
"description": "Controls whether the server is started automatically after the extension is activated."
8484
},
85-
"languageServerExample.maxNumberOfProblems": {
85+
"ZeroSyntax.displayAlphaWarning": {
86+
"type": "boolean",
87+
"default": true,
88+
"description": "Controls whether the alpha warning is displayed when the server is started."
89+
},
90+
"ZeroSyntax.forceAddModule": {
91+
"type": "boolean",
92+
"default": true,
93+
"description": "Controls whether AddModule around modules is forced."
94+
},
95+
"ZeroSyntax.precompileTransitionKeys": {
96+
"type": "boolean",
97+
"default": true,
98+
"description": "Controls whether the transition keys are precompiled when the server is started."
99+
},
100+
"ZeroSyntax.maxNumberOfProblems": {
86101
"scope": "resource",
87102
"type": "number",
88103
"default": 100,
89104
"description": "Controls the maximum number of problems produced by the server."
90105
},
91-
"languageServerExample.trace.server": {
106+
"ZeroSyntax.trace.server": {
92107
"scope": "window",
93108
"type": "string",
94109
"enum": [
@@ -105,6 +120,7 @@
105120
"scripts": {
106121
"vscode:prepublish": "npm run compile",
107122
"package": "npm run compile && vsce package",
123+
"cc": "npm run clean && npm run antlr4ng && npm run compile",
108124
"compile": "tsc -b",
109125
"clean": "rimraf ./client/out && rimraf ./server/out",
110126
"compile-test": "cd client && npm run compile-test && cd .. && cd server && npm run compile-test",

server/src/diagnostic/diagnosticsVisitor.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver";
33
import * as list from '../utils/lists'
44
import { Location } from "../utils/location";
55
import { MapIniVisitor } from "../utils/antlr4ng/MapIniVisitor";
6-
import { Armor_valueContext, Commandbutton_valueContext, CommandSet_valueContext, CommandSetClassPropertyContext, Cursorname_valueContext, DamageFX_valueContext, DrawModule_conditionBlockContext, DrawModule_conditionStateValueContext, DrawModule_transitionKeyPropertyContext, DrawModule_transitionStateBlockContext, EndContext, Fxlist_valueContext, Locomotor_valueContext, MapIniParser, Mappedimage_valueContext, Object_valueContext, ObjectClass_drawModulesContext, ObjectClass_propertiesContext, ObjectClass_setsContext, ObjectClass_soundsContext, ObjectClassContext, Particlesystem_valueContext, ParticleSystemClassContext, ProgramContext, Science_valueContext, Specialpower_valueContext, TransitionKey_valueContext, Upgrade_valueContext } from "../utils/antlr4ng/MapIniParser";
7-
import { AbstractParseTreeVisitor } from "antlr4ng";
6+
import { Armor_valueContext, Commandbutton_valueContext, CommandSet_valueContext, CommandSetClassPropertyContext, Cursorname_valueContext, DamageFX_valueContext, DrawModule_conditionBlockContext, DrawModule_conditionStatePropertiesContext, DrawModule_conditionStateValueContext, DrawModule_defaultconditionBlockContext, DrawModule_transitionKeyPropertyContext, DrawModule_transitionStateBlockContext, EndContext, Fxlist_valueContext, Locomotor_valueContext, MapIniParser, Mappedimage_valueContext, Object_valueContext, ObjectClass_drawModulesContext, ObjectClass_propertiesContext, ObjectClass_setsContext, ObjectClass_soundsContext, ObjectClassContext, Particlesystem_valueContext, ParticleSystemClassContext, ProgramContext, Science_valueContext, Specialpower_valueContext, TransitionKey_valueContext, Upgrade_valueContext, W3dDebrisDrawModuleContext, W3dDefaultDrawModuleContext, W3dDependencyDrawModuleContext, W3dLaserDrawModuleContext, W3dModelDrawModuleContext, W3dOverlordTankDrawModuleContext, W3dProjectileStreamDrawModuleContext, W3dScienceModelDrawModuleContext, W3dSupplyDrawModuleContext, W3dTreeDrawModuleContext, W3dVehicleDrawModuleContext } from "../utils/antlr4ng/MapIniParser";
7+
import { AbstractParseTreeVisitor, ParserRuleContext } from "antlr4ng";
88
import { ErrorListener } from "../errorListener";
99
import { ClassVisitor } from './classVisitor';
1010

@@ -14,10 +14,12 @@ export class DiagnosticVisitor extends AbstractParseTreeVisitor<void> implements
1414
}
1515

1616
diagnostics: Diagnostic[]
17+
precompileTransitionKeys: boolean
1718

18-
constructor(diagnostics: Diagnostic[]) {
19+
constructor(diagnostics: Diagnostic[], precompileTransitionKeys: boolean) {
1920
super()
2021
this.diagnostics = diagnostics
22+
this.precompileTransitionKeys = precompileTransitionKeys
2123
}
2224

2325
visitProgram(ctx: ProgramContext): void {
@@ -177,11 +179,52 @@ export class DiagnosticVisitor extends AbstractParseTreeVisitor<void> implements
177179
// }
178180

179181
visitObjectClass_drawModules(ctx: ObjectClass_drawModulesContext): void {
180-
console.log(list.customConditionStates)
181182
list.customConditionStates.clear()
183+
184+
185+
if (this.precompileTransitionKeys) {
186+
for (const child of ctx.children || []) {
187+
if (child instanceof W3dModelDrawModuleContext ||
188+
child instanceof W3dVehicleDrawModuleContext ||
189+
child instanceof W3dOverlordTankDrawModuleContext
190+
) {
191+
this.collectTransitionKeys(child)
192+
}
193+
}
194+
}
195+
196+
182197
this.visitChildren(ctx)
183198
}
184199

200+
private collectTransitionKeys(ctx: ParserRuleContext): void {
201+
for (const child of ctx.children || []) {
202+
if (child instanceof DrawModule_conditionBlockContext) {
203+
// Recursively check condition block for TransitionKey properties
204+
this.collectTransitionKeysFromBlock(child)
205+
} else if (child instanceof DrawModule_defaultconditionBlockContext) {
206+
// Check default condition block
207+
this.collectTransitionKeysFromBlock(child)
208+
}
209+
// Recursively check other children
210+
this.collectTransitionKeys(child as ParserRuleContext)
211+
}
212+
}
213+
214+
private collectTransitionKeysFromBlock(ctx: ParserRuleContext): void {
215+
for (const child of ctx.children || []) {
216+
if (child instanceof DrawModule_conditionStatePropertiesContext) {
217+
const transitionKeyProp = child.drawModule_transitionKeyProperty?.()
218+
if (transitionKeyProp?.transitionKey_value()?.ID()) {
219+
const ID_text = transitionKeyProp.transitionKey_value().ID()!.getText()
220+
list.customConditionStates.remove(ID_text)
221+
list.customConditionStates.insert(ID_text)
222+
}
223+
}
224+
// Recursively check other children
225+
this.collectTransitionKeysFromBlock(child as ParserRuleContext)
226+
}
227+
}
185228

186229
// =====================================
187230
// =========== CLASS VALUES ============
@@ -403,6 +446,7 @@ export class DiagnosticVisitor extends AbstractParseTreeVisitor<void> implements
403446
}
404447
}
405448
}
449+
406450
this.visitChildren(ctx)
407451
}
408452

@@ -454,7 +498,7 @@ export class DiagnosticVisitor extends AbstractParseTreeVisitor<void> implements
454498
}
455499
}
456500

457-
export function computeDiagnostics(parser: MapIniParser): Diagnostic[] {
501+
export function computeDiagnostics(parser: MapIniParser, precompileTransitionKeys: boolean): Diagnostic[] {
458502

459503
let diagnostics: Diagnostic[] = []
460504

@@ -464,7 +508,7 @@ export function computeDiagnostics(parser: MapIniParser): Diagnostic[] {
464508
const tree = parser.program()
465509
// console.log(`Tree: ${tree.getText()}`)
466510

467-
const vistor = new DiagnosticVisitor(diagnostics)
511+
const vistor = new DiagnosticVisitor(diagnostics, precompileTransitionKeys)
468512
const classVisitor = new ClassVisitor()
469513

470514
classVisitor.visitProgram(tree)

server/src/parser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ export class Parser {
1313
private lexer: MapIniLexer | undefined;
1414
private tokenStream: CommonTokenStream | undefined;
1515

16+
private document: TextDocument | undefined;
17+
1618
constructor() {}
1719

1820
updateParser(document: TextDocument): MapIniParser {
1921
this.inputStream = CharStream.fromString(document.getText());
2022
this.lexer = new MapIniLexer(this.inputStream);
2123
this.tokenStream = new CommonTokenStream(this.lexer);
2224
this.latestParser = new MapIniParser(this.tokenStream);
25+
this.document = document;
2326

2427
return this.latestParser
2528
}
@@ -48,4 +51,8 @@ export class Parser {
4851
}
4952
}
5053

54+
getDocument(): TextDocument | undefined {
55+
return this.document
56+
}
57+
5158
}

server/src/server.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ let parser: Parser = new Parser();
5252
let currentParser: MapIniParser;
5353

5454
let forceAddModule: boolean = true
55+
let precompileTransitionKeys: boolean = false
5556

5657
connection.onInitialize((params: InitializeParams) => {
5758
const capabilities = params.capabilities;
@@ -93,6 +94,10 @@ connection.onInitialize((params: InitializeParams) => {
9394
// }
9495
}
9596
};
97+
98+
forceAddModule = options.forceAddModule !== undefined ? options.forceAddModule : true
99+
precompileTransitionKeys = options.precompileTransitionKeys !== undefined ? options.precompileTransitionKeys : false
100+
96101
if (hasWorkspaceFolderCapability) {
97102
result.capabilities.workspace = {
98103
workspaceFolders: {
@@ -120,12 +125,10 @@ connection.onInitialized(() => {
120125
connection.onDidChangeConfiguration(async (change: DidChangeConfigurationParams) => {
121126
const settings = await connection.workspace.getConfiguration('ZeroSyntax')
122127

123-
if (settings.forceAddModule !== null) {
124-
forceAddModule = settings.forceAddModule
125-
console.log(`Updated forceAddmodule to: ${forceAddModule}`)
126-
} else {
127-
// If setting is not null set forceAddmoule to setting else default to true
128-
change.settings.forceAddModule !== null ? forceAddModule = change.settings.forceAddModule : forceAddModule = true
128+
forceAddModule = settings.forceAddModule !== null ? settings.forceAddModule : true
129+
130+
if (settings.precompileTransitionKeys !== null) {
131+
precompileTransitionKeys = settings.precompileTransitionKeys
129132
}
130133
})
131134
}
@@ -181,7 +184,7 @@ documents.onDidChangeContent((change) => {
181184
currentParser = parser.updateParser(change.document) //Potentially add another timer that is shorter, but does not create a parser for every input.
182185

183186
diagnosticTimer = setTimeout(() => {
184-
let diagnostics = computeDiagnostics(currentParser)
187+
let diagnostics = computeDiagnostics(currentParser, precompileTransitionKeys)
185188
// console.log(`Diagnostics: ${diagnostics}`)
186189
connection.sendDiagnostics({ uri: change.document.uri, diagnostics })
187190
console.log(`Diagnostics sent!`)

0 commit comments

Comments
 (0)