diff --git a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalLanguageServices.java b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalLanguageServices.java index 543631e3..3b92760f 100644 --- a/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalLanguageServices.java +++ b/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalLanguageServices.java @@ -303,30 +303,14 @@ public InterruptibleFuture getModuleRenames(List fileRenames if (fileRenames.isEmpty()) return InterruptibleFuture.completedFuture(null); final ISet qualifiedNameChanges = qualfiedNameChangesFromRenames(fileRenames, workspaceFolders, getPathConfig); - - try { - return runEvaluator("Rascal module rename", semanticEvaluator, eval -> { - IFunction rascalGetPathConfig = eval.getFunctionValueFactory().function(getPathConfigType, (t, u) -> addResources(getPathConfig.apply((ISourceLocation) t[0]))); + return runEvaluator("Rascal module rename", semanticEvaluator, eval -> { + IFunction rascalGetPathConfig = eval.getFunctionValueFactory().function(getPathConfigType, (t, u) -> addResources(getPathConfig.apply((ISourceLocation) t[0]))); + try { return (ITuple) eval.call("rascalRenameModule", qualifiedNameChanges, VF.set(workspaceFolders.toArray(ISourceLocation[]::new)), rascalGetPathConfig); - }, VF.tuple(VF.list(), VF.map()), exec, false, client); - } catch (Throw e) { - if (e.getException() instanceof IConstructor) { - var exception = (IConstructor)e.getException(); - if (exception.getType().getAbstractDataType().getName().equals("RenameException")) { - // instead of the generic exception handler, we deal with these ourselfs - // and report an LSP error, such that the IDE shows them in a user friendly way - String message; - if (exception.has("message")) { - message = ((IString)exception.get("message")).getValue(); - } - else { - message = "Rename failed: " + exception.getName(); - } - throw new ResponseErrorException(new ResponseError(ResponseErrorCode.RequestFailed, message, null)); - } + } catch (Throw e) { + throw new RuntimeException(e.getMessage()); } - throw e; - } + }, VF.tuple(VF.list(), VF.map()), exec, false, client); } 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 371c4b24..740ef464 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 @@ -28,7 +28,6 @@ import java.util.List; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -36,13 +35,15 @@ 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.MessageParams; +import org.eclipse.lsp4j.MessageType; import org.eclipse.lsp4j.RenameFilesParams; import org.eclipse.lsp4j.ServerCapabilities; -import org.eclipse.lsp4j.WorkspaceEdit; import org.eclipse.lsp4j.WorkspaceFolder; import org.eclipse.lsp4j.services.LanguageClient; import org.rascalmpl.vscode.lsp.BaseWorkspaceService; @@ -60,16 +61,15 @@ public class RascalWorkspaceService extends BaseWorkspaceService { private final IBaseTextDocumentService docService; private final ExecutorService ownExecuter; - private final ColumnMaps columns; - private @MonotonicNonNull RascalLanguageServices rascalServices; private @MonotonicNonNull FileFacts facts; + private @MonotonicNonNull LanguageClient client; RascalWorkspaceService(ExecutorService exec, IBaseTextDocumentService documentService) { super(documentService); this.ownExecuter = exec; this.docService = documentService; - this.columns = new ColumnMaps(docService::getContents); + new ColumnMaps(docService::getContents); } @Override @@ -77,18 +77,19 @@ public void initialize(ClientCapabilities clientCap, @Nullable List willRenameFiles(RenameFilesParams params) { + public void didRenameFiles(RenameFilesParams params) { logger.debug("workspace/willRenameFiles: {}", params.getFiles()); Set workspaceFolders = workspaceFolders() @@ -96,8 +97,17 @@ public CompletableFuture willRenameFiles(RenameFilesParams params .map(f -> Locations.toLoc(f.getUri())) .collect(Collectors.toSet()); - return rascalServices.getModuleRenames(params.getFiles(), workspaceFolders, facts::getPathConfig) - .thenApply(t -> DocumentChanges.translateDocumentChanges(docService, t)) - .get(); + try { + 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()); + } + }).join(); + } catch (RuntimeException e) { + client.showMessage(new MessageParams(MessageType.Error, e.getMessage())); + } } }