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
@@ -0,0 +1,34 @@
package p.studio.lsp.messages;
import java.util.Objects;
public record BaselineCodeAction(
String title,
String diagnosticCode,
String uri,
int startLine,
int startCharacter,
int endLine,
int endCharacter,
String newText) {
public BaselineCodeAction {
title = requireText(title, "title");
diagnosticCode = requireText(diagnosticCode, "diagnosticCode");
uri = requireText(uri, "uri");
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("code action range coordinates must not be negative");
}
newText = Objects.requireNonNull(newText, "newText");
}
private static String requireText(
final String value,
final String field) {
final String candidate = Objects.requireNonNull(value, field).trim();
if (candidate.isEmpty()) {
throw new IllegalArgumentException(field + " must not be blank");
}
return candidate;
}
}
@@ -0,0 +1,16 @@
package p.studio.lsp.messages;
public record BaselineCodeActionDiagnostic(
String code,
int startLine,
int startCharacter,
int endLine,
int endCharacter) {
public BaselineCodeActionDiagnostic {
code = code == null ? "" : code.trim();
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("diagnostic range coordinates must not be negative");
}
}
}
@@ -0,0 +1,13 @@
package p.studio.lsp.messages;
import java.util.List;
public record BaselineCodeActions(List<BaselineCodeAction> actions) {
public BaselineCodeActions {
actions = actions == null ? List.of() : List.copyOf(actions);
}
public static BaselineCodeActions empty() {
return new BaselineCodeActions(List.of());
}
}
@@ -9,12 +9,14 @@ public record BaselineDocumentIssue(
int endCharacter,
BaselineIssueSeverity severity,
String source,
String message) {
String message,
String code) {
public BaselineDocumentIssue {
severity = Objects.requireNonNull(severity, "severity");
source = requireText(source, "source");
message = requireText(message, "message");
code = code == null ? "" : code.trim();
}
private static String requireText(
@@ -15,6 +15,7 @@ public record BaselineServerDescription(
boolean documentSymbolsSupported,
boolean workspaceSymbolsSupported,
boolean renameSupported,
boolean codeActionsSupported,
List<String> semanticTokenTypes,
List<BaselineSemanticHostProjection> semanticHostProjections,
List<BaselineVisualTheme> visualThemes,
@@ -7,6 +7,8 @@ import p.studio.lsp.messages.BaselineHover;
import p.studio.lsp.messages.BaselineDocumentSymbols;
import p.studio.lsp.messages.BaselineReferences;
import p.studio.lsp.messages.BaselineWorkspaceSymbols;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselinePrepareRename;
import p.studio.lsp.messages.BaselineRename;
import p.studio.lsp.messages.BaselineSemanticTokens;
@@ -14,6 +16,8 @@ import p.studio.lsp.messages.BaselineSignatureHelp;
import p.studio.lsp.messages.BaselineServerDescription;
import p.studio.lsp.messages.LspProjectContext;
import java.util.List;
public interface LanguageServiceBridge {
BaselineServerDescription describeServer(LspProjectContext context);
@@ -56,5 +60,11 @@ public interface LanguageServiceBridge {
BaselineSemanticTokens semanticTokens(LspProjectContext context, String documentUri, String text);
BaselineCodeActions codeActions(
LspProjectContext context,
String documentUri,
String text,
List<BaselineCodeActionDiagnostic> diagnostics);
String onSave(LspProjectContext context, String documentUri);
}
@@ -4,7 +4,9 @@ import org.apache.commons.lang3.StringUtils;
import p.studio.compiler.FrontendRegistryService;
import p.studio.compiler.messages.*;
import p.studio.compiler.models.*;
import p.studio.compiler.services.FrontendCodeAction;
import p.studio.compiler.services.FrontendCompletionCandidate;
import p.studio.compiler.services.FrontendLanguageService;
import p.studio.compiler.services.FrontendDefinitionLocation;
import p.studio.compiler.services.FrontendDocumentRequest;
import p.studio.compiler.services.FrontendDocumentSymbol;
@@ -50,7 +52,11 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
Objects.requireNonNull(context, "context");
final var frontend = frontendSpec(context);
final var presentation = frontend.getSemanticPresentation();
final boolean languageServicePresent = frontendProvider(context).languageService().isPresent();
final var languageService = frontendProvider(context).languageService();
final boolean languageServicePresent = languageService.isPresent();
final boolean codeActionsSupported = languageService
.map(FrontendLanguageService::codeActionsSupported)
.orElse(false);
return new BaselineServerDescription(
"Prometeu Studio LSP",
"0.1.0",
@@ -63,6 +69,7 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
languageServicePresent,
languageServicePresent,
languageServicePresent,
codeActionsSupported,
presentation.semanticKeys(),
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
presentation.themes().stream().map(this::mapVisualTheme).toList(),
@@ -354,6 +361,40 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
.orElseGet(() -> BaselineRename.refused("not renameable"));
}
@Override
public BaselineCodeActions codeActions(
final LspProjectContext context,
final String documentUri,
final String text,
final List<BaselineCodeActionDiagnostic> diagnostics) {
Objects.requireNonNull(context, "context");
final var languageService = frontendProvider(context).languageService();
if (languageService.isEmpty() || !languageService.orElseThrow().codeActionsSupported()) {
return BaselineCodeActions.empty();
}
if (diagnostics == null || diagnostics.isEmpty()) {
return BaselineCodeActions.empty();
}
return editorialDocument(context, documentUri, text)
.map(document -> {
final Path documentPath = normalizeDocumentPath(documentUri);
final FrontendDocumentRequest request = new FrontendDocumentRequest(
context.projectRoot(),
documentPath,
document.text());
final List<FrontendCodeAction> repairs = languageService.orElseThrow().codeActions(
request,
document.editorialContext());
return new BaselineCodeActions(mapCodeActions(
repairs,
documentUri,
documentPath,
document.text(),
diagnostics));
})
.orElseGet(BaselineCodeActions::empty);
}
@Override
public BaselineSignatureHelp signatureHelp(
final LspProjectContext context,
@@ -527,14 +568,16 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
final int endOffset = diagnostic.getEnd() == null ? startOffset : Math.max(startOffset, diagnostic.getEnd());
final DocumentPosition start = positionMapper.positionOf(startOffset);
final DocumentPosition end = positionMapper.positionOf(endOffset);
final String code = diagnostic.getCode() == null ? "" : diagnostic.getCode().trim();
return new BaselineDocumentIssue(
start.line(),
start.character(),
end.line(),
end.character(),
diagnostic.isError() ? BaselineIssueSeverity.ERROR : BaselineIssueSeverity.WARNING,
diagnostic.getCode() == null || diagnostic.getCode().isBlank() ? "Prometeu Studio" : diagnostic.getCode(),
diagnostic.getMessage());
code.isBlank() ? "Prometeu Studio" : code,
diagnostic.getMessage(),
code);
}
private Path normalizeDocumentPath(final String documentUri) {
@@ -715,6 +758,45 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
}
}
private List<BaselineCodeAction> mapCodeActions(
final List<FrontendCodeAction> repairs,
final String documentUri,
final Path documentPath,
final String documentText,
final List<BaselineCodeActionDiagnostic> diagnostics) {
if (repairs == null || repairs.isEmpty()) {
return List.of();
}
final DocumentPositionMapper mapper = new DocumentPositionMapper(documentText == null ? "" : documentText);
final ArrayList<BaselineCodeAction> mapped = new ArrayList<>();
for (final FrontendCodeAction repair : repairs) {
if (!repair.path().toAbsolutePath().normalize().equals(documentPath)) {
continue;
}
final DocumentPosition start = mapper.positionOf(repair.startOffset());
final DocumentPosition end = mapper.positionOf(repair.endOffset());
final boolean matched = diagnostics.stream().anyMatch(diagnostic ->
repair.diagnosticCode().equals(diagnostic.code())
&& diagnostic.startLine() == start.line()
&& diagnostic.startCharacter() == start.character()
&& diagnostic.endLine() == end.line()
&& diagnostic.endCharacter() == end.character());
if (!matched) {
continue;
}
mapped.add(new BaselineCodeAction(
repair.title(),
repair.diagnosticCode(),
documentUri,
start.line(),
start.character(),
end.line(),
end.character(),
repair.newText()));
}
return List.copyOf(mapped);
}
private List<BaselineRenameEdit> mapRenameEdits(
final List<FrontendRenameEdit> edits,
final Path currentDocumentPath,
@@ -8,6 +8,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.TextDocumentService;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.lsp.services.LanguageServiceBridge;
import p.studio.lsp.services.protocol.mapping.ProtocolMessageMapper;
@@ -167,6 +168,22 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
return CompletableFuture.completedFuture(protocolMessageMapper.mapRename(result));
}
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(final CodeActionParams params) {
if (!quickFixRequested(params.getContext())) {
return CompletableFuture.completedFuture(List.of());
}
final String uri = params.getTextDocument().getUri();
final String text = documentTextByUri.get(uri);
final List<Diagnostic> contextDiagnostics = params.getContext() == null
|| params.getContext().getDiagnostics() == null
? List.of()
: params.getContext().getDiagnostics();
return CompletableFuture.completedFuture(protocolMessageMapper.mapCodeActions(
languageServiceBridge.codeActions(project, uri, text, baselineDiagnostics(contextDiagnostics)),
contextDiagnostics));
}
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(
final DocumentSymbolParams params) {
@@ -201,6 +218,52 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
languageServiceBridge.semanticTokens(project, uri, text)));
}
private static boolean quickFixRequested(final CodeActionContext context) {
if (context == null || context.getOnly() == null || context.getOnly().isEmpty()) {
return true;
}
for (final String kind : context.getOnly()) {
if (kind == null || kind.isBlank()) {
return true;
}
if (CodeActionKind.QuickFix.equals(kind) || CodeActionKind.QuickFix.startsWith(kind + ".")) {
return true;
}
}
return false;
}
private static List<BaselineCodeActionDiagnostic> baselineDiagnostics(final List<Diagnostic> diagnostics) {
final ArrayList<BaselineCodeActionDiagnostic> mapped = new ArrayList<>();
for (final Diagnostic diagnostic : diagnostics) {
if (diagnostic == null || diagnostic.getRange() == null
|| diagnostic.getRange().getStart() == null
|| diagnostic.getRange().getEnd() == null) {
continue;
}
mapped.add(new BaselineCodeActionDiagnostic(
diagnosticCode(diagnostic),
diagnostic.getRange().getStart().getLine(),
diagnostic.getRange().getStart().getCharacter(),
diagnostic.getRange().getEnd().getLine(),
diagnostic.getRange().getEnd().getCharacter()));
}
return List.copyOf(mapped);
}
private static String diagnosticCode(final Diagnostic diagnostic) {
if (diagnostic.getCode() == null) {
return "";
}
if (diagnostic.getCode().isLeft()) {
return diagnostic.getCode().getLeft() == null ? "" : diagnostic.getCode().getLeft();
}
if (diagnostic.getCode().isRight() && diagnostic.getCode().getRight() != null) {
return diagnostic.getCode().getRight().toString();
}
return "";
}
private void publishAnalysis(
final String uri,
final String text) {
@@ -1,6 +1,7 @@
package p.studio.lsp.services.protocol.mapping;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
import p.studio.lsp.messages.*;
@@ -46,6 +47,12 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
renameOptions.setPrepareProvider(true);
capabilities.setRenameProvider(renameOptions);
}
if (description.codeActionsSupported()) {
final CodeActionOptions codeActionOptions = new CodeActionOptions();
codeActionOptions.setResolveProvider(false);
codeActionOptions.setCodeActionKinds(List.of(CodeActionKind.QuickFix));
capabilities.setCodeActionProvider(codeActionOptions);
}
final SemanticTokensWithRegistrationOptions semanticTokens = new SemanticTokensWithRegistrationOptions();
semanticTokens.setLegend(new SemanticTokensLegend(description.semanticTokenTypes(), List.of()));
semanticTokens.setFull(true);
@@ -152,6 +159,31 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
return edit;
}
@Override
public List<Either<Command, CodeAction>> mapCodeActions(
final BaselineCodeActions actions,
final List<Diagnostic> contextDiagnostics) {
if (actions == null || actions.actions().isEmpty()) {
return List.of();
}
final ArrayList<Either<Command, CodeAction>> mapped = new ArrayList<>();
for (final BaselineCodeAction action : actions.actions()) {
final Range range = new Range(
new Position(action.startLine(), action.startCharacter()),
new Position(action.endLine(), action.endCharacter()));
final TextEdit textEdit = new TextEdit(range, action.newText());
final WorkspaceEdit edit = new WorkspaceEdit();
edit.setChanges(Map.of(action.uri(), List.of(textEdit)));
final CodeAction codeAction = new CodeAction();
codeAction.setTitle(action.title());
codeAction.setKind(CodeActionKind.QuickFix);
codeAction.setEdit(edit);
codeAction.setDiagnostics(matchingDiagnostics(contextDiagnostics, action.diagnosticCode(), range));
mapped.add(Either.forRight(codeAction));
}
return List.copyOf(mapped);
}
@Override
public List<SymbolInformation> mapWorkspaceSymbols(final BaselineWorkspaceSymbols symbols) {
if (symbols == null || symbols.symbols().isEmpty()) {
@@ -323,6 +355,9 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
diagnostic.setSeverity(mapSeverity(issue.severity()));
diagnostic.setSource(issue.source());
diagnostic.setMessage(issue.message());
if (!issue.code().isBlank()) {
diagnostic.setCode(Either.forLeft(issue.code()));
}
return diagnostic;
}
@@ -360,6 +395,46 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
}
private List<Diagnostic> matchingDiagnostics(
final List<Diagnostic> contextDiagnostics,
final String diagnosticCode,
final Range range) {
if (contextDiagnostics == null || contextDiagnostics.isEmpty()) {
return List.of();
}
final ArrayList<Diagnostic> matched = new ArrayList<>();
for (final Diagnostic diagnostic : contextDiagnostics) {
if (diagnostic == null || diagnostic.getRange() == null) {
continue;
}
if (!diagnosticCode.equals(diagnosticCodeOf(diagnostic)) || !sameRange(diagnostic.getRange(), range)) {
continue;
}
matched.add(diagnostic);
}
return List.copyOf(matched);
}
private String diagnosticCodeOf(final Diagnostic diagnostic) {
if (diagnostic.getCode() == null) {
return "";
}
if (diagnostic.getCode().isLeft()) {
return diagnostic.getCode().getLeft() == null ? "" : diagnostic.getCode().getLeft().trim();
}
if (diagnostic.getCode().isRight() && diagnostic.getCode().getRight() != null) {
return diagnostic.getCode().getRight().toString();
}
return "";
}
private boolean sameRange(final Range left, final Range right) {
return left.getStart().getLine() == right.getStart().getLine()
&& left.getStart().getCharacter() == right.getStart().getCharacter()
&& left.getEnd().getLine() == right.getEnd().getLine()
&& left.getEnd().getCharacter() == right.getEnd().getCharacter();
}
private DiagnosticSeverity mapSeverity(final BaselineIssueSeverity severity) {
return switch (severity) {
case INFORMATION -> DiagnosticSeverity.Information;
@@ -1,22 +1,27 @@
package p.studio.lsp.services.protocol.mapping;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameResult;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineDocumentSymbols;
import p.studio.lsp.messages.BaselineWorkspaceSymbols;
import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselinePrepareRename;
import p.studio.lsp.messages.BaselineRename;
import p.studio.lsp.messages.BaselineCompletion;
@@ -52,6 +57,8 @@ public interface ProtocolMessageMapper {
WorkspaceEdit mapRename(BaselineRename rename);
List<Either<Command, CodeAction>> mapCodeActions(BaselineCodeActions actions, List<Diagnostic> contextDiagnostics);
CompletionList mapCompletion(BaselineCompletion completion);
SignatureHelp mapSignatureHelp(BaselineSignatureHelp signatureHelp);