implements PLN-0136
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview No changes detected, that affect the code coverage. * Line Coverage: 63.75% (19407/30442) * Branch Coverage: 54.17% (7740/14289) * Lines of Code: 30442 * Cyclomatic Complexity: 12365 #### Quality Gates Summary Output truncated.
Test / Build skipped: 15, passed: 685
Intrepid/Prometeu/Studio/pipeline/head This commit looks good

Add PBS quick fixes as frontend-owned repairs transported by the LSP.
Duplicate reserved attributes are deleted at the extra span. Interface
InitAllowed attributes with arguments become [InitAllowed]. The server
matches Diagnostic.code plus range, does not parse messages, and returns
an empty list when the capability or the fix is absent.

Housekeep DSC-0044 with LSN-0073.
This commit is contained in:
2026-09-22 07:43:05 +01:00
parent 6f45f4f205
commit ae891262e1
27 changed files with 1182 additions and 70 deletions
@@ -8,11 +8,13 @@ import p.studio.compiler.models.IRBackend;
import p.studio.compiler.services.FrontendPhaseService;
import p.studio.compiler.services.FrontendProvider;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.BaselineIssueSeverity;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.ReadOnlySet;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@@ -68,6 +70,10 @@ class CompilerLanguageServiceBridgeTest {
final var semanticTokens = bridge.semanticTokens(context, documentUri, "fn main() {}");
assertTrue(semanticTokens.legend().isEmpty());
assertTrue(semanticTokens.tokens().isEmpty());
assertTrue(bridge.codeActions(context, documentUri, "fn main() {}", List.of(
new BaselineCodeActionDiagnostic("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE", 0, 0, 0, 1)))
.actions().isEmpty());
}
@Test
@@ -555,6 +561,97 @@ class CompilerLanguageServiceBridgeTest {
assertTrue(stdlibRename.edits().isEmpty());
}
@Test
void quickFixDeletesTheDuplicateDocDiagnosticAndIgnoresOtherCodes() {
final Path projectRoot = findRepoRoot(Path.of("").toAbsolutePath().normalize())
.resolve("test-projects")
.resolve("main")
.toAbsolutePath()
.normalize();
final Path documentPath = projectRoot.resolve("src").resolve("main.pbs");
final String overlay = """
[Doc(markdown = \"""
First.
\""")]
[Doc(markdown = \"""
Second.
\""")]
fn duplicated() -> void { return; }
fn frame() -> void { return; }
""";
final CompilerLanguageServiceBridge bridge = new CompilerLanguageServiceBridge();
final LspProjectContext context = new LspProjectContext("main", "pbs", projectRoot);
final String documentUri = documentPath.toUri().toString();
final var analysis = bridge.analyzeDocument(context, documentUri, overlay);
final var duplicates = analysis.issues().stream()
.filter(issue -> issue.code().equals("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE"))
.toList();
assertEquals(1, duplicates.size(), analysis.issues().toString());
final var duplicate = duplicates.getFirst();
assertFalse(duplicate.code().isBlank());
final var diagnostic = new BaselineCodeActionDiagnostic(
duplicate.code(),
duplicate.startLine(),
duplicate.startCharacter(),
duplicate.endLine(),
duplicate.endCharacter());
final var actions = bridge.codeActions(context, documentUri, overlay, List.of(diagnostic));
assertEquals(1, actions.actions().size(), actions.toString());
final var action = actions.actions().getFirst();
assertEquals("Remove duplicate reserved attribute", action.title());
assertEquals(documentUri, action.uri());
assertEquals("", action.newText());
assertEquals(diagnostic.startLine(), action.startLine());
assertEquals(diagnostic.startCharacter(), action.startCharacter());
assertEquals(diagnostic.endLine(), action.endLine());
assertEquals(diagnostic.endCharacter(), action.endCharacter());
final String removed = sourceSlice(overlay, action.startLine(), action.startCharacter(), action.endLine(), action.endCharacter());
assertTrue(removed.contains("Second."), removed);
assertFalse(removed.contains("First."), removed);
final var wrongCode = new BaselineCodeActionDiagnostic(
"E_SEM_MALFORMED_RESERVED_ATTRIBUTE",
diagnostic.startLine(),
diagnostic.startCharacter(),
diagnostic.endLine(),
diagnostic.endCharacter());
assertTrue(bridge.codeActions(context, documentUri, overlay, List.of(wrongCode)).actions().isEmpty());
assertTrue(bridge.codeActions(context, documentUri, overlay, List.of()).actions().isEmpty());
final String malformed = """
[Doc(text = \"""
Wrong argument.
\""")]
fn frame() -> void { return; }
""";
final var malformedAnalysis = bridge.analyzeDocument(context, documentUri, malformed);
final var malformedDiagnostics = malformedAnalysis.issues().stream()
.map(issue -> new BaselineCodeActionDiagnostic(
issue.code(),
issue.startLine(),
issue.startCharacter(),
issue.endLine(),
issue.endCharacter()))
.toList();
assertTrue(bridge.codeActions(context, documentUri, malformed, malformedDiagnostics).actions().isEmpty());
}
private static String sourceSlice(
final String source,
final int startLine,
final int startCharacter,
final int endLine,
final int endCharacter) {
final DocumentPositionMapper mapper = new DocumentPositionMapper(source);
final byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
final int start = mapper.byteOffsetOf(startLine, startCharacter);
final int end = mapper.byteOffsetOf(endLine, endCharacter);
return new String(bytes, start, Math.max(0, end - start), StandardCharsets.UTF_8);
}
@Test
void documentSymbolsUseAstHierarchyOfTheOpenPbsFile() {
final Path projectRoot = findRepoRoot(Path.of("").toAbsolutePath().normalize())
@@ -667,6 +764,7 @@ class CompilerLanguageServiceBridgeTest {
assertTrue(description.documentSymbolsSupported());
assertTrue(description.workspaceSymbolsSupported());
assertTrue(description.renameSupported());
assertTrue(description.codeActionsSupported());
assertEquals(1, description.semanticHostProjections().size());
assertEquals("vscode", description.semanticHostProjections().getFirst().hostId());
assertTrue(description.semanticHostProjections().getFirst().tokenProjections().stream()
@@ -104,6 +104,7 @@ class PrometeuLanguageServerTest {
true,
true,
true,
false,
List.of(),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -223,6 +224,15 @@ class PrometeuLanguageServerTest {
return new BaselineSemanticTokens(List.of(), List.of());
}
@Override
public p.studio.lsp.messages.BaselineCodeActions codeActions(
final LspProjectContext context,
final String documentUri,
final String text,
final java.util.List<p.studio.lsp.messages.BaselineCodeActionDiagnostic> diagnostics) {
return p.studio.lsp.messages.BaselineCodeActions.empty();
}
@Override
public String onSave(final LspProjectContext project, final String documentUri) {
return "saved";
@@ -291,6 +301,13 @@ class PrometeuLanguageServerTest {
throw new UnsupportedOperationException();
}
@Override
public java.util.List<org.eclipse.lsp4j.jsonrpc.messages.Either<org.eclipse.lsp4j.Command, org.eclipse.lsp4j.CodeAction>> mapCodeActions(
final p.studio.lsp.messages.BaselineCodeActions actions,
final java.util.List<org.eclipse.lsp4j.Diagnostic> contextDiagnostics) {
throw new UnsupportedOperationException();
}
@Override
public org.eclipse.lsp4j.CompletionList mapCompletion(final BaselineCompletion completion) {
throw new UnsupportedOperationException();
@@ -264,6 +264,15 @@ final class PrometeuTextDocumentServiceTest {
return new BaselineSemanticTokens(List.of(), List.of());
}
@Override
public p.studio.lsp.messages.BaselineCodeActions codeActions(
final LspProjectContext context,
final String documentUri,
final String text,
final java.util.List<p.studio.lsp.messages.BaselineCodeActionDiagnostic> diagnostics) {
return p.studio.lsp.messages.BaselineCodeActions.empty();
}
@Override
public String onSave(final LspProjectContext context, final String documentUri) {
return "saved";
@@ -338,6 +347,13 @@ final class PrometeuTextDocumentServiceTest {
return new org.eclipse.lsp4j.WorkspaceEdit();
}
@Override
public java.util.List<org.eclipse.lsp4j.jsonrpc.messages.Either<org.eclipse.lsp4j.Command, org.eclipse.lsp4j.CodeAction>> mapCodeActions(
final p.studio.lsp.messages.BaselineCodeActions actions,
final java.util.List<org.eclipse.lsp4j.Diagnostic> contextDiagnostics) {
return java.util.List.of();
}
@Override
public CompletionList mapCompletion(final BaselineCompletion completion) {
return completionList;
@@ -166,6 +166,15 @@ final class PrometeuWorkspaceServiceTest {
throw new UnsupportedOperationException();
}
@Override
public p.studio.lsp.messages.BaselineCodeActions codeActions(
final LspProjectContext context,
final String documentUri,
final String text,
final java.util.List<p.studio.lsp.messages.BaselineCodeActionDiagnostic> diagnostics) {
throw new UnsupportedOperationException();
}
@Override
public String onSave(final LspProjectContext context, final String documentUri) {
throw new UnsupportedOperationException();
@@ -231,6 +240,13 @@ final class PrometeuWorkspaceServiceTest {
throw new UnsupportedOperationException();
}
@Override
public java.util.List<org.eclipse.lsp4j.jsonrpc.messages.Either<org.eclipse.lsp4j.Command, org.eclipse.lsp4j.CodeAction>> mapCodeActions(
final p.studio.lsp.messages.BaselineCodeActions actions,
final java.util.List<org.eclipse.lsp4j.Diagnostic> contextDiagnostics) {
throw new UnsupportedOperationException();
}
@Override
public org.eclipse.lsp4j.CompletionList mapCompletion(final BaselineCompletion completion) {
throw new UnsupportedOperationException();
@@ -1,10 +1,16 @@
package p.studio.lsp.services.protocol.mapping;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.jupiter.api.Test;
import p.studio.lsp.messages.BaselineCodeAction;
import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselineCompletion;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineDocumentIssue;
import p.studio.lsp.messages.BaselineIssueSeverity;
import p.studio.lsp.messages.BaselineCompletionItem;
import p.studio.lsp.messages.BaselineCompletionItemKind;
import p.studio.lsp.messages.BaselineDefinition;
@@ -28,6 +34,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
final class Lsp4jProtocolMessageMapperTest {
@@ -47,6 +54,7 @@ final class Lsp4jProtocolMessageMapperTest {
true,
true,
true,
true,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -72,6 +80,8 @@ final class Lsp4jProtocolMessageMapperTest {
assertEquals(Boolean.TRUE, result.getCapabilities().getDocumentSymbolProvider().getLeft());
assertEquals(Boolean.TRUE, result.getCapabilities().getWorkspaceSymbolProvider().getLeft());
assertEquals(Boolean.TRUE, result.getCapabilities().getRenameProvider().getRight().getPrepareProvider());
assertEquals(List.of(CodeActionKind.QuickFix), result.getCapabilities().getCodeActionProvider().getRight().getCodeActionKinds());
assertEquals(Boolean.FALSE, result.getCapabilities().getCodeActionProvider().getRight().getResolveProvider());
final var experimental = assertInstanceOf(Map.class, result.getCapabilities().getExperimental());
final var semanticPayload = assertInstanceOf(Map.class, experimental.get("prometeuSemanticHostProjections"));
@@ -248,4 +258,42 @@ final class Lsp4jProtocolMessageMapperTest {
assertEquals(SymbolKind.EnumMember, mapped.get(1).getKind());
assertTrue(mapper.mapWorkspaceSymbols(p.studio.lsp.messages.BaselineWorkspaceSymbols.empty()).isEmpty());
}
@Test
void diagnosticsPublishStableCodeAndQuickFixesAreEditsNotCommands() {
final var mapper = new Lsp4jProtocolMessageMapper();
final var published = mapper.mapDiagnostics("file:///tmp/demo.pbs", new BaselineDocumentAnalysis(List.of(
new BaselineDocumentIssue(
1,
2,
1,
18,
BaselineIssueSeverity.ERROR,
"E_SEM_DUPLICATE_RESERVED_ATTRIBUTE",
"Duplicate Doc attribute on the same declaration surface",
"E_SEM_DUPLICATE_RESERVED_ATTRIBUTE"))));
final var diagnostic = published.getDiagnostics().getFirst();
assertEquals("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE", diagnostic.getCode().getLeft());
assertNull(diagnostic.getData());
final var mapped = mapper.mapCodeActions(new BaselineCodeActions(List.of(new BaselineCodeAction(
"Remove duplicate reserved attribute",
"E_SEM_DUPLICATE_RESERVED_ATTRIBUTE",
"file:///tmp/demo.pbs",
1,
2,
1,
18,
""))), List.of(diagnostic));
assertEquals(1, mapped.size());
assertTrue(mapped.getFirst().isRight());
assertFalse(mapped.getFirst().isLeft());
final var action = mapped.getFirst().getRight();
assertEquals(CodeActionKind.QuickFix, action.getKind());
assertNull(action.getCommand());
assertEquals("", action.getEdit().getChanges().get("file:///tmp/demo.pbs").getFirst().getNewText());
assertEquals(1, action.getDiagnostics().size());
assertEquals(diagnostic, action.getDiagnostics().getFirst());
}
}