如何使eclipse内容辅助插入而不是覆盖

时间:2014-10-19 17:53:15

标签: eclipse content-assist

我正在为Eclipse编写插件,我正在尝试实现内容辅助。下面的代码适用于它覆盖现有文本的例外,我希望它插入。

感激不尽的任何帮助。

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;
    }
}

我添加到SourceConfigurationViewer的代码是:

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;
}

1 个答案:

答案 0 :(得分:0)

在你CompletionProposal创作中:

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

第三个参数是要在offset替换的文本的长度,因此使用template.length()会导致模板被覆盖。插入使用:

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

使用0作为第三个参数会导致插入文本。