Skip to content

Commit

Permalink
Add support for method reference content assist
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 15, 2018
1 parent ebd9549 commit 92be6be
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.codehaus.groovy.eclipse.codeassist.tests

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser
import static org.junit.Assert.fail
import static org.junit.Assume.assumeTrue

import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
Expand Down Expand Up @@ -469,30 +471,70 @@ final class MethodCompletionTests extends CompletionTestSuite {
'''.stripIndent())
}

@Test
void testMethodPointer0() {
String contents = 'class Foo { public static Foo instance }\nFoo.&in'
proposalExists(createProposalsAtOffset(contents, getLastIndexOf(contents, 'in')), 'instance', 0)
}

@Test
void testMethodPointer0a() {
String contents = 'class Foo { public static Foo instance }\nFoo::in'
proposalExists(createProposalsAtOffset(contents, getLastIndexOf(contents, 'in')), 'instance', 0)
}

@Test
void testMethodPointer1() {
String contents = 'String.&isE'
applyProposalAndCheck(checkUniqueProposal(contents, 'isE', 'isEmpty'), contents + 'mpty')
}

@Test
void testMethodPointer1a() {
assumeTrue(isParrotParser())
String contents = 'String::isE'
applyProposalAndCheck(checkUniqueProposal(contents, 'isE', 'isEmpty'), contents + 'mpty')
}

@Test
void testMethodPointer2() {
String contents = 'String.& isE'
applyProposalAndCheck(checkUniqueProposal(contents, 'isE', 'isEmpty'), contents + 'mpty')
}

@Test
void testMethodPointer2a() {
assumeTrue(isParrotParser())
String contents = 'String:: isE'
applyProposalAndCheck(checkUniqueProposal(contents, 'isE', 'isEmpty'), contents + 'mpty')
}

@Test
void testMethodPointer3() {
String contents = 'String.&isEmpty.mem'
applyProposalAndCheck(checkUniqueProposal(contents, 'mem', 'memoize()'), contents + 'oize()')
}

@Test
void testMethodPointer3a() {
assumeTrue(isParrotParser())
String contents = 'String::isEmpty.mem'
applyProposalAndCheck(checkUniqueProposal(contents, 'mem', 'memoize()'), contents + 'oize()')
}

@Test
void testMethodPointer4() {
String contents = '(String.&isEmpty).mem'
applyProposalAndCheck(checkUniqueProposal(contents, 'mem', 'memoize()'), contents + 'oize()')
}

@Test
void testMethodPointer4a() {
assumeTrue(isParrotParser())
String contents = '(String::isEmpty).mem'
applyProposalAndCheck(checkUniqueProposal(contents, 'mem', 'memoize()'), contents + 'oize()')
}

@Test
void testAnnotatedMethod1() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
import org.eclipse.jdt.ui.text.java.CompletionProposalLabelProvider;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.swt.graphics.Image;
Expand Down Expand Up @@ -306,25 +304,4 @@ public static boolean hasWhitespace(char[] chars) {
}
return false;
}

/** Checks '.&' operator before replacement offset. */
public static boolean isMethodPointerCompletion(IDocument document, int replacementOffset) {
try {
boolean seenAmpersand = false;
while (--replacementOffset > 0) {
char c = document.getChar(replacementOffset);
if (Character.isJavaIdentifierPart(c) || (!Character.isWhitespace(c) && c != '&' && c != '.')) break;
if (c == '&') {
if (seenAmpersand) break;
seenAmpersand = true;
} else if (c == '.') {
if (seenAmpersand)
return true;
break;
}
}
} catch (BadLocationException ignore) {
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -577,5 +577,5 @@ private List<IProposalCreator> chooseProposalCreators() {
}

public static final Pattern FIELD_ACCESS_COMPLETION = Pattern.compile(".+\\.@\\s*(?:\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)?", Pattern.DOTALL);
public static final Pattern METHOD_POINTER_COMPLETION = Pattern.compile(".+\\.&\\s*(?:\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)?", Pattern.DOTALL);
public static final Pattern METHOD_POINTER_COMPLETION = Pattern.compile(".+(\\.&|::)\\s*(?:\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)?", Pattern.DOTALL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ public Token next() throws TokenStreamException {
last = new Token(Token.Type.METHOD_POINTER, offset + 1, offset + 3, buffer.subSequence(offset + 1, offset + 3).toString());
}
break;
case ':':
nextChar();
if (ch == ':') {
nextChar();
last = new Token(Token.Type.METHOD_POINTER, offset + 1, offset + 3, buffer.subSequence(offset + 1, offset + 3).toString());
}
break;
case ';':
nextChar();
last = new Token(Token.Type.SEMI, offset + 1, offset + 2, buffer.subSequence(offset + 1, offset + 2).toString());
Expand Down

0 comments on commit 92be6be

Please sign in to comment.