Skip to content

Commit

Permalink
Guard the new CompletionOnRecordComponentName.eclipse-jdt#757
Browse files Browse the repository at this point in the history
Add proper guard condition for CompletionOnRecordComponentName so that
it is created only if the cursor location is in-between the record
declaration start and name end position.
  • Loading branch information
gayanper committed Feb 17, 2023
1 parent 127bb54 commit 963b422
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11394,7 +11394,7 @@ protected void consumeRecordComponent(boolean isVarArgs) {
RecordComponent recordComponent;
recordComponent = createComponent(identifierName, namePositions, type,
this.intStack[this.intPtr--] & ~ClassFileConstants.AccDeprecated // modifiers
);
, modifierPositions);
recordComponent.declarationSourceStart = modifierPositions;
recordComponent.bits |= (type.bits & ASTNode.HasTypeAnnotations);
// consume annotations
Expand Down Expand Up @@ -11716,7 +11716,8 @@ protected FieldDeclaration createFieldDeclaration(char[] fieldDeclarationName, i
return new FieldDeclaration(fieldDeclarationName, sourceStart, sourceEnd);
}

protected RecordComponent createComponent(char[] identifierName, long namePositions, TypeReference type, int modifier) {
protected RecordComponent createComponent(char[] identifierName, long namePositions, TypeReference type, int modifier,
int declStart) {
return new RecordComponent(identifierName, namePositions, type, modifier);
}
protected JavadocParser createJavadocParser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,28 @@ public void testGH667_CompletionOnRecordComponent_VariableName() throws JavaMode
requestor.getResults());
}

public void testGH757_CompletionOnTypeNameAboveRecordDeclaration() throws JavaModelException {
this.workingCopies = new ICompilationUnit[2];
this.workingCopies[0] = getWorkingCopy(
"/Completion/src/Person.java",
"public class Person {\n"
+ "private Name name = new Name \n"
+ "record Age(int value){};"
+ "}\n");
this.workingCopies[1] = getWorkingCopy(
"/Completion/src/Name.java",
"public class Name {\n"
+ "}\n");
CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, false);
String str = this.workingCopies[0].getSource();
String completeBehind = "new Name";
int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
assertResults(
"Name[TYPE_REF]{Name, , LName;, null, null, null, null, [46, 50], "
+ (R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_UNQUALIFIED + R_NON_RESTRICTED
+ R_EXACT_NAME + R_EXACT_EXPECTED_TYPE)
+ "}",
requestor.getResults());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6327,12 +6327,13 @@ protected FieldDeclaration createFieldDeclaration(char[] assistName, int sourceS
}

@Override
protected RecordComponent createComponent(char[] identifierName, long namePositions, TypeReference type, int modifier) {
protected RecordComponent createComponent(char[] identifierName, long namePositions, TypeReference type, int modifier,
int declStart) {
int endPos = (int) namePositions;
if (this.cursorLocation <= endPos) {
if (this.cursorLocation > declStart && this.cursorLocation <= endPos) {
return new CompletionOnRecordComponentName(identifierName, namePositions, type, modifier);
}
return super.createComponent(identifierName, namePositions, type, modifier);
return super.createComponent(identifierName, namePositions, type, modifier, declStart);
}

/*
Expand Down

0 comments on commit 963b422

Please sign in to comment.