implements PLN-0135
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 63.56% (19159/30145)
* Branch Coverage: 54.14% (7612/14060)
* Lines of Code: 30145
* Cyclomatic Complexity: 12214
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 678
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.56% (19159/30145)
* Branch Coverage: 54.14% (7612/14060)
* Lines of Code: 30145
* Cyclomatic Complexity: 12214
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 678
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Add PBS rename as a validated WorkspaceEdit of physical identifier sites on FrontendLanguageService. Identity is definition; locals and params rename from their name span. Refuse aliases, virtual stdlib, builtins, error labels, keywords, and collisions. Advertise renameProvider with prepareProvider; refused cursors are LSP errors, not empty success. Housekeep DSC-0043 with LSN-0072.
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package p.studio.lsp.messages;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public record BaselinePrepareRename(Optional<BaselineDefinitionLocation> placeholder) {
|
||||
|
||||
public BaselinePrepareRename {
|
||||
placeholder = placeholder == null ? Optional.empty() : placeholder;
|
||||
}
|
||||
|
||||
public static BaselinePrepareRename none() {
|
||||
return new BaselinePrepareRename(Optional.empty());
|
||||
}
|
||||
|
||||
public static BaselinePrepareRename of(final BaselineDefinitionLocation location) {
|
||||
return new BaselinePrepareRename(Optional.of(location));
|
||||
}
|
||||
|
||||
public boolean renameable() {
|
||||
return placeholder.isPresent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package p.studio.lsp.messages;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record BaselineRename(
|
||||
boolean applied,
|
||||
String message,
|
||||
List<BaselineRenameEdit> edits) {
|
||||
|
||||
public BaselineRename {
|
||||
message = message == null ? "" : message;
|
||||
edits = edits == null ? List.of() : List.copyOf(edits);
|
||||
}
|
||||
|
||||
public static BaselineRename unsupported() {
|
||||
return new BaselineRename(false, "unsupported", List.of());
|
||||
}
|
||||
|
||||
public static BaselineRename refused(final String message) {
|
||||
return new BaselineRename(false, message == null || message.isBlank() ? "not renameable" : message, List.of());
|
||||
}
|
||||
|
||||
public static BaselineRename applied(final List<BaselineRenameEdit> edits) {
|
||||
return new BaselineRename(true, "", edits);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package p.studio.lsp.messages;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record BaselineRenameEdit(
|
||||
String uri,
|
||||
int startLine,
|
||||
int startCharacter,
|
||||
int endLine,
|
||||
int endCharacter,
|
||||
String newText) {
|
||||
|
||||
public BaselineRenameEdit {
|
||||
uri = requireText(uri, "uri");
|
||||
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
|
||||
throw new IllegalArgumentException("rename 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;
|
||||
}
|
||||
}
|
||||
+1
@@ -14,6 +14,7 @@ public record BaselineServerDescription(
|
||||
boolean referencesSupported,
|
||||
boolean documentSymbolsSupported,
|
||||
boolean workspaceSymbolsSupported,
|
||||
boolean renameSupported,
|
||||
List<String> semanticTokenTypes,
|
||||
List<BaselineSemanticHostProjection> semanticHostProjections,
|
||||
List<BaselineVisualTheme> visualThemes,
|
||||
|
||||
+17
@@ -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.BaselinePrepareRename;
|
||||
import p.studio.lsp.messages.BaselineRename;
|
||||
import p.studio.lsp.messages.BaselineSemanticTokens;
|
||||
import p.studio.lsp.messages.BaselineSignatureHelp;
|
||||
import p.studio.lsp.messages.BaselineServerDescription;
|
||||
@@ -35,6 +37,21 @@ public interface LanguageServiceBridge {
|
||||
|
||||
BaselineWorkspaceSymbols workspaceSymbols(LspProjectContext context, String query);
|
||||
|
||||
BaselinePrepareRename prepareRename(
|
||||
LspProjectContext context,
|
||||
String documentUri,
|
||||
String text,
|
||||
int line,
|
||||
int character);
|
||||
|
||||
BaselineRename rename(
|
||||
LspProjectContext context,
|
||||
String documentUri,
|
||||
String text,
|
||||
int line,
|
||||
int character,
|
||||
String newName);
|
||||
|
||||
BaselineSignatureHelp signatureHelp(LspProjectContext context, String documentUri, String text, int line, int character);
|
||||
|
||||
BaselineSemanticTokens semanticTokens(LspProjectContext context, String documentUri, String text);
|
||||
|
||||
+109
@@ -14,6 +14,8 @@ import p.studio.compiler.services.FrontendProvider;
|
||||
import p.studio.compiler.services.FrontendSignature;
|
||||
import p.studio.compiler.services.FrontendSignatureHelp;
|
||||
import p.studio.compiler.services.FrontendSymbolKind;
|
||||
import p.studio.compiler.services.FrontendRenameEdit;
|
||||
import p.studio.compiler.services.FrontendRenameResult;
|
||||
import p.studio.compiler.services.FrontendWorkspaceRequest;
|
||||
import p.studio.compiler.services.FrontendWorkspaceSymbol;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
@@ -60,6 +62,7 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
languageServicePresent,
|
||||
languageServicePresent,
|
||||
languageServicePresent,
|
||||
languageServicePresent,
|
||||
presentation.semanticKeys(),
|
||||
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
|
||||
presentation.themes().stream().map(this::mapVisualTheme).toList(),
|
||||
@@ -272,6 +275,85 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
.orElseGet(BaselineWorkspaceSymbols::empty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselinePrepareRename prepareRename(
|
||||
final LspProjectContext context,
|
||||
final String documentUri,
|
||||
final String text,
|
||||
final int line,
|
||||
final int character) {
|
||||
Objects.requireNonNull(context, "context");
|
||||
final var provider = frontendProvider(context);
|
||||
final var languageService = provider.languageService();
|
||||
if (languageService.isEmpty()) {
|
||||
return BaselinePrepareRename.none();
|
||||
}
|
||||
return editorialDocument(context, documentUri, text)
|
||||
.flatMap(document -> {
|
||||
final int offset = new DocumentPositionMapper(document.text()).byteOffsetOf(line, character);
|
||||
final FrontendDocumentRequest request = new FrontendDocumentRequest(
|
||||
context.projectRoot(),
|
||||
normalizeDocumentPath(documentUri),
|
||||
document.text());
|
||||
return languageService.orElseThrow().prepareRename(
|
||||
request,
|
||||
document.editorialContext(),
|
||||
offset);
|
||||
})
|
||||
.map(location -> {
|
||||
final Path documentPath = normalizeDocumentPath(documentUri);
|
||||
final List<BaselineDefinitionLocation> mapped = mapDefinitionLocations(
|
||||
List.of(location),
|
||||
documentPath,
|
||||
text == null ? "" : text);
|
||||
if (mapped.isEmpty()) {
|
||||
return BaselinePrepareRename.none();
|
||||
}
|
||||
return BaselinePrepareRename.of(mapped.getFirst());
|
||||
})
|
||||
.orElseGet(BaselinePrepareRename::none);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineRename rename(
|
||||
final LspProjectContext context,
|
||||
final String documentUri,
|
||||
final String text,
|
||||
final int line,
|
||||
final int character,
|
||||
final String newName) {
|
||||
Objects.requireNonNull(context, "context");
|
||||
final var provider = frontendProvider(context);
|
||||
final var languageService = provider.languageService();
|
||||
if (languageService.isEmpty()) {
|
||||
return BaselineRename.unsupported();
|
||||
}
|
||||
return editorialDocument(context, documentUri, text)
|
||||
.map(document -> {
|
||||
final int offset = new DocumentPositionMapper(document.text()).byteOffsetOf(line, character);
|
||||
final FrontendDocumentRequest request = new FrontendDocumentRequest(
|
||||
context.projectRoot(),
|
||||
normalizeDocumentPath(documentUri),
|
||||
document.text());
|
||||
final FrontendRenameResult result = languageService.orElseThrow().rename(
|
||||
request,
|
||||
document.editorialContext(),
|
||||
offset,
|
||||
newName);
|
||||
if (!result.applied()) {
|
||||
if (result.status() == FrontendRenameResult.Status.UNSUPPORTED) {
|
||||
return BaselineRename.unsupported();
|
||||
}
|
||||
return BaselineRename.refused(result.message());
|
||||
}
|
||||
return BaselineRename.applied(mapRenameEdits(
|
||||
result.edits(),
|
||||
request.documentPath(),
|
||||
document.text()));
|
||||
})
|
||||
.orElseGet(() -> BaselineRename.refused("not renameable"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaselineSignatureHelp signatureHelp(
|
||||
final LspProjectContext context,
|
||||
@@ -633,6 +715,33 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
|
||||
}
|
||||
}
|
||||
|
||||
private List<BaselineRenameEdit> mapRenameEdits(
|
||||
final List<FrontendRenameEdit> edits,
|
||||
final Path currentDocumentPath,
|
||||
final String currentDocumentText) {
|
||||
if (edits == null || edits.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
final ArrayList<BaselineRenameEdit> mapped = new ArrayList<>();
|
||||
for (final FrontendRenameEdit edit : edits) {
|
||||
final String targetText = textForDefinitionTarget(edit.path(), currentDocumentPath, currentDocumentText);
|
||||
if (targetText == null) {
|
||||
continue;
|
||||
}
|
||||
final DocumentPositionMapper mapper = new DocumentPositionMapper(targetText);
|
||||
final DocumentPosition start = mapper.positionOf(edit.startOffset());
|
||||
final DocumentPosition end = mapper.positionOf(edit.endOffset());
|
||||
mapped.add(new BaselineRenameEdit(
|
||||
edit.path().toUri().toString(),
|
||||
start.line(),
|
||||
start.character(),
|
||||
end.line(),
|
||||
end.character(),
|
||||
edit.newText()));
|
||||
}
|
||||
return List.copyOf(mapped);
|
||||
}
|
||||
|
||||
private List<BaselineDefinitionLocation> mapDefinitionLocations(
|
||||
final List<FrontendDefinitionLocation> locations,
|
||||
final Path currentDocumentPath,
|
||||
|
||||
+39
@@ -1,7 +1,11 @@
|
||||
package p.studio.lsp.services.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
|
||||
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.LspProjectContext;
|
||||
@@ -128,6 +132,41 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
|
||||
includeDeclaration)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior>> prepareRename(
|
||||
final PrepareRenameParams params) {
|
||||
final String uri = params.getTextDocument().getUri();
|
||||
final String text = documentTextByUri.get(uri);
|
||||
return CompletableFuture.completedFuture(protocolMessageMapper.mapPrepareRename(
|
||||
languageServiceBridge.prepareRename(
|
||||
project,
|
||||
uri,
|
||||
text,
|
||||
params.getPosition().getLine(),
|
||||
params.getPosition().getCharacter())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<WorkspaceEdit> rename(final RenameParams params) {
|
||||
final String uri = params.getTextDocument().getUri();
|
||||
final String text = documentTextByUri.get(uri);
|
||||
final var result = languageServiceBridge.rename(
|
||||
project,
|
||||
uri,
|
||||
text,
|
||||
params.getPosition().getLine(),
|
||||
params.getPosition().getCharacter(),
|
||||
params.getNewName());
|
||||
if (!result.applied()) {
|
||||
final ResponseError error = new ResponseError(
|
||||
ResponseErrorCode.InvalidRequest,
|
||||
result.message() == null || result.message().isBlank() ? "not renameable" : result.message(),
|
||||
null);
|
||||
return CompletableFuture.failedFuture(new ResponseErrorException(error));
|
||||
}
|
||||
return CompletableFuture.completedFuture(protocolMessageMapper.mapRename(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(
|
||||
final DocumentSymbolParams params) {
|
||||
|
||||
+37
@@ -1,6 +1,7 @@
|
||||
package p.studio.lsp.services.protocol.mapping;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
|
||||
import p.studio.lsp.messages.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -40,6 +41,11 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
|
||||
capabilities.setReferencesProvider(description.referencesSupported());
|
||||
capabilities.setDocumentSymbolProvider(description.documentSymbolsSupported());
|
||||
capabilities.setWorkspaceSymbolProvider(description.workspaceSymbolsSupported());
|
||||
if (description.renameSupported()) {
|
||||
final RenameOptions renameOptions = new RenameOptions();
|
||||
renameOptions.setPrepareProvider(true);
|
||||
capabilities.setRenameProvider(renameOptions);
|
||||
}
|
||||
final SemanticTokensWithRegistrationOptions semanticTokens = new SemanticTokensWithRegistrationOptions();
|
||||
semanticTokens.setLegend(new SemanticTokensLegend(description.semanticTokenTypes(), List.of()));
|
||||
semanticTokens.setFull(true);
|
||||
@@ -115,6 +121,37 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
|
||||
return List.copyOf(mapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior> mapPrepareRename(
|
||||
final BaselinePrepareRename prepareRename) {
|
||||
if (prepareRename == null || prepareRename.placeholder().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
final BaselineDefinitionLocation location = prepareRename.placeholder().orElseThrow();
|
||||
return Either3.forFirst(new Range(
|
||||
new Position(location.startLine(), location.startCharacter()),
|
||||
new Position(location.endLine(), location.endCharacter())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkspaceEdit mapRename(final BaselineRename rename) {
|
||||
final WorkspaceEdit edit = new WorkspaceEdit();
|
||||
if (rename == null || !rename.applied() || rename.edits().isEmpty()) {
|
||||
edit.setChanges(Map.of());
|
||||
return edit;
|
||||
}
|
||||
final LinkedHashMap<String, List<TextEdit>> changes = new LinkedHashMap<>();
|
||||
for (final BaselineRenameEdit renameEdit : rename.edits()) {
|
||||
changes.computeIfAbsent(renameEdit.uri(), ignored -> new ArrayList<>()).add(new TextEdit(
|
||||
new Range(
|
||||
new Position(renameEdit.startLine(), renameEdit.startCharacter()),
|
||||
new Position(renameEdit.endLine(), renameEdit.endCharacter())),
|
||||
renameEdit.newText()));
|
||||
}
|
||||
edit.setChanges(changes);
|
||||
return edit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SymbolInformation> mapWorkspaceSymbols(final BaselineWorkspaceSymbols symbols) {
|
||||
if (symbols == null || symbols.symbols().isEmpty()) {
|
||||
|
||||
+12
@@ -8,10 +8,17 @@ import org.eclipse.lsp4j.SignatureHelp;
|
||||
import org.eclipse.lsp4j.SemanticTokens;
|
||||
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.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.BaselinePrepareRename;
|
||||
import p.studio.lsp.messages.BaselineRename;
|
||||
import p.studio.lsp.messages.BaselineCompletion;
|
||||
import p.studio.lsp.messages.BaselineDefinition;
|
||||
import p.studio.lsp.messages.BaselineReferences;
|
||||
@@ -40,6 +47,11 @@ public interface ProtocolMessageMapper {
|
||||
|
||||
List<SymbolInformation> mapWorkspaceSymbols(BaselineWorkspaceSymbols symbols);
|
||||
|
||||
Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior> mapPrepareRename(
|
||||
BaselinePrepareRename prepareRename);
|
||||
|
||||
WorkspaceEdit mapRename(BaselineRename rename);
|
||||
|
||||
CompletionList mapCompletion(BaselineCompletion completion);
|
||||
|
||||
SignatureHelp mapSignatureHelp(BaselineSignatureHelp signatureHelp);
|
||||
|
||||
Reference in New Issue
Block a user