Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Create fully-featured IJavaCompletionProposal from IMethod
Create fully-featured IJavaCompletionProposal from IMethod [message #774410] Tue, 03 January 2012 21:39 Go to next message
Marcel Bruch is currently offline Marcel BruchFriend
Messages: 289
Registered: July 2009
Senior Member

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 Go to previous messageGo to next message
Marcel Bruch is currently offline Marcel BruchFriend
Messages: 289
Registered: July 2009
Senior Member

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 Smile 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));
        }
    }
Re: Create fully-featured IJavaCompletionProposal from IMethod [message #775773 is a reply to message #774552] Fri, 06 January 2012 16:52 Go to previous message
Ayushman Jain is currently offline Ayushman JainFriend
Messages: 3
Registered: September 2010
Junior Member
Have already replied on twitter, but writing here just for the record - CompletionEngine.createTypeProposal(char[], char[], int, int, char[], int) also does something like what Marcel has done in the above code and its the right way to go.
Previous Topic:JDT editor can find my Jar on classpath, but launch cannot
Next Topic:Accessing the latest jdt.core.dom.CompilationUnit from completion context
Goto Forum:
  


Current Time: Thu Apr 25 05:59:48 GMT 2024

Powered by FUDForum. Page generated in 0.02918 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top