Skip to content

Commit ca975ae

Browse files
committed
refact: simplify HighlightReconcilingStrategy#collectHighlights
1 parent 969d796 commit ca975ae

1 file changed

Lines changed: 8 additions & 22 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/highlight/HighlightReconcilingStrategy.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import java.net.URI;
1919
import java.time.Duration;
20-
import java.util.ArrayList;
2120
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map.Entry;
23+
import java.util.Objects;
2424
import java.util.concurrent.CompletableFuture;
2525

2626
import org.eclipse.core.runtime.ICoreRunnable;
@@ -84,7 +84,7 @@ public class HighlightReconcilingStrategy
8484
private static final int HIGHLIGHT_DEBOUNCE_MS = 75;
8585

8686
// Short-lived cache for highlights per document+normalized offset.
87-
private static final DocumentOffsetAsyncCache<List<DocumentHighlight>> HIGHLIGHT_CACHE =
87+
private static final DocumentOffsetAsyncCache<List<? extends DocumentHighlight>> HIGHLIGHT_CACHE =
8888
new DocumentOffsetAsyncCache<>(Duration.ofSeconds(10));
8989

9090
// Track the last normalized cache key to avoid canceling identical in-flight work.
@@ -229,28 +229,14 @@ private void collectHighlights(int caretOffset, @Nullable IProgressMonitor monit
229229
final var params = new DocumentHighlightParams(identifier, position);
230230

231231
// Use cache to deduplicate requests to the same symbol for a short period.
232-
final CompletableFuture<List<DocumentHighlight>> request = HIGHLIGHT_CACHE.computeIfAbsent(document,
232+
final CompletableFuture<List<? extends DocumentHighlight>> request = HIGHLIGHT_CACHE.computeIfAbsent(document,
233233
cacheKeyOffset, () -> {
234-
final var reqs = requests = LanguageServers.forDocument(document)
235-
.withCapability(ServerCapabilities::getDocumentHighlightProvider)
236-
.computeAll(ls -> ls.getTextDocumentService().documentHighlight(params));
237-
final CompletableFuture<?> all = CompletableFuture
238-
.allOf(reqs.stream().map(f -> (CompletableFuture<?>) f).toArray(CompletableFuture[]::new));
239-
return all.thenApply(unused -> {
240-
final var allHighlights = new ArrayList<DocumentHighlight>();
241-
for (final var req : reqs) {
242-
try {
243-
final List<? extends DocumentHighlight> highlight = req.join();
244-
if (highlight != null) {
245-
allHighlights.addAll(highlight);
246-
}
247-
} catch (Exception ex) {
248-
// ignore single-server failures
249-
}
250-
}
251-
return allHighlights;
234+
final var reqs = requests = LanguageServers.forDocument(document)
235+
.withCapability(ServerCapabilities::getDocumentHighlightProvider)
236+
.computeAll(ls -> ls.getTextDocumentService().documentHighlight(params));
237+
return CompletableFuture.supplyAsync(() -> reqs.stream().map(CompletableFuture::join) //
238+
.filter(Objects::nonNull).flatMap(List::stream).toList());
252239
});
253-
});
254240

255241
request.thenAcceptAsync(highlights -> {
256242
if (monitor == null || !monitor.isCanceled()) {

0 commit comments

Comments
 (0)