diff options
author | 2024-03-22 03:19:29 +0100 | |
---|---|---|
committer | 2024-03-22 03:19:29 +0100 | |
commit | ddd0e0746c30ccf0bfd9b32b6225cd2ce3eb240c (patch) | |
tree | 34806446ca566bebb6524a95b1337fd1280fcf13 /subprojects/language/src/main/java/tools | |
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/src/main/java/tools')
3 files changed, 56 insertions, 0 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/ProblemRuntimeModule.java b/subprojects/language/src/main/java/tools/refinery/language/ProblemRuntimeModule.java index f9a564b0..f7039027 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/ProblemRuntimeModule.java +++ b/subprojects/language/src/main/java/tools/refinery/language/ProblemRuntimeModule.java | |||
@@ -12,6 +12,7 @@ package tools.refinery.language; | |||
12 | import com.google.inject.Binder; | 12 | import com.google.inject.Binder; |
13 | import com.google.inject.name.Names; | 13 | import com.google.inject.name.Names; |
14 | import org.eclipse.xtext.conversion.IValueConverterService; | 14 | import org.eclipse.xtext.conversion.IValueConverterService; |
15 | import org.eclipse.xtext.documentation.impl.AbstractMultiLineCommentProvider; | ||
15 | import org.eclipse.xtext.linking.ILinkingService; | 16 | import org.eclipse.xtext.linking.ILinkingService; |
16 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | 17 | import org.eclipse.xtext.naming.IQualifiedNameConverter; |
17 | import org.eclipse.xtext.naming.IQualifiedNameProvider; | 18 | import org.eclipse.xtext.naming.IQualifiedNameProvider; |
@@ -144,4 +145,11 @@ public class ProblemRuntimeModule extends AbstractProblemRuntimeModule { | |||
144 | public Class<? extends IDiagnosticConverter> bindIDiagnosticConverter() { | 145 | public Class<? extends IDiagnosticConverter> bindIDiagnosticConverter() { |
145 | return ProblemDiagnosticConverter.class; | 146 | return ProblemDiagnosticConverter.class; |
146 | } | 147 | } |
148 | |||
149 | public void configureAbstractMultiLineCommentProvider(Binder binder) { | ||
150 | // Only parse documentation tags from Javadoc-style comments. | ||
151 | binder.bind(String.class) | ||
152 | .annotatedWith(Names.named(AbstractMultiLineCommentProvider.START_TAG)) | ||
153 | .toInstance("/\\*\\*"); | ||
154 | } | ||
147 | } | 155 | } |
diff --git a/subprojects/language/src/main/java/tools/refinery/language/documentation/DocumentationCommentParser.java b/subprojects/language/src/main/java/tools/refinery/language/documentation/DocumentationCommentParser.java new file mode 100644 index 00000000..3586abd2 --- /dev/null +++ b/subprojects/language/src/main/java/tools/refinery/language/documentation/DocumentationCommentParser.java | |||
@@ -0,0 +1,42 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.language.documentation; | ||
7 | |||
8 | import com.google.inject.Inject; | ||
9 | import com.google.inject.Singleton; | ||
10 | import org.eclipse.emf.ecore.EObject; | ||
11 | import org.eclipse.xtext.documentation.IEObjectDocumentationProvider; | ||
12 | |||
13 | import java.util.Locale; | ||
14 | import java.util.Map; | ||
15 | import java.util.regex.Pattern; | ||
16 | |||
17 | @Singleton | ||
18 | public class DocumentationCommentParser { | ||
19 | private static final String PREFIX = "tools.refinery.language.documentation.DocumentationCommentParser."; | ||
20 | public static final String COLOR_TAG = PREFIX + "COLOR_TAG"; | ||
21 | |||
22 | private static final Pattern COLOR_PATTERN = Pattern.compile( | ||
23 | "(?m)^@color[ \t]+(\\d|#[\\da-fA-F]{6}|#[\\da-fA-F]{3})"); | ||
24 | |||
25 | @Inject | ||
26 | private IEObjectDocumentationProvider documentationProvider; | ||
27 | |||
28 | public Map<String, String> parseDocumentation(EObject eObject) { | ||
29 | var documentation = documentationProvider.getDocumentation(eObject); | ||
30 | if (documentation == null) { | ||
31 | return Map.of(); | ||
32 | } | ||
33 | var colorMatch = COLOR_PATTERN.matcher(documentation); | ||
34 | if (colorMatch.find()) { | ||
35 | // Use a {@code _} instead of a {@code #} to signify hex codes, because the type hashes have to be valid | ||
36 | // CSS class names. | ||
37 | var color = colorMatch.group(1).toLowerCase(Locale.ROOT).replace("#", "_"); | ||
38 | return Map.of(COLOR_TAG, color); | ||
39 | } | ||
40 | return Map.of(); | ||
41 | } | ||
42 | } | ||
diff --git a/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescriptionStrategy.java b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescriptionStrategy.java index 3080a78e..3dcf6b1f 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescriptionStrategy.java +++ b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescriptionStrategy.java | |||
@@ -18,6 +18,7 @@ import org.eclipse.xtext.resource.EObjectDescription; | |||
18 | import org.eclipse.xtext.resource.IEObjectDescription; | 18 | import org.eclipse.xtext.resource.IEObjectDescription; |
19 | import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy; | 19 | import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy; |
20 | import org.eclipse.xtext.util.IAcceptor; | 20 | import org.eclipse.xtext.util.IAcceptor; |
21 | import tools.refinery.language.documentation.DocumentationCommentParser; | ||
21 | import tools.refinery.language.naming.ProblemQualifiedNameProvider; | 22 | import tools.refinery.language.naming.ProblemQualifiedNameProvider; |
22 | import tools.refinery.language.scoping.imports.ImportCollector; | 23 | import tools.refinery.language.scoping.imports.ImportCollector; |
23 | import tools.refinery.language.model.problem.*; | 24 | import tools.refinery.language.model.problem.*; |
@@ -56,6 +57,9 @@ public class ProblemResourceDescriptionStrategy extends DefaultResourceDescripti | |||
56 | @Inject | 57 | @Inject |
57 | private ImportCollector importCollector; | 58 | private ImportCollector importCollector; |
58 | 59 | ||
60 | @Inject | ||
61 | private DocumentationCommentParser documentationCommentParser; | ||
62 | |||
59 | @Override | 63 | @Override |
60 | public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) { | 64 | public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) { |
61 | if (!shouldExport(eObject)) { | 65 | if (!shouldExport(eObject)) { |
@@ -154,6 +158,8 @@ public class ProblemResourceDescriptionStrategy extends DefaultResourceDescripti | |||
154 | if (eObject instanceof PredicateDefinition predicateDefinition && predicateDefinition.isError()) { | 158 | if (eObject instanceof PredicateDefinition predicateDefinition && predicateDefinition.isError()) { |
155 | builder.put(ERROR_PREDICATE, ERROR_PREDICATE_TRUE); | 159 | builder.put(ERROR_PREDICATE, ERROR_PREDICATE_TRUE); |
156 | } | 160 | } |
161 | var documentationMap = documentationCommentParser.parseDocumentation(eObject); | ||
162 | builder.putAll(documentationMap); | ||
157 | return builder.build(); | 163 | return builder.build(); |
158 | } | 164 | } |
159 | 165 | ||