diff --git a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/IBaseTextDocumentService.java b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/IBaseTextDocumentService.java index 866495e4..b394042f 100644 --- a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/IBaseTextDocumentService.java +++ b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/IBaseTextDocumentService.java @@ -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; @@ -45,4 +48,6 @@ public interface IBaseTextDocumentService extends TextDocumentService { CompletableFuture executeCommand(String languageName, String command); LineColumnOffsetMap getColumnMap(ISourceLocation file); String getContents(ISourceLocation file); + + default void didRenameFiles(RenameFilesParams params, Set workspaceFolders) {} } diff --git a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalTextDocumentService.java b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalTextDocumentService.java index a8997bce..1fa9de38 100644 --- a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalTextDocumentService.java +++ b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalTextDocumentService.java @@ -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; @@ -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; @@ -389,6 +393,24 @@ public CompletableFuture> foldingRange(FoldingRangeRequestPar ); } + @Override + public void didRenameFiles(RenameFilesParams params, Set 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 last(List l) { diff --git a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalWorkspaceService.java b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalWorkspaceService.java index ca8caa23..3de2550e 100644 --- a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalWorkspaceService.java +++ b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalWorkspaceService.java @@ -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; @@ -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) { @@ -86,8 +78,6 @@ public void initialize(ClientCapabilities clientCap, @Nullable List 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); } }