Skip to content

Commit

Permalink
Merge pull request #463 from groovy/issue404
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles authored Feb 11, 2018
2 parents 9d2cb4d + 3a9cf25 commit a2136fe
Show file tree
Hide file tree
Showing 11 changed files with 520 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,25 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}
Outer.Inn
'''.stripIndent()
assertProposalCreated(contents, 'Inn', 'Inner - Outer')
applyProposalAndCheck(assertProposalCreated(contents, 'Inn', 'Inner - Outer'), contents.replaceFirst(/Inn\b/, 'Inner'))
}

@Test
void testInnerClass3a() {
void testInnerClass4() {
String contents = '''\
Map.Ent
'''.stripIndent()
applyProposalAndCheck(assertProposalCreated(contents, 'Ent', 'Entry - java.util.Map'), contents.replace('Ent', 'Entry'))
}

@Test
void testInnerClass3b() {
void testInnerClass5() {
setJavaPreference(PreferenceConstants.CODEASSIST_ADDIMPORT, 'false')
testInnerClass3a() // no difference; no qualifier should be inserted
testInnerClass4() // no difference; no qualifier should be inserted
}

@Test
void testInnerClass4() {
void testInnerClass6() {
addGroovySource '''\
class Outer {
class Inner {
Expand All @@ -150,7 +150,7 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass5() {
void testInnerClass7() {
addGroovySource '''\
class Outer {
class Inner {
Expand All @@ -165,7 +165,7 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass6() {
void testInnerClass8() {
addGroovySource '''\
class Outer {
class Inner {
Expand All @@ -181,7 +181,7 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass6a() {
void testInnerClass9() {
addGroovySource '''\
class Outer {
class Inner {
Expand All @@ -198,7 +198,7 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass7() {
void testInnerClass10() {
addGroovySource '''\
class Outer {
interface Inner {
Expand All @@ -215,7 +215,7 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass7a() {
void testInnerClass11() {
addGroovySource '''\
class Outer {
interface Inner {
Expand All @@ -231,20 +231,94 @@ final class InnerTypeCompletionTests extends CompletionTestSuite {
}

@Test
void testInnerClass8() {
void testInnerClass12() {
addGroovySource '''\
class Outer {
class Inner {
}
}
''', 'Outer', 'v'

String contents = '''\
Outer.Inn
'''.stripIndent()
applyProposalAndCheck(assertProposalCreated(contents, 'Inn', 'Inner - v.Outer'), '''\
|import v.Outer
|
|Outer.Inner
|'''.stripMargin())
}

@Test
void testInnerClass13() {
addGroovySource '''\
class Outer {
class Inner {
}
}
''', 'Outer', 'w'

String contents = '''\
Outer.Inn
'''.stripIndent()
setJavaPreference(PreferenceConstants.CODEASSIST_ADDIMPORT, 'false')
applyProposalAndCheck(assertProposalCreated(contents, 'Inn', 'Inner - w.Outer'), '''\
|w.Outer.Inner
|'''.stripMargin())
}

@Test
void testInnerClass14() {
addGroovySource '''\
class Outer {
class Inner {
class Nucleus {
}
}
}
''', 'Outer', 'q'
''', 'Outer', 'x'

String contents = '''\
q.Outer.Inner.N
x.Outer.Inner.N
'''.stripIndent()
applyProposalAndCheck(assertProposalCreated(contents, 'N', 'Nucleus - q.Outer.Inner'), contents.replace('N', 'Nucleus'))
applyProposalAndCheck(assertProposalCreated(contents, 'N', 'Nucleus - x.Outer.Inner'), contents.replace('N', 'Nucleus'))
}

@Test
void testInnerClass15() {
addGroovySource '''\
class Outer {
class Inner {
}
}
''', 'Outer', 'y'

String contents = '''\
Inn
'''.stripIndent()
applyProposalAndCheck(assertProposalCreated(contents, 'Inn', 'Inner - y.Outer'), '''\
|import y.Outer.Inner
|
|Inner
|'''.stripMargin())
}

@Test
void testInnerClass16() {
addGroovySource '''\
class Outer {
class Inner {
}
}
''', 'Outer', 'z'

String contents = '''\
Inn
'''.stripIndent()
setJavaPreference(PreferenceConstants.CODEASSIST_ADDIMPORT, 'false')
applyProposalAndCheck(assertProposalCreated(contents, 'Inn', 'Inner - z.Outer'), '''\
|z.Outer.Inner
|'''.stripMargin())
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.codehaus.groovy.eclipse.codeassist.creators.IProposalCreator;
import org.codehaus.groovy.eclipse.codeassist.creators.MethodProposalCreator;
import org.codehaus.groovy.eclipse.codeassist.requestor.ContentAssistContext;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.core.SearchableEnvironment;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;

Expand All @@ -35,15 +36,15 @@ public AbstractGroovyCompletionProcessor(ContentAssistContext context, JavaConte
this.javaContext = javaContext;
}

public ContentAssistContext getContext() {
public final ContentAssistContext getContext() {
return context;
}

public SearchableEnvironment getNameEnvironment() {
public final SearchableEnvironment getNameEnvironment() {
return nameEnvironment;
}

public JavaContentAssistInvocationContext getJavaContext() {
public final JavaContentAssistInvocationContext getJavaContext() {
return javaContext;
}

Expand All @@ -60,4 +61,11 @@ protected final GroovyCompletionProposal createProposal(int kind, int completion
proposal.setNameLookup(nameEnvironment.nameLookup);
return proposal;
}

protected static boolean matches(String prefix, String candidate, boolean camelCaseMatch) {
if (!camelCaseMatch) {
return candidate.startsWith(prefix);
}
return SearchPattern.camelCaseMatch(prefix, candidate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,47 @@

public class ConstructorCompletionProcessor extends AbstractGroovyCompletionProcessor implements ITypeResolver {

private JDTResolver resolver;
protected ModuleNode module;
protected JDTResolver resolver;

public ConstructorCompletionProcessor(ContentAssistContext context, JavaContentAssistInvocationContext javaContext, SearchableEnvironment nameEnvironment) {
super(context, javaContext, nameEnvironment);
}

@Override
public void setResolverInformation(ModuleNode module, JDTResolver resolver) {
this.module = module;
this.resolver = resolver;
}

@Override
public List<ICompletionProposal> generateProposals(IProgressMonitor monitor) {
ContentAssistContext context = getContext();
char[] constructorText; int constructorStart;
char[] completionChars; int completionStart;
switch (context.location) {
case CONSTRUCTOR:
context.extend(getJavaContext().getCoreContext(), null);
constructorText = context.fullCompletionExpression.replaceFirst("^new\\s+", "").toCharArray();
constructorStart = context.completionLocation - CharOperation.lastSegment(constructorText, '.').length;
completionChars = context.fullCompletionExpression.replaceAll("^new|\\s+", "").toCharArray();
completionStart = context.completionLocation - CharOperation.lastSegment(completionChars, '.').length;
break;
case METHOD_CONTEXT:
constructorText = ((MethodInfoContentAssistContext) context).methodName.toCharArray();
constructorStart = ((MethodInfoContentAssistContext) context).methodNameEnd - CharOperation.lastSegment(constructorText, '.').length;;
completionChars = ((MethodInfoContentAssistContext) context).methodName.replace('$', '.').toCharArray();
completionStart = ((MethodInfoContentAssistContext) context).methodNameEnd - CharOperation.lastSegment(completionChars, '.').length;
break;
default:
throw new IllegalStateException("Invalid constructor completion location: " + context.location.name());
}

GroovyProposalTypeSearchRequestor requestor = new GroovyProposalTypeSearchRequestor(context, getJavaContext(),
constructorStart, context.completionEnd - constructorStart, getNameEnvironment().nameLookup, monitor);

getNameEnvironment().findConstructorDeclarations(constructorText, true, requestor, monitor);
SearchableEnvironment environment = getNameEnvironment();
int replacementLength = context.completionEnd - completionStart;
GroovyProposalTypeSearchRequestor requestor = new GroovyProposalTypeSearchRequestor(
context, getJavaContext(), completionStart, replacementLength, environment.nameLookup, monitor);
environment.findConstructorDeclarations(completionChars, requestor.options.camelCaseMatch, requestor, monitor);

return requestor.processAcceptedConstructors(findUsedParameters(context), resolver);
}

private Set<String> findUsedParameters(ContentAssistContext context) {
protected static Set<String> findUsedParameters(ContentAssistContext context) {
if (context.location != ContentAssistLocation.METHOD_CONTEXT) {
return Collections.emptySet();
}
Expand All @@ -95,13 +98,10 @@ private Set<String> findUsedParameters(ContentAssistContext context) {
}
}
}

// now remove the arguments that are already written
if (arguments instanceof MapExpression) {
// Do extra filtering to determine what parameters are still
// available
MapExpression enclosingCallArgs = (MapExpression) arguments;
for (MapEntryExpression entry : enclosingCallArgs.getMapEntryExpressions()) {
// do extra filtering to determine what parameters are still available
for (MapEntryExpression entry : ((MapExpression) arguments).getMapEntryExpressions()) {
usedParams.add(entry.getKeyExpression().getText());
}
}
Expand Down
Loading

0 comments on commit a2136fe

Please sign in to comment.