Commit 890dc8eb authored by Jay Herron's avatar Jay Herron

Adding partitioning to fix multi-line editing issues

parent c28f51ff
......@@ -5,6 +5,7 @@ import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
......@@ -22,42 +23,46 @@ public class TrioConfiguration extends TextSourceViewerConfiguration {
public TrioConfiguration(ColorManager colorManager) {
this.colorManager = colorManager;
scanner = new TrioScanner(colorManager);
IToken defaultToken = new Token(new TextAttribute(colorManager.getColor(ITrioColorConstants.DEFAULT)));
scanner.setDefaultReturnToken(defaultToken);
}
@Override
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
return new String[] {
IDocument.DEFAULT_CONTENT_TYPE,
TrioPartitionScanner.TRIO_RECORD
TrioPartitionScanner.TRIO_COMMENT,
TrioPartitionScanner.TRIO_STRING
};
}
protected TrioScanner getTrioScanner() {
if (scanner == null) {
scanner = new TrioScanner(colorManager);
scanner.setDefaultReturnToken(
new Token(
new TextAttribute(
colorManager.getColor(ITrioColorConstants.DEFAULT))));
}
protected TrioScanner getScanner() {
return scanner;
}
@Override
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler reconciler = new PresentationReconciler();
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTrioScanner());
// Set how we deal with default content partitions
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getScanner());
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
/*
NonRuleBasedDamagerRepairer ndr = new NonRuleBasedDamagerRepairer(
new TextAttribute(colorManager.getColor(ITrioColorConstants.COMMENT)));
reconciler.setDamager(ndr, TrioPartitionScanner.TRIO_COMMENT);
reconciler.setRepairer(ndr, TrioPartitionScanner.TRIO_COMMENT);
*/
// Set how we deal with comment partitions
TextAttribute comment = new TextAttribute(colorManager.getColor(ITrioColorConstants.COMMENT));
NonRuleBasedDamagerRepairer commentDr = new NonRuleBasedDamagerRepairer(comment);
reconciler.setDamager(commentDr, TrioPartitionScanner.TRIO_COMMENT);
reconciler.setRepairer(commentDr, TrioPartitionScanner.TRIO_COMMENT);
// Set how we deal with string partitions
TextAttribute string = new TextAttribute(colorManager.getColor(ITrioColorConstants.STRING));
NonRuleBasedDamagerRepairer stringDr = new NonRuleBasedDamagerRepairer(string);
reconciler.setDamager(stringDr, TrioPartitionScanner.TRIO_STRING);
reconciler.setRepairer(stringDr, TrioPartitionScanner.TRIO_STRING);
return reconciler;
}
......
......@@ -11,15 +11,12 @@ public class TrioDocumentProvider extends FileDocumentProvider {
@Override
protected IDocument createDocument(Object element) throws CoreException {
IDocument document = super.createDocument(element);
// TODO Implement partitioning once I know what that is.
/* // This caused there to be strange areas that didn't get any formatting.
if (document != null) {
IDocumentPartitioner partitioner = new FastPartitioner(new TrioPartitionScanner(),
new String[] { TrioPartitionScanner.TRIO_RECORD });
new String[] { TrioPartitionScanner.TRIO_COMMENT, TrioPartitionScanner.TRIO_STRING });
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}
*/
return document;
}
}
\ No newline at end of file
package org.jayherron.trioeditor.editors;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.*;
/**
* The parition scanner allows us to define different functionality in
* different parts of the file. This is from the default
* editor plugin stub, and is not currently used.
* different parts of the file.
*
* We must define multi-line rules as unique partitions because otherwise
* it will break the damage/repair process for the line that was edited.
* This is because the default damage/repairer works on a line-by-line basis.
* Instead, we must partition each multi-line format and force it to just
* return the correct text attribute.
*
* @author jay
*
*/
public class TrioPartitionScanner extends RuleBasedPartitionScanner {
public final static String TRIO_RECORD = "__trio_record";
public final static String TRIO_COMMENT = "__trio_comment";
public final static String TRIO_STRING = "__trio_string";
public TrioPartitionScanner() {
IToken trioRecord = new Token(TRIO_RECORD);
IToken comment = new Token(TRIO_COMMENT);
IToken string = new Token(TRIO_STRING);
IPredicateRule[] rules = new IPredicateRule[1];
IPredicateRule[] rules = new IPredicateRule[2];
// Multiline-comment
rules[0] = new MultiLineRule("---", "---", trioRecord);
// Comment
rules[0] = new MultiLineRule("/*", "*/", comment);
// String: triple-quoted
rules[1] = new MultiLineRule("\"\"\"", "\"\"\"", string);
setPredicateRules(rules);
}
......
......@@ -22,35 +22,29 @@ public class TrioScanner extends RuleBasedScanner {
IToken keyword = new Token(new TextAttribute(manager.getColor(ITrioColorConstants.KEYWORD)));
IToken bracket = new Token(new TextAttribute(manager.getColor(ITrioColorConstants.BRACKET)));
IRule[] rules = new IRule[17];
// Multi-line rules
// Comment
rules[0] = new MultiLineRule("/*", "*/", comment, '\\');
// String: triple-quoted
rules[1] = new MultiLineRule("\"\"\"", "\"\"\"", string, '\\');
IRule[] rules = new IRule[15];
// End-of-line rules
// Comment
rules[2] = new EndOfLineRule("//", comment, '\\');
rules[0] = new EndOfLineRule("//", comment, '\\');
// Single-line rules
// Rec split
rules[3] = new SingleLineRule("---", "", split);
rules[1] = new SingleLineRule("---", "", split);
// String
rules[4] = new SingleLineRule("\"", "\"", string, '\\');
rules[2] = new SingleLineRule("\"", "\"", string, '\\');
// Uri
rules[5] = new SingleLineRule("`", "`", uri, '\\');
rules[3] = new SingleLineRule("`", "`", uri, '\\');
// Coordinates
rules[6] = new SingleLineRule("C(", ")", coordinate);
rules[4] = new SingleLineRule("C(", ")", coordinate);
// Numbers
rules[7] = new HaystackNumberRule(number);
rules[5] = new HaystackNumberRule(number);
// Refs
rules[8] = new RefRule(ref);
rules[6] = new RefRule(ref);
// Add generic whitespace rule.
rules[9] = new WhitespaceRule(new TrioWhitespaceDetector());
rules[7] = new WhitespaceRule(new TrioWhitespaceDetector());
// Keywords
KeywordRule keywords = new KeywordRule();
......@@ -68,15 +62,15 @@ public class TrioScanner extends RuleBasedScanner {
keywords.addWord("null", keyword);
keywords.addWord("true", keyword);
keywords.addWord("false", keyword);
rules[10] = keywords;
rules[8] = keywords;
// Character rules
rules[11] = new CharRule('(', bracket);
rules[12] = new CharRule(')', bracket);
rules[13] = new CharRule('{', bracket);
rules[14] = new CharRule('}', bracket);
rules[15] = new CharRule('[', bracket);
rules[16] = new CharRule(']', bracket);
rules[9] = new CharRule('(', bracket);
rules[10] = new CharRule(')', bracket);
rules[11] = new CharRule('{', bracket);
rules[12] = new CharRule('}', bracket);
rules[13] = new CharRule('[', bracket);
rules[14] = new CharRule(']', bracket);
setRules(rules);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment