Search code examples
eclipsecontent-assist

How to make eclipse content assist insert instead of overwrite


I am writing a plugin for Eclipse and I am trying to implement content assist. The code below works with the exception that it overwrites the existing text and I want it to insert instead.

Any help gratefully received.

The ContentAssistantProvider:

package astra.ide.editor.astra;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;

import astra.ast.core.ASTRACore;
import astra.ast.core.ParseException;
import astra.ast.jdt.JDTHelper;

public class ASTRAContentAssistantProcessor implements IContentAssistProcessor {
    static Set<Character> set = new HashSet<Character>();

    static {
        set.add(' ');
        set.add('(');
        set.add(',');
        set.add(')');
        set.add(';');
        set.add('{');
        set.add('}');
    }

    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
        IDocument doc = viewer.getDocument();

        String context = "";
        int i = offset-2;
        try {
            char ch = doc.getChar(i);
            while (!set.contains(ch)) {
                context = ch + context;
                ch = doc.getChar(--i);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        String text = doc.get();
        String cls = "";
        int index = text.indexOf("package");
        if (index > -1) {
            int index2 = text.indexOf(";", index);
            cls = text.substring(index+8, index2-1).trim() + ".";
        }


        index = text.indexOf("agent", index);
        int index2 = text.indexOf("extends", index);
        if (index2 == -1) {
            index2 = text.indexOf("{", index);
        }
        System.out.println("cls: " + text.substring(index+6, index2-1).trim());
        cls += text.substring(index+6, index2-1).trim();


        List<ICompletionProposal> list = new ArrayList<ICompletionProposal>();

        IEditorPart  editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        if(editorPart != null) {
            IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ;
            IFile file = input.getFile();

            JDTHelper helper = new JDTHelper(file.getProject());
            try {
                String moduleClass = ASTRACore.getModuleClass(helper, cls, context);
                IType type = helper.resolveClass(moduleClass);
                for (IMethod mthd : type.getMethods()) {
                    String template = mthd.getElementName() + "()";
                    list.add(new CompletionProposal(template, offset, template.length(), template.length()));
                }

            } catch (ParseException e) {
                e.printStackTrace();
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }

        return list.toArray(new ICompletionProposal[list.size()]);
    }

    public IContextInformation[] computeContextInformation(ITextViewer viewer,
            int offset) {
        return null;
    }

    public char[] getCompletionProposalAutoActivationCharacters() {
        return new char[] { '.' };
    }

    public char[] getContextInformationAutoActivationCharacters() {
        return null;
    }

    public String getErrorMessage() {
        return null;
    }

    public IContextInformationValidator getContextInformationValidator() {
        return null;
    }
}

The code i added to the SourceConfigurationViewer is:

public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
    ContentAssistant assistant = new ContentAssistant();
    assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
    assistant.setContentAssistProcessor(new ASTRAContentAssistantProcessor(),IDocument.DEFAULT_CONTENT_TYPE);
    assistant.enableAutoActivation(true);

    return assistant;
}

Solution

  • In you CompletionProposal creation:

    new CompletionProposal(template, offset, template.length(), template.length())
    

    the third argument is the length of text to replace at offset, so using template.length() causes the template to overwrite. To insert use:

    new CompletionProposal(template, offset, 0, template.length())
    

    using 0 as the third argument causes the text to be inserted.