Skip to content

Commit 89a9b43

Browse files
committed
Removed redundant code.
1 parent 9536b05 commit 89a9b43

7 files changed

Lines changed: 74 additions & 122 deletions

File tree

com.microsoft.copilot.eclipse.core.test/src/com/microsoft/copilot/eclipse/core/lsp/CopilotLanguageClientTests.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
import static org.mockito.Mockito.verify;
1111
import static org.mockito.Mockito.when;
1212

13-
import java.lang.reflect.Field;
1413
import java.util.HashMap;
1514
import java.util.Map;
1615
import java.util.concurrent.CompletableFuture;
1716
import java.util.concurrent.ExecutionException;
1817

1918
import org.eclipse.core.resources.IFile;
20-
import org.eclipse.e4.core.services.events.IEventBroker;
2119
import org.junit.jupiter.api.BeforeEach;
2220
import org.junit.jupiter.api.Test;
2321
import org.junit.jupiter.api.extension.ExtendWith;
@@ -30,12 +28,10 @@
3028
import com.microsoft.copilot.eclipse.core.FeatureFlags;
3129
import com.microsoft.copilot.eclipse.core.chat.service.IChatServiceManager;
3230
import com.microsoft.copilot.eclipse.core.chat.service.IReferencedFileService;
33-
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
3431
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationCapabilities;
3532
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationContextParams;
3633
import com.microsoft.copilot.eclipse.core.lsp.protocol.CurrentEditorContext;
3734
import com.microsoft.copilot.eclipse.core.lsp.protocol.DidChangeFeatureFlagsParams;
38-
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningNotification;
3935
import com.microsoft.copilot.eclipse.core.utils.FileUtils;
4036

4137
@ExtendWith(MockitoExtension.class)
@@ -52,9 +48,6 @@ class CopilotLanguageClientTests {
5248
@Mock
5349
private IReferencedFileService fileService;
5450

55-
@Mock
56-
private IEventBroker eventBroker;
57-
5851
@BeforeEach
5952
void setUp() {
6053
client = new CopilotLanguageClient();
@@ -138,25 +131,4 @@ void testOnDidChangeFeatureFlagsWithEmptyFeatureFlags() {
138131
verify(mockFeatureFlags).setByokEnabled(true);
139132
}
140133
}
141-
142-
@Test
143-
void testOnQuotaWarning_PostsNotificationToEventBroker() {
144-
QuotaWarningNotification notification = new QuotaWarningNotification(
145-
"Copilot Quota Usage Alert", "Approaching quota", "warning", null, null);
146-
setEventBroker(eventBroker);
147-
148-
client.onQuotaWarning(notification);
149-
150-
verify(eventBroker).post(CopilotEventConstants.TOPIC_QUOTA_WARNING, notification);
151-
}
152-
153-
private void setEventBroker(IEventBroker broker) {
154-
try {
155-
Field field = CopilotLanguageClient.class.getDeclaredField("eventBroker");
156-
field.setAccessible(true);
157-
field.set(client, broker);
158-
} catch (ReflectiveOperationException e) {
159-
throw new AssertionError("Failed to inject event broker", e);
160-
}
161-
}
162-
}
134+
}

com.microsoft.copilot.eclipse.core/src/com/microsoft/copilot/eclipse/core/lsp/CopilotLanguageClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import com.microsoft.copilot.eclipse.core.lsp.protocol.codingagent.CodingAgentMessageRequestParams;
6565
import com.microsoft.copilot.eclipse.core.lsp.protocol.codingagent.CodingAgentMessageResult;
6666
import com.microsoft.copilot.eclipse.core.lsp.protocol.policy.DidChangePolicyParams;
67-
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningNotification;
67+
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningParams;
6868
import com.microsoft.copilot.eclipse.core.utils.FileUtils;
6969
import com.microsoft.copilot.eclipse.core.utils.PlatformUtils;
7070

