implements PLN-0025

This commit is contained in:
2026-03-31 17:19:08 +01:00
parent bcc89dcfbd
commit b64be2a9a1
13 changed files with 325 additions and 10 deletions
@@ -0,0 +1,6 @@
package p.studio.workspaces.editor;
enum EditorDocumentHighlightOwner {
LOCAL,
LSP
}
@@ -0,0 +1,16 @@
package p.studio.workspaces.editor;
import org.fxmisc.richtext.model.StyleSpans;
import java.util.Collection;
import java.util.Objects;
record EditorDocumentHighlightingResult(
EditorDocumentHighlightOwner owner,
StyleSpans<Collection<String>> styleSpans) {
EditorDocumentHighlightingResult {
owner = Objects.requireNonNull(owner, "owner");
styleSpans = Objects.requireNonNull(styleSpans, "styleSpans");
}
}
@@ -0,0 +1,25 @@
package p.studio.workspaces.editor;
import p.lsp.messages.PrometeuLspAnalyzeDocumentResult;
import p.studio.workspaces.editor.syntaxhighlight.EditorDocumentSemanticHighlighting;
final class EditorDocumentHighlightingRouter {
private EditorDocumentHighlightingRouter() {
}
static EditorDocumentHighlightingResult route(
final EditorOpenFileBuffer fileBuffer,
final EditorDocumentPresentation presentation,
final PrometeuLspAnalyzeDocumentResult analysis) {
if (fileBuffer.frontendDocument()
&& analysis != null
&& !analysis.semanticHighlights().isEmpty()) {
return new EditorDocumentHighlightingResult(
EditorDocumentHighlightOwner.LSP,
EditorDocumentSemanticHighlighting.highlight(fileBuffer.content(), analysis.semanticHighlights()));
}
return new EditorDocumentHighlightingResult(
EditorDocumentHighlightOwner.LOCAL,
presentation.highlight(fileBuffer.content()));
}
}
@@ -21,7 +21,7 @@ final class EditorDocumentPresentationRegistry {
EditorDocumentSyntaxHighlighting.bash());
private static final EditorDocumentPresentation FRONTEND_PRESENTATION = new EditorDocumentPresentation(
"fe",
java.util.List.of(),
java.util.List.of(stylesheet("presentations/fe.css")),
EditorDocumentSyntaxHighlighting.plainText());
EditorDocumentPresentation resolve(final String typeId) {
@@ -128,11 +128,18 @@ public final class EditorWorkspace extends Workspace {
final var fileBuffer = activeFile.orElseThrow();
final EditorDocumentPresentation presentation = presentationRegistry.resolve(fileBuffer.typeId());
final PrometeuLspAnalyzeDocumentResult analysis = fileBuffer.frontendDocument()
? prometeuLspService.analyzeDocument(new PrometeuLspAnalyzeDocumentRequest(fileBuffer.path()))
: null;
final EditorDocumentHighlightingResult highlighting = EditorDocumentHighlightingRouter.route(
fileBuffer,
presentation,
analysis);
applyPresentationStylesheets(presentation);
syncingEditor = true;
try {
codeArea.replaceText(fileBuffer.content());
codeArea.setStyleSpans(0, presentation.highlight(fileBuffer.content()));
codeArea.setStyleSpans(0, highlighting.styleSpans());
} finally {
syncingEditor = false;
}
@@ -140,7 +147,7 @@ public final class EditorWorkspace extends Workspace {
EditorDocumentPresentationStyles.applyToCodeArea(codeArea, presentation);
refreshCommandSurfaces(fileBuffer);
statusBar.showFile(projectReference, fileBuffer, presentation);
refreshSemanticOutline(fileBuffer);
refreshSemanticOutline(fileBuffer, analysis);
}
private void revealActiveFileInNavigator() {
@@ -317,13 +324,13 @@ public final class EditorWorkspace extends Workspace {
textDocument.dirty());
}
private void refreshSemanticOutline(final EditorOpenFileBuffer fileBuffer) {
if (!fileBuffer.frontendDocument()) {
private void refreshSemanticOutline(
final EditorOpenFileBuffer fileBuffer,
final PrometeuLspAnalyzeDocumentResult analysis) {
if (!fileBuffer.frontendDocument() || analysis == null) {
outlinePanel.showPlaceholder();
return;
}
final PrometeuLspAnalyzeDocumentResult analysis = prometeuLspService.analyzeDocument(
new PrometeuLspAnalyzeDocumentRequest(fileBuffer.path()));
outlinePanel.showSemanticReadResult(
fileBuffer.path(),
analysis.diagnostics(),
@@ -0,0 +1,43 @@
package p.studio.workspaces.editor.syntaxhighlight;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import p.lsp.dtos.PrometeuLspHighlightSpanDTO;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class EditorDocumentSemanticHighlighting {
private EditorDocumentSemanticHighlighting() {
}
public static StyleSpans<Collection<String>> highlight(
final String content,
final List<PrometeuLspHighlightSpanDTO> semanticHighlights) {
final StyleSpansBuilder<Collection<String>> builder = new StyleSpansBuilder<>();
final List<PrometeuLspHighlightSpanDTO> orderedHighlights = semanticHighlights.stream()
.sorted(Comparator.comparingInt(highlight -> highlight.range().startOffset()))
.toList();
int cursor = 0;
for (final PrometeuLspHighlightSpanDTO highlight : orderedHighlights) {
final int start = Math.max(cursor, highlight.range().startOffset());
final int end = Math.min(content.length(), highlight.range().endOffset());
if (start > cursor) {
builder.add(Collections.emptyList(), start - cursor);
}
if (end > start) {
builder.add(List.of("editor-syntax-" + highlight.semanticKey()), end - start);
cursor = end;
}
}
if (cursor < content.length()) {
builder.add(Collections.emptyList(), content.length() - cursor);
}
if (content.isEmpty()) {
builder.add(Collections.emptyList(), 0);
}
return builder.create();
}
}
@@ -0,0 +1,61 @@
.editor-workspace-code-area-type-fe {
-fx-highlight-fill: #1b3244;
}
.editor-workspace-code-area-type-fe .text {
-fx-fill: #edf4fb;
}
.editor-workspace-code-area-type-fe .lineno {
-fx-text-fill: #71859a;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-keyword {
-fx-fill: #8dc7ff;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-callable {
-fx-fill: #f0cb79;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-type {
-fx-fill: #9ddba8;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-binding {
-fx-fill: #ffb1c8;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-string {
-fx-fill: #e2c48c;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-number {
-fx-fill: #c4e58a;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-comment {
-fx-fill: #6f8192;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-literal {
-fx-fill: #c8a2ff;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-operator {
-fx-fill: #dbe6f1;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-punctuation {
-fx-fill: #adc1d4;
}
.editor-workspace-code-area-type-fe .text.editor-syntax-fe-identifier {
-fx-fill: #edf4fb;
}
.editor-workspace-status-chip-type-fe {
-fx-background-color: #152432;
-fx-border-color: #4d8db9;
-fx-text-fill: #e8f5ff;
}
@@ -0,0 +1,71 @@
package p.studio.workspaces.editor;
import org.junit.jupiter.api.Test;
import p.lsp.dtos.PrometeuLspHighlightSpanDTO;
import p.lsp.dtos.PrometeuLspRangeDTO;
import p.lsp.dtos.PrometeuLspSessionStateDTO;
import p.lsp.messages.PrometeuLspAnalyzeDocumentResult;
import p.studio.vfs.VfsDocumentAccessMode;
import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
final class EditorDocumentHighlightingRouterTest {
@Test
void frontendDocumentsUseLspOwnedHighlightsWhenSemanticSpansExist() {
final EditorDocumentPresentationRegistry registry = new EditorDocumentPresentationRegistry();
final EditorOpenFileBuffer fileBuffer = new EditorOpenFileBuffer(
Path.of("/tmp/example/src/main.pbs"),
"main.pbs",
"pbs",
"fn main() -> void {}",
"LF",
true,
VfsDocumentAccessMode.READ_ONLY,
false);
final PrometeuLspAnalyzeDocumentResult analysis = new PrometeuLspAnalyzeDocumentResult(
new PrometeuLspSessionStateDTO(true, List.of("highlight")),
List.of(),
List.of(new PrometeuLspHighlightSpanDTO(new PrometeuLspRangeDTO(0, 2), "fe-keyword")),
List.of(),
List.of());
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
fileBuffer,
registry.resolve("pbs"),
analysis);
assertEquals(EditorDocumentHighlightOwner.LSP, result.owner());
}
@Test
void nonFrontendDocumentsStayOnLocalHighlighting() {
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 PrometeuLspAnalyzeDocumentResult analysis = new PrometeuLspAnalyzeDocumentResult(
new PrometeuLspSessionStateDTO(true, List.of("highlight")),
List.of(),
List.of(new PrometeuLspHighlightSpanDTO(new PrometeuLspRangeDTO(0, 1), "fe-punctuation")),
List.of(),
List.of());
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
fileBuffer,
registry.resolve("json"),
analysis);
assertEquals(EditorDocumentHighlightOwner.LOCAL, result.owner());
}
}