Create fully-featured IJavaCompletionProposal from IMethod [message #774410] |
Tue, 03 January 2012 21:39 |
|
Hi JDT,
I'm replacing some old/odd code that created it's own Completion Engine to collect jdt.core.CompletionProposals (see IntelligentCompletionContext#initializeCompletionEngine for more details.) These proposals then are filtered based on how likely it is that a developer would use that proposal.
Now, I would like to get rid of that "trigger code completion, collect and filter, and create IJavaCompletionProposals from CompletionProposal" approach and directly create the proposals myself - and best with exactly the same behavior as standard JDT proposals (variable names, cycling through parameters, proposals for best matching variables etc.)
Is there an easy way to create such proposals from outside JDT, given that I have a handle on IMethod, IField or IType elements?
Thanks,
Marcel
|
|
|
Re: Create fully-featured IJavaCompletionProposal from IMethod [message #774552 is a reply to message #774410] |
Wed, 04 January 2012 06:42 |
|
After experimenting with CompletionProposal a while now, this is the resulting (working but not cleaned) code to create a parameter guessing java completion proposal. I'm neither sure that it will work as expected in all cases nor do I know whether this is the smartest approach to create a proposal.
Please, let me know whether there is a better way Thanks.
private void createProspsals() {
final String prefix = ctx.getPrefix();
for (final CallsRecommendation r : recommendations) {
if (!r.method.getName().startsWith(prefix)) {
continue;
}
final IMethod method = jdtResolver.toJdtMethod(r.method);
if (method == null) {
continue;
}
final int start = ctx.getInvocationOffset() - ctx.getPrefix().length();
final int end = ctx.getInvocationOffset();
final InternalCompletionProposalExtension proposal = new InternalCompletionProposalExtension(
CompletionProposal.METHOD_REF, ctx.getInvocationOffset());
try {
final IType declaringType = method.getDeclaringType();
final String returnType = method.getReturnType();
final char[] declaringClassSignature = declaringType.getKey().replace('/', '.').toCharArray();
final char[] methodSignature = method.getSignature().replace('/', '.').toCharArray();
final char[] declaringClassQualifiedPackageName = Signature
.getSignatureQualifier(declaringClassSignature);
final char[] declaringTypeFullyQualifiedSourceName = declaringType.getFullyQualifiedName()
.toCharArray();
final char[][] parameterPackageNames = null;
final char[][] parameterTypeNames = toCharArray(method.getParameterTypes());
final char[] methodReturnTypeQualifiedPackageName = Signature.getSignatureQualifier(returnType)
.toCharArray();
final char[] methodReturnTypeQualifiedSourceName = returnType.replace('/', '.').toCharArray();
final char[] methodSelector = method.getElementName().toCharArray();
final char[] completion = (method.getElementName() + "()").toCharArray();
final char[][] parameterNames = toCharArray(method.getParameterNames());
proposal.setDeclarationSignature(declaringClassSignature);
proposal.setSignature(methodSignature);
proposal.setDeclarationPackageName(declaringClassQualifiedPackageName);
proposal.setDeclarationTypeName(declaringTypeFullyQualifiedSourceName);
// proposal.setParameterPackageNames(parameterPackageNames);
proposal.setParameterTypeNames(parameterTypeNames);
proposal.setPackageName(methodReturnTypeQualifiedPackageName);
proposal.setTypeName(methodReturnTypeQualifiedSourceName);
proposal.setName(methodSelector);
proposal.setCompletion(completion);
proposal.setFlags(method.getFlags());
proposal.setReplaceRange(start, end);
proposal.setTokenRange(start, end);
proposal.setRelevance(100);
if (parameterNames != null) {
proposal.setParameterNames(parameterNames);
}
} catch (final JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final ParameterGuessingProposal javaProposal = ParameterGuessingProposal.createProposal(proposal,
ctx.getJavaContext(), true);
proposals.add(javaProposal);
}
if (!proposals.isEmpty()) {
proposals.add(new JavaCompletionProposal("", ctx.getInvocationOffset(), 0, null, "--------------", 1600));
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.02770 seconds