]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
6c274f30b54d0402cbb23a4865496f557a79753d
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.equation.expressions;\r
2 \r
3 import org.eclipse.jface.text.DefaultTextHover;\r
4 import org.eclipse.jface.text.IDocument;\r
5 import org.eclipse.jface.text.ITextHover;\r
6 import org.eclipse.jface.text.TextAttribute;\r
7 import org.eclipse.jface.text.presentation.IPresentationReconciler;\r
8 import org.eclipse.jface.text.presentation.PresentationReconciler;\r
9 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;\r
10 import org.eclipse.jface.text.rules.IRule;\r
11 import org.eclipse.jface.text.rules.ITokenScanner;\r
12 import org.eclipse.jface.text.rules.IWordDetector;\r
13 import org.eclipse.jface.text.rules.PatternRule;\r
14 import org.eclipse.jface.text.rules.RuleBasedScanner;\r
15 import org.eclipse.jface.text.rules.Token;\r
16 import org.eclipse.jface.text.rules.WordRule;\r
17 import org.eclipse.jface.text.source.DefaultAnnotationHover;\r
18 import org.eclipse.jface.text.source.IAnnotationHover;\r
19 import org.eclipse.jface.text.source.ISourceViewer;\r
20 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.graphics.RGB;\r
23 \r
24 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
25 \r
26 \r
27     ColorManager colorManager;\r
28 \r
29     public ExpressionFieldConfiguration(ColorManager colorManager) {\r
30         super();\r
31         this.colorManager = colorManager;\r
32     }\r
33 \r
34     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
35         return new String[] {\r
36                 IDocument.DEFAULT_CONTENT_TYPE\r
37         };\r
38     }\r
39 \r
40     /*\r
41     @Override\r
42     public IAutoEditStrategy[] getAutoEditStrategies(\r
43             ISourceViewer sourceViewer, String contentType) {\r
44         return new IAutoEditStrategy[] {\r
45                 new ReplaceTabsBySpaces(),\r
46                 new IndentLineEditStrategy(),\r
47                 new MatchingBracketsEditStrategy()\r
48         };\r
49     }\r
50     */\r
51 \r
52     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
53         PresentationReconciler reconciler = new PresentationReconciler();\r
54 \r
55         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
56 \r
57         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
58         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
59 \r
60         return reconciler;\r
61     }\r
62 \r
63     /*\r
64      * \r
65      */\r
66      ITokenScanner getSclTokenScanner() {\r
67         RuleBasedScanner scanner = new RuleBasedScanner();\r
68 \r
69         final Token string = new Token(new TextAttribute(colorManager.getColor(new RGB(42, 0, 255))));  \r
70         final Token reserved = new Token(\r
71                 new TextAttribute(\r
72                         colorManager.getColor(new RGB(127, 0, 85)),\r
73                         null,\r
74                         SWT.BOLD\r
75                 ));\r
76         final Token comment = new Token(new TextAttribute(colorManager.getColor(new RGB(63, 127, 95))));\r
77 \r
78         WordRule reservedWord = new WordRule(new IWordDetector() {   \r
79             @Override\r
80             public boolean isWordStart(char c) {\r
81                 return c=='@';\r
82             }\r
83 \r
84             @Override\r
85             public boolean isWordPart(char c) {\r
86                 return Character.isJavaIdentifierPart(c);\r
87             }\r
88         });\r
89 \r
90         reservedWord.addWord("@a", reserved);\r
91         reservedWord.addWord("@keywords", reserved);\r
92         reservedWord.addWord("@forAll", reserved);\r
93         reservedWord.addWord("@forSome", reserved);\r
94         reservedWord.addWord("@true", reserved);\r
95         reservedWord.addWord("@false", reserved);\r
96         reservedWord.addWord("@base", reserved);\r
97         reservedWord.addWord("@prefix", reserved);\r
98 \r
99         IRule[] rules = new IRule[] {\r
100                 //new MultiLineRule("\"\"\"", "\"\"\"", string),\r
101                 new PatternRule("\"", "\"", string, '\\', true),   \r
102                 new PatternRule("\"\"\"", "\"\"\"", string, '\\', false),\r
103                 new PatternRule("<", ">", string, '\\', false),\r
104                 new PatternRule("#", null, comment, '\0', true),\r
105                 reservedWord\r
106         };\r
107         scanner.setRules(rules);\r
108 \r
109         return scanner;  \r
110      }\r
111 \r
112      @Override\r
113      public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
114          return new DefaultTextHover(sourceViewer);\r
115      }\r
116 \r
117      @Override\r
118      public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
119          return new DefaultAnnotationHover();\r
120      }\r
121 \r
122 }\r