Skip to content

Commit

Permalink
Use didRenameFiles callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
toinehartman committed Dec 5, 2024
1 parent 9dc8e7d commit 292acd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,30 +303,14 @@ public InterruptibleFuture<ITuple> getModuleRenames(List<FileRename> fileRenames
if (fileRenames.isEmpty()) return InterruptibleFuture.completedFuture(null);

Check failure on line 303 in rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalLanguageServices.java

View workflow job for this annotation

GitHub Actions / checkstyle

com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck

'if' construct must use '{}'s.

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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@

import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
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;
Expand All @@ -60,44 +61,53 @@ 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
public void initialize(ClientCapabilities clientCap, @Nullable List<WorkspaceFolder> currentWorkspaceFolders,
ServerCapabilities capabilities) {
super.initialize(clientCap, currentWorkspaceFolders, capabilities);

capabilities.getWorkspace().getFileOperations().setWillRename(new FileOperationOptions(List.of(new FileOperationFilter(new FileOperationPattern("**/*.rsc")))));
capabilities.getWorkspace().getFileOperations().setDidRename(new FileOperationOptions(List.of(new FileOperationFilter(new FileOperationPattern("**/*.rsc")))));
}

@Override
public void connect(LanguageClient client) {
super.connect(client);
this.client = client;
this.rascalServices = new RascalLanguageServices((RascalTextDocumentService) docService, this, (IBaseLanguageClient) client, ownExecuter);
this.facts = ((RascalTextDocumentService) docService).getFileFacts();
}

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

Set<ISourceLocation> workspaceFolders = workspaceFolders()
.stream()
.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()));
}
}
}

0 comments on commit 292acd6

Please sign in to comment.