Skip to content

Commit

Permalink
Provide cycle diagnostic on non scg cnfg
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlek committed Aug 30, 2023
1 parent 03dfb4c commit 797cc61
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 81 deletions.
74 changes: 48 additions & 26 deletions server/src/language-service/diagnosticsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Diagnostic, DiagnosticSeverity, Range } from "vscode-languageserver";
import {
Diagnostic,
DiagnosticRelatedInformation,
DiagnosticSeverity,
Range,
} from "vscode-languageserver";
import { ISepticConfigProvider } from "./septicConfigProvider";
import { ITextDocument } from "./types/textDocument";
import {
Expand All @@ -19,8 +24,6 @@ import {
Attribute,
SepticObject,
defaultRefValidationFunction,
Alg,
findAlgCycles,
} from "../septic";
import { SettingsManager } from "../settings";
import { isPureJinja } from "../util";
Expand Down Expand Up @@ -60,14 +63,16 @@ function createDiagnostic(
severity: DiagnosticSeverity,
range: Range,
message: string,
code: DiagnosticCode
code: DiagnosticCode,
relatedInformation: DiagnosticRelatedInformation[] = []
): Diagnostic {
return {
severity: severity,
range: range,
message: message,
code: code,
source: "septic",
relatedInformation: relatedInformation,
};
}

Expand Down Expand Up @@ -112,7 +117,11 @@ export function getDiagnostics(
const diagnostics: Diagnostic[] = [];
diagnostics.push(...objDiagnostics(cnfg, doc, refProvider));
diagnostics.push(...algDiagnostic(cnfg, doc, refProvider));
diagnostics.push(...validateAlgCycles(refProvider, doc));
const isContext = !(refProvider instanceof SepticCnfg);
if (!isContext) {
diagnostics.push(...validateAlgCycles(cnfg, doc));
}

let disabledLines = getDisabledLines(cnfg.comments, doc);
let filteredDiags = diagnostics.filter((diag) => {
let disabledLine = disabledLines.get(diag.range.start.line);
Expand Down Expand Up @@ -513,37 +522,50 @@ function getDiagnosticCodes(codes: string): string[] {
}

export function validateAlgCycles(
refProvider: SepticReferenceProvider,
cnfg: SepticCnfg,
doc: ITextDocument
): Diagnostic[] {
const diagnostics: Diagnostic[] = [];
for (let cycle of refProvider.getCycles()) {
for (let cycle of cnfg.findCycles()) {
let cycleStr = [...cycle.nodes, cycle.nodes[0]]
.map((node) => node.name)
.join("->");
for (let node of cycle.nodes) {
let xvrRefs = refProvider.getXvrRefs(node.name);
if (!xvrRefs) {
continue;
}
let xvrObjs = xvrRefs.filter((xvr) => xvr.obj?.isType("CalcPvr"));
if (!xvrObjs.length) {
const relatedInformation: DiagnosticRelatedInformation[] = [];
const rootNode = cycle.nodes[0];
const rootObject = cnfg.getObject(rootNode.name, "CalcPvr");
if (!rootObject) {
continue;
}
for (let node of cycle.nodes.slice(1)) {
let nodeObj = cnfg.getObject(node.name, "CalcPvr");
if (!nodeObj) {
continue;
}
if (xvrObjs[0].location.uri === doc.uri) {
diagnostics.push(
createDiagnostic(
DiagnosticSeverity.Warning,
{
start: doc.positionAt(xvrObjs[0].location.start),
end: doc.positionAt(xvrObjs[0].location.end),
relatedInformation.push(
DiagnosticRelatedInformation.create(
{
uri: doc.uri,
range: {
start: doc.positionAt(nodeObj.identifier!.start),
end: doc.positionAt(nodeObj.identifier!.end),
},
`Cycle in algs detected for CalcPvr. ${cycleStr}`,
DiagnosticCode.W201
)
);
}
},
`Cycle in algs detected for CalcPvr. ${cycleStr}`
)
);
}
diagnostics.push(
createDiagnostic(
DiagnosticSeverity.Warning,
{
start: doc.positionAt(rootObject.identifier!.start),
end: doc.positionAt(rootObject.identifier!.end),
},
`Cycle in algs detected for CalcPvr. ${cycleStr}`,
DiagnosticCode.W201,
relatedInformation
)
);
}
return diagnostics;
}
3 changes: 0 additions & 3 deletions server/src/septic/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,4 @@ export interface SepticReferenceProvider {
name: string,
validationFunction: RefValidationFunction
): boolean;
getCalcPvrs(): SepticObject[];
detectCycles(): void;
getCycles(): Cycle[];
}
27 changes: 16 additions & 11 deletions server/src/septic/septicCnfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ export class SepticCnfg implements SepticReferenceProvider {
return this.xvrRefs.get(removeSpaces(name));
}

public getObject(
name: string,
type: string | undefined = undefined
): SepticObject | undefined {
let objects = this.objects.filter((obj) => {
return obj.identifier?.name === name;
});
if (type) {
objects = objects.filter((obj) => obj.isType(type));
}
return objects.length ? objects[0] : undefined;
}

public validateRef(
name: string,
validationFunction: RefValidationFunction = defaultRefValidationFunction
Expand Down Expand Up @@ -109,16 +122,8 @@ export class SepticCnfg implements SepticReferenceProvider {
return undefined;
}

public getCalcPvrs(): SepticObject[] {
return this.objects.filter((obj) => obj.isType("CalcPvr"));
}

public getCycles(): Cycle[] {
return this.cycles;
}

public detectCycles(): void {
let calcPvrs = this.getCalcPvrs();
public findCycles(): Cycle[] {
let calcPvrs = this.objects.filter((obj) => obj.isType("CalcPvr"));
let algs: Alg[] = [];
for (let calcPvr of calcPvrs) {
let alg = calcPvr.getAttribute("Alg");
Expand All @@ -131,7 +136,7 @@ export class SepticCnfg implements SepticReferenceProvider {
content: content,
});
}
this.cycles = findAlgCycles(algs);
return findAlgCycles(algs);
}

private extractXvrRefs(): void {
Expand Down
35 changes: 0 additions & 35 deletions server/src/septic/septicScgContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { SepticObject } from "./septicElements";
import { SepticConfigProvider } from "../language-service/septicConfigProvider";
import { SepticCnfg } from "./septicCnfg";
import { Alg, Cycle, findAlgCycles } from "./cycle";

export interface ScgConfig {
outputfile?: string;
Expand Down Expand Up @@ -44,7 +43,6 @@ export interface ScgTemplate {
export class ScgContext implements SepticReferenceProvider {
public name: string;
public filePath: string;
public cycles: Cycle[] = [];

private cnfgProvider: SepticConfigProvider;
private cnfgCache = new Map<string, SepticCnfg | undefined>();
Expand Down Expand Up @@ -149,37 +147,4 @@ export class ScgContext implements SepticReferenceProvider {
}
return xvrObjs;
}

public getCalcPvrs(): SepticObject[] {
let calcPvrs: SepticObject[] = [];
for (const file of this.files) {
let cnfg = this.cnfgCache.get(file);
if (!cnfg) {
continue;
}
calcPvrs.push(...cnfg.getCalcPvrs());
}
return calcPvrs;
}

public getCycles(): Cycle[] {
return this.cycles;
}

public detectCycles(): void {
let calcPvrs = this.getCalcPvrs();
let algs: Alg[] = [];
for (let calcPvr of calcPvrs) {
let alg = calcPvr.getAttribute("Alg");
let content = alg?.getAttrValue()?.getValue();
if (!content || !calcPvr.identifier?.name) {
continue;
}
algs.push({
calcPvrName: calcPvr.identifier?.name!,
content: content,
});
}
this.cycles = findAlgCycles(algs);
}
}
2 changes: 0 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ let hasDiagnosticRelatedInformationCapability = false;

async function publishDiagnosticsContext(context: ScgContext): Promise<void> {
await context.load();
context.detectCycles();
const diagnosticsPromises = context.files.map(async (file) => {
let doc = await documentProvider.getDocument(file);
if (!doc) {
Expand All @@ -70,7 +69,6 @@ async function publishDiagnosticsContext(context: ScgContext): Promise<void> {
}

async function publishDiagnosticsCnfg(cnfg: SepticCnfg): Promise<void> {
cnfg.detectCycles();
let doc = await documentProvider.getDocument(cnfg.uri);
if (!doc) {
return;
Expand Down
5 changes: 1 addition & 4 deletions server/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ describe("Test validation of cycles", () => {

const doc = new MockDocument(text);
const cnfg = parseSeptic(doc.getText());
cnfg.detectCycles();
let diag = validateAlgCycles(cnfg, doc);
expect(diag.length).to.equal(0);
});
Expand All @@ -261,7 +260,6 @@ describe("Test validation of cycles", () => {

const doc = new MockDocument(text);
const cnfg = parseSeptic(doc.getText());
cnfg.detectCycles();
let diag = validateAlgCycles(cnfg, doc);
expect(diag.length).to.equal(1);
});
Expand All @@ -278,8 +276,7 @@ describe("Test validation of cycles", () => {

const doc = new MockDocument(text);
const cnfg = parseSeptic(doc.getText());
cnfg.detectCycles();
let diag = validateAlgCycles(cnfg, doc);
expect(diag.length).to.equal(2);
expect(diag.length).to.equal(1);
});
});

0 comments on commit 797cc61

Please sign in to comment.