Skip to content

Commit 87d423e

Browse files
committed
text highlighting was added to the plugin.
1 parent 36f11ee commit 87d423e

File tree

11 files changed

+290
-2
lines changed

11 files changed

+290
-2
lines changed

‎org.cruise.umple.eclipse.plugin.feature/feature.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<import plugin="org.eclipse.core.runtime"/>
2222
<import plugin="org.eclipse.ui.ide" version="3.11.0" match="greaterOrEqual"/>
2323
<import plugin="org.eclipse.ui.console" version="3.6.100" match="greaterOrEqual"/>
24+
<import plugin="org.eclipse.ui.editors" version="3.10.0" match="greaterOrEqual"/>
25+
<import plugin="org.eclipse.jface.text" version="3.11.0" match="greaterOrEqual"/>
2426
<import plugin="org.eclipse.core.resources"/>
2527
</requires>
2628

-746 Bytes
Binary file not shown.
-2.01 KB
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.Map;
6+
7+
import org.eclipse.swt.graphics.Color;
8+
import org.eclipse.swt.graphics.RGB;
9+
import org.eclipse.swt.widgets.Display;
10+
11+
public class ColorManager {
12+
13+
protected Map fColorTable = new HashMap(10);
14+
15+
public void dispose() {
16+
Iterator e = fColorTable.values().iterator();
17+
while (e.hasNext())
18+
((Color) e.next()).dispose();
19+
}
20+
public Color getColor(RGB rgb) {
21+
Color color = (Color) fColorTable.get(rgb);
22+
if (color == null) {
23+
color = new Color(Display.getCurrent(), rgb);
24+
fColorTable.put(rgb, color);
25+
}
26+
return color;
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
import org.eclipse.swt.graphics.RGB;
4+
5+
public interface IUMPColorConstants {
6+
RGB UMP_COMMENT = new RGB(0, 128, 0);
7+
RGB STRING = new RGB(0, 0, 255);
8+
RGB KEYWOED = new RGB(128, 0, 0);
9+
10+
RGB DEFAULT = new RGB(0, 0, 0);
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
public class Parser {
4+
public static final String[] KEYWORDS = {
5+
"class","trait","interface","association","associationClass","namespace",
6+
"use","isA","const","lazy","settable","internal","autounique","defaulted",
7+
"after","before","--","->","<-","<@>-","-<@>","sorted","return",
8+
"entry","do","exit","queued","pooled","||","active",
9+
"singleton","immutable","trace"};
10+
public static final String[] KEYWORDS_TYPES = {"String","Boolean","Integer","Double","Float","void","Date","Time"};
11+
public static final String[] KEYWORDS_VISIBILITY = {"public","private","protected"};
12+
public static final String[] KEYWORDS_LANGUAGES = {"Java","Php","Cpp","Ruby"};
13+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
import org.eclipse.jface.text.IDocument;
4+
import org.eclipse.jface.text.ITextDoubleClickStrategy;
5+
import org.eclipse.jface.text.TextAttribute;
6+
import org.eclipse.jface.text.presentation.IPresentationReconciler;
7+
import org.eclipse.jface.text.presentation.PresentationReconciler;
8+
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
9+
import org.eclipse.jface.text.rules.Token;
10+
import org.eclipse.jface.text.source.ISourceViewer;
11+
import org.eclipse.jface.text.source.SourceViewerConfiguration;
12+
13+
public class UMPConfiguration extends SourceViewerConfiguration {
14+
private UMPDoubleClickStrategy doubleClickStrategy;
15+
private UMPScanner scanner;
16+
private ColorManager colorManager;
17+
18+
public UMPConfiguration(ColorManager colorManager) {
19+
this.colorManager = colorManager;
20+
}
21+
22+
@Override
23+
public ITextDoubleClickStrategy getDoubleClickStrategy(
24+
ISourceViewer sourceViewer,
25+
String contentType) {
26+
if (doubleClickStrategy == null)
27+
doubleClickStrategy = new UMPDoubleClickStrategy();
28+
return doubleClickStrategy;
29+
}
30+
31+
protected UMPScanner getXMLScanner() {
32+
if (scanner == null) {
33+
scanner = new UMPScanner(colorManager);
34+
scanner.setDefaultReturnToken(
35+
new Token(
36+
new TextAttribute(
37+
colorManager.getColor(IUMPColorConstants.DEFAULT))));
38+
}
39+
return scanner;
40+
}
41+
42+
@Override
43+
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
44+
PresentationReconciler reconciler = new PresentationReconciler();
45+
46+
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getXMLScanner());
47+
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
48+
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
49+
50+
return reconciler;
51+
}
52+
53+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
import org.eclipse.jface.text.*;
4+
5+
public class UMPDoubleClickStrategy implements ITextDoubleClickStrategy {
6+
protected ITextViewer fText;
7+
8+
@Override
9+
public void doubleClicked(ITextViewer part) {
10+
int pos = part.getSelectedRange().x;
11+
12+
if (pos < 0)
13+
return;
14+
15+
fText = part;
16+
17+
if (!selectComment(pos)) {
18+
selectWord(pos);
19+
}
20+
}
21+
protected boolean selectComment(int caretPos) {
22+
IDocument doc = fText.getDocument();
23+
int startPos, endPos;
24+
25+
try {
26+
int pos = caretPos;
27+
char c = ' ';
28+
29+
while (pos >= 0) {
30+
c = doc.getChar(pos);
31+
if (c == '\\') {
32+
pos -= 2;
33+
continue;
34+
}
35+
if (c == Character.LINE_SEPARATOR || c == '\"')
36+
break;
37+
--pos;
38+
}
39+
40+
if (c != '\"')
41+
return false;
42+
43+
startPos = pos;
44+
45+
pos = caretPos;
46+
int length = doc.getLength();
47+
c = ' ';
48+
49+
while (pos < length) {
50+
c = doc.getChar(pos);
51+
if (c == Character.LINE_SEPARATOR || c == '\"')
52+
break;
53+
++pos;
54+
}
55+
if (c != '\"')
56+
return false;
57+
58+
endPos = pos;
59+
60+
int offset = startPos + 1;
61+
int len = endPos - offset;
62+
fText.setSelectedRange(offset, len);
63+
return true;
64+
} catch (BadLocationException x) {
65+
}
66+
67+
return false;
68+
}
69+
protected boolean selectWord(int caretPos) {
70+
71+
IDocument doc = fText.getDocument();
72+
int startPos, endPos;
73+
74+
try {
75+
76+
int pos = caretPos;
77+
char c;
78+
79+
while (pos >= 0) {
80+
c = doc.getChar(pos);
81+
if (!Character.isJavaIdentifierPart(c))
82+
break;
83+
--pos;
84+
}
85+
86+
startPos = pos;
87+
88+
pos = caretPos;
89+
int length = doc.getLength();
90+
91+
while (pos < length) {
92+
c = doc.getChar(pos);
93+
if (!Character.isJavaIdentifierPart(c))
94+
break;
95+
++pos;
96+
}
97+
98+
endPos = pos;
99+
selectRange(startPos, endPos);
100+
return true;
101+
102+
} catch (BadLocationException x) {
103+
}
104+
105+
return false;
106+
}
107+
108+
private void selectRange(int startPos, int stopPos) {
109+
int offset = startPos + 1;
110+
int length = stopPos - offset;
111+
fText.setSelectedRange(offset, length);
112+
}
113+
}

‎org.cruise.umple.eclipse.plugin/src/org/cruise/umple/eclipse/plugin/editors/UMPEditor.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22

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

5+
56
public class UMPEditor extends TextEditor {
67

8+
private ColorManager colorManager;
9+
710
public UMPEditor() {
8-
// TODO Auto-generated constructor stub
11+
super();
12+
colorManager = new ColorManager();
13+
setSourceViewerConfiguration(new UMPConfiguration(colorManager));
14+
}
15+
@Override
16+
public void dispose() {
17+
colorManager.dispose();
18+
super.dispose();
919
}
10-
1120
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.cruise.umple.eclipse.plugin.editors;
2+
3+
import org.eclipse.jface.text.rules.*;
4+
import org.eclipse.jface.text.*;
5+
6+
public class UMPScanner extends RuleBasedScanner {
7+
8+
public UMPScanner(ColorManager manager) {
9+
10+
WordRule rule = new WordRule(new IWordDetector() {
11+
public boolean isWordStart(char c) {
12+
return Character.isJavaIdentifierStart(c);
13+
}
14+
public boolean isWordPart(char c) {
15+
return Character.isJavaIdentifierPart(c);
16+
}
17+
});
18+
19+
IToken keyword = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.KEYWOED), null, 1));
20+
IToken comment = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.UMP_COMMENT)));
21+
IToken string = new Token(new TextAttribute(manager.getColor(IUMPColorConstants.STRING)));
22+
23+
//add tokens for each reserved word
24+
for (int n = 0; n < Parser.KEYWORDS.length; n++) {
25+
rule.addWord(Parser.KEYWORDS[n], keyword);
26+
}
27+
for (int n = 0; n < Parser.KEYWORDS_TYPES.length; n++) {
28+
rule.addWord(Parser.KEYWORDS_TYPES[n], keyword);
29+
}
30+
for (int n = 0; n < Parser.KEYWORDS_VISIBILITY.length; n++) {
31+
rule.addWord(Parser.KEYWORDS_VISIBILITY[n], keyword);
32+
}
33+
for (int n = 0; n < Parser.KEYWORDS_LANGUAGES.length; n++) {
34+
rule.addWord(Parser.KEYWORDS_LANGUAGES[n], keyword);
35+
}
36+
37+
38+
IRule[] rules = new IRule[5];
39+
rules[0] = rule;
40+
rules[1] = new MultiLineRule("/*", "*/", comment);
41+
rules[2] = new SingleLineRule("//","", comment);
42+
rules[3] = new SingleLineRule("\"","\"", string);
43+
44+
// Add generic whitespace rule.
45+
rules[4] = new WhitespaceRule(new UMPWhitespaceDetector());
46+
setRules(rules);
47+
}
48+
}

0 commit comments

Comments
 (0)