Eclipse PDE: Everything about Editor Part 4

Auto-Edit Strategy

This post of Everything About Editor series explains about editors auto complete feature. Auto edit feature of any editor helps developer while writing code, for example in java editor of eclipse provides auto complete features for string quotes , open braces etc. When you type open curly braces ‘{‘ , eclipse java editor auto inserts closing curly braces considering line and indentation.

Here I am going to implement auto-edit strategy for open single , double quotes and opening curly braces ‘{‘. To use auto edits, you must implement the org.eclipse.jface.text.IAutoEditStrategy interface, which contains just one method:

void customizeDocumentCommand(IDocument document,DocumentCommand command);


This is called each time you make a change to the document with the
DocumentCommand object containing the information about that change. So, adding
indents or making any change to the text being changed is as simple as modifying
the DocumentCommand object.


1. Implement IAutoEditStrategy



package com.ydtech.eclipse.editor;
 
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
 
public class MyEditStrategy implements IAutoEditStrategy {
 
    @Override
    public void customizeDocumentCommand(IDocument document,
            DocumentCommand command) {
        try{
        if(command.text.equals("'")){
            command.text="''";
            configureCaret(command);
        } else if(command.text.equals("\"")){
            command.text ="\"\"";
            configureCaret(command);
        } else if(command.text.equals("{")){
            int currentLine = document.getLineOfOffset(command.offset);
            String indent = getIntend(document, currentLine);
            command.text="{"+"\r\n"+indent+"}";
            command.caretOffset = command.offset+1;
            command.shiftsCaret = false;
        }
        }catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
    
    private void configureCaret(DocumentCommand command){
        command.caretOffset = command.offset + 1;
        command.shiftsCaret = false;
    }
    
    private String getIntend(IDocument document, int currentLine) throws BadLocationException{
        if(currentLine > -1){
            int start = document.getLineOffset(currentLine);
            int end = document.getLineLength(currentLine)-1 +start;
            int whiteSpaceEnd = getEndOfWhiteSpace(document, start, end);
            return document.get(start, whiteSpaceEnd-start);
        }
        return "";
    }
    
    private int getEndOfWhiteSpace(IDocument document, int start, int end) throws BadLocationException{
        while(start<end){
            char c = document.getChar(start);
            if(c==' ' || c=='\t'){
                start++;
                continue;
            }
            return start;
        }
        return end;
    }
 
}

Above code listen determine when quote or double quote or opening curly brace inserted to editor and insert corresponding pairing character.


2. Configure SourceViewConfiguration , override getAutoEditStrategies method



@Override
    public IAutoEditStrategy[] getAutoEditStrategies(
            ISourceViewer sourceViewer, String contentType) {
        IAutoEditStrategy strategy = IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)?new MyEditStrategy() : new DefaultIndentLineAutoEditStrategy();
        return new IAutoEditStrategy[]{strategy};
    }

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester