Eclipse PDE: Everything about Editor Part 2

Document Partition

In this series of “Eclipse PDE: Everything about Editor”, this post explains how to configure your document to represent different partition. Document partitions helps to manipulate different part of document in different way. For example there is not need to showing content assist in single line comment or multi line comment.
Here I am explaining defining partition for single and multi line comments


1. Define a custom partition scanner
Eclipse framework provides Rule based partition scanner, which allows us to define single or multi line rule for partitioning document content. We need to define Custome partition scanner extending org.eclipse.jface.text.rules.RuleBasedPartitionScanner. Override the constructor and define rules for partition . Below code snippets defines rule for single and multi line comments
public class MyPartitionScanner extends RuleBasedPartitionScanner {
    //Define COnstants for supported partition types 
    public static final String SINGLE_LINE_COMMENT = "SINGLE_LINE_COMMENT";
    public static final String MULTI_LINE_COMMENT = "MULTI_LINE_COMMENT";
    public static final String[] PARTITION_TYPES = {SINGLE_LINE_COMMENT, MULTI_LINE_COMMENT};
    
    public MyPartitionScanner() {
        IToken singleLineToken = new Token(SINGLE_LINE_COMMENT);
        IToken multiLineToken = new Token(MULTI_LINE_COMMENT);
        
        List rules = new ArrayList();
        
        rules.add(new EndOfLineRule("//", singleLineToken));
        rules.add(new MultiLineRule("/*","*/",multiLineToken));
        
        IPredicateRule[] result= new IPredicateRule[rules.size()];
        rules.toArray(result);
        setPredicateRules(result);
    }
}

2. Connect the above scanner to your DocumentProvider

Document provider connect partition scanner to underline document. This will be done in DocumentProvider’s constructor ( Defined in previous post)


@Override
    protected IDocument createDocument(Object element) throws CoreException {
        IDocument doc = super.createDocument(element);
        if (doc instanceof IDocumentExtension3) {
            IDocumentExtension3 extension3 = (IDocumentExtension3) doc;
            IDocumentPartitioner partitioner = new FastPartitioner(MyPlugin.getDefault().getMyPartitionScanner(),
                    MyPartitionScanner.PARTITION_TYPES);
            extension3.setDocumentPartitioner(MyPlugin.MY_PARTITIONING,
                    partitioner);
            partitioner.connect(doc);
        }
        return doc;
    }

In above code, MyPlugin calss is being used to get instance of document partition class.


public class MyPlugin extends AbstractUIPlugin {
    
    public final static String MY_PARTITIONING = "___my__partitioning____";
    private MyPartitionScanner fPartitionScanner;
    private static MyPlugin myPlugin =null;
    public MyPartitionScanner getMyPartitionScanner() {
        if (fPartitionScanner == null)
        fPartitionScanner= new MyPartitionScanner();
        return fPartitionScanner;
    }
    
    public static MyPlugin getDefault(){
        if(myPlugin == null){
            myPlugin = new MyPlugin();
        }
        return myPlugin;
    }
}

3. Tell SourceViewConfiguration about document partition

@Override
    public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
        return MyPlugin.MY_PARTITIONING;
    }
    
    @Override
    public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
        return new String[] {IDocument.DEFAULT_CONTENT_TYPE,MyPartitionScanner.MULTI_LINE_COMMENT,MyPartitionScanner.SINGLE_LINE_COMMENT};
    }

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester