Skip to content

Commit 8813aae

Browse files
committed
Refactor parsing of cnfg using docs
1 parent 6ff3139 commit 8813aae

18 files changed

Lines changed: 328 additions & 323 deletions

server/src/configProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
CancellationTokenSource,
1010
URI,
1111
} from "vscode-languageserver";
12-
import { SepticCnfg, parseSepticAsync } from "./septic";
12+
import { SepticCnfg } from "./septic";
1313
import { ResourceMap } from "./util/resourceMap";
1414
import { ITextDocument } from "./language-service";
1515
import { Lazy, lazy } from "./util/lazy";
@@ -29,8 +29,8 @@ async function getValueCnfg(
2929
document: ITextDocument,
3030
token: CancellationToken
3131
): Promise<SepticCnfg> {
32-
const text = document.getText();
33-
const cnfg = await parseSepticAsync(text, token);
32+
const cnfg = new SepticCnfg(document);
33+
await cnfg.parseAsync(token);
3434
cnfg.setUri(document.uri);
3535
return cnfg;
3636
}

server/src/septic/cnfg.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,61 @@ import {
2323
ReferenceType,
2424
} from "./reference";
2525
import { SepticContext } from './context';
26-
import { removeSpaces, transformPositionsToOriginal } from "../util";
26+
import { removeSpaces, sleep, transformPositionsToOriginal } from "../util";
2727
import { updateParentObjects } from "./hierarchy";
28-
import { Alg, Cycle, findAlgCycles } from "./cycle";
2928
import { getFunctionsFromCalcPvrs, SepticFunction } from './function';
29+
import { ITextDocument } from '../language-service';
30+
import { CancellationToken } from 'vscode-languageserver';
31+
import { SepticParser, SepticScanner } from './parser';
3032

