Skip to content

Commit 3db679f

Browse files
committed
Add code examples for missing array dimension
1 parent 38910bd commit 3db679f

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

java/src/processing/mode/java/pdex/JavaHint.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static List<EditorHints.Hint> fromIProblem(IProblem compilerError, ASTNod
2525
);
2626

2727
switch (compilerError.getID()) {
28+
case IProblem.MustDefineEitherDimensionExpressionsOrInitializer:
29+
return getArrDimHints(problemNode);
2830
case IProblem.TypeMismatch:
2931
String providedType = truncateClass(problemArguments[0]);
3032
String requiredType = truncateClass(problemArguments[1]);
@@ -34,6 +36,25 @@ public static List<EditorHints.Hint> fromIProblem(IProblem compilerError, ASTNod
3436
return Collections.emptyList();
3537
}
3638

39+
private static List<EditorHints.Hint> getArrDimHints(ASTNode problemNode) {
40+
List<EditorHints.Hint> hints = new ArrayList<>();
41+
42+
String arrType = problemNode.toString();
43+
String arrName = ((VariableDeclarationFragment) problemNode.getParent().getParent().getParent())
44+
.getName().toString();
45+
String problemTitle = "You have not given the array a certain size.";
46+
47+
// Suggest adding array dimension
48+
JavaHint addDim = new JavaHint(problemTitle,
49+
"You have not given the array a certain size."
50+
);
51+
addDim.addBadCode(arrType + "[] " + arrName + " = new " + arrType + "[];");
52+
addDim.addGoodCode(arrType + "[] " + arrName + " = new " + arrType + "[5];");
53+
hints.add(addDim);
54+
55+
return hints;
56+
}
57+
3758
private static List<EditorHints.Hint> getTypeMismatchHints(String providedType, String requiredType,
3859
ASTNode problemNode) {
3960
List<EditorHints.Hint> hints = new ArrayList<>();
@@ -42,6 +63,7 @@ private static List<EditorHints.Hint> getTypeMismatchHints(String providedType,
4263
String problemTitle = "You are trying to use the " + getVarDescription(requiredType)
4364
+ " " + varName + " as a " + getVarDescription(providedType) + ".";
4465

66+
// Suggest changing variable declaration
4567
JavaHint changeVarDec = new JavaHint(problemTitle,
4668
"You might need to change the variable declaration of "
4769
+ varName + " to type " + providedType + "."
@@ -50,6 +72,7 @@ private static List<EditorHints.Hint> getTypeMismatchHints(String providedType,
5072
changeVarDec.addGoodCode(getDemoDeclaration(providedType, varName));
5173
hints.add(changeVarDec);
5274

75+
// Suggest changing variable value
5376
JavaHint changeValue = new JavaHint(problemTitle,
5477
"You might need to change the value of "
5578
+ varName + " to a " + requiredType + "."
@@ -58,6 +81,7 @@ private static List<EditorHints.Hint> getTypeMismatchHints(String providedType,
5881
changeValue.addGoodCode(getDemoDeclaration(requiredType, varName));
5982
hints.add(changeValue);
6083

84+
// Suggest changing return type
6185
JavaHint changeReturnType = new JavaHint(problemTitle,
6286
"You might need to change the method's return type to "
6387
+ providedType + "."

0 commit comments

Comments
 (0)