Skip to content

Commit

Permalink
Move rename files callback to prevent multiple evaluators.
Browse files Browse the repository at this point in the history
This works around a more generic solution of sharing evaluators beteen
the document and workspace services, whic is being tracked in #541.
  • Loading branch information
toinehartman committed Dec 9, 2024
1 parent dd91729 commit a62056b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
*/
package org.rascalmpl.vscode.lsp;

import java.util.Set;
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.RenameFilesParams;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.TextDocumentService;
Expand All @@ -45,4 +48,6 @@ public interface IBaseTextDocumentService extends TextDocumentService {
CompletableFuture<IValue> executeCommand(String languageName, String command);
LineColumnOffsetMap getColumnMap(ISourceLocation file);
String getContents(ISourceLocation file);

default void didRenameFiles(RenameFilesParams params, Set<ISourceLocation> workspaceFolders) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.CodeLens;
Expand All @@ -67,11 +68,14 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.RenameFilesParams;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SemanticTokens;
Expand Down Expand Up @@ -389,6 +393,24 @@ public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestPar
);
}

@Override
public void didRenameFiles(RenameFilesParams params, Set<ISourceLocation> workspaceFolders) {
logger.debug("workspace/willRenameFiles: {}", params.getFiles());

rascalServices.getModuleRenames(params.getFiles(), workspaceFolders, facts::getPathConfig).get()
.thenApply(edits -> DocumentChanges.translateDocumentChanges(this, edits))
.thenCompose(docChanges -> client.applyEdit(new ApplyWorkspaceEditParams(docChanges)))
.thenAccept(editResponse -> {
if (!editResponse.isApplied()) {
throw new RuntimeException("Applying module rename failed: " + editResponse.getFailureReason());
}
})
.exceptionally(e -> {
client.showMessage(new MessageParams(MessageType.Error, e.getMessage()));
return null;
});
}

// Private utility methods

private static <T> T last(List<T> l) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,17 @@
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.FileOperationFilter;
import org.eclipse.lsp4j.FileOperationOptions;
import org.eclipse.lsp4j.FileOperationPattern;
import org.eclipse.lsp4j.FileOperationsServerCapabilities;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.RenameFilesParams;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.services.LanguageClient;
import org.rascalmpl.vscode.lsp.BaseWorkspaceService;
import org.rascalmpl.vscode.lsp.IBaseLanguageClient;
import org.rascalmpl.vscode.lsp.IBaseTextDocumentService;
import org.rascalmpl.vscode.lsp.rascal.model.FileFacts;
import org.rascalmpl.vscode.lsp.util.DocumentChanges;
import org.rascalmpl.vscode.lsp.util.locations.Locations;

import io.usethesource.vallang.ISourceLocation;
Expand All @@ -60,8 +54,6 @@ public class RascalWorkspaceService extends BaseWorkspaceService {
private static final Logger logger = LogManager.getLogger(RascalWorkspaceService.class);

private final IBaseTextDocumentService docService;
private @MonotonicNonNull RascalLanguageServices rascalServices;
private @MonotonicNonNull FileFacts facts;
private @MonotonicNonNull LanguageClient client;

RascalWorkspaceService(ExecutorService exec, IBaseTextDocumentService documentService) {
Expand All @@ -86,8 +78,6 @@ public void initialize(ClientCapabilities clientCap, @Nullable List<WorkspaceFol
public void connect(LanguageClient client) {
super.connect(client);
this.client = client;
this.rascalServices = new RascalLanguageServices((RascalTextDocumentService) docService, this, (IBaseLanguageClient) client, getExecuter());
this.facts = ((RascalTextDocumentService) docService).getFileFacts();
}

@Override
Expand All @@ -99,17 +89,6 @@ public void didRenameFiles(RenameFilesParams params) {
.map(f -> Locations.toLoc(f.getUri()))
.collect(Collectors.toSet());

rascalServices.getModuleRenames(params.getFiles(), workspaceFolders, facts::getPathConfig).get()
.thenApply(edits -> DocumentChanges.translateDocumentChanges(docService, edits))
.thenCompose(docChanges -> client.applyEdit(new ApplyWorkspaceEditParams(docChanges)))
.thenAccept(editResponse -> {
if (!editResponse.isApplied()) {
throw new RuntimeException("Applying module rename failed: " + editResponse.getFailureReason());
}
})
.exceptionally(e -> {
client.showMessage(new MessageParams(MessageType.Error, e.getMessage()));
return null;
});
((RascalTextDocumentService) docService).didRenameFiles(params, workspaceFolders);
}
}

0 comments on commit a62056b

Please sign in to comment.