Skip to content

Commit

Permalink
Fix for #1543: link the source method for method-based property proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 5, 2024
1 parent 0233324 commit ce114e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,7 +71,9 @@ protected static FieldNode createMockField(final MethodNode method) {

FieldNode fieldNode = new FieldNode(fieldName, fieldModifiers, fieldType, method.getDeclaringClass(), null);
fieldNode.setDeclaringClass(method.getDeclaringClass());
fieldNode.setNodeMetaData("groovy.method", method);
fieldNode.setSourcePosition(method);
fieldNode.setSynthetic(true);
return fieldNode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.eclipse.codeassist.ProposalUtils;
import org.codehaus.groovy.eclipse.codeassist.completions.GroovyJavaFieldCompletionProposal;
import org.codehaus.groovy.eclipse.codeassist.processors.GroovyCompletionProposal;
Expand All @@ -31,6 +32,9 @@
import org.eclipse.jdt.groovy.search.VariableScope;
import org.eclipse.jdt.internal.codeassist.InternalCompletionProposal;
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
import org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.text.java.MemberProposalInfo;
import org.eclipse.jdt.internal.ui.text.java.ProposalInfo;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
import org.eclipse.jface.viewers.StyledString;
Expand Down Expand Up @@ -105,7 +109,24 @@ public IJavaCompletionProposal createJavaProposal(ContentAssistContext context,
proposal.setRequiredProposals(new CompletionProposal[] {importProposal});
}

return new GroovyJavaFieldCompletionProposal(proposal, createDisplayString(field), javaContext);
var javaProposal = new GroovyJavaFieldCompletionProposal(proposal, createDisplayString(field), javaContext);
var groovyMethod = field.<MethodNode>getNodeMetaData("groovy.method");
if (groovyMethod != null) {
try {
var type = javaContext.getProject().findType(groovyMethod.getDeclaringClass().getName());
if (type != null) {
var method = type.getMethod(groovyMethod.getName(), GroovyUtils.getParameterTypeSignatures(groovyMethod, true));
if (method != null && method.exists()) {
ProposalInfo proposalInfo = ReflectionUtils.executePrivateMethod(AbstractJavaCompletionProposal.class, "getProposalInfo", javaProposal);
ReflectionUtils.throwableSetPrivateField(MemberProposalInfo.class, "fJavaElementResolved", proposalInfo, Boolean.TRUE);
ReflectionUtils.throwableSetPrivateField(ProposalInfo.class, "fElement", proposalInfo, method);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return javaProposal;
}

protected StyledString createDisplayString(FieldNode field) {
Expand Down

0 comments on commit ce114e7

Please sign in to comment.