Skip to content

Commit 1342744

Browse files
committed
Add diagnostics for duplicate calcpvrs
1 parent 1f7a5b4 commit 1342744

4 files changed

Lines changed: 54 additions & 11 deletions

File tree

server/src/language-service/diagnosticsProvider.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,12 +906,26 @@ function validateCalcPvrIdentifierReferences(
906906
refProvider: SepticReferenceProvider,
907907
doc: ITextDocument
908908
): Diagnostic[] {
909+
const diagnostics: Diagnostic[] = [];
910+
if (refProvider.validateRef(obj.identifier!.name, hasDuplicateCalcPvrRef)) {
911+
diagnostics.push(
912+
createDiagnostic(
913+
DiagnosticSeverity.Warning,
914+
{
915+
start: doc.positionAt(obj.identifier!.start),
916+
end: doc.positionAt(obj.identifier!.end),
917+
},
918+
`Duplicate CalcPvr with name: ${obj.identifier!.name}`,
919+
DiagnosticCode.duplicate
920+
)
921+
);
922+
}
909923
const referenceToEvr = refProvider.validateRef(
910924
obj.identifier!.name,
911925
hasReferenceToEvr
912926
);
913927
if (referenceToEvr) {
914-
return [];
928+
return diagnostics;
915929
}
916930
const referenceToXvr = refProvider.validateRef(
917931
obj.identifier!.name,
@@ -926,7 +940,7 @@ function validateCalcPvrIdentifierReferences(
926940
const code = referenceToXvr
927941
? DiagnosticCode.invalidReference
928942
: DiagnosticCode.missingReference;
929-
return [
943+
diagnostics.push(
930944
createDiagnostic(
931945
severity,
932946
{
@@ -936,7 +950,8 @@ function validateCalcPvrIdentifierReferences(
936950
message,
937951
code
938952
),
939-
];
953+
);
954+
return diagnostics;
940955
}
941956

942957
function validateUAApplReferences(
@@ -973,6 +988,19 @@ const hasReferenceToEvr: RefValidationFunction = (refs: SepticReference[]) => {
973988
return false;
974989
};
975990

991+
const hasDuplicateCalcPvrRef: RefValidationFunction = (refs: SepticReference[]) => {
992+
let seen = false;
993+
for (const ref of refs) {
994+
if (ref.obj?.isType("CalcPvr")) {
995+
if (seen) {
996+
return true;
997+
}
998+
seen = true;
999+
}
1000+
}
1001+
return false;
1002+
};
1003+
9761004
export function validateEvrReferences(
9771005
obj: SepticObject,
9781006
refProvider: SepticReferenceProvider,

server/src/septic/septicCnfg.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ export function extractReferencesFromObj(obj: SepticObject): SepticReference[] {
241241
}
242242

243243
if (objectDef.refs.identifier && obj.identifier) {
244-
const isObjRef = obj.isXvr || obj.isOpcXvr;
245-
const isCalcPvr = obj.isType("CalcPvr");
244+
const isObjRef = obj.isXvr || obj.isOpcXvr || obj.isType("CalcPvr");
246245
const ref: SepticReference = createSepticReference(
247246
obj.identifier.name,
248247
{
@@ -253,9 +252,7 @@ export function extractReferencesFromObj(obj: SepticObject): SepticReference[] {
253252
isObjRef ? obj : undefined,
254253
isObjRef
255254
? ReferenceType.xvr
256-
: isCalcPvr
257-
? ReferenceType.calc
258-
: ReferenceType.identifier
255+
: ReferenceType.identifier
259256
);
260257
xvrRefs.push(ref);
261258
}

server/src/test/diagnostics.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,26 @@ describe("Test validation of object references", () => {
13591359
);
13601360
expect(diagFilterd.length).to.equal(0);
13611361
});
1362+
it("Expect diagnostics for duplicate calcpvr", () => {
1363+
const text = `
1364+
CalcPvr: Test
1365+
1366+
CalcPvr: Test
1367+
`;
1368+
const doc = new MockDocument(text);
1369+
const cnfg = parseSepticSync(doc.getText());
1370+
const objectInfo = metaInfoProvider.getObject("CalcPvr");
1371+
const diag = validateObjectReferences(
1372+
cnfg.objects[0],
1373+
doc,
1374+
cnfg,
1375+
objectInfo!
1376+
);
1377+
const diagFilterd = diag.filter(
1378+
(d) => d.code === DiagnosticCode.duplicate
1379+
);
1380+
expect(diagFilterd.length).to.equal(1);
1381+
});
13621382
});
13631383

13641384
describe("Test validation of object structure", () => {

server/src/test/references.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getDeclaration,
55
getReferences,
66
} from "../language-service/referenceProvider";
7-
import { Attribute, AttributeValue, extractReferencesFromObj, Identifier, parseSepticSync, ReferenceType, SepticObject, SepticTokenType } from "../septic";
7+
import { Attribute, AttributeValue, extractReferencesFromObj, Identifier, parseSepticSync, SepticObject, SepticTokenType } from "../septic";
88
import { MockDocument } from "./util";
99

1010
describe("Test extraction of refs from config file", () => {
@@ -289,8 +289,6 @@ describe("Test getReferences", () => {
289289
calcPvr.addAttribute(algAttribute);
290290
const refs = extractReferencesFromObj(calcPvr);
291291
expect(refs.length).to.equal(1);
292-
expect(refs[0].type).to.equal(ReferenceType.calc);
293-
294292
});
295293
it("Expect references from alg that contain other jinja expressions", () => {
296294
const alg = `"maxselection(6,{% for Wellname in (wells | unpack('Wellname'))[:5] %} {{ Wellname }}Priority,{% endfor %} -1,{% for Wellname in (wells | unpack('Wellname'))[:5] %} {{ Wellname }}Priority < {{ CurrentWell }}Priority,{% endfor %} 1)"`

0 commit comments

Comments
 (0)