PBS highlight colors
This commit is contained in:
+5
-2
@@ -11,16 +11,19 @@ final class EditorDocumentHighlightingRouter {
|
||||
final EditorOpenFileBuffer fileBuffer,
|
||||
final EditorDocumentPresentation presentation,
|
||||
final LspAnalyzeDocumentResult analysis) {
|
||||
final var localHighlighting = presentation.highlight(fileBuffer.content());
|
||||
if (fileBuffer.frontendDocument()
|
||||
&& analysis != null
|
||||
&& presentation.supportsSemanticHighlighting()
|
||||
&& !analysis.semanticHighlights().isEmpty()) {
|
||||
return new EditorDocumentHighlightingResult(
|
||||
EditorDocumentHighlightOwner.LSP,
|
||||
EditorDocumentSemanticHighlighting.highlight(fileBuffer.content(), analysis.semanticHighlights()));
|
||||
EditorDocumentSemanticHighlighting.overlay(
|
||||
localHighlighting,
|
||||
EditorDocumentSemanticHighlighting.highlight(fileBuffer.content(), analysis.semanticHighlights())));
|
||||
}
|
||||
return new EditorDocumentHighlightingResult(
|
||||
EditorDocumentHighlightOwner.LOCAL,
|
||||
presentation.highlight(fileBuffer.content()));
|
||||
localHighlighting);
|
||||
}
|
||||
}
|
||||
|
||||
+35
-13
@@ -1,6 +1,7 @@
|
||||
package p.studio.workspaces.editor;
|
||||
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.models.FrontendSemanticPresentationSpec;
|
||||
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
|
||||
import p.studio.vfs.messages.VfsDocumentTypeIds;
|
||||
import p.studio.workspaces.editor.syntaxhighlight.EditorDocumentSyntaxHighlighting;
|
||||
@@ -55,21 +56,19 @@ final class EditorDocumentPresentationRegistry {
|
||||
private EditorDocumentPresentation frontendPresentation(
|
||||
final String normalizedTypeId,
|
||||
final LspSemanticPresentationDTO semanticPresentation) {
|
||||
if (semanticPresentation == null) {
|
||||
return new EditorDocumentPresentation(
|
||||
normalizedTypeId,
|
||||
java.util.List.of(),
|
||||
java.util.List.of(),
|
||||
EditorDocumentSyntaxHighlighting.plainText());
|
||||
}
|
||||
final var frontendSpec = FrontendRegistryService.getFrontendSpec(normalizedTypeId).orElseThrow();
|
||||
final FrontendSemanticPresentationSpec frontendPresentationSpec = frontendSpec.getSemanticPresentation();
|
||||
final java.util.List<String> stylesheetUrls = resolveFrontendStylesheets(
|
||||
semanticPresentation == null ? java.util.List.of() : semanticPresentation.resources(),
|
||||
frontendPresentationSpec.resources());
|
||||
final java.util.List<String> semanticKeys = semanticPresentation == null
|
||||
? frontendPresentationSpec.semanticKeys()
|
||||
: semanticPresentation.semanticKeys();
|
||||
return new EditorDocumentPresentation(
|
||||
normalizedTypeId,
|
||||
semanticPresentation.resources().stream()
|
||||
.map(EditorDocumentPresentationRegistry::resourceStylesheet)
|
||||
.flatMap(Optional::stream)
|
||||
.toList(),
|
||||
semanticPresentation.semanticKeys(),
|
||||
EditorDocumentSyntaxHighlighting.plainText());
|
||||
stylesheetUrls,
|
||||
semanticKeys,
|
||||
frontendSyntaxHighlighting(normalizedTypeId));
|
||||
}
|
||||
|
||||
private String normalize(final String typeId) {
|
||||
@@ -93,6 +92,29 @@ final class EditorDocumentPresentationRegistry {
|
||||
return Optional.of(resource.toExternalForm());
|
||||
}
|
||||
|
||||
private static java.util.List<String> resolveFrontendStylesheets(
|
||||
final java.util.List<String> primaryResources,
|
||||
final java.util.List<String> fallbackResources) {
|
||||
final java.util.List<String> primary = primaryResources.stream()
|
||||
.map(EditorDocumentPresentationRegistry::resourceStylesheet)
|
||||
.flatMap(Optional::stream)
|
||||
.toList();
|
||||
if (!primary.isEmpty()) {
|
||||
return primary;
|
||||
}
|
||||
return fallbackResources.stream()
|
||||
.map(EditorDocumentPresentationRegistry::resourceStylesheet)
|
||||
.flatMap(Optional::stream)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static EditorDocumentSyntaxHighlighting frontendSyntaxHighlighting(final String normalizedTypeId) {
|
||||
return switch (normalizedTypeId) {
|
||||
case "pbs" -> EditorDocumentSyntaxHighlighting.pbs();
|
||||
default -> EditorDocumentSyntaxHighlighting.plainText();
|
||||
};
|
||||
}
|
||||
|
||||
private static String normalizeResourcePath(final String resourcePath) {
|
||||
final String normalized = Objects.requireNonNull(resourcePath, "resourcePath").trim();
|
||||
if (normalized.isEmpty()) {
|
||||
|
||||
+21
@@ -7,6 +7,7 @@ import p.studio.lsp.dtos.LspHighlightSpanDTO;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
public final class EditorDocumentSemanticHighlighting {
|
||||
@@ -40,4 +41,24 @@ public final class EditorDocumentSemanticHighlighting {
|
||||
}
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
public static StyleSpans<Collection<String>> overlay(
|
||||
final StyleSpans<Collection<String>> baseHighlighting,
|
||||
final StyleSpans<Collection<String>> semanticHighlighting) {
|
||||
return baseHighlighting.overlay(semanticHighlighting, EditorDocumentSemanticHighlighting::mergeStyles);
|
||||
}
|
||||
|
||||
private static Collection<String> mergeStyles(
|
||||
final Collection<String> baseStyles,
|
||||
final Collection<String> semanticStyles) {
|
||||
if (baseStyles.isEmpty()) {
|
||||
return semanticStyles;
|
||||
}
|
||||
if (semanticStyles.isEmpty()) {
|
||||
return baseStyles;
|
||||
}
|
||||
final LinkedHashSet<String> merged = new LinkedHashSet<>(baseStyles);
|
||||
merged.addAll(semanticStyles);
|
||||
return List.copyOf(merged);
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -36,6 +36,10 @@ public record EditorDocumentSyntaxHighlighting(
|
||||
return EditorDocumentSyntaxHighlightingBash.BASH;
|
||||
}
|
||||
|
||||
public static EditorDocumentSyntaxHighlighting pbs() {
|
||||
return EditorDocumentSyntaxHighlightingPbs.PBS;
|
||||
}
|
||||
|
||||
public StyleSpans<Collection<String>> highlight(final String content) {
|
||||
final StyleSpansBuilder<Collection<String>> builder = new StyleSpansBuilder<>();
|
||||
if (tokenPattern == null) {
|
||||
|
||||
+41
-4
@@ -46,7 +46,8 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
analysis);
|
||||
|
||||
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
|
||||
assertEquals(List.of("editor-semantic-pbs-keyword"), List.copyOf(result.styleSpans().getStyleSpan(0).getStyle()));
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,7 +81,7 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void frontendDocumentsDegradeToLocalWhenSemanticPresentationResourcesAreUnavailable() {
|
||||
void frontendDocumentsFallbackToLocalPbsSyntaxWhenSemanticPresentationResourcesAreUnavailable() {
|
||||
final EditorDocumentPresentationRegistry registry = new EditorDocumentPresentationRegistry();
|
||||
final EditorOpenFileBuffer fileBuffer = new EditorOpenFileBuffer(
|
||||
Path.of("/tmp/example/src/main.pbs"),
|
||||
@@ -108,8 +109,9 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
registry.resolve("pbs", analysis.semanticPresentation()),
|
||||
analysis);
|
||||
|
||||
assertEquals(EditorDocumentHighlightOwner.LOCAL, result.owner());
|
||||
assertTrue(result.styleSpans().getStyleSpan(0).getStyle().isEmpty());
|
||||
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -145,6 +147,41 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-service"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void frontendDocumentsKeepLocalSyntaxForRangesWithoutSemanticCoverage() {
|
||||
final EditorDocumentPresentationRegistry registry = new EditorDocumentPresentationRegistry();
|
||||
final EditorOpenFileBuffer fileBuffer = new EditorOpenFileBuffer(
|
||||
Path.of("/tmp/example/src/main.pbs"),
|
||||
"main.pbs",
|
||||
"pbs",
|
||||
"fn main() -> void { Game.tick(1); }",
|
||||
"LF",
|
||||
true,
|
||||
VfsDocumentAccessMode.READ_ONLY,
|
||||
false);
|
||||
|
||||
final LspAnalyzeDocumentResult analysis = new LspAnalyzeDocumentResult(
|
||||
new LspSessionStateDTO(true, List.of("highlight")),
|
||||
new LspSemanticPresentationDTO(
|
||||
List.of("pbs-service"),
|
||||
List.of("/themes/pbs/semantic-highlighting.css")),
|
||||
List.of(),
|
||||
List.of(new LspHighlightSpanDTO(new LspRangeDTO(19, 23), "pbs-service")),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
|
||||
fileBuffer,
|
||||
registry.resolve("pbs", analysis.semanticPresentation()),
|
||||
analysis);
|
||||
|
||||
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-service"));
|
||||
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
|
||||
}
|
||||
|
||||
private boolean containsStyle(
|
||||
final org.fxmisc.richtext.model.StyleSpans<Collection<String>> styleSpans,
|
||||
final String styleClass) {
|
||||
|
||||
+4
-2
@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
final class EditorDocumentPresentationRegistryTest {
|
||||
@@ -24,7 +25,7 @@ final class EditorDocumentPresentationRegistryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingFrontendResourcesDegradeToPlainFrontendPresentation() {
|
||||
void missingFrontendResourcesFallbackToFrontendOwnedPresentationResources() {
|
||||
final EditorDocumentPresentation presentation = registry.resolve(
|
||||
"pbs",
|
||||
new LspSemanticPresentationDTO(
|
||||
@@ -33,7 +34,8 @@ final class EditorDocumentPresentationRegistryTest {
|
||||
|
||||
assertEquals("pbs", presentation.styleKey());
|
||||
assertEquals(java.util.List.of("pbs-keyword"), presentation.semanticKeys());
|
||||
assertTrue(presentation.stylesheetUrls().isEmpty());
|
||||
assertEquals(1, presentation.stylesheetUrls().size());
|
||||
assertFalse(presentation.highlight("fn main() -> void {}").getStyleSpan(0).getStyle().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user