Skip to content

Commit

Permalink
Fix for issue #362: qualified and unqualified constructor completions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 2, 2017
1 parent 7689870 commit de5175b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,38 @@ final class ConstructorCompletionTests extends CompletionTestSuite {
checkProposalApplicationNonType(contents, expected, getIndexOf(contents, 'new YY'), 'YYY')
}

@Test
void testContructorCompletionWithQualifier() {
String contents = 'new java.text.Anno'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, contents.length());
proposalExists(proposals, 'AnnotationVisitor', 0)
proposalExists(proposals, 'Annotation', 1)
}

@Test
void testContructorCompletionImportHandling0() {
String contents = '''\
def a = new java.text.Anno
'''.stripIndent()
String expected = '''\
def a = new java.text.Annotation(value)
'''.stripIndent()
checkProposalApplicationNonType(contents, expected, getIndexOf(contents, 'Anno'), 'Annotation')
}

@Test
void testContructorCompletionImportHandling1() {
String contents = '''\
def a = new Anno
'''.stripIndent()
String expected = '''\
import java.text.Annotation
def a = new Annotation(value)
'''.stripIndent()
checkProposalApplicationNonType(contents, expected, getIndexOf(contents, 'new Anno'), 'Annotation')
}

@Test
void testNamedArgs1() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.codehaus.groovy.eclipse.codeassist.proposals.ProposalFormattingOptions;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.EditorHighlightingSynchronizer;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.text.java.JavaMethodCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.LazyGenericTypeProposal;
import org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.LazyJavaTypeCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.MethodProposalInfo;
import org.eclipse.jdt.internal.ui.text.java.ProposalContextInformation;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
Expand Down Expand Up @@ -63,7 +65,8 @@ public class GroovyJavaMethodCompletionProposal extends JavaMethodCompletionProp

private int[] fArgumentOffsets;
private int[] fArgumentLengths;
private IRegion fSelectedRegion; // initialized by apply()
private IRegion fSelectedRegion;
private ImportRewrite fImportRewite;

public GroovyJavaMethodCompletionProposal(GroovyCompletionProposal proposal, JavaContentAssistInvocationContext context, ProposalFormattingOptions options) {
this(proposal, context, options, null);
Expand All @@ -79,6 +82,10 @@ public GroovyJavaMethodCompletionProposal(GroovyCompletionProposal proposal, Jav
this.setTriggerCharacters(!proposal.hasParameters() ? ProposalUtils.METHOD_TRIGGERS : ProposalUtils.METHOD_WITH_ARGUMENTS_TRIGGERS);
}

public void setImportRewite(ImportRewrite importRewite) {
fImportRewite = importRewite;
}

public void contextOnly() {
contextOnly = true;
}
Expand Down Expand Up @@ -128,14 +135,19 @@ protected IContextInformation computeContextInformation() {
@Override
protected LazyJavaCompletionProposal createRequiredTypeCompletionProposal(CompletionProposal completionProposal, JavaContentAssistInvocationContext invocationContext) {
LazyJavaCompletionProposal requiredProposal = super.createRequiredTypeCompletionProposal(completionProposal, invocationContext);
// disable generics completion for constructors (i.e. complete expression as "new ArrayList()" instead of "new ArrayList<E>()"
if (fProposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION && requiredProposal instanceof LazyGenericTypeProposal) {
try {
//requiredProposal.fTypeArgumentProposals = new LazyGenericTypeProposal.TypeArgumentProposal[0];
ReflectionUtils.setPrivateField(LazyGenericTypeProposal.class, "fTypeArgumentProposals", requiredProposal,
Array.newInstance(Class.forName("org.eclipse.jdt.internal.ui.text.java.LazyGenericTypeProposal$TypeArgumentProposal"), 0));
} catch (Exception e) {
GroovyContentAssist.logError(e);
if (fProposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION && requiredProposal instanceof LazyJavaTypeCompletionProposal) {
if (fImportRewite != null) {
ReflectionUtils.setPrivateField(LazyJavaTypeCompletionProposal.class, "fImportRewrite", requiredProposal, fImportRewite);
}
if (requiredProposal instanceof LazyGenericTypeProposal) {
// disable generics completion for constructors (i.e. complete expression as "new ArrayList()" instead of "new ArrayList<E>()"
try {
//requiredProposal.fTypeArgumentProposals = new LazyGenericTypeProposal.TypeArgumentProposal[0];
ReflectionUtils.setPrivateField(LazyGenericTypeProposal.class, "fTypeArgumentProposals", requiredProposal,
Array.newInstance(Class.forName("org.eclipse.jdt.internal.ui.text.java.LazyGenericTypeProposal$TypeArgumentProposal"), 0));
} catch (Exception e) {
GroovyContentAssist.logError(e);
}
}
}
return requiredProposal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.codehaus.groovy.eclipse.codeassist.requestor.MethodInfoContentAssistContext;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTResolver;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.search.ITypeResolver;
import org.eclipse.jdt.internal.core.SearchableEnvironment;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
Expand All @@ -54,12 +55,12 @@ public List<ICompletionProposal> generateProposals(IProgressMonitor monitor) {
char[] constructorText; int constructorStart;
switch (context.location) {
case CONSTRUCTOR:
constructorText = context.completionExpression.replaceFirst("^new\\s+", "").toCharArray();
constructorStart = context.completionLocation - constructorText.length;
constructorText = context.fullCompletionExpression.replaceFirst("^new\\s+", "").toCharArray();
constructorStart = context.completionLocation - CharOperation.lastSegment(constructorText, '.').length;
break;
case METHOD_CONTEXT:
constructorText = ((MethodInfoContentAssistContext) context).methodName.toCharArray();
constructorStart = ((MethodInfoContentAssistContext) context).methodNameEnd - constructorText.length;
constructorStart = ((MethodInfoContentAssistContext) context).methodNameEnd - CharOperation.lastSegment(constructorText, '.').length;;
break;
default:
throw new IllegalStateException("Invalid constructor completion location: " + context.location.name());
Expand Down
Loading

0 comments on commit de5175b

Please sign in to comment.