implements PLN-0036 studio inline hint rendering and rollout

This commit is contained in:
2026-04-03 11:40:52 +01:00
parent 09d9bb4c96
commit 55e821989c
11 changed files with 496 additions and 7 deletions
@@ -1,16 +1,20 @@
package p.studio.workspaces.editor;
import org.fxmisc.richtext.model.StyleSpans;
import p.studio.lsp.dtos.LspInlineHintDTO;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
record EditorDocumentHighlightingResult(
EditorDocumentHighlightOwner owner,
StyleSpans<Collection<String>> styleSpans) {
StyleSpans<Collection<String>> styleSpans,
List<LspInlineHintDTO> inlineHints) {
EditorDocumentHighlightingResult {
owner = Objects.requireNonNull(owner, "owner");
styleSpans = Objects.requireNonNull(styleSpans, "styleSpans");
inlineHints = List.copyOf(Objects.requireNonNull(inlineHints, "inlineHints"));
}
}
@@ -12,6 +12,7 @@ final class EditorDocumentHighlightingRouter {
final EditorDocumentPresentation presentation,
final LspAnalyzeDocumentResult analysis) {
final var localHighlighting = presentation.highlight(fileBuffer.content());
final var inlineHints = EditorDocumentInlineHintRouter.route(fileBuffer, analysis);
if (fileBuffer.frontendDocument()
&& analysis != null
&& presentation.supportsSemanticHighlighting()
@@ -20,10 +21,12 @@ final class EditorDocumentHighlightingRouter {
EditorDocumentHighlightOwner.LSP,
EditorDocumentSemanticHighlighting.overlay(
localHighlighting,
EditorDocumentSemanticHighlighting.highlight(fileBuffer.content(), analysis.semanticHighlights())));
EditorDocumentSemanticHighlighting.highlight(fileBuffer.content(), analysis.semanticHighlights())),
inlineHints);
}
return new EditorDocumentHighlightingResult(
EditorDocumentHighlightOwner.LOCAL,
localHighlighting);
localHighlighting,
inlineHints);
}
}
@@ -0,0 +1,20 @@
package p.studio.workspaces.editor;
import p.studio.lsp.dtos.LspInlineHintDTO;
import p.studio.lsp.messages.LspAnalyzeDocumentResult;
import java.util.List;
final class EditorDocumentInlineHintRouter {
private EditorDocumentInlineHintRouter() {
}
static List<LspInlineHintDTO> route(
final EditorOpenFileBuffer fileBuffer,
final LspAnalyzeDocumentResult analysis) {
if (!fileBuffer.frontendDocument() || analysis == null || analysis.inlineHints().isEmpty()) {
return List.of();
}
return analysis.inlineHints();
}
}
@@ -0,0 +1,72 @@
package p.studio.workspaces.editor;
import p.studio.lsp.dtos.LspInlineHintDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
final class EditorInlineHintLayout {
static final double HORIZONTAL_GAP = 6.0;
private EditorInlineHintLayout() {
}
static List<Placement> layout(
final List<LspInlineHintDTO> inlineHints,
final AnchorBoundsResolver anchorBoundsResolver) {
final var placements = new ArrayList<Placement>();
for (final var inlineHint : inlineHints) {
final var anchorBounds = anchorBoundsResolver.resolve(inlineHint);
if (anchorBounds.isEmpty()) {
continue;
}
final var bounds = anchorBounds.orElseThrow();
placements.add(new Placement(
inlineHint,
displayText(inlineHint),
categoryStyleClass(inlineHint),
bounds.maxX() + HORIZONTAL_GAP,
bounds.minY(),
bounds.height()));
}
return List.copyOf(placements);
}
static String displayText(final LspInlineHintDTO inlineHint) {
return "[%s]".formatted(inlineHint.label());
}
static String categoryStyleClass(final LspInlineHintDTO inlineHint) {
final var normalizedCategory = inlineHint.category()
.toLowerCase(Locale.ROOT)
.replaceAll("[^a-z0-9]+", "-")
.replaceAll("^-+", "")
.replaceAll("-+$", "");
return normalizedCategory.isBlank()
? "editor-inline-hint-generic"
: "editor-inline-hint-" + normalizedCategory;
}
@FunctionalInterface
interface AnchorBoundsResolver {
Optional<AnchorBounds> resolve(LspInlineHintDTO inlineHint);
}
record AnchorBounds(
double minX,
double minY,
double maxX,
double height) {
}
record Placement(
LspInlineHintDTO inlineHint,
String displayText,
String categoryStyleClass,
double screenX,
double screenY,
double anchorHeight) {
}
}
@@ -0,0 +1,122 @@
package p.studio.workspaces.editor;
import javafx.application.Platform;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.model.TwoDimensional;
import org.reactfx.Subscription;
import p.studio.lsp.dtos.LspInlineHintDTO;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
final class EditorInlineHintOverlay {
private final CodeArea codeArea;
private final Pane overlayPane = new Pane();
private final Map<LspInlineHintDTO, Label> labelsByHint = new LinkedHashMap<>();
private final Subscription viewportSubscription;
private List<LspInlineHintDTO> inlineHints = List.of();
EditorInlineHintOverlay(final CodeArea codeArea) {
this.codeArea = Objects.requireNonNull(codeArea, "codeArea");
overlayPane.setMouseTransparent(true);
overlayPane.setPickOnBounds(false);
overlayPane.getStyleClass().add("editor-inline-hint-layer");
codeArea.estimatedScrollXProperty().addListener((ignored, previous, current) -> refresh());
codeArea.estimatedScrollYProperty().addListener((ignored, previous, current) -> refresh());
codeArea.layoutBoundsProperty().addListener((ignored, previous, current) -> refresh());
codeArea.textProperty().addListener((ignored, previous, current) -> refresh());
overlayPane.layoutBoundsProperty().addListener((ignored, previous, current) -> refresh());
viewportSubscription = codeArea.viewportDirtyEvents().subscribe(ignored -> refresh());
}
Node node() {
return overlayPane;
}
void setInlineHints(final List<LspInlineHintDTO> inlineHints) {
this.inlineHints = List.copyOf(Objects.requireNonNull(inlineHints, "inlineHints"));
rebuildLabels();
scheduleRefresh();
}
void clear() {
setInlineHints(List.of());
}
void dispose() {
viewportSubscription.unsubscribe();
}
private void rebuildLabels() {
labelsByHint.clear();
overlayPane.getChildren().clear();
for (final var inlineHint : inlineHints) {
final var label = new Label(EditorInlineHintLayout.displayText(inlineHint));
label.setManaged(false);
label.setMouseTransparent(true);
label.setPickOnBounds(false);
label.getStyleClass().add("editor-inline-hint");
label.getStyleClass().add(EditorInlineHintLayout.categoryStyleClass(inlineHint));
labelsByHint.put(inlineHint, label);
overlayPane.getChildren().add(label);
}
}
private void scheduleRefresh() {
Platform.runLater(this::refresh);
}
private void refresh() {
if (overlayPane.getScene() == null || inlineHints.isEmpty()) {
overlayPane.getChildren().forEach(node -> node.setVisible(false));
return;
}
final var placements = EditorInlineHintLayout.layout(inlineHints, this::resolveAnchorBounds);
final var placedHints = new java.util.HashSet<LspInlineHintDTO>();
for (final var placement : placements) {
final var label = labelsByHint.get(placement.inlineHint());
if (label == null) {
continue;
}
label.applyCss();
label.autosize();
final Point2D localPoint = overlayPane.screenToLocal(placement.screenX(), placement.screenY());
if (!Double.isFinite(localPoint.getX()) || !Double.isFinite(localPoint.getY())) {
label.setVisible(false);
continue;
}
final double layoutY = localPoint.getY() + Math.max(0.0d, (placement.anchorHeight() - label.getHeight()) / 2.0d);
label.relocate(localPoint.getX(), layoutY);
label.setVisible(true);
placedHints.add(placement.inlineHint());
}
labelsByHint.forEach((inlineHint, label) -> {
if (!placedHints.contains(inlineHint)) {
label.setVisible(false);
}
});
}
private Optional<EditorInlineHintLayout.AnchorBounds> resolveAnchorBounds(final LspInlineHintDTO inlineHint) {
if (codeArea.getLength() == 0) {
return Optional.empty();
}
final int anchorOffsetExclusive = Math.max(inlineHint.anchor().startOffset() + 1, inlineHint.anchor().endOffset());
final int anchorCharOffset = Math.max(0, Math.min(codeArea.getLength() - 1, anchorOffsetExclusive - 1));
final var position = codeArea.offsetToPosition(anchorCharOffset, TwoDimensional.Bias.Forward);
final Optional<Bounds> characterBounds = codeArea.getCharacterBoundsOnScreen(position.getMajor(), position.getMinor());
return characterBounds.map(bounds -> new EditorInlineHintLayout.AnchorBounds(
bounds.getMinX(),
bounds.getMinY(),
bounds.getMaxX(),
bounds.getHeight()));
}
}
@@ -30,6 +30,7 @@ public final class EditorWorkspace extends Workspace {
private final BorderPane root = new BorderPane();
private final CodeArea codeArea = new CodeArea();
private final VirtualizedScrollPane<CodeArea> codeScroller = new VirtualizedScrollPane<>(codeArea);
private final EditorInlineHintOverlay inlineHintOverlay = new EditorInlineHintOverlay(codeArea);
private final Button saveButton = new Button();
private final Button saveAllButton = new Button();
private final EditorWarningStrip warningStrip = new EditorWarningStrip();
@@ -90,7 +91,7 @@ public final class EditorWorkspace extends Workspace {
@Override
public void unLoad() {
inlineHintOverlay.dispose();
}
public CodeArea codeArea() { return codeArea; }
@@ -146,6 +147,7 @@ public final class EditorWorkspace extends Workspace {
try {
codeArea.replaceText(fileBuffer.content());
codeArea.setStyleSpans(0, highlighting.styleSpans());
inlineHintOverlay.setInlineHints(highlighting.inlineHints());
codeArea.moveTo(0);
codeArea.requestFollowCaret();
} finally {
@@ -195,6 +197,7 @@ public final class EditorWorkspace extends Workspace {
try {
codeArea.replaceText("");
codeArea.setStyleSpans(0, presentation.highlight(""));
inlineHintOverlay.clear();
codeArea.moveTo(0);
codeArea.requestFollowCaret();
} finally {
@@ -247,10 +250,12 @@ public final class EditorWorkspace extends Workspace {
}
private VBox buildCenterColumn() {
final var editorSurface = new VBox(0, warningStrip, codeScroller);
final var editorViewport = new StackPane(codeScroller, inlineHintOverlay.node());
editorViewport.getStyleClass().add("editor-workspace-editor-viewport");
final var editorSurface = new VBox(0, warningStrip, editorViewport);
editorSurface.getStyleClass().add("editor-workspace-editor-surface");
codeScroller.getStyleClass().add("editor-workspace-code-scroller");
VBox.setVgrow(codeScroller, Priority.ALWAYS);
VBox.setVgrow(editorViewport, Priority.ALWAYS);
final var centerColumn = new VBox(12, tabStrip, editorSurface);
centerColumn.getStyleClass().add("editor-workspace-center-column");
VBox.setVgrow(editorSurface, Priority.ALWAYS);
@@ -707,6 +707,25 @@
-fx-border-width: 1;
}
.editor-workspace-editor-viewport {
-fx-background-color: transparent;
}
.editor-inline-hint-layer {
-fx-background-color: transparent;
}
.editor-inline-hint {
-fx-text-fill: rgba(182, 196, 209, 0.72);
-fx-font-family: "JetBrains Mono Medium", "JetBrains Mono", "Iosevka", "Cascadia Mono", "IBM Plex Mono", monospace;
-fx-font-size: 14px;
-fx-opacity: 0.82;
}
.editor-inline-hint-type {
-fx-text-fill: rgba(135, 198, 114, 0.82);
}
.editor-workspace-code-area-type-text .text {
-fx-fill: #eef3f8;
}
@@ -13,6 +13,7 @@ import java.util.Collection;
import java.util.List;
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 EditorDocumentHighlightingRouterTest {
@@ -47,6 +48,7 @@ final class EditorDocumentHighlightingRouterTest {
analysis);
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
assertTrue(result.inlineHints().isEmpty());
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
}
@@ -80,6 +82,7 @@ final class EditorDocumentHighlightingRouterTest {
analysis);
assertEquals(EditorDocumentHighlightOwner.LOCAL, result.owner());
assertTrue(result.inlineHints().isEmpty());
}
@Test
@@ -113,6 +116,7 @@ final class EditorDocumentHighlightingRouterTest {
analysis);
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
assertTrue(result.inlineHints().isEmpty());
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
}
@@ -148,6 +152,7 @@ final class EditorDocumentHighlightingRouterTest {
analysis);
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
assertTrue(result.inlineHints().isEmpty());
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-service"));
}
@@ -182,11 +187,79 @@ final class EditorDocumentHighlightingRouterTest {
analysis);
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
assertTrue(result.inlineHints().isEmpty());
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-keyword"));
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-service"));
assertTrue(containsStyle(result.styleSpans(), "editor-semantic-pbs-function"));
}
@Test
void frontendDocumentsPreserveTransportedInlineHintsEvenWithoutSemanticHighlightCoverage() {
final EditorDocumentPresentationRegistry registry = new EditorDocumentPresentationRegistry();
final EditorOpenFileBuffer fileBuffer = new EditorOpenFileBuffer(
Path.of("/tmp/example/src/main.pbs"),
"main.pbs",
"pbs",
"fn main() -> void { let value = 1; }",
"LF",
true,
VfsDocumentAccessMode.READ_ONLY,
false);
final LspAnalyzeDocumentResult analysis = new LspAnalyzeDocumentResult(
new LspSessionStateDTO(true, List.of("highlight")),
new LspSemanticPresentationDTO(
List.of("pbs-function"),
List.of("/themes/pbs/semantic-highlighting.css")),
List.of(),
List.of(),
List.of(new p.studio.lsp.dtos.LspInlineHintDTO(new LspRangeDTO(24, 29), "int", "type")),
List.of(),
List.of(),
List.of());
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
fileBuffer,
registry.resolve("pbs", analysis.semanticPresentation()),
analysis);
assertEquals(EditorDocumentHighlightOwner.LOCAL, result.owner());
assertEquals(1, result.inlineHints().size());
assertEquals("int", result.inlineHints().getFirst().label());
}
@Test
void nonFrontendDocumentsDiscardTransportedInlineHints() {
final EditorDocumentPresentationRegistry registry = new EditorDocumentPresentationRegistry();
final EditorOpenFileBuffer fileBuffer = new EditorOpenFileBuffer(
Path.of("/tmp/example/prometeu.json"),
"prometeu.json",
"json",
"{\n \"name\": \"Example\"\n}\n",
"LF",
false,
VfsDocumentAccessMode.EDITABLE,
false);
final LspAnalyzeDocumentResult analysis = new LspAnalyzeDocumentResult(
new LspSessionStateDTO(true, List.of("highlight")),
new LspSemanticPresentationDTO(List.of(), List.of()),
List.of(),
List.of(),
List.of(new p.studio.lsp.dtos.LspInlineHintDTO(new LspRangeDTO(0, 1), "ignored", "type")),
List.of(),
List.of(),
List.of());
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
fileBuffer,
registry.resolve("json"),
analysis);
assertEquals(EditorDocumentHighlightOwner.LOCAL, result.owner());
assertTrue(result.inlineHints().isEmpty());
}
private boolean containsStyle(
final org.fxmisc.richtext.model.StyleSpans<Collection<String>> styleSpans,
final String styleClass) {
@@ -0,0 +1,44 @@
package p.studio.workspaces.editor;
import org.junit.jupiter.api.Test;
import p.studio.lsp.dtos.LspInlineHintDTO;
import p.studio.lsp.dtos.LspRangeDTO;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
final class EditorInlineHintLayoutTest {
@Test
void layoutCreatesDecorativePlacementsForHintsWithResolvedAnchors() {
final var hint = new LspInlineHintDTO(new LspRangeDTO(10, 15), "int", "type");
final var placements = EditorInlineHintLayout.layout(
List.of(hint),
ignored -> Optional.of(new EditorInlineHintLayout.AnchorBounds(20.0d, 8.0d, 40.0d, 16.0d)));
assertEquals(1, placements.size());
assertEquals("[int]", placements.getFirst().displayText());
assertEquals("editor-inline-hint-type", placements.getFirst().categoryStyleClass());
assertEquals(46.0d, placements.getFirst().screenX());
assertEquals(8.0d, placements.getFirst().screenY());
assertEquals(16.0d, placements.getFirst().anchorHeight());
}
@Test
void layoutKeepsValidHintsWhenOnlySomeAnchorsResolve() {
final var stable = new LspInlineHintDTO(new LspRangeDTO(10, 15), "int", "type");
final var missing = new LspInlineHintDTO(new LspRangeDTO(30, 35), "Player", "type");
final var placements = EditorInlineHintLayout.layout(
List.of(stable, missing),
hint -> hint == stable
? Optional.of(new EditorInlineHintLayout.AnchorBounds(12.0d, 4.0d, 28.0d, 14.0d))
: Optional.empty());
assertEquals(1, placements.size());
assertEquals(stable, placements.getFirst().inlineHint());
assertEquals("[int]", placements.getFirst().displayText());
}
}