1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.equation.expressions;
\r
14 import java.util.HashSet;
\r
15 import java.util.Set;
\r
17 import org.eclipse.jface.text.DefaultTextHover;
\r
18 import org.eclipse.jface.text.IDocument;
\r
19 import org.eclipse.jface.text.ITextHover;
\r
20 import org.eclipse.jface.text.TextAttribute;
\r
21 import org.eclipse.jface.text.presentation.IPresentationReconciler;
\r
22 import org.eclipse.jface.text.presentation.PresentationReconciler;
\r
23 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
\r
24 import org.eclipse.jface.text.rules.IRule;
\r
25 import org.eclipse.jface.text.rules.ITokenScanner;
\r
26 import org.eclipse.jface.text.rules.IWordDetector;
\r
27 import org.eclipse.jface.text.rules.RuleBasedScanner;
\r
28 import org.eclipse.jface.text.rules.Token;
\r
29 import org.eclipse.jface.text.rules.WordRule;
\r
30 import org.eclipse.jface.text.source.DefaultAnnotationHover;
\r
31 import org.eclipse.jface.text.source.IAnnotationHover;
\r
32 import org.eclipse.jface.text.source.ISourceViewer;
\r
33 import org.eclipse.jface.text.source.SourceViewerConfiguration;
\r
34 import org.eclipse.swt.SWT;
\r
35 import org.eclipse.swt.graphics.RGB;
\r
37 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
\r
40 ColorManager colorManager;
\r
42 public ExpressionFieldConfiguration(ColorManager colorManager) {
\r
44 this.colorManager = colorManager;
\r
47 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
\r
48 return new String[] {
\r
49 IDocument.DEFAULT_CONTENT_TYPE
\r
55 public IAutoEditStrategy[] getAutoEditStrategies(
\r
56 ISourceViewer sourceViewer, String contentType) {
\r
57 return new IAutoEditStrategy[] {
\r
58 new ReplaceTabsBySpaces(),
\r
59 new IndentLineEditStrategy(),
\r
60 new MatchingBracketsEditStrategy()
\r
65 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
\r
66 PresentationReconciler reconciler = new PresentationReconciler();
\r
68 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());
\r
70 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
71 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
79 ITokenScanner getSclTokenScanner() {
\r
80 RuleBasedScanner scanner = new RuleBasedScanner();
\r
82 final Token reserved = new Token(
\r
84 colorManager.getColor(new RGB(127, 0, 85)),
\r
90 WordRule reservedWord = new WordRule(new IWordDetector() {
\r
92 public boolean isWordStart(char c) {
\r
93 return Character.isLetter(c);
\r
97 public boolean isWordPart(char c) {
\r
98 return Character.isLetter(c);
\r
102 for(String s : keywords) {
\r
103 reservedWord.addWord(s, reserved);
\r
106 IRule[] rules = new IRule[] {
\r
109 scanner.setRules(rules);
\r
115 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
\r
116 return new DefaultTextHover(sourceViewer);
\r
120 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
\r
121 return new DefaultAnnotationHover();
\r
124 static final Set<String> keywords = new HashSet<String>();
\r
127 keywords.add("within");
\r
128 keywords.add("final");
\r
129 keywords.add("public");
\r
130 keywords.add("protected");
\r
131 keywords.add("connect");
\r
132 keywords.add("when");
\r
133 keywords.add("then");
\r
134 keywords.add("elsewhen");
\r
135 keywords.add("if");
\r
136 keywords.add("end");
\r
137 keywords.add("elseif");
\r
138 keywords.add("else");
\r
139 keywords.add("for");
\r
140 keywords.add("while");
\r
141 keywords.add("loop");
\r
142 keywords.add("der");
\r
143 keywords.add("enumeration");
\r
144 keywords.add("extends");
\r
145 keywords.add("class");
\r
146 keywords.add("partial");
\r
147 keywords.add("encapsulated");
\r
148 keywords.add("model");
\r
149 keywords.add("record");
\r
150 keywords.add("block");
\r
151 keywords.add("expandable");
\r
152 keywords.add("connector");
\r
153 keywords.add("type");
\r
154 keywords.add("package");
\r
155 keywords.add("function");
\r
156 keywords.add("import");
\r
157 keywords.add("external");
\r
158 keywords.add("constrainedby");
\r
159 keywords.add("redeclare");
\r
160 keywords.add("replaceable");
\r
161 keywords.add("flow");
\r
162 keywords.add("discrete");
\r
163 keywords.add("parameter");
\r
164 keywords.add("constant");
\r
165 keywords.add("input");
\r
166 keywords.add("output");
\r
167 keywords.add("annotation");
\r
168 keywords.add("false");
\r
169 keywords.add("true");
\r
170 keywords.add("each");
\r
171 keywords.add("initial");
\r
172 keywords.add("algorithm");
\r
173 keywords.add("equation");
\r
174 keywords.add("or");
\r
175 keywords.add("and");
\r
176 keywords.add("not");
\r
177 keywords.add("break");
\r
178 keywords.add("return");
\r