Skip to content

Commit b6ac712

Browse files
author
Alexander Chen
committed
Added diagnostic and CodeAction to use let to replace with
Signed-off-by: Alexander Chen <alchen@redhat.com>
1 parent 3ff7394 commit b6ac712

File tree

7 files changed

+143
-25
lines changed

7 files changed

+143
-25
lines changed

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/ls/commons/CodeActionFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ public static CodeAction replace(String title, Range range, String replaceText,
106106
return replace(title, Collections.singletonList(replace), document, diagnostic);
107107
}
108108

109+
@SuppressWarnings("null")
110+
public static CodeAction replace(String title, List<Range> ranges, String replaceText, TextDocumentItem document,
111+
Diagnostic diagnostic) {
112+
List<TextEdit> edits = null;
113+
for (Range range : ranges) {
114+
edits.add(new TextEdit(range, replaceText));
115+
}
116+
return replace(title, edits, document, diagnostic);
117+
}
118+
109119
public static CodeAction replace(String title, List<TextEdit> replace, TextDocumentItem document,
110120
Diagnostic diagnostic) {
111121

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import java.util.concurrent.CompletableFuture;
2424
import java.util.logging.Level;
2525
import java.util.logging.Logger;
26+
import java.util.stream.Collectors;
2627

2728
import org.eclipse.lsp4j.CodeAction;
2829
import org.eclipse.lsp4j.CodeActionContext;
2930
import org.eclipse.lsp4j.Diagnostic;
3031
import org.eclipse.lsp4j.Position;
3132
import org.eclipse.lsp4j.Range;
33+
import org.eclipse.lsp4j.TextEdit;
3234

3335
import com.google.gson.JsonObject;
3436
import com.redhat.qute.ls.commons.BadLocationException;
@@ -42,6 +44,7 @@
4244
import com.redhat.qute.parser.template.NodeKind;
4345
import com.redhat.qute.parser.template.Section;
4446
import com.redhat.qute.parser.template.Template;
47+
import com.redhat.qute.parser.template.sections.WithSection;
4548
import com.redhat.qute.project.QuteProject;
4649
import com.redhat.qute.services.commands.QuteClientCommandConstants;
4750
import com.redhat.qute.services.diagnostics.QuteErrorCode;
@@ -50,7 +53,7 @@
5053

5154
/**
5255
* Qute code actions support.
53-
*
56+
*
5457
* @author Angelo ZERR
5558
*
5659
*/
@@ -72,6 +75,8 @@ class QuteCodeActions {
7275

7376
private static final String EXCLUDED_VALIDATION_TITLE = "Exclude this file from validation.";
7477

78+
private static final String QUTE_DEPRICATED_WITH_SECTION = "Replace `#with` with `#let`.";
79+
7580
public CompletableFuture<List<CodeAction>> doCodeActions(Template template, CodeActionContext context, Range range,
7681
SharedSettings sharedSettings) {
7782
List<CodeAction> codeActions = new ArrayList<>();
@@ -109,6 +114,15 @@ public CompletableFuture<List<CodeAction>> doCodeActions(Template template, Code
109114
// Create `undefinedTag`"
110115
doCodeActionsForUndefinedSectionTag(template, diagnostic, codeActions);
111116
break;
117+
case NotRecommendedWithSection:
118+
// The following Qute template:
119+
// {#with }
120+
//
121+
// will provide a quickfix like:
122+
//
123+
// Replace `with` with `let`.
124+
doCodeActionsForNotRecommendedWithSection(template, diagnostic, codeActions);
125+
break;
112126
default:
113127
break;
114128
}
@@ -186,15 +200,62 @@ private static void doCodeActionToDisableValidation(Template template, List<Diag
186200
codeActions.add(disableValidationForTemplateQuickFix);
187201
}
188202

203+
/**
204+
* Create CodeAction for deprecated `with` Qute syntax.
205+
*
206+
* @param template the Qute template.
207+
* @param diagnostic the diagnostic list that this CodeAction will fix.
208+
* @param codeActions the list of CodeActions to perform.
209+
* @throws BadLocationException
210+
*
211+
*/
212+
private void doCodeActionsForNotRecommendedWithSection(Template template, Diagnostic diagnostic,
213+
List<CodeAction> codeActions) {
214+
Range withSectionRange = diagnostic.getRange();
215+
try {
216+
int withSectionStart = template.offsetAt(withSectionRange.getStart());
217+
WithSection withSection = (WithSection) template.findNodeAt(withSectionStart + 1);
218+
String javaResourceClassName = withSection.getObjectParameter().getName();
219+
List<Node> withExpressions = withSection.getChildren().stream().filter(s -> s instanceof Expression)
220+
.collect(Collectors.toList());
221+
List<String> letExpressionParameterList = new ArrayList<String>();
222+
for (Node expression : withExpressions) {
223+
letExpressionParameterList.add(MessageFormat.format("{1}={0}.{1}", javaResourceClassName,
224+
((Expression) expression).getContent()));
225+
}
226+
String letExpressionParameters = String.join(" ", letExpressionParameterList);
227+
228+
List<TextEdit> edits = new ArrayList<TextEdit>();
229+
230+
String withSectionOpenReplacementText = MessageFormat.format("#let {0}", letExpressionParameters);
231+
Range withSectionOpen = new Range(template.positionAt(withSection.getStartTagNameOpenOffset()),
232+
template.positionAt(withSection.getStartTagCloseOffset()));
233+
TextEdit withSectionOpenEdit = new TextEdit(withSectionOpen, withSectionOpenReplacementText);
234+
edits.add(withSectionOpenEdit);
235+
236+
String withSectionCloseReplacementText = "/let";
237+
Range withSectionClose = new Range(template.positionAt(withSection.getEndTagNameOpenOffset()),
238+
template.positionAt(withSection.getEndTagCloseOffset()));
239+
TextEdit withSectionCloseEdit = new TextEdit(withSectionClose, withSectionCloseReplacementText);
240+
edits.add(withSectionCloseEdit);
241+
242+
CodeAction replaceWithSection = CodeActionFactory.replace(QUTE_DEPRICATED_WITH_SECTION, edits,
243+
template.getTextDocument(), diagnostic);
244+
codeActions.add(replaceWithSection);
245+
} catch (BadLocationException e) {
246+
return;
247+
}
248+
}
249+
189250
/**
190251
* Create the configuration update (done on client side) quick fix.
191-
*
252+
*
192253
* @param title the displayed name of the QuickFix.
193254
* @param sectionName the section name of the settings to update.
194255
* @param item the section value of the settings to update.
195256
* @param editType the configuration edit type.
196257
* @param diagnostic the diagnostic list that this CodeAction will fix.
197-
*
258+
*
198259
* @return the configuration update (done on client side) quick fix.
199260
*/
200261
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: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.redhat.qute.parser.template.Template;
5555
import com.redhat.qute.parser.template.sections.IncludeSection;
5656
import com.redhat.qute.parser.template.sections.LoopSection;
57+
import com.redhat.qute.parser.template.sections.WithSection;
5758
import com.redhat.qute.project.JavaMemberResult;
5859
import com.redhat.qute.project.QuteProject;
5960
import com.redhat.qute.project.datamodel.JavaDataModelCache;
@@ -246,6 +247,9 @@ private void validateDataModel(Node parent, Template template, ResolvingJavaType
246247
case INCLUDE:
247248
validateIncludeSection((IncludeSection) section, diagnostics);
248249
break;
250+
case WITH:
251+
validateWithSection((WithSection) section, diagnostics);
252+
break;
249253
default:
250254
validateSectionTag(section, template, resolvingJavaTypeContext, diagnostics);
251255
}
@@ -345,6 +349,22 @@ private static void validateIncludeSection(IncludeSection includeSection, List<D
345349
}
346350
}
347351

352+
/**
353+
* Report that `#with` section is deprecated.
354+
*
355+
* @param withSection the with section
356+
* @param diagnostics the diagnostics to fill
357+
*/
358+
private static void validateWithSection(WithSection withSection, List<Diagnostic> diagnostics) {
359+
// List<DiagnosticTag> tags = Collections.singletonList(DiagnosticTag.Deprecated);
360+
Range range = QutePositionUtility.createRange(withSection);
361+
// Diagnostic diagnostic = createDiagnosticWithTags(range, DiagnosticSeverity.Warning,
362+
// QuteErrorCode.DeprecatedWithSection, tags);
363+
Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Warning,
364+
QuteErrorCode.NotRecommendedWithSection);
365+
diagnostics.add(diagnostic);
366+
}
367+
348368
private ResolvedJavaTypeInfo validateExpression(Expression expression, Section ownerSection, Template template,
349369
ResolutionContext resolutionContext, ResolvingJavaTypeContext resolvingJavaTypeContext,
350370
List<Diagnostic> diagnostics) {
@@ -391,7 +411,7 @@ private ResolvedJavaTypeInfo validateExpressionParts(Parts parts, Section ownerS
391411
ResolvedJavaTypeInfo resolvedJavaType = null;
392412
String namespace = null;
393413
for (int i = 0; i < parts.getChildCount(); i++) {
394-
Part current = ((Part) parts.getChild(i));
414+
Part current = (parts.getChild(i));
395415

396416
if (current.isLast()) {
397417
// It's the last part, check if it is not ended with '.'
@@ -586,7 +606,7 @@ private ResolvedJavaTypeInfo validateObjectPart(ObjectPart objectPart, Section o
586606

587607
/**
588608
* Validate the given property, method part.
589-
*
609+
*
590610
* @param part the property, method part to validate.
591611
* @param ownerSection the owner section and null otherwise.
592612
* @param template the template.
@@ -596,7 +616,7 @@ private ResolvedJavaTypeInfo validateObjectPart(ObjectPart objectPart, Section o
596616
* @param iterableOfType the iterable of type.
597617
* @param diagnostics the diagnostic list to fill.
598618
* @param resolvingJavaTypeContext the resolving Java type context.
599-
*
619+
*
600620
* @return the Java type returned by the member part and null otherwise.
601621
*/
602622
private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection, Template template,
@@ -617,7 +637,7 @@ private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection,
617637

618638
/**
619639
* Validate the given property part.
620-
*
640+
*
621641
* @param part the property part to validate.
622642
* @param ownerSection the owner section and null otherwise.
623643
* @param template the template.
@@ -627,7 +647,7 @@ private ResolvedJavaTypeInfo validateMemberPart(Part part, Section ownerSection,
627647
* @param iterableOfType the iterable of type.
628648
* @param diagnostics the diagnostic list to fill.
629649
* @param resolvingJavaTypeContext the resolving Java type context.
630-
*
650+
*
631651
* @return the Java type returned by the member part and null otherwise.
632652
*/
633653
private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section ownerSection, Template template,
@@ -650,7 +670,7 @@ private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section own
650670

651671
/**
652672
* Validate the given method part.
653-
*
673+
*
654674
* @param part the method part to validate.
655675
* @param ownerSection the owner section and null otherwise.
656676
* @param template the template.
@@ -660,7 +680,7 @@ private ResolvedJavaTypeInfo validatePropertyPart(PropertyPart part, Section own
660680
* @param iterableOfType the iterable of type.
661681
* @param diagnostics the diagnostic list to fill.
662682
* @param resolvingJavaTypeContext the resolving Java type context.
663-
*
683+
*
664684
* @return the Java type returned by the member part and null otherwise.
665685
*/
666686
private ResolvedJavaTypeInfo validateMethodPart(MethodPart methodPart, Section ownerSection, Template template,

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
import static com.redhat.qute.services.diagnostics.QuteDiagnosticContants.DIAGNOSTIC_DATA_TAG;
1717
import static com.redhat.qute.services.diagnostics.QuteDiagnosticContants.QUTE_SOURCE;
1818

19+
import java.util.List;
20+
1921
import org.eclipse.lsp4j.Diagnostic;
2022
import org.eclipse.lsp4j.DiagnosticSeverity;
23+
import org.eclipse.lsp4j.DiagnosticTag;
2124
import org.eclipse.lsp4j.Range;
2225

2326
import com.google.gson.JsonObject;
2427

2528
/**
2629
* Diagnostic factory.
27-
*
30+
*
2831
* @author Angelo ZERR
2932
*
3033
*/
@@ -55,4 +58,13 @@ public static Diagnostic createDiagnostic(Range range, String message, Diagnosti
5558
errorCode != null ? errorCode.getCode() : null);
5659
return diagnostic;
5760
}
61+
62+
public static Diagnostic createDiagnosticWithTags(Range range, DiagnosticSeverity severity,
63+
IQuteErrorCode errorCode, List<DiagnosticTag> tags, Object... arguments) {
64+
String message = errorCode.getMessage(arguments);
65+
Diagnostic diagnostic = new Diagnostic(range, message, severity, QUTE_SOURCE,
66+
errorCode != null ? errorCode.getCode() : null);
67+
diagnostic.setTags(tags);
68+
return diagnostic;
69+
}
5870
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ private QuteDiagnosticContants() {
2424
public static final String DIAGNOSTIC_DATA_ITERABLE = "iterable";
2525

2626
public static final String DIAGNOSTIC_DATA_TAG = "tag";
27+
2728
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public enum QuteErrorCode implements IQuteErrorCode {
4545

4646
UndefinedSectionTag("No section helper found for `{0}`."), //
4747

48-
SyntaxError("Syntax error: `{0}`.");
48+
SyntaxError("Syntax error: `{0}`."),
49+
50+
// Error code for deprecated #with section
51+
NotRecommendedWithSection("`with` is not recommended. Use `let` instead.");
4952

5053
private final String rawMessage;
5154

qute.ls/com.redhat.qute.ls/src/test/java/com/redhat/qute/services/diagnostics/QuteDiagnosticsInExpressionWithWithSectionTest.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* Test with #with section
26-
*
26+
*
2727
* @author Angelo ZERR
2828
*
2929
*/
@@ -34,23 +34,28 @@ public void undefinedObject() throws Exception {
3434
String template = "{#with item}\r\n" + //
3535
"{/with}";
3636

37-
Diagnostic d = d(0, 7, 0, 11, QuteErrorCode.UndefinedVariable, "`item` cannot be resolved to a variable.",
37+
Diagnostic d1 = d(0, 7, 0, 11, QuteErrorCode.UndefinedVariable, "`item` cannot be resolved to a variable.",
3838
DiagnosticSeverity.Warning);
39-
d.setData(DiagnosticDataFactory.createUndefinedVariableData("item", false));
39+
d1.setData(DiagnosticDataFactory.createUndefinedVariableData("item", false));
4040

41-
testDiagnosticsFor(template, d);
42-
testCodeActionsFor(template, d, //
43-
ca(d, te(0, 0, 0, 0, "{@java.lang.String item}\r\n")));
41+
Diagnostic d2 = d(0, 0, 1, 7, QuteErrorCode.NotRecommendedWithSection,
42+
"`with` is not recommended. Use `let` instead.", DiagnosticSeverity.Warning);
43+
44+
testDiagnosticsFor(template, d1, d2);
45+
testCodeActionsFor(template, d1, //
46+
ca(d1, te(0, 0, 0, 0, "{@java.lang.String item}\r\n")));
4447
}
4548

4649
@Test
47-
public void noError() throws Exception {
50+
public void singleSection() throws Exception {
4851
String template = "{@org.acme.Item item}\r\n" + //
4952
"{#with item}\r\n" + //
5053
" <h1>{name}</h1> \r\n" + //
5154
" <p>{price}</p> \r\n" + //
5255
"{/with}";
53-
testDiagnosticsFor(template);
56+
Diagnostic d = d(1, 0, 4, 7, QuteErrorCode.NotRecommendedWithSection,
57+
"`with` is not recommended. Use `let` instead.", DiagnosticSeverity.Warning);
58+
testDiagnosticsFor(template, d);
5459
}
5560

5661
@Test
@@ -68,13 +73,19 @@ public void nested() throws Exception {
6873
" {/with}\r\n" + //
6974
"{/with}";
7075

71-
Diagnostic d = d(6, 5, 6, 12, QuteErrorCode.UndefinedVariable, "`average` cannot be resolved to a variable.",
76+
Diagnostic d1 = d(1, 0, 11, 7, QuteErrorCode.NotRecommendedWithSection,
77+
"`with` is not recommended. Use `let` instead.", DiagnosticSeverity.Warning);
78+
79+
Diagnostic d2 = d(4, 2, 10, 9, QuteErrorCode.NotRecommendedWithSection,
80+
"`with` is not recommended. Use `let` instead.", DiagnosticSeverity.Warning);
81+
82+
Diagnostic d3 = d(6, 5, 6, 12, QuteErrorCode.UndefinedVariable, "`average` cannot be resolved to a variable.",
7283
DiagnosticSeverity.Warning);
73-
d.setData(DiagnosticDataFactory.createUndefinedVariableData("average", false));
84+
d3.setData(DiagnosticDataFactory.createUndefinedVariableData("average", false));
7485

75-
testDiagnosticsFor(template, d);
76-
testCodeActionsFor(template, d, //
77-
ca(d, te(0, 0, 0, 0, "{@java.lang.String average}\r\n")));
86+
testDiagnosticsFor(template, d1, d2, d3);
87+
testCodeActionsFor(template, d3, //
88+
ca(d3, te(0, 0, 0, 0, "{@java.lang.String average}\r\n")));
7889

7990
}
8091
}

0 commit comments

Comments
 (0)