]> gerrit.simantics Code Review - simantics/platform.git/blob
bad6703b0971c6abdefdb62ae059d6301fa49df7
[simantics/platform.git] /
1 package org.simantics.modeling.ui.componentTypeEditor;
2
3 import org.eclipse.jface.text.DefaultTextHover;
4 import org.eclipse.jface.text.IDocument;
5 import org.eclipse.jface.text.ITextHover;
6 import org.eclipse.jface.text.TextAttribute;
7 import org.eclipse.jface.text.presentation.IPresentationReconciler;
8 import org.eclipse.jface.text.presentation.PresentationReconciler;
9 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
10 import org.eclipse.jface.text.rules.IRule;
11 import org.eclipse.jface.text.rules.ITokenScanner;
12 import org.eclipse.jface.text.rules.IWordDetector;
13 import org.eclipse.jface.text.rules.MultiLineRule;
14 import org.eclipse.jface.text.rules.PatternRule;
15 import org.eclipse.jface.text.rules.RuleBasedScanner;
16 import org.eclipse.jface.text.rules.Token;
17 import org.eclipse.jface.text.rules.WordRule;
18 import org.eclipse.jface.text.source.DefaultAnnotationHover;
19 import org.eclipse.jface.text.source.IAnnotationHover;
20 import org.eclipse.jface.text.source.ISharedTextColors;
21 import org.eclipse.jface.text.source.ISourceViewer;
22 import org.eclipse.jface.text.source.SourceViewerConfiguration;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Device;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.RGB;
27
28 public class PGraphSourceViewerConfigurationNew extends SourceViewerConfiguration {
29
30     public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };
31     Device device;
32     
33     ISharedTextColors sharedTextColors;
34     
35     public PGraphSourceViewerConfigurationNew(Device device,
36             ISharedTextColors sharedTextColors) {
37         this.device = device;
38         this.sharedTextColors = sharedTextColors;
39     }
40
41     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
42         return new String[] {
43             IDocument.DEFAULT_CONTENT_TYPE
44         };
45     }
46     
47     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
48         PresentationReconciler reconciler = new PresentationReconciler();
49         
50         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());
51         
52         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
53         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
54         
55         return reconciler;
56     }
57     
58     ITokenScanner getSclTokenScanner() {
59         RuleBasedScanner scanner = new RuleBasedScanner();
60         
61         Font font = new Font(device, "Courier New", 10, SWT.NORMAL);
62         Font boldFont = new Font(device, "Courier New", 10, SWT.BOLD);
63
64         Token defaultToken = new Token(
65                 new TextAttribute(
66                         sharedTextColors.getColor(new RGB(0, 0, 0)),
67                         null,
68                         0,
69                         font
70                 ));
71         Token string = new Token(new TextAttribute(
72                 sharedTextColors.getColor(new RGB(42, 0, 255)),
73                 null,
74                 0,
75                 font
76                 ));
77         Token reserved = new Token(
78                 new TextAttribute(
79                         sharedTextColors.getColor(new RGB(127, 0, 85)),
80                         null,
81                         SWT.BOLD,
82                         boldFont
83                 ));
84         Token comment = new Token(new TextAttribute(
85                 sharedTextColors.getColor(new RGB(63, 127, 95)),
86                 null,
87                 0,
88                 font
89                 ));
90
91         WordRule reservedWord = new WordRule(new IWordDetector() {          
92             @Override
93             public boolean isWordStart(char c) {
94                 return Character.isJavaIdentifierStart(c);
95             }
96             
97             @Override
98             public boolean isWordPart(char c) {
99                 return Character.isJavaIdentifierPart(c) || c=='.';
100             }
101         });
102
103         reservedWord.addWord("if", reserved);
104         reservedWord.addWord("then", reserved);
105         reservedWord.addWord("else", reserved);
106         reservedWord.addWord("match", reserved);
107         reservedWord.addWord("with", reserved);
108         reservedWord.addWord("data", reserved);
109         reservedWord.addWord("type", reserved);
110         reservedWord.addWord("class", reserved);
111         
112         IRule[] rules = new IRule[] {
113             //new MultiLineRule("\"\"\"", "\"\"\"", string),
114             new PatternRule("\"", "\"", string, '\\', true),
115             new MultiLineRule("/*", "*/", comment),
116             new PatternRule("//", null, comment, '\0', true),
117             reservedWord
118         };
119         scanner.setRules(rules);
120         scanner.setDefaultReturnToken(defaultToken);
121         
122         return scanner;     
123     }
124     
125     @Override
126     public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
127         return new DefaultTextHover(sourceViewer);
128     }
129     
130     @Override
131     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
132         return new DefaultAnnotationHover();
133     }
134 }