Commit 1e2b090c authored by jayherron's avatar jayherron

Initial commit

parents
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TrioEditor
Bundle-SymbolicName: TrioEditor;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Jay Herron
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
org.eclipse.jface.text,
org.eclipse.ui.editors
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.editors">
<editor
name="Trio Editor"
extensions="trio"
icon="icons/sample.gif"
contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"
class="trioeditor.editors.TrioEditor"
id="trioeditor.editors.TrioEditor">
</editor>
</extension>
</plugin>
package trioeditor.editors;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
public class ColorManager {
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;
}
}
package trioeditor.editors;
import org.eclipse.swt.graphics.RGB;
public interface ITrioColorConstants {
RGB COMMENT = new RGB(128, 0, 0);
RGB COORDINATE = new RGB(0, 128, 128);
RGB DEFAULT = new RGB(0, 0, 0);
RGB PROC_INSTR = new RGB(128, 128, 128);
RGB STRING = new RGB(0, 128, 0);
RGB TAG = new RGB(0, 0, 128);
}
package trioeditor.editors;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.presentation.IPresentationDamager;
import org.eclipse.jface.text.presentation.IPresentationRepairer;
import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.custom.StyleRange;
public class NonRuleBasedDamagerRepairer implements IPresentationDamager, IPresentationRepairer {
/** The document this object works on */
protected IDocument fDocument;
/** The default text attribute if non is returned as data by the current token */
protected TextAttribute fDefaultTextAttribute;
public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
Assert.isNotNull(defaultTextAttribute);
fDefaultTextAttribute = defaultTextAttribute;
}
@Override
public void setDocument(IDocument document) {
fDocument = document;
}
/**
* Returns the end offset of the line that contains the specified offset or
* if the offset is inside a line delimiter, the end offset of the next line.
*
* @param offset the offset whose line end offset must be computed
* @return the line end offset for the given offset
* @exception BadLocationException if offset is invalid in the current document
*/
protected int endOfLineOf(int offset) throws BadLocationException {
IRegion info = fDocument.getLineInformationOfOffset(offset);
if (offset <= info.getOffset() + info.getLength())
return info.getOffset() + info.getLength();
int line = fDocument.getLineOfOffset(offset);
try {
info = fDocument.getLineInformation(line + 1);
return info.getOffset() + info.getLength();
} catch (BadLocationException x) {
return fDocument.getLength();
}
}
@Override
public IRegion getDamageRegion(
ITypedRegion partition,
DocumentEvent event,
boolean documentPartitioningChanged) {
if (!documentPartitioningChanged) {
try {
IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
int start = Math.max(partition.getOffset(), info.getOffset());
int end =
event.getOffset()
+ (event.getText() == null
? event.getLength()
: event.getText().length());
if (info.getOffset() <= end
&& end <= info.getOffset() + info.getLength()) {
// optimize the case of the same line
end = info.getOffset() + info.getLength();
} else
end = endOfLineOf(end);
end =
Math.min(
partition.getOffset() + partition.getLength(),
end);
return new Region(start, end - start);
} catch (BadLocationException x) {
}
}
return partition;
}
@Override
public void createPresentation(TextPresentation presentation, ITypedRegion region) {
addRange(
presentation,
region.getOffset(),
region.getLength(),
fDefaultTextAttribute);
}
/**
* Adds style information to the given text presentation.
*
* @param presentation the text presentation to be extended
* @param offset the offset of the range to be styled
* @param length the length of the range to be styled
* @param attr the attribute describing the style of the range to be styled
*/
protected void addRange(
TextPresentation presentation,
int offset,
int length,
TextAttribute attr) {
if (attr != null)
presentation.addStyleRange(
new StyleRange(
offset,
length,
attr.getForeground(),
attr.getBackground(),
attr.getStyle()));
}
}
\ No newline at end of file
package trioeditor.editors;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
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.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
public class TrioConfiguration extends SourceViewerConfiguration {
private TrioDoubleClickStrategy doubleClickStrategy;
private TrioScanner scanner;
private ColorManager colorManager;
public TrioConfiguration(ColorManager colorManager) {
this.colorManager = colorManager;
}
@Override
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
return new String[] {
IDocument.DEFAULT_CONTENT_TYPE,
TrioPartitionScanner.TRIO_RECORD
};
}
@Override
public ITextDoubleClickStrategy getDoubleClickStrategy(
ISourceViewer sourceViewer,
String contentType) {
if (doubleClickStrategy == null)
doubleClickStrategy = new TrioDoubleClickStrategy();
return doubleClickStrategy;
}
protected TrioScanner getTrioScanner() {
if (scanner == null) {
scanner = new TrioScanner(colorManager);
scanner.setDefaultReturnToken(
new Token(
new TextAttribute(
colorManager.getColor(ITrioColorConstants.DEFAULT))));
}
return scanner;
}
@Override
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler reconciler = new PresentationReconciler();
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTrioScanner());
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);
*/
return reconciler;
}
}
\ No newline at end of file
package trioeditor.editors;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.ui.editors.text.FileDocumentProvider;
public class TrioDocumentProvider extends FileDocumentProvider {
@Override
protected IDocument createDocument(Object element) throws CoreException {
IDocument document = super.createDocument(element);
if (document != null) {
IDocumentPartitioner partitioner = new FastPartitioner(new TrioPartitionScanner(),
new String[] { TrioPartitionScanner.TRIO_RECORD });
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}
return document;
}
}
\ No newline at end of file
package trioeditor.editors;
import org.eclipse.jface.text.*;
public class TrioDoubleClickStrategy implements ITextDoubleClickStrategy {
protected ITextViewer fText;
@Override
public void doubleClicked(ITextViewer part) {
int pos = part.getSelectedRange().x;
if (pos < 0)
return;
fText = part;
if (!selectComment(pos)) {
selectWord(pos);
}
}
protected boolean selectComment(int caretPos) {
IDocument doc = fText.getDocument();
int startPos, endPos;
try {
int pos = caretPos;
char c = ' ';
while (pos >= 0) {
c = doc.getChar(pos);
if (c == '\\') {
pos -= 2;
continue;
}
if (c == Character.LINE_SEPARATOR || c == '\"')
break;
--pos;
}
if (c != '\"')
return false;
startPos = pos;
pos = caretPos;
int length = doc.getLength();
c = ' ';
while (pos < length) {
c = doc.getChar(pos);
if (c == Character.LINE_SEPARATOR || c == '\"')
break;
++pos;
}
if (c != '\"')
return false;
endPos = pos;
int offset = startPos + 1;
int len = endPos - offset;
fText.setSelectedRange(offset, len);
return true;
} catch (BadLocationException x) {
}
return false;
}
protected boolean selectWord(int caretPos) {
IDocument doc = fText.getDocument();
int startPos, endPos;
try {
int pos = caretPos;
char c;
while (pos >= 0) {
c = doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
--pos;
}
startPos = pos;
pos = caretPos;
int length = doc.getLength();
while (pos < length) {
c = doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c))
break;
++pos;
}
endPos = pos;
selectRange(startPos, endPos);
return true;
} catch (BadLocationException x) {
}
return false;
}
private void selectRange(int startPos, int stopPos) {
int offset = startPos + 1;
int length = stopPos - offset;
fText.setSelectedRange(offset, length);
}
}
\ No newline at end of file
package trioeditor.editors;
import org.eclipse.ui.editors.text.TextEditor;
public class TrioEditor extends TextEditor {
private ColorManager colorManager;
public TrioEditor() {
super();
colorManager = new ColorManager();
setSourceViewerConfiguration(new TrioConfiguration(colorManager));
setDocumentProvider(new TrioDocumentProvider());
}
@Override
public void dispose() {
colorManager.dispose();
super.dispose();
}
}
package trioeditor.editors;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.*;
public class TrioPartitionScanner extends RuleBasedPartitionScanner {
public final static String TRIO_RECORD = "__trio_record";
public TrioPartitionScanner() {
IToken trioRecord = new Token(TRIO_RECORD);
IPredicateRule[] rules = new IPredicateRule[1];
// Multiline-comment
rules[0] = new MultiLineRule("---", "---", trioRecord);
setPredicateRules(rules);
}
}
package trioeditor.editors;
import org.eclipse.jface.text.rules.*;
import org.eclipse.jface.text.*;
/**
* Defines the rules by
* @author jay
*
*/
public class TrioScanner extends RuleBasedScanner {
public TrioScanner(ColorManager manager) {
IToken comment = new Token(new TextAttribute(manager.getColor(ITrioColorConstants.COMMENT)));
IToken string = new Token(new TextAttribute(manager.getColor(ITrioColorConstants.STRING)));
IToken coordinate = new Token(new TextAttribute(manager.getColor(ITrioColorConstants.COORDINATE)));
IRule[] rules = new IRule[5];
// Multi-line rules
// Comment
rules[0] = new MultiLineRule("/*", "*/", comment);
// Single-line rules
// Comment
rules[1] = new SingleLineRule("//", "", comment);
// String
rules[2] = new SingleLineRule("\"", "\"", string);
// Coordinates
rules[3] = new SingleLineRule("C(", ")", coordinate);
// Add generic whitespace rule.
rules[4] = new WhitespaceRule(new TrioWhitespaceDetector());
setRules(rules);
}
}
package trioeditor.editors;
import org.eclipse.jface.text.rules.IWhitespaceDetector;
public class TrioWhitespaceDetector implements IWhitespaceDetector {
@Override
public boolean isWhitespace(char c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
}
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