Skip to content

Commit

Permalink
Autocompletion fails if Class name contains $
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Sep 25, 2017
1 parent f5b33f3 commit 7b2df83
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
*/
public class CompletionProposalReplacementProvider {

private static final String ESCAPE_DOLLAR = "\\\\\\$";
private static final String DOLLAR = "\\$";
private static final String CURSOR_POSITION = "${0}";
private static final char SPACE = ' ';
private static final char LPAREN = '(';
Expand Down Expand Up @@ -136,7 +138,6 @@ public void updateReplacement(CompletionProposal proposal, CompletionItem item,
if (range == null) {
range = toReplacementRange(proposal);
}

if(proposal.getKind() == CompletionProposal.METHOD_DECLARATION){
appendMethodOverrideReplacement(completionBuffer, proposal);
} else if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION && proposal instanceof GetterSetterCompletionProposal) {
Expand Down Expand Up @@ -309,6 +310,10 @@ private void appendMethodPotentialReplacement(StringBuilder completionBuffer, Ge
}

private void appendBody(StringBuilder completionBuffer) {
if (client.isCompletionSnippetsSupported()) {
String replace = completionBuffer.toString().replaceAll(DOLLAR, ESCAPE_DOLLAR);
completionBuffer.replace(0, completionBuffer.toString().length(), replace);
}
completionBuffer.append(" {\n\t");
if (client.isCompletionSnippetsSupported()) {
completionBuffer.append(CURSOR_POSITION);
Expand Down Expand Up @@ -365,7 +370,11 @@ private boolean isInJavadoc() {

private void appendReplacementString(StringBuilder buffer, CompletionProposal proposal) {
if (!hasArgumentList(proposal)) {
buffer.append(proposal.getKind() == CompletionProposal.TYPE_REF ? computeJavaTypeReplacementString(proposal) : String.valueOf(proposal.getCompletion()));
String str = proposal.getKind() == CompletionProposal.TYPE_REF ? computeJavaTypeReplacementString(proposal) : String.valueOf(proposal.getCompletion());
if (client.isCompletionSnippetsSupported()) {
str = str.replaceAll(DOLLAR, ESCAPE_DOLLAR);
}
buffer.append(str);
return;
}

Expand Down Expand Up @@ -399,15 +408,22 @@ private boolean hasParameters(CompletionProposal proposal) throws IllegalArgumen

private void appendMethodNameReplacement(StringBuilder buffer, CompletionProposal proposal) {
if (proposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER) {
String coreCompletion= String.valueOf(proposal.getCompletion());
String coreCompletion = String.valueOf(proposal.getCompletion());
if (client.isCompletionSnippetsSupported()) {
coreCompletion = coreCompletion.replaceAll(DOLLAR, ESCAPE_DOLLAR);
}
// String lineDelimiter = TextUtilities.getDefaultLineDelimiter(getTextViewer().getDocument());
// String replacement= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, coreCompletion, 0, lineDelimiter, fInvocationContext.getProject());
// buffer.append(replacement.substring(0, replacement.lastIndexOf('.') + 1));
buffer.append(coreCompletion);
}

if (proposal.getKind() != CompletionProposal.CONSTRUCTOR_INVOCATION) {
buffer.append(proposal.getName());
String str = new String(proposal.getName());
if (client.isCompletionSnippetsSupported()) {
str = str.replaceAll(DOLLAR, ESCAPE_DOLLAR);
}
buffer.append(str);
}

}
Expand All @@ -423,8 +439,12 @@ private void appendGuessingCompletion(StringBuilder buffer, CompletionProposal p
buffer.append(COMMA);
buffer.append(SPACE);
}

char[] argument = parameterNames[i];
if (client.isCompletionSnippetsSupported()) {
String replace = new String(argument);
replace = replace.replaceAll(DOLLAR, ESCAPE_DOLLAR);
argument = replace.toCharArray();
}
buffer.append("${");
buffer.append(Integer.toString(i+1));
buffer.append(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ public static MethodDeclaration createImplementationStub(ICompilationUnit unit,
if (!bodyStatement.isEmpty()) {
placeHolder.append(":");
}
bodyStatement = bodyStatement.replaceAll("\\$", "\\\\\\$");
}
placeHolder.append(bodyStatement);
if (snippetStringSupport) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package java;

public class Foo$Bar {

}
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,64 @@ public void testCompletion_AnonymousDeclarationType5() throws Exception {
"}", resolvedItem.getTextEdit());
}

@Test
public void testCompletion_class_name_contains_$() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo$Bar.java",
"public class Foo$Bar {\n"+
" public static void main(String[] args) {\n" +
" new Foo\n" +
" }\n" +
"}\n");
waitForBackgroundJobs();
int[] loc = findCompletionLocation(unit, "new Foo");
CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
assertNotNull(list);
CompletionItem ci = list.getItems().stream()
.filter(item -> item.getLabel().startsWith("Foo$Bar"))
.findFirst().orElse(null);
assertNotNull(ci);

assertEquals("Foo$Bar", ci.getInsertText());
assertEquals(CompletionItemKind.Constructor, ci.getKind());
assertEquals("999999115", ci.getSortText());
assertNull(ci.getTextEdit());

CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
assertNotNull(resolvedItem.getTextEdit());
assertTextEdit(2, 12, 15, "Foo\\$Bar()", resolvedItem.getTextEdit());
}

@Test
public void testCompletion_class_name_contains_$withoutSnippetSupport() throws Exception {
mockLSPClient(false, true);
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo$Bar.java",
"public class Foo$Bar {\n"+
" public static void main(String[] args) {\n" +
" new Foo\n" +
" }\n" +
"}\n");
waitForBackgroundJobs();
int[] loc = findCompletionLocation(unit, "new Foo");
CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
assertNotNull(list);
CompletionItem ci = list.getItems().stream()
.filter(item -> item.getLabel().startsWith("Foo$Bar"))
.findFirst().orElse(null);
assertNotNull(ci);

assertEquals("Foo$Bar", ci.getInsertText());
assertEquals(CompletionItemKind.Constructor, ci.getKind());
assertEquals("999999115", ci.getSortText());
assertNull(ci.getTextEdit());

CompletionItem resolvedItem = server.resolveCompletionItem(ci).join();
assertNotNull(resolvedItem.getTextEdit());
assertTextEdit(2, 12, 15, "Foo$Bar", resolvedItem.getTextEdit());
}


private String createCompletionRequest(ICompilationUnit unit, int line, int kar) {
return COMPLETION_TEMPLATE.replace("${file}", JDTUtils.getFileURI(unit))
.replace("${line}", String.valueOf(line))
Expand Down

0 comments on commit 7b2df83

Please sign in to comment.