Eclipse PDE: Everything about Editor Part 3

Syntax Highlighting

Part 3 of Eclipse PDE: Everything about Editor series deals with Syntax coloring. In previous post I have explained about all Eclipse Editor relegated concepts and document partition.

I have tried to explains Syntax highlighting in editor in this post. Syntax highlighting helps to provides different color and styles to different part of code. Eclipse provides a presentation reconciler, which divides the document into a set of tokens that define the background and foreground color, along with the font style for a section of text.

The document partitions define the content types within a document, while the presentation reconciler divided those individual content types into tokens for sets of characters that share the same color and font style. The presentation of the document must be maintained while the user continues to modify it. This is done through the use of a damager and a repairer.

A Damager takes the description of how the document was modified and returns a description of regions of the document that must be updated. For example, if a user types /*, all the text in the document up to */ must be repaired

The Repairer takes care of actually repairing the region of the document that the damager output by using the rules for dividing the region into tokens and the color and font information associated with the tokens.

Following steps explains process of defining syntax highlighter for custom editor.

1. Define Color provider

This class is responsible for providing color for specific set of tokens.

public class MyColorProvider {
    public static final RGB MULTI_LINE_COMMENT = new RGB(128, 0, 0);
    public static final RGB SINGLE_LINE_COMMENT = new RGB(128, 128, 0);
    public static final RGB DEFAULT = new RGB(0, 0, 0);
    protected Map fColorTable = new HashMap(10);
 
    public void dispose() {
        Iterator e = fColorTable.values().iterator();
        while (e.hasNext())
            ((Color) e.next()).dispose();
    }
 
    public Color getColor(RGB rgb) {
        Color color = (Color) fColorTable.get(rgb);
        if (color == null) {
            color = new Color(Display.getCurrent(), rgb);
            fColorTable.put(rgb, color);
        }
        return color;
    }
}

2. Create rule for document scanner.



public class MyCodeScanner extends RuleBasedScanner {
 
    public MyCodeScanner(MyColorProvider provider) {
        IToken singleLineComment=
        new Token(new
        TextAttribute(provider.getColor(MyColorProvider.SINGLE_LINE_COMMENT)));
        IToken multiLineComment=
            new Token(new
            TextAttribute(provider.getColor(MyColorProvider.MULTI_LINE_COMMENT)));
        List rules= new ArrayList();
        // Add rule for single & multi line comments.
        rules.add(new EndOfLineRule("//", singleLineComment));
        rules.add(new MultiLineRule("/*","*/", multiLineComment));
        
        
        IRule[] result= new IRule[rules.size()];
        rules.toArray(result);
        setRules(result);
    }
}

3. Provide PresentationReconciler in SourceViewConfiguration, which defines damager & repairer for each segment of code.



@Override
    public IPresentationReconciler getPresentationReconciler(
            ISourceViewer sourceViewer) {
        PresentationReconciler rc = new PresentationReconciler();
        
        DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getCodeScanner());
        rc.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
        rc.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
 
        dr= new DefaultDamagerRepairer(getCodeScanner());
        rc.setDamager(dr, MyPartitionScanner.SINGLE_LINE_COMMENT);
        rc.setRepairer(dr, MyPartitionScanner.SINGLE_LINE_COMMENT);
        
        dr= new DefaultDamagerRepairer(getCodeScanner());
        rc.setDamager(dr, MyPartitionScanner.MULTI_LINE_COMMENT);
        rc.setRepairer(dr, MyPartitionScanner.MULTI_LINE_COMMENT);
        
        return rc;
    }
    
    public MyCodeScanner getCodeScanner(){
        if(cScanner == null){
            cScanner = new MyCodeScanner(new MyColorProvider());
        }
        return cScanner;
    }

 

Run the plug-in as Eclipse Application, create .properties file (As we have defined custom editor in previous post in this series) and try typing single line comment and multi line comment. You will notice , color of this comment will be different than normal code section as shown in figure below.

 

image

 

 

 

Comments

  1. I'm not sure that the DefaultDamagerRepairer will correctly manage your multi-line comment and text following it. Try including some other text after it, and then delete the "*/" and type it in again. I think the following text won't be updated properly.

    If I'm correct, the easiest solution would be to set up the multi-line comment as its own partition, because edits that alter partitioning will result in repartitioning more of the following text. You can either use a deprecated constructor for DefaultDamagerRepairer(eep!) to set the color and font, or grab a copy of NonRuleBasedDamagerRepairer.java from one of the examples to do the same thing.

    ReplyDelete
  2. NonRuleBasedDamagerRepairer.java really helpful. thanks.

    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