]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLPresentationReconciler.java
LabelDecorator.decorateLabel can return null
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / SCLPresentationReconciler.java
1 package org.simantics.scl.ui.editor2;
2
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;
21
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);
25     
26     public SCLPresentationReconciler(ResourceManager resourceManager) {
27         Font font = resourceManager.createFont(FONT_NORMAL);
28         {
29             DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(
30                     resourceManager.createColor(new RGB(63, 127, 95)),
31                     null,
32                     0,
33                     font
34                     )));
35             setDamager(dr, SCLPartitionScanner.SCL_COMMENT);
36             setRepairer(dr, SCLPartitionScanner.SCL_COMMENT);
37         }
38         {
39             DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new StringTokenScanner(new TextAttribute(
40                     resourceManager.createColor(new RGB(42, 0, 255)),
41                     null,
42                     0,
43                     font
44                     )));
45             setDamager(dr, SCLPartitionScanner.SCL_STRING);
46             setRepairer(dr, SCLPartitionScanner.SCL_STRING);
47         }
48         {
49             DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner(resourceManager));
50
51             setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
52             setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
53         }
54     }
55     
56     private static ITokenScanner getSclTokenScanner(ResourceManager resourceManager) {
57         RuleBasedScanner scanner = new RuleBasedScanner();
58         
59         Token defaultToken = new Token(
60                 new TextAttribute(
61                         resourceManager.createColor(new RGB(0, 0, 0)),
62                         null,
63                         0,
64                         resourceManager.createFont(FONT_NORMAL)
65                 ));
66         Token reserved = new Token(
67                 new TextAttribute(
68                         resourceManager.createColor(new RGB(127, 0, 85)),
69                         null,
70                         SWT.BOLD,
71                         resourceManager.createFont(FONT_BOLD)
72                 ));
73
74         WordRule reservedWord = new WordRule(new IWordDetector() {
75             @Override
76             public boolean isWordStart(char c) {
77                 return ('a' <= c && c <= 'z')
78                      || ('A' <= c && c <= 'Z')
79                      || c == '_' || c == '?';
80             }
81             @Override
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 == '\'';
87             }
88         }, defaultToken);
89
90         for(String word : SCLInfo.RESERVED_WORDS)
91             reservedWord.addWord(word, reserved);
92         
93         scanner.setRules(new IRule[] {
94                 new WhitespaceRule(new IWhitespaceDetector() {
95                     @Override
96                     public boolean isWhitespace(char c) {
97                         return c==' ' || c=='\t' || c=='\n' || c=='\r';
98                     }
99                 }, defaultToken),
100                 reservedWord
101         });
102         scanner.setDefaultReturnToken(defaultToken);
103         
104         return scanner;
105     }
106 }