aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-ide
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2024-03-22 03:19:29 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2024-03-22 03:19:29 +0100
commitddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c (patch)
tree34806446ca566bebb6524a95b1337fd1280fcf13 /subprojects/language-ide
parentrefactor(web): improve web app styling (diff)
downloadrefinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.tar.gz
refinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.tar.zst
refinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.zip
feat: custom identifier coloring
Diffstat (limited to 'subprojects/language-ide')
-rw-r--r--subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java24
1 files changed, 18 insertions, 6 deletions
diff --git a/subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java b/subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java
index dd9f1053..82a6af06 100644
--- a/subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java
+++ b/subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java
@@ -5,16 +5,17 @@
5 */ 5 */
6package tools.refinery.language.ide.syntaxcoloring; 6package tools.refinery.language.ide.syntaxcoloring;
7 7
8import com.google.common.collect.ImmutableMap;
9import com.google.inject.Inject; 8import com.google.inject.Inject;
10import com.google.inject.Singleton; 9import com.google.inject.Singleton;
11import org.eclipse.xtext.EcoreUtil2; 10import org.eclipse.xtext.EcoreUtil2;
12import org.eclipse.xtext.naming.IQualifiedNameConverter; 11import org.eclipse.xtext.naming.IQualifiedNameConverter;
13import org.eclipse.xtext.naming.IQualifiedNameProvider; 12import org.eclipse.xtext.naming.IQualifiedNameProvider;
13import org.eclipse.xtext.resource.IEObjectDescription;
14import org.eclipse.xtext.resource.IResourceDescription; 14import org.eclipse.xtext.resource.IResourceDescription;
15import org.eclipse.xtext.scoping.IScopeProvider; 15import org.eclipse.xtext.scoping.IScopeProvider;
16import org.eclipse.xtext.scoping.impl.GlobalResourceDescriptionProvider; 16import org.eclipse.xtext.scoping.impl.GlobalResourceDescriptionProvider;
17import org.eclipse.xtext.util.IResourceScopeCache; 17import org.eclipse.xtext.util.IResourceScopeCache;
18import tools.refinery.language.documentation.DocumentationCommentParser;
18import tools.refinery.language.model.problem.*; 19import tools.refinery.language.model.problem.*;
19import tools.refinery.language.resource.ProblemResourceDescriptionStrategy; 20import tools.refinery.language.resource.ProblemResourceDescriptionStrategy;
20import tools.refinery.language.scoping.imports.ImportCollector; 21import tools.refinery.language.scoping.imports.ImportCollector;
@@ -65,21 +66,30 @@ public class TypeHashProvider {
65 66
66 private Map<String, String> computeHashes(Problem problem) { 67 private Map<String, String> computeHashes(Problem problem) {
67 var resourceDescriptions = getResourceDescriptions(problem); 68 var resourceDescriptions = getResourceDescriptions(problem);
69 var map = new HashMap<String, String>();
68 var qualifiedNameStrings = new TreeSet<String>(); 70 var qualifiedNameStrings = new TreeSet<String>();
69 for (var resourceDescription : resourceDescriptions) { 71 for (var resourceDescription : resourceDescriptions) {
70 for (var description : resourceDescription.getExportedObjectsByType(ProblemPackage.Literals.RELATION)) { 72 for (var description : resourceDescription.getExportedObjectsByType(ProblemPackage.Literals.RELATION)) {
71 if (ProblemResourceDescriptionStrategy.COLOR_RELATION_TRUE.equals( 73 if (ProblemResourceDescriptionStrategy.COLOR_RELATION_TRUE.equals(
72 description.getUserData(ProblemResourceDescriptionStrategy.COLOR_RELATION))) { 74 description.getUserData(ProblemResourceDescriptionStrategy.COLOR_RELATION))) {
73 var qualifiedNameString = qualifiedNameConverter.toString(description.getQualifiedName()); 75 var qualifiedNameString = qualifiedNameConverter.toString(description.getQualifiedName());
76 var presetColor = getPresetColor(description);
77 if (presetColor != null) {
78 map.put(qualifiedNameString, presetColor);
79 }
74 qualifiedNameStrings.add(qualifiedNameString); 80 qualifiedNameStrings.add(qualifiedNameString);
75 } 81 }
76 } 82 }
77 } 83 }
78 var stringList = new ArrayList<>(qualifiedNameStrings); 84 var stringList = new ArrayList<>(qualifiedNameStrings);
79 int size = stringList.size(); 85 int size = stringList.size();
80 if (size == 0) { 86 if (size != 0) {
81 return Map.of(); 87 shuffleColors(size, stringList, map);
82 } 88 }
89 return Collections.unmodifiableMap(map);
90 }
91
92 private static void shuffleColors(int size, ArrayList<String> stringList, Map<String, String> map) {
83 // The use of a non-cryptographic random generator is safe here, because we only use it to shuffle the color 93 // The use of a non-cryptographic random generator is safe here, because we only use it to shuffle the color
84 // IDs in a pseudo-random way. The shuffle depends on the size of the list of identifiers before padding to 94 // IDs in a pseudo-random way. The shuffle depends on the size of the list of identifiers before padding to
85 // make sure that adding a new class randomizes all color IDs. 95 // make sure that adding a new class randomizes all color IDs.
@@ -91,15 +101,13 @@ public class TypeHashProvider {
91 } 101 }
92 size += padding; 102 size += padding;
93 Collections.shuffle(stringList, random); 103 Collections.shuffle(stringList, random);
94 var mapBuilder = ImmutableMap.<String, String>builder();
95 for (int i = 0; i < size; i++) { 104 for (int i = 0; i < size; i++) {
96 var key = stringList.get(i); 105 var key = stringList.get(i);
97 if (key != null) { 106 if (key != null) {
98 int colorId = i % COLOR_COUNT; 107 int colorId = i % COLOR_COUNT;
99 mapBuilder.put(key, Integer.toString(colorId)); 108 map.putIfAbsent(key, Integer.toString(colorId));
100 } 109 }
101 } 110 }
102 return mapBuilder.build();
103 } 111 }
104 112
105 private List<IResourceDescription> getResourceDescriptions(Problem problem) { 113 private List<IResourceDescription> getResourceDescriptions(Problem problem) {
@@ -127,4 +135,8 @@ public class TypeHashProvider {
127 } 135 }
128 return resourceDescriptions; 136 return resourceDescriptions;
129 } 137 }
138
139 private String getPresetColor(IEObjectDescription description) {
140 return description.getUserData(DocumentationCommentParser.COLOR_TAG);
141 }
130} 142}