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
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:
+34
@@ -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;
|
||||
}
|
||||
}
|
||||
+16
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -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());
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -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(
|
||||
|
||||
+1
@@ -15,6 +15,7 @@ public record BaselineServerDescription(
|
||||
boolean documentSymbolsSupported,
|
||||
boolean workspaceSymbolsSupported,
|
||||
boolean renameSupported,
|
||||
boolean codeActionsSupported,
|
||||
List<String> semanticTokenTypes,
|
||||
List<BaselineSemanticHostProjection> semanticHostProjections,
|
||||
List<BaselineVisualTheme> visualThemes,
|
||||
|
||||
+10
@@ -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);
|
||||
}
|
||||
|
||||
+85
-3
@@ -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,
|
||||
|
||||
+63
@@ -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) {
|
||||
|
||||
+75
@@ -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;
|
||||
|
||||
+7
@@ -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);
|
||||
|
||||
+98
@@ -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()
|
||||
|
||||
+17
@@ -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();
|
||||
|
||||
+16
@@ -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;
|
||||
|
||||
+16
@@ -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();
|
||||
|
||||
+48
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user