Skip to content

Commit

Permalink
text highlighting was added to the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
vahdat-ab committed Jul 9, 2016
1 parent 36f11ee commit 87d423e
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 2 deletions.
2 changes: 2 additions & 0 deletions org.cruise.umple.eclipse.plugin.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<import plugin="org.eclipse.core.runtime"/>
<import plugin="org.eclipse.ui.ide" version="3.11.0" match="greaterOrEqual"/>
<import plugin="org.eclipse.ui.console" version="3.6.100" match="greaterOrEqual"/>
<import plugin="org.eclipse.ui.editors" version="3.10.0" match="greaterOrEqual"/>
<import plugin="org.eclipse.jface.text" version="3.11.0" match="greaterOrEqual"/>
<import plugin="org.eclipse.core.resources"/>
</requires>

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.cruise.umple.eclipse.plugin.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cruise.umple.eclipse.plugin.editors;

import org.eclipse.swt.graphics.RGB;

public interface IUMPColorConstants {
RGB UMP_COMMENT = new RGB(0, 128, 0);
RGB STRING = new RGB(0, 0, 255);
RGB KEYWOED = new RGB(128, 0, 0);

RGB DEFAULT = new RGB(0, 0, 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.cruise.umple.eclipse.plugin.editors;

public class Parser {
public static final String[] KEYWORDS = {
"class","trait","interface","association","associationClass","namespace",
"use","isA","const","lazy","settable","internal","autounique","defaulted",
"after","before","--","->","<-","<@>-","-<@>","sorted","return",
"entry","do","exit","queued","pooled","||","active",
"singleton","immutable","trace"};
public static final String[] KEYWORDS_TYPES = {"String","Boolean","Integer","Double","Float","void","Date","Time"};
public static final String[] KEYWORDS_VISIBILITY = {"public","private","protected"};
public static final String[] KEYWORDS_LANGUAGES = {"Java","Php","Cpp","Ruby"};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.cruise.umple.eclipse.plugin.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 UMPConfiguration extends SourceViewerConfiguration {
private UMPDoubleClickStrategy doubleClickStrategy;
private UMPScanner scanner;
private ColorManager colorManager;

public UMPConfiguration(ColorManager colorManager) {
this.colorManager = colorManager;
}

@Override
public ITextDoubleClickStrategy getDoubleClickStrategy(
ISourceViewer sourceViewer,
String contentType) {
if (doubleClickStrategy == null)
doubleClickStrategy = new UMPDoubleClickStrategy();
return doubleClickStrategy;
}

protected UMPScanner getXMLScanner() {
if (scanner == null) {
scanner = new UMPScanner(colorManager);
scanner.setDefaultReturnToken(
new Token(
new TextAttribute(
colorManager.getColor(IUMPColorConstants.DEFAULT))));
}
return scanner;
}

@Override
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler reconciler = new PresentationReconciler();

DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getXMLScanner());
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

return reconciler;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.cruise.umple.eclipse.plugin.editors;

import org.eclipse.jface.text.*;

public class UMPDoubleClickStrategy 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

import org.eclipse.ui.editors.text.TextEditor;


public class UMPEditor extends TextEditor {

private ColorManager colorManager;

public UMPEditor() {
// TODO Auto-generated constructor stub
super();
colorManager = new ColorManager();
setSourceViewerConfiguration(new UMPConfiguration(colorManager));
}
@Override
public void dispose() {
colorManager.dispose();
super.dispose();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.cruise.umple.eclipse.plugin.editors;

import org.eclipse.jface.text.rules.*;
import org.eclipse.jface.text.*;

public class UMPScanner extends RuleBasedScanner {

public UMPScanner(ColorManager manager) {

WordRule rule = new WordRule(new IWordDetector() {
public boolean isWordStart(char c) {
return Character.isJavaIdentifierStart(c);
}
public boolean isWordPart(char c) {
return Character.isJavaIdentifierPart(c);
}
});

IToken keyword = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.KEYWOED), null, 1));
IToken comment = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.UMP_COMMENT)));
IToken string = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.STRING)));

//add tokens for each reserved word
for (int n = 0; n < Parser.KEYWORDS.length; n++) {
rule.addWord(Parser.KEYWORDS[n], keyword);
}
for (int n = 0; n < Parser.KEYWORDS_TYPES.length; n++) {
rule.addWord(Parser.KEYWORDS_TYPES[n], keyword);
}
for (int n = 0; n < Parser.KEYWORDS_VISIBILITY.length; n++) {
rule.addWord(Parser.KEYWORDS_VISIBILITY[n], keyword);
}
for (int n = 0; n < Parser.KEYWORDS_LANGUAGES.length; n++) {
rule.addWord(Parser.KEYWORDS_LANGUAGES[n], keyword);
}


IRule[] rules = new IRule[5];
rules[0] = rule;
rules[1] = new MultiLineRule("/*", "*/", comment);
rules[2] = new SingleLineRule("//","", comment);
rules[3] = new SingleLineRule("\"","\"", string);

// Add generic whitespace rule.
rules[4] = new WhitespaceRule(new UMPWhitespaceDetector());
setRules(rules);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.cruise.umple.eclipse.plugin.editors;

import org.eclipse.jface.text.rules.IWhitespaceDetector;

public class UMPWhitespaceDetector implements IWhitespaceDetector {

@Override
public boolean isWhitespace(char c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
}

0 comments on commit 87d423e

Please sign in to comment.