Eclipse PDE: Everything about Editor Part 5

Code Completion using Content Assist

This post explains about completion proposal feature of eclipse editor framework. This is part of Everything About Editor series. Content assist provides content assist help to developer while writing code in editor. It helps from static suggestion of language keyword to methods suggestion for dynamic reference on hitting “Ctrl-Space” or entering  any of activator character like ‘.’.

Following two interfaces plays key role in implementing code completion for customer editor.

  • ICompletionProposal – Responsible for providing set of proposals text, associated icons etc.
  • IContentAssistProcessor – Responsible for calculating the completion proposals at a point in the editor. It also defines character on which it shoud auto-activate.

In this example we are going to see providing content assist then user enters ‘#’. To achieve this we need to implement IComppletionProposal that will return set of proposals as well as tells framework which character activate content assist by overriding getCompletionProposalAutoActivationCharacters() method from same interface.

public class MyContentAssistProcessor implements IContentAssistProcessor {
 
    @Override
    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
            int offset) {
        String text = getTobeCompleteText(viewer.getDocument(), offset);
        return new ICompletionProposal[]{
            new MyCompletionProposal("Proposal 1",offset,text.length()),
            new MyCompletionProposal("Proposal 2",offset,text.length())
        };
    }
 
    @Override
    public IContextInformation[] computeContextInformation(ITextViewer viewer,
            int offset) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public char[] getCompletionProposalAutoActivationCharacters() {
        return new char[] {'#'};
    }
 
    @Override
    public char[] getContextInformationAutoActivationCharacters() {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public String getErrorMessage() {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public IContextInformationValidator getContextInformationValidator() {
        // TODO Auto-generated method stub
        return null;
    }
    
    private boolean isWhiteSpace(char c){
        if(c=='\n'||c=='\t'||c==' ')
            return true;
        return false;
    }
    
    private String getTobeCompleteText(IDocument document, int currentOffset){
 
        int count = currentOffset;
        String tobeCompleteText="";
        try{
            while(--count>=0){
                char c = ' ';
                c = document.getChar(count);
 
                if(isWhiteSpace(c)){
                    break;
                }
            }
            tobeCompleteText = document.get(count+1, currentOffset-(count+1));
 
        }catch (Exception e) {
            e.printStackTrace();
        }
        return tobeCompleteText;
    }
}

Second task is to implement ICompletionProposal interface that actually handles proposals. It provides text and image to be displayed in proposal window, inserting proposal text to editor.



public class MyCompletionProposal implements ICompletionProposal {
 
    String text;
    int offset,contextTextLength;
    MyCompletionProposal(String text,int offset,int contextTextLength){
        this.text = text;
        this.offset = offset;
        this.contextTextLength = contextTextLength;
    }
    @Override
    public void apply(IDocument document) {
        try {
            document.replace(offset, 0, text);
            
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public Point getSelection(IDocument document) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public String getAdditionalProposalInfo() {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public String getDisplayString() {
        return text;
    }
 
    @Override
    public Image getImage() {
        return null;
    }
 
    @Override
    public IContextInformation getContextInformation() {
        return null;
    }
 
}

And Final task to update SourceViewConfiguration to install the custom content assist proposal


image

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice work man! Your PDE series is clear and precise.

    Can you tell me how to add the default content assist for (say) javascript provided by eclipse to my editor.I don't want to write the entire content assist as is given by eclipse again for my editor.

    Thanks in advance.

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester