-
Notifications
You must be signed in to change notification settings - Fork 67
feat: auto cancel superseded codemining requests per document #1410
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...pse.lsp4e.test/src/org/eclipse/lsp4e/test/internal/AbstractLSPCodeMiningProviderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Vegard IT GmbH and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Sebastian Thomschke (Vegard IT GmbH) - initial implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.lsp4e.test.internal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.runtime.NullProgressMonitor; | ||
| import org.eclipse.jdt.annotation.Nullable; | ||
| import org.eclipse.jface.text.IDocument; | ||
| import org.eclipse.jface.text.ITextViewer; | ||
| import org.eclipse.jface.text.codemining.ICodeMining; | ||
| import org.eclipse.lsp4e.internal.AbstractLSPCodeMiningProvider; | ||
| import org.eclipse.lsp4e.test.utils.AbstractTestWithProject; | ||
| import org.eclipse.lsp4e.test.utils.TestUtils; | ||
| import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Verifies that {@link AbstractLSPCodeMiningProvider} cancels the previous | ||
| * in-flight request for the same document when a new one starts. | ||
| */ | ||
| class AbstractLSPCodeMiningProviderTest extends AbstractTestWithProject { | ||
|
|
||
| private static final class StubProvider extends AbstractLSPCodeMiningProvider { | ||
| @Override | ||
| protected @Nullable CompletableFuture<List<? extends ICodeMining>> doProvideCodeMinings(IDocument doc, | ||
| TextDocumentIdentifier docId) { | ||
| return new CompletableFuture<>(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void cancelsPreviousRequestForSameDocument() throws Exception { | ||
| IFile file = TestUtils.createUniqueTestFile(project, "txt", "content"); | ||
| ITextViewer viewer = TestUtils.openTextViewer(file); | ||
|
|
||
| var provider = new StubProvider(); | ||
|
|
||
| var first = provider.provideCodeMinings(viewer, new NullProgressMonitor()); | ||
| assertNotNull(first); | ||
| assertFalse(first.isCancelled(), "first request should start uncancelled"); | ||
|
|
||
| var second = provider.provideCodeMinings(viewer, new NullProgressMonitor()); | ||
| assertNotNull(second); | ||
|
|
||
| assertTrue(first.isCancelled(), "previous request should be cancelled when a new one starts"); | ||
| assertFalse(second.isCancelled(), "new request should remain active"); | ||
| } | ||
|
|
||
| @Test | ||
| void keepsRequestsIndependentAcrossDocuments() throws Exception { | ||
| IFile file1 = TestUtils.createUniqueTestFile(project, "txt", "one"); | ||
| IFile file2 = TestUtils.createUniqueTestFile(project, "txt", "two"); | ||
| ITextViewer viewer1 = TestUtils.openTextViewer(file1); | ||
| ITextViewer viewer2 = TestUtils.openTextViewer(file2); | ||
|
|
||
| var provider = new StubProvider(); | ||
|
|
||
| var first = provider.provideCodeMinings(viewer1, new NullProgressMonitor()); | ||
| var second = provider.provideCodeMinings(viewer2, new NullProgressMonitor()); | ||
|
|
||
| assertNotNull(first); | ||
| assertNotNull(second); | ||
| assertFalse(first.isCancelled(), "request for doc1 must remain active when doc2 starts"); | ||
| assertFalse(second.isCancelled(), "request for doc2 must start active"); | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
org.eclipse.lsp4e/src/org/eclipse/lsp4e/internal/AbstractLSPCodeMiningProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2025 Vegard IT GmbH and others. | ||
| * This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License 2.0 | ||
| * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Sebastian Thomschke (Vegard IT GmbH) - initial implementation. | ||
| *******************************************************************************/ | ||
| package org.eclipse.lsp4e.internal; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.jdt.annotation.Nullable; | ||
| import org.eclipse.jface.text.IDocument; | ||
| import org.eclipse.jface.text.ITextViewer; | ||
| import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider; | ||
| import org.eclipse.jface.text.codemining.ICodeMining; | ||
| import org.eclipse.lsp4e.LSPEclipseUtils; | ||
| import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
|
|
||
| /** | ||
| * Base class for LSP-backed code mining providers that: | ||
| * <ul> | ||
| * <li>compute code minings asynchronously per document using LSP requests</li> | ||
| * <li>track at most one in-flight request per document and cancel the previous | ||
| * one when a new computation starts</li> | ||
| * </ul> | ||
| */ | ||
| public abstract class AbstractLSPCodeMiningProvider extends AbstractCodeMiningProvider { | ||
|
|
||
| private final ConcurrentMap<IDocument, CompletableFuture<List<? extends ICodeMining>>> pendingRequests = new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * Computes code minings for the given document. | ||
| * | ||
| * @return a future producing the list of code minings, or {@code null} if no | ||
| * code minings are available | ||
| */ | ||
| protected abstract @Nullable CompletableFuture<List<? extends ICodeMining>> doProvideCodeMinings(IDocument doc, | ||
| TextDocumentIdentifier docId); | ||
|
|
||
| @Override | ||
| public final @Nullable CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(final ITextViewer viewer, | ||
| final IProgressMonitor monitor) { | ||
| final IDocument document = viewer.getDocument(); | ||
| if (document == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final URI docURI = LSPEclipseUtils.toUri(document); | ||
| if (docURI == null) | ||
| return null; | ||
|
|
||
| final TextDocumentIdentifier docId = LSPEclipseUtils.toTextDocumentIdentifier(docURI); | ||
|
|
||
| final var current = doProvideCodeMinings(document, docId); | ||
| final CompletableFuture<List<? extends ICodeMining>> previous; | ||
| if (current == null) { | ||
| previous = pendingRequests.remove(document); | ||
| } else { | ||
| previous = pendingRequests.put(document, current); | ||
| } | ||
| if (previous != null && !previous.isDone()) { | ||
| previous.cancel(true); | ||
| } | ||
|
|
||
| return current; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.