1 package org.simantics.scl.ui.editor2;
3 import org.eclipse.jface.resource.FontDescriptor;
4 import org.eclipse.jface.resource.ResourceManager;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.TextAttribute;
7 import org.eclipse.jface.text.presentation.PresentationReconciler;
8 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
9 import org.eclipse.jface.text.rules.IRule;
10 import org.eclipse.jface.text.rules.ITokenScanner;
11 import org.eclipse.jface.text.rules.IWhitespaceDetector;
12 import org.eclipse.jface.text.rules.IWordDetector;
13 import org.eclipse.jface.text.rules.RuleBasedScanner;
14 import org.eclipse.jface.text.rules.Token;
15 import org.eclipse.jface.text.rules.WhitespaceRule;
16 import org.eclipse.jface.text.rules.WordRule;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Font;
19 import org.eclipse.swt.graphics.RGB;
20 import org.simantics.scl.ui.info.SCLInfo;
22 public class SCLPresentationReconciler extends PresentationReconciler {
23 private static final FontDescriptor FONT_NORMAL = FontDescriptor.createFrom("Consolas", 10, SWT.NORMAL);
24 private static final FontDescriptor FONT_BOLD = FontDescriptor.createFrom("Consolas", 10, SWT.BOLD);
26 public SCLPresentationReconciler(ResourceManager resourceManager) {
27 Font font = resourceManager.createFont(FONT_NORMAL);
29 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(
30 resourceManager.createColor(new RGB(63, 127, 95)),
35 setDamager(dr, SCLPartitionScanner.SCL_COMMENT);
36 setRepairer(dr, SCLPartitionScanner.SCL_COMMENT);
39 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new StringTokenScanner(new TextAttribute(
40 resourceManager.createColor(new RGB(42, 0, 255)),
45 setDamager(dr, SCLPartitionScanner.SCL_STRING);
46 setRepairer(dr, SCLPartitionScanner.SCL_STRING);
49 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner(resourceManager));
51 setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
52 setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
56 private static ITokenScanner getSclTokenScanner(ResourceManager resourceManager) {
57 RuleBasedScanner scanner = new RuleBasedScanner();
59 Token defaultToken = new Token(
61 resourceManager.createColor(new RGB(0, 0, 0)),
64 resourceManager.createFont(FONT_NORMAL)
66 Token reserved = new Token(
68 resourceManager.createColor(new RGB(127, 0, 85)),
71 resourceManager.createFont(FONT_BOLD)
74 WordRule reservedWord = new WordRule(new IWordDetector() {
76 public boolean isWordStart(char c) {
77 return ('a' <= c && c <= 'z')
78 || ('A' <= c && c <= 'Z')
79 || c == '_' || c == '?';
82 public boolean isWordPart(char c) {
83 return ('a' <= c && c <= 'z')
84 || ('A' <= c && c <= 'Z')
85 || ('0' <= c && c <= '9')
86 || c == '_' || c == '.' || c == '\'';
90 for(String word : SCLInfo.RESERVED_WORDS)
91 reservedWord.addWord(word, reserved);
93 scanner.setRules(new IRule[] {
94 new WhitespaceRule(new IWhitespaceDetector() {
96 public boolean isWhitespace(char c) {
97 return c==' ' || c=='\t' || c=='\n' || c=='\r';
102 scanner.setDefaultReturnToken(defaultToken);