Skip to content

Commit

Permalink
feat: support 'window/workDoneProgress/cancel' lsp event
Browse files Browse the repository at this point in the history
Signed-off-by: Aman Prashant <aman.prashant@broadcom.com>
  • Loading branch information
ap891843 committed Apr 29, 2024
1 parent e88db02 commit f986eca
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import javax.annotation.Nullable;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp.cobol.lsp.events.notifications.CancelProgressNotification;
import org.eclipse.lsp.cobol.lsp.events.notifications.InitializedNotification;
import org.eclipse.lsp.cobol.lsp.events.queries.InitializeQuery;
import org.eclipse.lsp.cobol.lsp.events.queries.ShutdownQuery;
import org.eclipse.lsp.cobol.lsp.handlers.server.CancelProgressHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.ExitHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.InitializeHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.InitializedHandler;
Expand All @@ -48,6 +50,7 @@ public class CobolLanguageServer implements LanguageServer {
private final ShutdownHandler shutdownHandler;
private final InitializeHandler initializeHandler;
private final InitializedHandler initializedHandler;
private final CancelProgressHandler cancelProgressHandler;

@Inject
@SuppressWarnings("squid:S107")
Expand All @@ -59,7 +62,8 @@ public CobolLanguageServer(
ShutdownHandler shutdownHandler,
InitializeHandler initializeHandler,
InitializedHandler initializedHandler,
LspEventConsumer lspEventConsumer) {
LspEventConsumer lspEventConsumer,
CancelProgressHandler cancelProgressHandler) {
this.lspMessageBroker = lspMessageBroker;
this.textService = textService;
this.workspaceService = workspaceService;
Expand All @@ -68,6 +72,7 @@ public CobolLanguageServer(
this.initializeHandler = initializeHandler;
this.initializedHandler = initializedHandler;
this.lspEventConsumer = lspEventConsumer;
this.cancelProgressHandler = cancelProgressHandler;
}

@Override
Expand Down Expand Up @@ -113,4 +118,9 @@ public void exit() {
// Kill the server (loop should be already stopped at this time)
exitHandler.exit();
}

@Override
public void cancelProgress(WorkDoneProgressCancelParams params) {
lspMessageBroker.notify(new CancelProgressNotification(params.getToken().getLeft(), cancelProgressHandler));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* 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:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.lsp.events.notifications;

import lombok.SneakyThrows;
import org.eclipse.lsp.cobol.lsp.LspNotification;
import org.eclipse.lsp.cobol.lsp.handlers.server.CancelProgressHandler;

/** `window/workDoneProgress/cancel` language server event */
public class CancelProgressNotification implements LspNotification {
private final String uri;
private final CancelProgressHandler cancelProgressHandler;

public CancelProgressNotification(String uri, CancelProgressHandler cancelProgressHandler) {
this.uri = uri;
this.cancelProgressHandler = cancelProgressHandler;
}

@SneakyThrows
@Override
public void execute() {
this.cancelProgressHandler.cancel(uri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* 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:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.lsp.handlers.server;

import com.google.inject.Inject;
import org.eclipse.lsp.cobol.lsp.analysis.AsyncAnalysisService;

/** LSP `window/workDoneProgress/cancel` Handler */
public class CancelProgressHandler {
private final AsyncAnalysisService asyncAnalysisService;

@Inject
public CancelProgressHandler(AsyncAnalysisService asyncAnalysisService) {
this.asyncAnalysisService = asyncAnalysisService;
}

/**
* Cancel analysis for the passed uri
* @param uri
* @throws InterruptedException
*/
public void cancel(String uri) throws InterruptedException {
asyncAnalysisService.cancelAnalysis(uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private void handleDanglingProgressNotifications(String uri) {

private void notifyWorkProgress(String uri) {
WorkDoneProgressReport workDoneProgressReport = new WorkDoneProgressReport();
workDoneProgressReport.setCancellable(true);
ProgressParams params = new ProgressParams(Either.forLeft(uri), Either.forLeft(workDoneProgressReport));
getClient().notifyProgress(params);
}
Expand All @@ -155,6 +156,7 @@ private void notifyWorkProgressBegin(String uri) {
workDoneProgressBegin.setTitle(messageService.getMessage(
"Communications.syntaxAnalysisInProgressTitle",
files.getNameFromURI(uri)));
workDoneProgressBegin.setCancellable(true);
params.setValue(Either.forLeft(workDoneProgressBegin));
getClient().notifyProgress(params);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* 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:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.lsp.events.notifications;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.eclipse.lsp.cobol.lsp.handlers.server.CancelProgressHandler;
import org.junit.jupiter.api.Test;

/** Test {@link CancelProgressNotification} */
class CancelProgressNotificationTest {
@Test
void test() throws InterruptedException {
CancelProgressHandler handler = mock(CancelProgressHandler.class);
CancelProgressNotification notification = new CancelProgressNotification("uri", handler);
notification.execute();
verify(handler).cancel("uri");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* 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:
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.lsp.handlers.server;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.eclipse.lsp.cobol.lsp.analysis.AsyncAnalysisService;
import org.junit.jupiter.api.Test;

/** Test {@link CancelProgressHandler} */
class CancelProgressHandlerTest {

@Test
void whenCancelIsCalled_properMethodIsInvoked() throws InterruptedException {
AsyncAnalysisService asyncAnalysisService = mock(AsyncAnalysisService.class);
CancelProgressHandler handler = new CancelProgressHandler(asyncAnalysisService);
handler.cancel("test-uri");
verify(asyncAnalysisService).cancelAnalysis("test-uri");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.lsp.cobol.common.dialects.CobolLanguageId;
import org.eclipse.lsp.cobol.common.error.ErrorCodes;
import org.eclipse.lsp.cobol.common.message.LocaleStore;
Expand All @@ -41,10 +40,7 @@
import org.eclipse.lsp.cobol.lsp.*;
import org.eclipse.lsp.cobol.lsp.analysis.AsyncAnalysisService;
import org.eclipse.lsp.cobol.lsp.events.queries.CodeActionQuery;
import org.eclipse.lsp.cobol.lsp.handlers.server.ExitHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.InitializeHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.InitializedHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.ShutdownHandler;
import org.eclipse.lsp.cobol.lsp.handlers.server.*;
import org.eclipse.lsp.cobol.lsp.handlers.text.CodeActionHandler;
import org.eclipse.lsp.cobol.lsp.handlers.workspace.DidChangeConfigurationHandler;
import org.eclipse.lsp.cobol.lsp.handlers.workspace.ExecuteCommandHandler;
Expand Down Expand Up @@ -87,6 +83,7 @@ class CobolLanguageServerTest {

private DisposableLSPStateService stateService;
private final UriDecodeService uriDecodeService = mock(UriDecodeService.class);
private final CancelProgressHandler cancelProgressHandler = mock(CancelProgressHandler.class);

@BeforeEach
void getStateService() {
Expand Down Expand Up @@ -130,7 +127,8 @@ void initialized() throws InterruptedException {
new ShutdownHandler(stateService, lspMessageBroker),
mock(InitializeHandler.class),
initializedHandler,
lspEventConsumer);
lspEventConsumer,
cancelProgressHandler);


server.initialized(new InitializedParams());
Expand Down Expand Up @@ -208,7 +206,8 @@ void initializedConfig() throws InterruptedException {
new ShutdownHandler(stateService, lspMessageBroker),
new InitializeHandler(watchingService),
new InitializedHandler(watchingService, copybookNameService, keywords, settingsService, localeStore, analysisService, messageService, layoutStore),
lspEventConsumer);
lspEventConsumer,
cancelProgressHandler);

server.initialized(new InitializedParams());
waitingQuery(lspMessageBroker).join();
Expand Down Expand Up @@ -253,7 +252,8 @@ private void testServerInitialization(InitializeParams initializeParams) {
new ShutdownHandler(stateService, lspMessageBroker),
new InitializeHandler(mock(WatcherServiceImpl.class)),
new InitializedHandler(mock(WatcherServiceImpl.class), null, null, null, null, null, null, null),
lspEventConsumer);
lspEventConsumer,
cancelProgressHandler);

try {
InitializeResult result = server.initialize(initializeParams).get();
Expand Down Expand Up @@ -314,7 +314,8 @@ void shutdown() {
new ShutdownHandler(stateService, lspMessageBroker),
new InitializeHandler(null),
new InitializedHandler(null, null, null, null, null, null, null, null),
lspEventConsumer);
lspEventConsumer,
cancelProgressHandler);
assertEquals(1, stateService.getExitCode());
server.shutdown();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@ void testNotifyProgressBegin() throws NoSuchFieldException {
expectedNotifyBeginParams.setToken(uri);
WorkDoneProgressBegin workDoneProgressBegin = new WorkDoneProgressBegin();
workDoneProgressBegin.setTitle("TITLE");
workDoneProgressBegin.setCancellable(true);
expectedNotifyBeginParams.setValue(Either.forLeft(workDoneProgressBegin));

communications.notifyProgressBegin(uri);
verify(client).notifyProgress(expectedNotifyBeginParams);
verify(client).notifyProgress(new ProgressParams(Either.forLeft(uri), Either.forLeft(new WorkDoneProgressReport())));
WorkDoneProgressReport expectedProgressReport = new WorkDoneProgressReport();
expectedProgressReport.setCancellable(true);
verify(client).notifyProgress(new ProgressParams(Either.forLeft(uri), Either.forLeft(expectedProgressReport)));
}

@Test
Expand Down

0 comments on commit f986eca

Please sign in to comment.