Skip to content

Commit 70b248e

Browse files
robstrykerRob Stryker
andauthored
DOM Code selector failing to properly convert VariableDeclarationFragment (eclipse-jdt#4054)
Signed-off-by: Rob Stryker <stryker@redhat.com> Co-authored-by: Rob Stryker <stryker@redhat.com>
1 parent e46bffe commit 70b248e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/DOMCodeSelector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public IJavaElement[] codeSelect(int offset, int length) throws JavaModelExcepti
287287
if (parent != null && bindingNode instanceof SingleVariableDeclaration variableDecl) {
288288
return new IJavaElement[] { DOMToModelPopulator.toLocalVariable(variableDecl, (JavaElement)parent) };
289289
}
290+
if( parent != null && bindingNode instanceof VariableDeclarationFragment vdf) {
291+
// Parent might be statement or expression
292+
return new IJavaElement[] { DOMToModelPopulator.toLocalVariable(vdf, (JavaElement)parent) };
293+
}
290294
}
291295
}
292296
}

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/DOMToModelPopulator.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,33 @@ public static LocalVariable toLocalVariable(SingleVariableDeclaration parameter,
887887
return toLocalVariable(parameter, parent, parameter.getParent() instanceof MethodDeclaration);
888888
}
889889

890+
public static LocalVariable toLocalVariable(VariableDeclarationFragment fragment, JavaElement parent) {
891+
if (fragment.getParent() instanceof VariableDeclarationStatement variableDeclaration) {
892+
return new LocalVariable(parent,
893+
fragment.getName().getIdentifier(),
894+
variableDeclaration.getStartPosition(),
895+
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
896+
fragment.getName().getStartPosition(),
897+
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
898+
Util.getSignature(variableDeclaration.getType()),
899+
null, // I don't think we need this, also it's the ECJ's annotation node
900+
toModelFlags(variableDeclaration.getModifiers(), false),
901+
false);
902+
} else if (fragment.getParent() instanceof VariableDeclarationExpression variableDeclaration) {
903+
return new LocalVariable(parent,
904+
fragment.getName().getIdentifier(),
905+
variableDeclaration.getStartPosition(),
906+
variableDeclaration.getStartPosition() + variableDeclaration.getLength() - 1,
907+
fragment.getName().getStartPosition(),
908+
fragment.getName().getStartPosition() + fragment.getName().getLength() - 1,
909+
Util.getSignature(variableDeclaration.getType()),
910+
null, // I don't think we need this, also it's the ECJ's annotation node
911+
toModelFlags(variableDeclaration.getModifiers(), false),
912+
false);
913+
}
914+
return null;
915+
}
916+
890917
private static LocalVariable toLocalVariable(SingleVariableDeclaration parameter, JavaElement parent, boolean isParameter) {
891918
return new LocalVariable(parent,
892919
parameter.getName().getIdentifier(),

0 commit comments

Comments
 (0)