Skip to content

Commit

Permalink
Add support for array type completions (#2610)
Browse files Browse the repository at this point in the history
* Add array completions. Fixes #2609
  • Loading branch information
gayanper authored May 15, 2023
1 parent 21c9a79 commit 0331b07
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,17 @@ private String extractDeclaringTypeFQN(CompletionProposal methodProposal) {
private void createTypeProposalLabel(CompletionProposal typeProposal, CompletionItem item) {
char[] signature;
if (fContext != null && fContext.isInJavadoc()) {
signature= Signature.getTypeErasure(typeProposal.getSignature());
if (typeProposal.getArrayDimensions() > 0) {
signature = Signature.createArraySignature(Signature.getTypeErasure(typeProposal.getSignature()), typeProposal.getArrayDimensions());
} else {
signature = Signature.getTypeErasure(typeProposal.getSignature());
}
} else {
signature= typeProposal.getSignature();
if (typeProposal.getArrayDimensions() > 0) {
signature = Signature.createArraySignature(typeProposal.getSignature(), typeProposal.getArrayDimensions());
} else {
signature = typeProposal.getSignature();
}
}
char[] fullName= Signature.toCharArray(signature);
createTypeProposalLabel(fullName, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,36 @@ private boolean isInJavadoc() {
}

private void appendReplacementString(StringBuilder buffer, CompletionProposal proposal) {
final boolean completionSnippetsSupported = client.isCompletionSnippetsSupported();
if (!hasArgumentList(proposal)) {
String str = proposal.getKind() == CompletionProposal.TYPE_REF ? computeJavaTypeReplacementString(proposal) : String.valueOf(proposal.getCompletion());
if (client.isCompletionSnippetsSupported()) {
str = CompletionUtils.sanitizeCompletion(str);
if (proposal.getKind() == CompletionProposal.PACKAGE_REF && str != null && str.endsWith(".*;")) {
str = str.replace(".*;", ".${0:*};");
String str = null;
if (proposal.getKind() == CompletionProposal.TYPE_REF) {
str = computeJavaTypeReplacementString(proposal);
if (completionSnippetsSupported) {
str = CompletionUtils.sanitizeCompletion(str);
}

if (proposal.getArrayDimensions() > 0) {
StringBuilder arrayString = new StringBuilder(str);
for (int i = 0; i < proposal.getArrayDimensions(); i++) {
arrayString.append("[");
if (completionSnippetsSupported) {
arrayString.append("$").append(i + 1);
}
arrayString.append("]");
}
if (completionSnippetsSupported) {
arrayString.append("$0");
}
str = arrayString.toString();
}
} else {
str = String.valueOf(proposal.getCompletion());
if (completionSnippetsSupported) {
str = CompletionUtils.sanitizeCompletion(str);
if (proposal.getKind() == CompletionProposal.PACKAGE_REF && str != null && str.endsWith(".*;")) {
str = str.replace(".*;", ".${0:*};");
}
}
}
buffer.append(str);
Expand All @@ -518,7 +542,7 @@ private void appendReplacementString(StringBuilder buffer, CompletionProposal pr

// we're inserting a method plus the argument list - respect formatter preferences
appendMethodNameReplacement(buffer, proposal);
final boolean addParen = client.isCompletionSnippetsSupported();
final boolean addParen = completionSnippetsSupported;
if(addParen) {
buffer.append(LPAREN);
}
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<unit id="org.eclipse.jdt.source.feature.group" version="0.0.0"/>
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
<unit id="org.mockito.mockito-core" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.28-I-builds/I20230416-0550/"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.28-I-builds/I20230511-1800/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.xtext.xbase.lib" version="0.0.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3533,7 +3533,7 @@ public void testCompletion_syserrSnipper() throws JavaModelException {
public class Foo {
void f() {
syser
}
}
};
""");
//@formatter:on
Expand All @@ -3545,6 +3545,54 @@ void f() {
assertEquals(new Range(new Position(2, 2), new Position(2, 7)), item.getTextEdit().map(TextEdit::getRange, InsertReplaceEdit::getReplace));
}

@Test
public void testCompletion_forNonPrimitiveArrayTypeReceivers() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Arr.java", """
public class Arr {
void foo() {
String[] names = new S
}
}
""");

CompletionList list = requestCompletions(unit, "new ");
CompletionItem completionItem = list.getItems().get(0);
assertEquals("Array type completion EditText", "String[]", completionItem.getInsertText());
assertEquals("Array type completion Label", "String[] - java.lang", completionItem.getLabel());
}

@Test
public void testCompletion_forPrimitiveArrayTypeReceivers() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Arr.java", """
public class Arr {
void foo() {
int[] ages = new i
}
}
""");

CompletionList list = requestCompletions(unit, "new ");
CompletionItem completionItem = list.getItems().get(0);
assertEquals("Array type completion EditText", "int[]", completionItem.getInsertText());
assertEquals("Array type completion Label", "int[]", completionItem.getLabel());
}

@Test
public void testCompletion_forEnclosingTypeArrayTypeReceivers() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Arr.java", """
public class Arr {
void foo() {
Arr[] ages = new A
}
}
""");

CompletionList list = requestCompletions(unit, "new ");
CompletionItem completionItem = list.getItems().get(0);
assertEquals("Array type completion EditText", "Arr[]", completionItem.getInsertText());
assertEquals("Array type completion Label", "Arr[] - java", completionItem.getLabel());
}

private CompletionList requestCompletions(ICompilationUnit unit, String completeBehind) throws JavaModelException {
return requestCompletions(unit, completeBehind, 0);
}
Expand Down

0 comments on commit 0331b07

Please sign in to comment.