Skip to content

Commit

Permalink
Improve support for completing constructor args
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Jan 20, 2025
1 parent 485d20d commit c8324f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,14 @@ public void run() {
suggestDefaultCompletions = false;
}
}
if (context instanceof ClassInstanceCreation) {
if (context instanceof ClassInstanceCreation newObj) {
var type = newObj.getType();
var binding = type.resolveBinding();
if (this.offset > type.getStartPosition() + type.getLength() &&
binding != null &&
newObj.getType().resolveBinding().isEnum()) {
return; // don't complete illegal enum constructor
}
if (this.expectedTypes.getExpectedTypes() != null && !this.expectedTypes.getExpectedTypes().isEmpty() && !this.expectedTypes.getExpectedTypes().get(0).isRecovered()) {
completeConstructor(this.expectedTypes.getExpectedTypes().get(0), context, this.modelUnit.getJavaProject());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,18 @@ private void computeExpectedTypes(){
return;
}
if (parent2 instanceof ClassInstanceCreation newObj && this.offset > newObj.getType().getStartPosition() + newObj.getType().getLength()) {
// TODO find params
int argIndex = 0;
var args = (List<Expression>)newObj.arguments();
for (argIndex = 0; argIndex < args.size(); argIndex++) {
var arg = args.get(argIndex);
if (this.offset < arg.getStartPosition() || this.offset > arg.getStartPosition() + arg.getLength()) {
break;
}
}
argIndex = Math.max(0, argIndex - 1);
var binding = newObj.getType().resolveBinding();
break;

}
if (parent2 instanceof CastExpression cast && this.offset > cast.getType().getStartPosition() + cast.getType().getLength()) {
this.expectedTypes.add(cast.getType().resolveBinding());
Expand Down Expand Up @@ -220,12 +230,16 @@ private void computeExpectedTypes(){
}
}
} else if(parent instanceof ClassInstanceCreation allocationExpression) {
ITypeBinding binding = allocationExpression.resolveTypeBinding();
if(binding != null) {
computeExpectedTypesForAllocationExpression(
binding,
allocationExpression.arguments(),
allocationExpression);
if (this.offset <= allocationExpression.getType().getStartPosition() + allocationExpression.getType().getLength()) {
ITypeBinding binding = allocationExpression.resolveTypeBinding();
if(binding != null) {
computeExpectedTypesForAllocationExpression(
binding,
allocationExpression.arguments(),
allocationExpression);
}
} else {
//int itemIndexAtOffset =z
}
} else if(parent instanceof InstanceofExpression e) {
ITypeBinding binding = e.getLeftOperand().resolveTypeBinding();
Expand Down

0 comments on commit c8324f0

Please sign in to comment.