3133
export class SepticCnfg implements SepticContext {
32-
public objects: SepticObject[];
33-
public comments: SepticComment[];
34+
public objects: SepticObject[] = [];
35+
public comments: SepticComment[] = [];
36+
public readonly doc: ITextDocument;
3437
private xvrRefs = new Map<string, SepticReference[]>();
3538
private xvrRefsExtracted = false;
3639
public uri: string = "";
3740

38-
constructor(objects: SepticObject[], comments: SepticComment[] = []) {
39-
this.objects = objects;
40-
this.comments = comments;
41+
constructor(doc: ITextDocument) {
42+
this.doc = doc;
43+
}
44+
45+
public parse(cts: CancellationToken): void {
46+
const scanner = new SepticScanner(this.doc.getText());
47+
const tokens = scanner.scanTokens();
48+
if (!tokens.tokens.length) {
49+
return;
50+
}
51+
const parser = new SepticParser(tokens.tokens);
52+
53+
this.objects = parser.parse(cts);
54+
this.comments = tokens.comments.map((comment) => {
55+
return new SepticComment(
56+
comment.content,
57+
comment.type,
58+
comment.start,
59+
comment.end
60+
);
61+
});
62+
}
63+
64+
public async parseAsync(token: CancellationToken): Promise<void> {
65+
const scanner = new SepticScanner(this.doc.getText());
66+
const tokens = scanner.scanTokens();
67+
if (!tokens.tokens.length) {
68+
return;
69+
}
70+
await sleep(1); // Sleep to prevent starvation of other async tasks
71+
const parser = new SepticParser(tokens.tokens);
72+
this.objects = parser.parse(token);
73+
this.comments = tokens.comments.map((comment) => {
74+
return new SepticComment(
75+
comment.content,
76+
comment.type,
77+
comment.start,
78+
comment.end
79+
);
80+
});
4181
}
4282

4383
public async load(): Promise<void> {

server/src/septic/parser.ts

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { CancellationToken } from "vscode-languageserver";
77
import { Parser, IToken, ParserError } from "../util/parser";
8-
import { SepticCnfg } from "./cnfg";
98
import { SepticToken, SepticTokenType } from "./tokens";
109
import {
1110
Attribute,
@@ -14,53 +13,6 @@ import {
1413
SepticComment,
1514
SepticObject,
1615
} from "./elements";
17-
import { sleep } from "../util";
18-
19-
export function parseSepticSync(
20-
input: string,
21-
token: CancellationToken | undefined = undefined
22-
): SepticCnfg {
23-
const scanner = new SepticScanner(input);
24-
const tokens = scanner.scanTokens();
25-
if (!tokens.tokens.length) {
26-
return new SepticCnfg([]);
27-
}
28-
const parser = new SepticParser(tokens.tokens);
29-
30-
const cnfg = parser.parse(token);
31-
cnfg.comments = tokens.comments.map((comment) => {
32-
return new SepticComment(
33-
comment.content,
34-
comment.type,
35-
comment.start,
36-
comment.end
37-
);
38-
});
39-
return cnfg;
40-
}
41-
42-
export async function parseSepticAsync(
43-
input: string,
44-
token: CancellationToken | undefined = undefined
45-
): Promise<SepticCnfg> {
46-
const scanner = new SepticScanner(input);
47-
const tokens = scanner.scanTokens();
48-
if (!tokens.tokens.length) {
49-
return new SepticCnfg([]);
50-
}
51-
await sleep(1); // Short sleep to prevent starvation of other processes
52-
const parser = new SepticParser(tokens.tokens);
53-
const cnfg = parser.parse(token);
54-
cnfg.comments = tokens.comments.map((comment) => {
55-
return new SepticComment(
56-
comment.content,
57-
comment.type,
58-
comment.start,
59-
comment.end
60-
);
61-
});
62-
return cnfg;
63-
}
6416

6517
const validAttributeTokens = [
6618
SepticTokenType.numeric,
@@ -69,14 +21,14 @@ const validAttributeTokens = [
6921
SepticTokenType.path,
7022
];
7123

72-
export class SepticParser extends Parser<SepticTokenType, SepticCnfg> {
24+
export class SepticParser extends Parser<SepticTokenType, SepticObject[]> {
7325
public errors: ParserError<SepticTokenType>[] = [];
7426

75-
parse(token: CancellationToken | undefined = undefined): SepticCnfg {
27+
parse(token: CancellationToken | undefined = undefined): SepticObject[] {
7628
const septicObjects = [];
7729
while (!this.isAtEnd()) {
7830
if (token?.isCancellationRequested) {
79-
return new SepticCnfg([]);
31+
return [];
8032
}
8133
if (this.match(SepticTokenType.object)) {
8234
const septicObj = this.septicObject();
@@ -88,7 +40,7 @@ export class SepticParser extends Parser<SepticTokenType, SepticCnfg> {
8840
);
8941
}
9042
}
91-
return new SepticCnfg(septicObjects);
43+
return septicObjects;
9244
}
9345

9446
septicObject(): SepticObject {

server/src/test/codeActions.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TextDocument } from "vscode-languageserver-textdocument";
2-
import { SepticMetaInfoProvider, parseSepticSync } from "../septic";
3-
import { loadFile } from "./util";
2+
import { SepticMetaInfoProvider } from "../septic";
3+
import { loadFile, parseSepticForTest } from "./util";
44
import {
55
Position,
66
CodeActionParams,
@@ -35,7 +35,7 @@ describe("Test codeaction for inserting evr", () => {
3535
},
3636
};
3737
const content = loadFile("codeAction.cnfg");
38-
const cnfg = parseSepticSync(content);
38+
const cnfg = parseSepticForTest(content);
3939
await cnfg.updateObjectParents(
4040
SepticMetaInfoProvider.getInstance().getObjectHierarchy()
4141
);
@@ -66,7 +66,7 @@ describe("Test codeaction for inserting evr", () => {
6666
},
6767
};
6868
const content = loadFile("codeAction.cnfg");
69-
const cnfg = parseSepticSync(content);
69+
const cnfg = parseSepticForTest(content);
7070
cnfg.updateObjectParents(
7171
SepticMetaInfoProvider.getInstance().getObjectHierarchy()
7272
);
@@ -97,7 +97,7 @@ describe("Test codeaction for inserting evr", () => {
9797
},
9898
};
9999
const content = loadFile("codeAction.cnfg");
100-
const cnfg = parseSepticSync(content);
100+
const cnfg = parseSepticForTest(content);
101101
cnfg.updateObjectParents(
102102
SepticMetaInfoProvider.getInstance().getObjectHierarchy()
103103
);
@@ -121,7 +121,7 @@ describe("Test codeaction for inserting evr", () => {
121121
},
122122
};
123123
const content = loadFile("codeAction.cnfg");
124-
const cnfg = parseSepticSync(content);
124+
const cnfg = parseSepticForTest(content);
125125
cnfg.updateObjectParents(
126126
SepticMetaInfoProvider.getInstance().getObjectHierarchy()
127127
);
@@ -152,7 +152,7 @@ describe("Test codeaction for ignoring warning", () => {
152152
},
153153
};
154154
const content = loadFile("codeActionIgnore.cnfg");
155-
const cnfg = parseSepticSync(content);
155+
const cnfg = parseSepticForTest(content);
156156
const doc = TextDocument.create("", "", 0, content);
157157
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
158158
expect(codeActions.length).to.equal(2);
@@ -179,7 +179,7 @@ describe("Test codeaction for ignoring warning", () => {
179179
},
180180
};
181181
const content = loadFile("codeActionIgnore.cnfg");
182-
const cnfg = parseSepticSync(content);
182+
const cnfg = parseSepticForTest(content);
183183
const doc = TextDocument.create("", "", 0, content);
184184
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
185185
expect(codeActions.length).to.equal(1);
@@ -188,7 +188,7 @@ describe("Test codeaction for ignoring warning", () => {
188188
0
189189
) as TextDocumentEdit;
190190
const updatedeContent = TextDocument.applyEdits(doc, textEdits.edits);
191-
const updatedCnfg = parseSepticSync(updatedeContent);
191+
const updatedCnfg = parseSepticForTest(updatedeContent);
192192
expect(updatedCnfg.comments[0].content).to.equal(
193193
"{# noqa: W501, E202 #}"
194194
);
@@ -213,7 +213,7 @@ describe("Test codeaction for ignoring warning", () => {
213213
},
214214
};
215215
const content = loadFile("codeActionIgnore.cnfg");
216-
const cnfg = parseSepticSync(content);
216+
const cnfg = parseSepticForTest(content);
217217
const doc = TextDocument.create("", "", 0, content);
218218
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
219219
expect(codeActions.length).to.equal(1);
@@ -222,7 +222,7 @@ describe("Test codeaction for ignoring warning", () => {
222222
0
223223
) as TextDocumentEdit;
224224
const updatedeContent = TextDocument.applyEdits(doc, textEdits.edits);
225-
const updatedCnfg = parseSepticSync(updatedeContent);
225+
const updatedCnfg = parseSepticForTest(updatedeContent);
226226
expect(updatedCnfg.comments[1].content).to.equal("// noqa: W501, E202");
227227
});
228228
it("Expect code actions to suggest updating ignore comments for all codes", async () => {
@@ -251,7 +251,7 @@ describe("Test codeaction for ignoring warning", () => {
251251
},
252252
};
253253
const content = loadFile("codeActionIgnore.cnfg");
254-
const cnfg = parseSepticSync(content);
254+
const cnfg = parseSepticForTest(content);
255255
const doc = TextDocument.create("", "", 0, content);
256256
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
257257
expect(codeActions.length).to.equal(2);
@@ -285,7 +285,7 @@ describe("Test codeaction for ignoring warning", () => {
285285
},
286286
};
287287
const content = loadFile("codeActionIgnore.cnfg");
288-
const cnfg = parseSepticSync(content);
288+
const cnfg = parseSepticForTest(content);
289289
const doc = TextDocument.create("", "", 0, content);
290290
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
291291
expect(codeActions.length).to.equal(2);
@@ -312,7 +312,7 @@ describe("Test codeaction for ignoring warning", () => {
312312
},
313313
};
314314
const content = loadFile("codeActionIgnore.cnfg");
315-
const cnfg = parseSepticSync(content);
315+
const cnfg = parseSepticForTest(content);
316316
const doc = TextDocument.create("", "", 0, content);
317317
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
318318
expect(codeActions.length).to.equal(0);
@@ -337,7 +337,7 @@ describe("Test codeaction for ignoring warning", () => {
337337
},
338338
};
339339
const content = loadFile("codeActionIgnore.cnfg");
340-
const cnfg = parseSepticSync(content);
340+
const cnfg = parseSepticForTest(content);
341341
const doc = TextDocument.create("", "", 0, content);
342342
const codeActions = getCodeActionIgnoreDiagnostics(params, cnfg, doc);
343343
expect(codeActions.length).to.equal(0);

0 commit comments

Comments
 (0)