diff options
author | Kristóf Marussy <kristof@marussy.com> | 2024-03-22 03:19:29 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2024-03-22 03:19:29 +0100 |
commit | ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c (patch) | |
tree | 34806446ca566bebb6524a95b1337fd1280fcf13 /subprojects/language-ide/src/main/java | |
parent | refactor(web): improve web app styling (diff) | |
download | refinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.tar.gz refinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.tar.zst refinery-ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c.zip |
feat: custom identifier coloring
Diffstat (limited to 'subprojects/language-ide/src/main/java')
-rw-r--r-- | subprojects/language-ide/src/main/java/tools/refinery/language/ide/syntaxcoloring/TypeHashProvider.java | 24 |
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 | */ |
6 | package tools.refinery.language.ide.syntaxcoloring; | 6 | package tools.refinery.language.ide.syntaxcoloring; |
7 | 7 | ||
8 | import com.google.common.collect.ImmutableMap; | ||
9 | import com.google.inject.Inject; | 8 | import com.google.inject.Inject; |
10 | import com.google.inject.Singleton; | 9 | import com.google.inject.Singleton; |
11 | import org.eclipse.xtext.EcoreUtil2; | 10 | import org.eclipse.xtext.EcoreUtil2; |
12 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | 11 | import org.eclipse.xtext.naming.IQualifiedNameConverter; |
13 | import org.eclipse.xtext.naming.IQualifiedNameProvider; | 12 | import org.eclipse.xtext.naming.IQualifiedNameProvider; |
13 | import org.eclipse.xtext.resource.IEObjectDescription; | ||
14 | import org.eclipse.xtext.resource.IResourceDescription; | 14 | import org.eclipse.xtext.resource.IResourceDescription; |
15 | import org.eclipse.xtext.scoping.IScopeProvider; | 15 | import org.eclipse.xtext.scoping.IScopeProvider; |
16 | import org.eclipse.xtext.scoping.impl.GlobalResourceDescriptionProvider; | 16 | import org.eclipse.xtext.scoping.impl.GlobalResourceDescriptionProvider; |
17 | import org.eclipse.xtext.util.IResourceScopeCache; | 17 | import org.eclipse.xtext.util.IResourceScopeCache; |
18 | import tools.refinery.language.documentation.DocumentationCommentParser; | ||
18 | import tools.refinery.language.model.problem.*; | 19 | import tools.refinery.language.model.problem.*; |
19 | import tools.refinery.language.resource.ProblemResourceDescriptionStrategy; | 20 | import tools.refinery.language.resource.ProblemResourceDescriptionStrategy; |
20 | import tools.refinery.language.scoping.imports.ImportCollector; | 21 | import 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 | } |