diff options
Diffstat (limited to 'Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring')
2 files changed, 114 insertions, 0 deletions
diff --git a/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticHighlightCalculator.xtend b/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticHighlightCalculator.xtend new file mode 100644 index 00000000..5242a82e --- /dev/null +++ b/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticHighlightCalculator.xtend | |||
@@ -0,0 +1,59 @@ | |||
1 | package org.eclipse.viatra.solver.language.ui.syntaxcoloring | ||
2 | |||
3 | import java.util.Random | ||
4 | import java.util.WeakHashMap | ||
5 | import org.eclipse.viatra.solver.language.solverLanguage.Reference | ||
6 | import org.eclipse.xtext.ide.editor.syntaxcoloring.DefaultSemanticHighlightingCalculator | ||
7 | import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor | ||
8 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils | ||
9 | import org.eclipse.xtext.resource.XtextResource | ||
10 | import org.eclipse.xtext.util.CancelIndicator | ||
11 | |||
12 | class SolverSemanticHighlightCalculator extends DefaultSemanticHighlightingCalculator { | ||
13 | public static val SYMBOL_CODE = "SymbolCode::" | ||
14 | |||
15 | val symbolName2ID = new WeakHashMap<String, String> | ||
16 | |||
17 | override provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, | ||
18 | CancelIndicator cancelIndicator) { | ||
19 | if (resource === null || resource.getParseResult() === null) { | ||
20 | return | ||
21 | } else { | ||
22 | val modelSymbols = resource.allContents.filter(Reference) | ||
23 | while (modelSymbols.hasNext) { | ||
24 | val symbolElement = modelSymbols.next | ||
25 | val node = NodeModelUtils.getNode(symbolElement) | ||
26 | acceptor.addPosition( | ||
27 | node.getOffset(), | ||
28 | node.getLength(), | ||
29 | getSymbolCode(node.text) | ||
30 | ) | ||
31 | } | ||
32 | } | ||
33 | } | ||
34 | |||
35 | def getSymbolCode(String name) { | ||
36 | if (symbolName2ID.containsKey(name)) { | ||
37 | return symbolName2ID.get(name) | ||
38 | } else { | ||
39 | val id = calculateSymbolCode(name) | ||
40 | symbolName2ID.put(name, id) | ||
41 | return id | ||
42 | } | ||
43 | } | ||
44 | |||
45 | def String calculateSymbolCode(String name) { | ||
46 | val Random random = new Random(name.hashCode) | ||
47 | val hue = huePick(random.nextFloat) | ||
48 | val saturation = 0.2 + random.nextFloat * 0.3 | ||
49 | val brightness = random.nextFloat * 0.2 + 0.7f | ||
50 | return '''«SYMBOL_CODE» «hue» «saturation» «brightness»''' | ||
51 | } | ||
52 | |||
53 | private def huePick(float random) { | ||
54 | val pink = 300 | ||
55 | val tolerance = 10 | ||
56 | val degree = random * (360 - 2 * tolerance) | ||
57 | if(degree < pink - tolerance) return degree else return degree + 2 * tolerance | ||
58 | } | ||
59 | } | ||
diff --git a/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticTextAttributeProvider.xtend b/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticTextAttributeProvider.xtend new file mode 100644 index 00000000..ca74a9a2 --- /dev/null +++ b/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticTextAttributeProvider.xtend | |||
@@ -0,0 +1,55 @@ | |||
1 | package org.eclipse.viatra.solver.language.ui.syntaxcoloring | ||
2 | |||
3 | import com.google.inject.Inject | ||
4 | import java.util.WeakHashMap | ||
5 | import org.eclipse.jface.text.TextAttribute | ||
6 | import org.eclipse.swt.graphics.RGB | ||
7 | import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess | ||
8 | import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration | ||
9 | import org.eclipse.xtext.ui.editor.syntaxcoloring.PreferenceStoreAccessor | ||
10 | import org.eclipse.xtext.ui.editor.syntaxcoloring.TextAttributeProvider | ||
11 | import org.eclipse.xtext.ui.editor.utils.EditorUtils | ||
12 | import org.eclipse.xtext.ui.editor.utils.TextStyle | ||
13 | |||
14 | class SolverSemanticTextAttributeProvider extends TextAttributeProvider { | ||
15 | |||
16 | val defaultTextStyle = new TextStyle | ||
17 | val colorID2TextAttribute = new WeakHashMap<String, TextAttribute> | ||
18 | |||
19 | @Inject | ||
20 | new(IHighlightingConfiguration highlightingConfig, IPreferenceStoreAccess preferenceStoreAccess, | ||
21 | PreferenceStoreAccessor prefStoreAccessor) { | ||
22 | super(highlightingConfig, preferenceStoreAccess, prefStoreAccessor) | ||
23 | } | ||
24 | |||
25 | override getAttribute(String id) { | ||
26 | if (isMetamodelElementColorID(id)) { | ||
27 | if (colorID2TextAttribute.containsKey(id)) { | ||
28 | return colorID2TextAttribute.get(id) | ||
29 | } else { | ||
30 | val style = metamodelElementTextStyle(id) | ||
31 | colorID2TextAttribute.put(id, style) | ||
32 | return style | ||
33 | } | ||
34 | } else { | ||
35 | super.getAttribute(id) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | private def isMetamodelElementColorID(String id) { | ||
40 | id.startsWith(SolverSemanticHighlightCalculator.SYMBOL_CODE) | ||
41 | } | ||
42 | |||
43 | private def TextAttribute metamodelElementTextStyle(String id) { | ||
44 | val texts = id.split(' ') | ||
45 | val backgroundColor = new RGB( | ||
46 | Float.parseFloat(texts.get(1)), | ||
47 | Float.parseFloat(texts.get(2)), | ||
48 | Float.parseFloat(texts.get(3)) | ||
49 | ) | ||
50 | return new TextAttribute(EditorUtils.colorFromRGB(defaultTextStyle.color), | ||
51 | EditorUtils.colorFromRGB(backgroundColor), defaultTextStyle.style, | ||
52 | EditorUtils.fontFromFontData(defaultTextStyle.getFontData())); | ||
53 | } | ||
54 | |||
55 | } | ||