Search code examples
eclipse-plugincode-completioncontent-assist

PyDev custom code complete plug-in only detects every other key stroke


I have an Eclipse plug-in that I created to add Code Completion entries. I configured Eclipse to automatically show code completion as I type (Windows | Preferences | PyDev | Editor | Code Completion | Request completion on all letter chars and '_'?). At first when I typed I kept getting the templates showing instead of my code completion entries, so I removed all the templates ( Windows | Preferences | PyDev | Templates --selected all, then "Remove"). Now when I type it works properly for every other key pressed. For example, when I type 'print', the code completion list drops down with my entries as expected when I press 'p'. However, when I press 'r', the list disappears. When I press 'i' the list shows again, but disappears when I press the next key ('n'), etc. Is this a Pydev defect, or am I doing something wrong? It works fine for the templates and other default code completion, just not for my plug-in. Here is a code snipped of a watered down version of my code:

//...
public class MyPlugin implements IPyDevCompletionParticipant
@Override
public Collection<Object> getGlobalCompletions(CompletionRequest arg0,
    ICompletionState arg1) throws MisconfigurationException {
    String replacementString = "{" + arg0.qualifier + "}";
    int replacementOffset = arg0.documentOffset - arg0.qlen;
    int replacementLength = arg0.qlen;
    int cursorPosition = arg0.documentOffset;
    String displayString = arg0.qualifier;
    final IContextInformation contextInformation = new ContextInformation(
            "displayStr", "message");
    String additionalProposalInfo = "additionalProposalInfo";
    final String bundle = "com.github.EclipseChameleonPlugins";
    final org.eclipse.swt.graphics.Image image = new org.eclipse.swt.graphics.Image(getDisplay(), locateFile(bundle, "icons/smiley.gif").getPath());
    arg0.showTemplates = false;

    final CompletionProposal proposal = new CompletionProposal(
        replacementString, replacementOffset, replacementLength,
        cursorPosition, image, displayString, contextInformation, additionalProposalInfo);

    List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    // ADD IT...
    proposals.add(proposal);
    final Collection<Object> returnProposals = new ArrayList<Object>(
            proposals);
    return returnProposals;
}

I have searched Google and StackOverflow, and have seen very little about code development for PyDev plug-ins, and nothing that mentions or addresses this issue.

Here are a few of the links I have looked at, but none have answered my question:


Solution

  • Well, plain PyDev behaves as expected for me (i.e.: the code completions appear on all the key strokes).

    Now, let's see if we can track it down a bit better:

    1. instead of removing the templates, what you should do is go to the preferences > pydev > editor > code completion (ctx insensitive and common tokens) and disable the 'use common tokens auto code completion?'.

    2. The reference code for you to follow is: com.python.pydev.codecompletion.participant.ImportsCompletionParticipant and com.python.pydev.codecompletion.ctxinsensitive.CtxParticipant (i.e.: the IPyDevCompletionParticipant interface -- as you're doing already)

    3. I think the main issue you're having is because you're not implementing the additional extensions for completions (to validate its context and keep it there) -- either you can make your own subclass of org.python.pydev.editor.codecompletion.AbstractPyCompletionProposalExtension2 or you can use org.python.pydev.editor.codecompletion.PyLinkedModeCompletionProposal (just constructing it with the proper parameters -- I believe it supports having a null IToken -- and you can pass an image to it which will be used if the token is null).

    4. You should probably not mess with the CompletionRequest at that point (when it gets there to an extension it should be considered immutable -- even if it's not in reality).