@@ -358,10 +358,10 @@ public CompletableFuture<CodingAgentMessageResult> onCodingAgentMessage(CodingAg
358358
* Notify when a quota warning is received from the language server.
359359
*/
360360
@JsonNotification("copilot/quotaWarning")
361-
public void onQuotaWarning(QuotaWarningNotification notification) {
362-
CopilotCore.LOGGER.info("Quota warning received: " + notification);
361+
public void onQuotaWarning(QuotaWarningParams params) {
362+
CopilotCore.LOGGER.info("Quota warning received: " + params);
363363
if (eventBroker != null) {
364-
eventBroker.post(CopilotEventConstants.TOPIC_QUOTA_WARNING, notification);
364+
eventBroker.post(CopilotEventConstants.TOPIC_QUOTA_WARNING, params);
365365
}
366366
}
367367

com.microsoft.copilot.eclipse.core/src/com/microsoft/copilot/eclipse/core/lsp/protocol/quota/IntervalQuota.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

com.microsoft.copilot.eclipse.core/src/com/microsoft/copilot/eclipse/core/lsp/protocol/quota/QuotaChangeParams.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

com.microsoft.copilot.eclipse.core/src/com/microsoft/copilot/eclipse/core/lsp/protocol/quota/QuotaWarningNotification.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

com.microsoft.copilot.eclipse.ui/src/com/microsoft/copilot/eclipse/ui/chat/ChatView.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
import com.microsoft.copilot.eclipse.core.lsp.protocol.TodoItem;
6363
import com.microsoft.copilot.eclipse.core.lsp.protocol.Turn;
6464
import com.microsoft.copilot.eclipse.core.lsp.protocol.codingagent.CodingAgentMessageRequestParams;
65-
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningNotification;
65+
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningParams;
6666
import com.microsoft.copilot.eclipse.core.persistence.AbstractTurnData;
6767
import com.microsoft.copilot.eclipse.core.persistence.ConversationPersistenceManager;
6868
import com.microsoft.copilot.eclipse.core.persistence.ConversationXmlData;
@@ -375,11 +375,11 @@ public void done(IJobChangeEvent event) {
375375

376376
this.quotaWarningHandler = event -> {
377377
Object data = event.getProperty(IEventBroker.DATA);
378-
if (data instanceof QuotaWarningNotification notification) {
378+
if (data instanceof QuotaWarningParams params) {
379379
SwtUtils.invokeOnDisplayThreadAsync(() -> {
380380
if (actionBar != null && !actionBar.isDisposed()) {
381-
boolean warning = "warning".equalsIgnoreCase(notification.severity());
382-
actionBar.createQuotaWarningBanner(notification.message(), notification.copilotPlan(), warning);
381+
boolean warning = "warning".equalsIgnoreCase(params.severity());
382+
actionBar.createQuotaWarningBanner(params.message(), params.copilotPlan(), warning);
383383
}
384384
}, parent);
385385
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
package com.microsoft.copilot.eclipse.ui.notifications;
5+
6+
import org.eclipse.e4.core.services.events.IEventBroker;
7+
import org.eclipse.swt.widgets.Display;
8+
import org.eclipse.ui.PlatformUI;
9+
import org.osgi.service.event.EventHandler;
10+
11+
import com.microsoft.copilot.eclipse.core.CopilotCore;
12+
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
13+
import com.microsoft.copilot.eclipse.core.lsp.protocol.quota.QuotaWarningParams;
14+
15+
/**
16+
* Handles quota notifications from the language server and displays
17+
* them as JFace notification popups.
18+
*/
19+
public class QuotaNotificationManager {
20+
21+
private static final int NOTIFICATION_DELAY_MS = 10000;
22+
23+
private final IEventBroker eventBroker;
24+
private EventHandler quotaWarningEventHandler;
25+
26+
/**
27+
* Creates a new QuotaNotificationManager and subscribes to quota events.
28+
*/
29+
public QuotaNotificationManager() {
30+
this.eventBroker = PlatformUI.getWorkbench().getService(IEventBroker.class);
31+
if (eventBroker != null) {
32+
this.quotaWarningEventHandler = event -> {
33+
Object data = event.getProperty(IEventBroker.DATA);
34+
if (data instanceof QuotaWarningParams params) {
35+
showQuotaWarningNotification(params);
36+
}
37+
};
38+
eventBroker.subscribe(CopilotEventConstants.TOPIC_QUOTA_WARNING, quotaWarningEventHandler);
39+
}
40+
}
41+
42+
/**
43+
* Unsubscribes the quota event handler.
44+
*/
45+
public void dispose() {
46+
if (eventBroker != null && quotaWarningEventHandler != null) {
47+
eventBroker.unsubscribe(quotaWarningEventHandler);
48+
quotaWarningEventHandler = null;
49+
}
50+
}
51+
52+
private void showQuotaWarningNotification(QuotaWarningParams params) {
53+
Display display = PlatformUI.getWorkbench().getDisplay();
54+
if (display == null || display.isDisposed()) {
55+
return;
56+
}
57+
display.asyncExec(() -> {
58+
try {
59+
new QuotaWarningNotificationPopup(display, params.message(), NOTIFICATION_DELAY_MS).open();
60+
} catch (Exception e) {
61+
CopilotCore.LOGGER.error("Failed to show quota warning notification", e);
62+
}
63+
});
64+
}
65+
}

0 commit comments

Comments
 (0)