javaeclipse-plugineclipse-jdtcode-completioncontent-assist

Eclipse Plugin: Adding Custom Suggestion in Default Code Completion Pop-up window


I'm making an eclipse plugin that requires to add some custom suggestions in the default eclipse pop-up window. For doing so, I came to know from eclipse doc that I have to implement the IJavaCompletionProposalComputer to participate in content assist process . Accordingly, I tried the below implementation found in github. I understand that it overrides the computeCompletionProposals() method that computes suggestions and returns as ICompletionProposal List. But I failed to find what to do for adding my custom suggestions in the default pop-up window.

Any idea how I can do that?

package zzz.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.ui.texteditor.HippieProposalProcessor;
import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
import java.util.*;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
/**
 * A computer wrapper for the hippie processor.
 *
 * @since 3.2
 */
public final class HippieProposalComputer implements IJavaCompletionProposalComputer {
    /** The wrapped processor. */
    private final HippieProposalProcessor fProcessor= new HippieProposalProcessor();

    /**
     * Default ctor to make it instantiatable via the extension mechanism.
     */
    public HippieProposalComputer() {
    }

    /*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
        return Arrays.asList(fProcessor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset()));
    }

    /*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
        return Arrays.asList(fProcessor.computeContextInformation(context.getViewer(), context.getInvocationOffset()));
    }

    /*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#getErrorMessage()
     */
    @Override
    public String getErrorMessage() {
        return fProcessor.getErrorMessage();
    }

    /*
     * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionStarted()
     */
    @Override
    public void sessionStarted() {
    }

    /*
     * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer#sessionEnded()
     */
    @Override
    public void sessionEnded() {
    }
}

My plugin.xml is as follows...

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

 <extension point="org.eclipse.jdt.ui.javaCompletionProposalComputer"
       id="WordCompletionProposalComputer"
       name="Word Completion Proposal Computer">
       <javaCompletionProposalComputer
          class="org.eclipse.jdt.internal.ui.text.java.HippieProposalComputer"
          categoryId="org.eclipse.ui.texteditor.textual_proposals">
          <partition type="__java_javadoc"/>
       </javaCompletionProposalComputer>
     </extension>

   <extension
         point="org.eclipse.ui.commands">
      <category
            name="Sample Category"
            id="zadawdaw.commands.category">
      </category>
      <command
            name="Sample Command"
            categoryId="zadawdaw.commands.category"
            id="zadawdaw.commands.sampleCommand">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            commandId="zadawdaw.commands.sampleCommand"
            class="zadawdaw.handlers.SampleHandler">
      </handler>
   </extension>

   <extension point="org.eclipse.jdt.ui.javaCompletionProposalComputer"
   id="textual_proposals"
   name="Text Proposals">
   <proposalCategory icon="icons/wordcompletions.png"/>
 </extension>

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               label="Sample Menu"
               mnemonic="M"
               id="zadawdaw.menus.sampleMenu">
            <command
                  commandId="zadawdaw.commands.sampleCommand"
                  mnemonic="S"
                  id="zadawdaw.menus.sampleCommand">
            </command>
         </menu>
      </menuContribution>
      <menuContribution
            locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
         <toolbar
               id="zadawdaw.toolbars.sampleToolbar">
            <command
                  commandId="zadawdaw.commands.sampleCommand"
                  icon="icons/sample.png"
                  tooltip="Say hello world"
                  id="zadawdaw.toolbars.sampleCommand">
            </command>
         </toolbar>
      </menuContribution>
   </extension>

</plugin>

Solution

  • It seems the main mistake so far is restricting the proposal provider to the partition type __java_javadoc.

    You were already looking (almost?) in the right place - look for type. Please see that this definition also refers to IJavaPartitions. In terse words, partitions refer to segments of text in an editor. The linked interface lists the possible constants together with a short description.

    If you want completions to be proposed while editing Java code, try IJavaPartitions.JAVA_PARTITIONING.