-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a Progress support API for lemminx extension
Fixes #1562 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
2b0fe29
commit acab0cf
Showing
5 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
90 changes: 90 additions & 0 deletions
90
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/progress/ProgressMonitor.java
This file contains 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,90 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.progress; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.lsp4j.WorkDoneProgressBegin; | ||
import org.eclipse.lsp4j.WorkDoneProgressCreateParams; | ||
import org.eclipse.lsp4j.WorkDoneProgressEnd; | ||
import org.eclipse.lsp4j.WorkDoneProgressReport; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
|
||
/** | ||
* Progress monitor which wraps the LSP progress API to simplify the | ||
* implementation of progress. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class ProgressMonitor implements CancelChecker { | ||
|
||
private final String progressId; | ||
|
||
private final ProgressSupport progressSupport; | ||
|
||
private CompletableFuture<Void> future; | ||
|
||
public ProgressMonitor(ProgressSupport progressSupport) { | ||
this(UUID.randomUUID().toString(), progressSupport); | ||
} | ||
|
||
public ProgressMonitor(String progressId, ProgressSupport progressSupport) { | ||
this.progressId = progressId; | ||
this.progressSupport = progressSupport; | ||
|
||
// Initialize progress | ||
WorkDoneProgressCreateParams create = new WorkDoneProgressCreateParams(Either.forLeft(progressId)); | ||
future = progressSupport.createProgress(create); | ||
} | ||
|
||
public void begin(String title, String message, Integer percentage, Boolean cancellable) { | ||
// Start progress | ||
WorkDoneProgressBegin begin = new WorkDoneProgressBegin(); | ||
begin.setTitle(title); | ||
begin.setMessage(message); | ||
begin.setPercentage(percentage); | ||
begin.setCancellable(cancellable); | ||
progressSupport.notifyProgress(progressId, begin); | ||
} | ||
|
||
public void report(String message, Integer percentage, Boolean cancellable) { | ||
// report message | ||
WorkDoneProgressReport report = new WorkDoneProgressReport(); | ||
report.setMessage(message); | ||
report.setPercentage(percentage); | ||
report.setCancellable(cancellable); | ||
progressSupport.notifyProgress(progressId, report); | ||
} | ||
|
||
public void end(String message) { | ||
WorkDoneProgressEnd end = new WorkDoneProgressEnd(); | ||
end.setMessage(message); | ||
progressSupport.notifyProgress(progressId, end); | ||
} | ||
|
||
@Override | ||
public void checkCanceled() { | ||
if (future != null && future.isCancelled()) { | ||
throw new CancellationException(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCanceled() { | ||
return future != null ? future.isCancelled() : true; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/progress/ProgressSupport.java
This file contains 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,63 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.progress; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.lsp4j.WorkDoneProgressCreateParams; | ||
import org.eclipse.lsp4j.WorkDoneProgressNotification; | ||
|
||
/** | ||
* LSP Progress support API. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public interface ProgressSupport { | ||
|
||
default ProgressMonitor createProgressMonitor() { | ||
if (!isWorkDoneProgressSupported()) { | ||
return null; | ||
} | ||
return new ProgressMonitor(this); | ||
} | ||
|
||
default ProgressMonitor createProgressMonitor(String progressId) { | ||
if (!isWorkDoneProgressSupported()) { | ||
return null; | ||
} | ||
return new ProgressMonitor(progressId, this); | ||
} | ||
|
||
/** | ||
* Returns true if the LSP client can support LSP progress and false otherwise. | ||
* | ||
* @return true if the LSP client can support LSP progress and false otherwise. | ||
*/ | ||
boolean isWorkDoneProgressSupported(); | ||
|
||
/** | ||
* Create a progress. | ||
* | ||
* @param params the progress create parameters | ||
* @return | ||
*/ | ||
CompletableFuture<Void> createProgress(WorkDoneProgressCreateParams params); | ||
|
||
/** | ||
* Notify the the progress notification with the given process id. | ||
* | ||
* @param progressId the progress id. | ||
* @param notification the progress notification. | ||
*/ | ||
void notifyProgress(String progressId, WorkDoneProgressNotification notification); | ||
} |
This file contains 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 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