]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
098a2622a79db8ed738a87354f8c40ccb24f9dae
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.expressions;\r
2 \r
3 import java.util.HashSet;\r
4 import java.util.Set;\r
5 \r
6 import org.eclipse.jface.text.DefaultTextHover;\r
7 import org.eclipse.jface.text.IDocument;\r
8 import org.eclipse.jface.text.ITextHover;\r
9 import org.eclipse.jface.text.TextAttribute;\r
10 import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
11 import org.eclipse.jface.text.presentation.PresentationReconciler;\r
12 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
13 import org.eclipse.jface.text.rules.IRule;\r
14 import org.eclipse.jface.text.rules.ITokenScanner;\r
15 import org.eclipse.jface.text.rules.IWordDetector;\r
16 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
17 import org.eclipse.jface.text.rules.Token;\r
18 import org.eclipse.jface.text.rules.WordRule;\r
19 import org.eclipse.jface.text.source.DefaultAnnotationHover;\r
20 import org.eclipse.jface.text.source.IAnnotationHover;\r
21 import org.eclipse.jface.text.source.ISourceViewer;\r
22 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.graphics.RGB;\r
25 \r
26 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
27 \r
28 \r
29     ColorManager colorManager;\r
30 \r
31     public ExpressionFieldConfiguration(ColorManager colorManager) {\r
32         super();\r
33         this.colorManager = colorManager;\r
34     }\r
35 \r
36     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
37         return new String[] {\r
38                 IDocument.DEFAULT_CONTENT_TYPE\r
39         };\r
40     }\r
41 \r
42     /*\r
43     @Override\r
44     public IAutoEditStrategy[] getAutoEditStrategies(\r
45             ISourceViewer sourceViewer, String contentType) {\r
46         return new IAutoEditStrategy[] {\r
47                 new ReplaceTabsBySpaces(),\r
48                 new IndentLineEditStrategy(),\r
49                 new MatchingBracketsEditStrategy()\r
50         };\r
51     }\r
52     */\r
53 \r
54     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
55         PresentationReconciler reconciler = new PresentationReconciler();\r
56 \r
57         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
58 \r
59         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
60         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
61 \r
62         return reconciler;\r
63     }\r
64 \r
65     /*\r
66      * \r
67      */\r
68      ITokenScanner getSclTokenScanner() {\r
69         RuleBasedScanner scanner = new RuleBasedScanner();\r
70 \r
71         final Token reserved = new Token(\r
72                 new TextAttribute(\r
73                         colorManager.getColor(new RGB(127, 0, 85)),\r
74                         null,\r
75                         SWT.BOLD\r
76                 ));\r
77         \r
78 \r
79         WordRule reservedWord = new WordRule(new IWordDetector() {   \r
80             @Override\r
81             public boolean isWordStart(char c) {\r
82                 return Character.isLetter(c);\r
83             }\r
84 \r
85             @Override\r
86             public boolean isWordPart(char c) {\r
87                 return Character.isLetter(c);\r
88             }\r
89         });\r
90 \r
91         for(String s : keywords) {\r
92             reservedWord.addWord(s, reserved);\r
93         }\r
94 \r
95         IRule[] rules = new IRule[] {\r
96                 reservedWord\r
97         };\r
98         scanner.setRules(rules);\r
99 \r
100         return scanner;  \r
101      }\r
102 \r
103      @Override\r
104      public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
105          return new DefaultTextHover(sourceViewer);\r
106      }\r
107 \r
108      @Override\r
109      public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
110          return new DefaultAnnotationHover();\r
111      }\r
112 \r
113      static final Set<String> keywords = new HashSet<String>();\r
114      \r
115      static {\r
116          keywords.add("within");\r
117          keywords.add("final");\r
118          keywords.add("public");\r
119          keywords.add("protected");\r
120          keywords.add("connect");\r
121          keywords.add("when");\r
122          keywords.add("then");\r
123          keywords.add("elsewhen");\r
124          keywords.add("if");\r
125          keywords.add("end");\r
126          keywords.add("elseif");\r
127          keywords.add("else");\r
128          keywords.add("for");\r
129          keywords.add("while");\r
130          keywords.add("loop");\r
131          keywords.add("der");\r
132          keywords.add("enumeration");\r
133          keywords.add("extends");\r
134          keywords.add("class");\r
135          keywords.add("partial");\r
136          keywords.add("encapsulated");\r
137          keywords.add("model");\r
138          keywords.add("record");\r
139          keywords.add("block");\r
140          keywords.add("expandable");\r
141          keywords.add("connector");\r
142          keywords.add("type");\r
143          keywords.add("package");\r
144          keywords.add("function");\r
145          keywords.add("import");\r
146          keywords.add("external");\r
147          keywords.add("constrainedby");\r
148          keywords.add("redeclare");\r
149          keywords.add("replaceable");\r
150          keywords.add("flow");\r
151          keywords.add("discrete");\r
152          keywords.add("parameter");\r
153          keywords.add("constant");\r
154          keywords.add("input");\r
155          keywords.add("output");\r
156          keywords.add("annotation");\r
157          keywords.add("false");\r
158          keywords.add("true");\r
159          keywords.add("each");\r
160          keywords.add("initial");\r
161          keywords.add("algorithm");\r
162          keywords.add("equation");\r
163          keywords.add("or");\r
164          keywords.add("and");\r
165          keywords.add("not");\r
166          keywords.add("break");\r
167          keywords.add("return");     \r
168          // functions\r
169          keywords.add("abs");\r
170          keywords.add("min");\r
171          keywords.add("max");\r
172      }\r
173 }\r