Skip to content

Commit d9b8f83

Browse files
Alexander Chenangelozerr
authored andcommitted
Replaced UndefinedVariable by adding validation severity setting for Qute UndefinedObject error
Signed-off-by: Alexander Chen <alchen@redhat.com>
1 parent 1cd7821 commit d9b8f83

17 files changed

+320
-101
lines changed

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/services/QuteCodeActions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
/**
5252
* Qute code actions support.
53-
*
53+
*
5454
* @author Angelo ZERR
5555
*
5656
*/
@@ -91,13 +91,13 @@ public CompletableFuture<List<CodeAction>> doCodeActions(Template template, Code
9191
QuteErrorCode errorCode = QuteErrorCode.getErrorCode(diagnostic.getCode());
9292
if (errorCode != null) {
9393
switch (errorCode) {
94-
case UndefinedVariable:
94+
case UndefinedObject:
9595
// The following Qute template:
96-
// {undefinedVariable}
96+
// {undefinedObject}
9797
//
9898
// will provide a quickfix like:
9999
//
100-
// Declare `undefinedVariable` with parameter declaration."
100+
// Declare `undefinedObject` with parameter declaration."
101101
doCodeActionsForUndefinedVariable(template, diagnostic, codeActions);
102102
break;
103103
case UndefinedSectionTag:
@@ -188,13 +188,13 @@ private static void doCodeActionToDisableValidation(Template template, List<Diag
188188

189189
/**
190190
* Create the configuration update (done on client side) quick fix.
191-
*
191+
*
192192
* @param title the displayed name of the QuickFix.
193193
* @param sectionName the section name of the settings to update.
194194
* @param item the section value of the settings to update.
195195
* @param editType the configuration edit type.
196196
* @param diagnostic the diagnostic list that this CodeAction will fix.
197-
*
197+
*
198198
* @return the configuration update (done on client side) quick fix.
199199
*/
200200
private static CodeAction createConfigurationUpdateCodeAction(String title, String scopeUri, String sectionName,

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/services/QuteDiagnostics.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public List<Diagnostic> doDiagnostics(Template template, QuteValidationSettings
172172
List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
173173
try {
174174
diagnosticsForSyntax.validateWithRealQuteParser(template, diagnostics);
175-
validateDataModel(template, template, resolvingJavaTypeContext, new ResolutionContext(), diagnostics);
175+
validateDataModel(template, template, validationSettings, resolvingJavaTypeContext, new ResolutionContext(),
176+
diagnostics);
176177
} catch (CancellationException e) {
177178
throw e;
178179
} catch (Exception e) {
@@ -182,8 +183,9 @@ public List<Diagnostic> doDiagnostics(Template template, QuteValidationSettings
182183
return diagnostics;
183184
}
184185

185-
private void validateDataModel(Node parent, Template template, ResolvingJavaTypeContext resolvingJavaTypeContext,
186-
ResolutionContext currentContext, List<Diagnostic> diagnostics) {
186+
private void validateDataModel(Node parent, Template template, QuteValidationSettings validationSettings,
187+
ResolvingJavaTypeContext resolvingJavaTypeContext, ResolutionContext currentContext,
188+
List<Diagnostic> diagnostics) {
187189
ResolutionContext previousContext = currentContext;
188190
List<Node> children = parent.getChildren();
189191
for (Node node : children) {
@@ -223,8 +225,8 @@ private void validateDataModel(Node parent, Template template, ResolvingJavaType
223225
for (Parameter parameter : parameters) {
224226
Expression expression = parameter.getJavaTypeExpression();
225227
if (expression != null) {
226-
ResolvedJavaTypeInfo result = validateExpression(expression, section, template, previousContext,
227-
resolvingJavaTypeContext, diagnostics);
228+
ResolvedJavaTypeInfo result = validateExpression(expression, section, template,
229+
validationSettings, previousContext, resolvingJavaTypeContext, diagnostics);
228230
switch (section.getSectionKind()) {
229231
case FOR:
230232
case EACH:
@@ -252,13 +254,14 @@ private void validateDataModel(Node parent, Template template, ResolvingJavaType
252254
break;
253255
}
254256
case Expression: {
255-
validateExpression((Expression) node, null, template, previousContext, resolvingJavaTypeContext,
256-
diagnostics);
257+
validateExpression((Expression) node, null, template, validationSettings, previousContext,
258+
resolvingJavaTypeContext, diagnostics);
257259
break;
258260
}
259261
default:
260262
}
261-
validateDataModel(node, template, resolvingJavaTypeContext, currentContext, diagnostics);
263+
validateDataModel(node, template, validationSettings, resolvingJavaTypeContext, currentContext,
264+
diagnostics);
262265
}
263266
}
264267

@@ -346,8 +349,8 @@ private static void validateIncludeSection(IncludeSection includeSection, List<D
346349
}
347350

348351
private ResolvedJavaTypeInfo validateExpression(Expression expression, Section ownerSection, Template template,
349-
ResolutionContext resolutionContext, ResolvingJavaTypeContext resolvingJavaTypeContext,
350-
List<Diagnostic> diagnostics) {
352+
QuteValidationSettings validationSettings, ResolutionContext resolutionContext,
353+
ResolvingJavaTypeContext resolvingJavaTypeContext, List<Diagnostic> diagnostics) {
351354
try {
352355
String projectUri = template.getProjectUri();
353356
String literalJavaType = expression.getLiteralJavaType();
@@ -372,7 +375,7 @@ private ResolvedJavaTypeInfo validateExpression(Expression expression, Section o
372375
if (expressionChild.getKind() == NodeKind.ExpressionParts) {
373376
Parts parts = (Parts) expressionChild;
374377
resolvedJavaType = validateExpressionParts(parts, ownerSection, template, projectUri,
375-
resolutionContext, resolvingJavaTypeContext, diagnostics);
378+
validationSettings, resolutionContext, resolvingJavaTypeContext, diagnostics);
376379
}
377380
}
378381
return resolvedJavaType;
@@ -386,12 +389,12 @@ private ResolvedJavaTypeInfo validateExpression(Expression expression, Section o
386389
}
387390

388391
private ResolvedJavaTypeInfo validateExpressionParts(Parts parts, Section ownerSection, Template template,
389-
String projectUri, ResolutionContext resolutionContext, ResolvingJavaTypeContext resolvingJavaTypeContext,
390-
List<Diagnostic> diagnostics) {
392+
String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext,
393+
ResolvingJavaTypeContext resolvingJavaTypeContext, List<Diagnostic> diagnostics) {
391394
ResolvedJavaTypeInfo resolvedJavaType = null;
392395
String namespace = null;
393396
for (int i = 0; i < parts.getChildCount(); i++) {
394-
Part current = ((Part) parts.getChild(i));
397+
Part current = parts.getChild(i);
395398

396399
if (current.isLast()) {
397400
// It's the last part, check if it is not ended with '.'
@@ -423,7 +426,7 @@ private ResolvedJavaTypeInfo validateExpressionParts(Parts parts, Section ownerS
423426
case Object: {
424427
ObjectPart objectPart = (ObjectPart) current;
425428
resolvedJavaType = validateObjectPart(namespace, objectPart, ownerSection, template, projectUri,
426-
resolutionContext, diagnostics, resolvingJavaTypeContext);
429+
validationSettings, resolutionContext, diagnostics, resolvingJavaTypeContext);
427430
if (resolvedJavaType == null) {
428431
// The Java type of the object part cannot be resolved, stop the validation of
429432
// property, method.
@@ -450,8 +453,8 @@ private ResolvedJavaTypeInfo validateExpressionParts(Parts parts, Section ownerS
450453
}
451454
}
452455

453-
resolvedJavaType = validateMemberPart(current, ownerSection, template, projectUri, resolutionContext,
454-
resolvedJavaType, iter, diagnostics, resolvingJavaTypeContext);
456+
resolvedJavaType = validateMemberPart(current, ownerSection, template, projectUri, validationSettings,
457+
resolutionContext, resolvedJavaType, iter, diagnostics, resolvingJavaTypeContext);
455458
if (resolvedJavaType == null) {
456459
// The Java type of the previous part cannot be resolved, stop the validation of
457460
// followings property, method.
@@ -515,7 +518,8 @@ private String validateNamespace(NamespacePart namespacePart, String projectUri,
515518
}
516519

517520
private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart objectPart, Section ownerSection,
518-
Template template, String projectUri, ResolutionContext resolutionContext, List<Diagnostic> diagnostics,
521+
Template template, String projectUri, QuteValidationSettings validationSettings,
522+
ResolutionContext resolutionContext, List<Diagnostic> diagnostics,
519523
ResolvingJavaTypeContext resolvingJavaTypeContext) {
520524
JavaMemberInfo javaMember = resolutionContext.findMemberWithObject(objectPart.getPartName(), projectUri);
521525
if (javaMember != null) {
@@ -540,8 +544,12 @@ private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart obj
540544
}
541545

542546
// ex : {item} --> undefined variable
547+
DiagnosticSeverity severity = validationSettings.getUndefinedObject().getDiagnosticSeverity();
548+
if (severity == null) {
549+
return null;
550+
}
543551
Range range = QutePositionUtility.createRange(objectPart);
544-
Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Warning, QuteErrorCode.UndefinedVariable,
552+
Diagnostic diagnostic = createDiagnostic(range, severity, QuteErrorCode.UndefinedObject,
545553
objectPart.getPartName());
546554
// Create data information helpful for code action
547555
diagnostic.setData(DiagnosticDataFactory.createUndefinedVariableData(objectPart.getPartName(),
@@ -585,7 +593,7 @@ private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart obj
585593

586594
/**
587595
* Validate the given property, method part.
588-
*
596+
*
589597
* @param part the property, method part to validate.
590598
* @param ownerSection the owner section and null otherwise.
591599
* @param template the template.
@@ -595,18 +603,18 @@ private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart obj
595603
* @param iterableOfType the iterable of type.
596604
* @param diagnostics the diagnostic list to fill.
597605
* @param resolvingJavaTypeContext the resolving Java type context.
598-
*
606+
*
599607
* @return the Java type returned by the member part and null otherwise.
600608
*/
601609
private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection, Template template,
602-
String projectUri, ResolutionContext resolutionContext, ResolvedJavaTypeInfo baseType,
603-
ResolvedJavaTypeInfo iterableOfType, List<Diagnostic> diagnostics,
610+
String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext,
611+
ResolvedJavaTypeInfo baseType, ResolvedJavaTypeInfo iterableOfType, List<Diagnostic> diagnostics,
604612
ResolvingJavaTypeContext resolvingJavaTypeContext) {
605613
if (part.getPartKind() == PartKind.Method) {
606614
// Validate method part
607615
// ex : {foo.method(1, 2)}
608-
return validateMethodPart((MethodPart) part, ownerSection, template, projectUri, resolutionContext,
609-
baseType, iterableOfType, diagnostics, resolvingJavaTypeContext);
616+
return validateMethodPart((MethodPart) part, ownerSection, template, projectUri, validationSettings,
617+
resolutionContext, baseType, iterableOfType, diagnostics, resolvingJavaTypeContext);
610618
}
611619
// Validate property part
612620
// ex : {foo.property}
@@ -616,7 +624,7 @@ private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection,
616624

617625
/**
618626
* Validate the given property part.
619-
*
627+
*
620628
* @param part the property part to validate.
621629
* @param ownerSection the owner section and null otherwise.
622630
* @param template the template.
@@ -626,7 +634,7 @@ private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection,
626634
* @param iterableOfType the iterable of type.
627635
* @param diagnostics the diagnostic list to fill.
628636
* @param resolvingJavaTypeContext the resolving Java type context.
629-
*
637+
*
630638
* @return the Java type returned by the member part and null otherwise.
631639
*/
632640
private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section ownerSection, Template template,
@@ -649,7 +657,7 @@ private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section own
649657

650658
/**
651659
* Validate the given method part.
652-
*
660+
*
653661
* @param part the method part to validate.
654662
* @param ownerSection the owner section and null otherwise.
655663
* @param template the template.
@@ -659,12 +667,12 @@ private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section own
659667
* @param iterableOfType the iterable of type.
660668
* @param diagnostics the diagnostic list to fill.
661669
* @param resolvingJavaTypeContext the resolving Java type context.
662-
*
670+
*
663671
* @return the Java type returned by the member part and null otherwise.
664672
*/
665673
private ResolvedJavaTypeInfo validateMethodPart(MethodPart methodPart, Section ownerSection, Template template,
666-
String projectUri, ResolutionContext resolutionContext, ResolvedJavaTypeInfo resolvedJavaType,
667-
ResolvedJavaTypeInfo iter, List<Diagnostic> diagnostics,
674+
String projectUri, QuteValidationSettings validationSettings, ResolutionContext resolutionContext,
675+
ResolvedJavaTypeInfo resolvedJavaType, ResolvedJavaTypeInfo iter, List<Diagnostic> diagnostics,
668676
ResolvingJavaTypeContext resolvingJavaTypeContext) {
669677

670678
// Validate parameters of the method part
@@ -674,7 +682,7 @@ private ResolvedJavaTypeInfo validateMethodPart(MethodPart methodPart, Section o
674682
ResolvedJavaTypeInfo result = null;
675683
Expression expression = parameter.getJavaTypeExpression();
676684
if (expression != null) {
677-
result = validateExpression(expression, ownerSection, template, resolutionContext,
685+
result = validateExpression(expression, ownerSection, template, validationSettings, resolutionContext,
678686
resolvingJavaTypeContext, diagnostics);
679687
}
680688
if (result == null) {
@@ -700,7 +708,7 @@ private ResolvedJavaTypeInfo validateMethodPart(MethodPart methodPart, Section o
700708
String arg = null;
701709
if (namespace != null) {
702710
// ex :{config.getXXXX()}
703-
errorCode = QuteErrorCode.UnkwownNamespaceResolverMethod;
711+
errorCode = QuteErrorCode.UnknownNamespaceResolverMethod;
704712
arg = namespace;
705713
} else {
706714
// ex : {@org.acme.Item item}

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/services/diagnostics/QuteErrorCode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public enum QuteErrorCode implements IQuteErrorCode {
2424
UndefinedNamespace("No namespace resolver found for: `{0}`"), //
2525

2626
// Error code for object, property, method parts
27-
UndefinedVariable("`{0}` cannot be resolved to a variable."), //
27+
UndefinedObject("`{0}` cannot be resolved to an object."), //
2828
UnknownType("`{0}` cannot be resolved to a type."), //
2929
UnknownMethod("`{0}` cannot be resolved or is not a method of `{1}` Java type."), //
30-
UnkwownNamespaceResolverMethod("`{0}` cannot be resolved or is not a method of `{1}` namespace resolver."), //
30+
UnknownNamespaceResolverMethod("`{0}` cannot be resolved or is not a method of `{1}` namespace resolver."), //
3131
InvalidMethodVoid("Invalid `{0}` method of `{1}` : void return is not allowed."), //
3232
InvalidMethodFromObject("Invalid `{0}` method of `{1}` : method from `java.lang.Object` is not allowed."), //
3333
InvalidMethodStatic("Invalid `{0}` method of `{1}` : static method is not allowed."), //

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/settings/QuteValidationSettings.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,25 @@
2727
*/
2828
public class QuteValidationSettings {
2929

30+
public static enum Severity {
31+
ignore, error, warning;
32+
}
33+
3034
public static final QuteValidationSettings DEFAULT;
3135

36+
private static final QuteValidationTypeSettings DEFAULT_UNDEFINED_OBJECT;
37+
3238
static {
39+
DEFAULT_UNDEFINED_OBJECT = new QuteValidationTypeSettings();
40+
DEFAULT_UNDEFINED_OBJECT.setSeverity(Severity.warning.name());
3341
DEFAULT = new QuteValidationSettings();
3442
DEFAULT.updateDefault();
3543
}
3644

3745
private boolean enabled;
3846

47+
private QuteValidationTypeSettings undefinedObject;
48+
3949
private List<String> excluded;
4050

4151
private transient boolean updated;
@@ -90,6 +100,7 @@ private void updateDefault() {
90100
if (updated) {
91101
return;
92102
}
103+
setUndefinedObject(undefinedObject != null ? undefinedObject : DEFAULT_UNDEFINED_OBJECT);
93104
updated = true;
94105
}
95106

@@ -101,6 +112,27 @@ private void updateDefault() {
101112
public void update(QuteValidationSettings newValidation) {
102113
this.setEnabled(newValidation.isEnabled());
103114
this.setExcluded(newValidation.getExcluded());
115+
this.setUndefinedObject(newValidation.getUndefinedObject());
116+
}
117+
118+
/**
119+
* Returns the settings for Qute undefined object validation.
120+
*
121+
* @return the settings for Qute undefined object validation
122+
*/
123+
public QuteValidationTypeSettings getUndefinedObject() {
124+
updateDefault();
125+
return this.undefinedObject;
126+
}
127+
128+
/**
129+
* Set the settings for Qute undefined object validation.
130+
*
131+
* @param undefined the settings for Qute undefined object validation.
132+
*/
133+
public void setUndefinedObject(QuteValidationTypeSettings undefined) {
134+
this.undefinedObject = undefined;
135+
this.updated = false;
104136
}
105137

106138
public boolean canValidate(String templateUri) {
@@ -182,6 +214,7 @@ public int hashCode() {
182214
int result = 1;
183215
result = prime * result + (enabled ? 1231 : 1237);
184216
result = prime * result + ((excluded == null) ? 0 : excluded.hashCode());
217+
result = prime * result + ((undefinedObject == null) ? 0 : undefinedObject.hashCode());
185218
return result;
186219
}
187220

@@ -201,6 +234,12 @@ public boolean equals(Object obj) {
201234
return false;
202235
} else if (!excluded.equals(other.excluded))
203236
return false;
237+
if (undefinedObject == null) {
238+
if (!getUndefinedObject().equals(other.getUndefinedObject())) {
239+
return false;
240+
}
241+
} else if (!undefinedObject.equals(other.undefinedObject))
242+
return false;
204243
return true;
205244
}
206245

0 commit comments

Comments
 (0)