Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel delegate commands for workspace symbols #353

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
}
}

}, null);
}, monitor);
} catch (CoreException | ClassCastException e) {
LOGGER.log(Level.SEVERE,
"While collecting symbols for project " + project.getResource().getLocationURI().toString(), e);
}
if (monitor.isCanceled()) {
return;
}
annotatables
.forEach(annotatable -> collectSymbolFromAnnotatable(symbols, annotatable, applicationPrefix, utils));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -28,6 +33,7 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.WorkspaceService;
import org.eclipse.lsp4mp.ls.java.JavaTextDocuments;
import org.eclipse.lsp4mp.utils.FutureUtils;

/**
* MicroProfile workspace service.
Expand All @@ -37,6 +43,8 @@ public class MicroProfileWorkspaceService implements WorkspaceService {

private static final Logger LOGGER = Logger.getLogger(MicroProfileWorkspaceService.class.getName());

private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

private final MicroProfileLanguageServer microprofileLanguageServer;
private final JavaTextDocuments javaTextDocuments;

Expand All @@ -58,33 +66,54 @@ public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
@Override
public CompletableFuture<Either<List<? extends SymbolInformation>, List<? extends WorkspaceSymbol>>> symbol(
WorkspaceSymbolParams params) {
return javaTextDocuments.getWorkspaceProjects() //
.thenCompose(workspaceProjects -> {

List<CompletableFuture<List<SymbolInformation>>> symbolFutures = workspaceProjects.stream() //
.map(projectLabelInfo -> {
String uri = projectLabelInfo.getUri();
return microprofileLanguageServer.getLanguageClient().getJavaWorkspaceSymbols(uri);
}) //
.collect(Collectors.toList());

// NOTE: we don't need to implement resolve, because resolve is just
// for calculating the source range. The source range is very cheap to calculate
// in comparison to invoking the search engine to locate the symbols.

return CompletableFuture
.allOf((CompletableFuture[]) symbolFutures.stream().toArray(CompletableFuture[]::new))
.exceptionally(e -> {
LOGGER.log(Level.SEVERE, "Failure while collecting symbols", e);
return null;
}).thenApply(_void -> {
return Either.forLeft(symbolFutures.stream() //
.flatMap(projectSymbolsFuture -> {
return projectSymbolsFuture.getNow(Collections.emptyList()).stream();
}) //
.collect(Collectors.toList()));
});
});
return FutureUtils.computeAsyncCompose(cancelChecker -> {

return javaTextDocuments.getWorkspaceProjects() //
.thenCompose((workspaceProjects) -> {

List<CompletableFuture<List<SymbolInformation>>> symbolFutures = workspaceProjects.stream() //
.map(projectLabelInfo -> {
String uri = projectLabelInfo.getUri();
return microprofileLanguageServer.getLanguageClient().getJavaWorkspaceSymbols(uri);
}) //
.collect(Collectors.toList());

// Set up a background task that cancels the java language server requests
// if the client indicates the server request was cancelled
Future<?> cancellationPolling = scheduler.scheduleWithFixedDelay(() -> {
if (cancelChecker.isCanceled()) {
symbolFutures.stream().forEach(symbolFuture -> symbolFuture.cancel(false));
}
}, 0, 10, TimeUnit.MILLISECONDS);

// NOTE: we don't need to implement resolve, because resolve is just
// for calculating the source range. The source range is very cheap to calculate
// in comparison to invoking the search engine to locate the symbols.

return CompletableFuture
.allOf((CompletableFuture[]) symbolFutures.stream().toArray(CompletableFuture[]::new)) //
.exceptionally(e -> { //
if (!(e instanceof CancellationException)
&& !(e.getCause() instanceof CancellationException)) {
LOGGER.log(Level.SEVERE, "Failure while collecting symbols", e);
}
return null;
}) //
.thenApply(_void -> {
// remove the background task to cancel the delegate commands, since they should
// all be completed or cancelled by now
cancellationPolling.cancel(false);
cancelChecker.checkCanceled();

return Either.forLeft(symbolFutures.stream() //
.flatMap(projectSymbolsFuture -> {
return projectSymbolsFuture.getNow(Collections.emptyList()).stream();
}) //
.collect(Collectors.toList()));
});
});
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4mp.utils;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;

/**
* Utilities for working with <code>CompletableFuture</code>s.
*/
public class FutureUtils {

/**
* It's a copy of
* {@link org.eclipse.lsp4j.jsonrpc.CompletableFutures#computeAsync} that
* accepts a function that returns a CompletableFuture.
*
* @see CompletableFutures#computeAsync
*
* @param <R> the return type of the asynchronous computation
* @param code the code to run asynchronously
* @return a future that sends the correct $/cancelRequest notification when
* canceled
*/
public static <R> CompletableFuture<R> computeAsyncCompose(Function<CancelChecker, CompletableFuture<R>> code) {
CompletableFuture<CancelChecker> start = new CompletableFuture<>();
CompletableFuture<R> result = start.thenComposeAsync(code);
start.complete(new CompletableFutures.FutureCancelChecker(result));
return result;
}

}