Skip to content

Commit

Permalink
Add array completions. Fix eclipse-jdtls#2609
Browse files Browse the repository at this point in the history
  • Loading branch information
gayanper committed May 9, 2023
1 parent daa965d commit a340db5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,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 @@ -481,12 +481,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 @@ -495,7 +519,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
Original file line number Diff line number Diff line change
Expand Up @@ -4015,7 +4015,7 @@ public void testCompletion_syserrSnipper() throws JavaModelException {
public class Foo {
void f() {
syser
}
}
};
""");
//@formatter:on
Expand All @@ -4027,6 +4027,22 @@ void f() {
assertEquals(new Range(new Position(2, 2), new Position(2, 7)), item.getTextEdit().map(TextEdit::getRange, InsertReplaceEdit::getReplace));
}

@Test
public void testCompletion_forArrayTypeReceivers() 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());
}

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

0 comments on commit a340db5

Please sign in to comment.