Repository navigation
Expand file tree
/
Copy pathAmazonQLspClientImpl.java
More file actions
700 lines (627 loc) · 31.7 KB
/
Copy pathAmazonQLspClientImpl.java
File metadata and controls
700 lines (627 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.aws.toolkits.eclipse.amazonq.lsp;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITextViewerExtension;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.lsp4e.LanguageClientImpl;
import org.eclipse.lsp4j.ConfigurationParams;
import org.eclipse.lsp4j.ProgressParams;
import org.eclipse.lsp4j.ShowDocumentParams;
import org.eclipse.lsp4j.ShowDocumentResult;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import com.github.difflib.DiffUtils;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.Patch;
import software.amazon.awssdk.services.toolkittelemetry.model.Sentiment;
import software.aws.toolkits.eclipse.amazonq.chat.ChatAsyncResultManager;
import software.aws.toolkits.eclipse.amazonq.chat.ChatCommunicationManager;
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommand;
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommandName;
import software.aws.toolkits.eclipse.amazonq.chat.models.GetSerializedChatParams;
import software.aws.toolkits.eclipse.amazonq.chat.models.GetSerializedChatResult;
import software.aws.toolkits.eclipse.amazonq.chat.models.SerializedChatResult;
import software.aws.toolkits.eclipse.amazonq.chat.models.ShowSaveFileDialogParams;
import software.aws.toolkits.eclipse.amazonq.chat.models.ShowSaveFileDialogResult;
import software.aws.toolkits.eclipse.amazonq.editor.InMemoryInput;
import software.aws.toolkits.eclipse.amazonq.editor.MemoryStorage;
import software.aws.toolkits.eclipse.amazonq.inlineChat.TextDiff;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.LoginType;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.SsoTokenChangedKind;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.SsoTokenChangedParams;
import software.aws.toolkits.eclipse.amazonq.lsp.model.ConnectionMetadata;
import software.aws.toolkits.eclipse.amazonq.lsp.model.OpenFileDiffParams;
import software.aws.toolkits.eclipse.amazonq.lsp.model.ShowNotificationParams;
import software.aws.toolkits.eclipse.amazonq.broker.events.QDevAccessBlockedState;
import software.aws.toolkits.eclipse.amazonq.lsp.model.OpenTabUiResponse;
import software.aws.toolkits.eclipse.amazonq.lsp.model.SsoProfileData;
import software.aws.toolkits.eclipse.amazonq.lsp.model.TelemetryEvent;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.util.QEclipseEditorUtils;
import software.aws.toolkits.eclipse.amazonq.preferences.AmazonQPreferencePage;
import software.aws.toolkits.eclipse.amazonq.telemetry.service.DefaultTelemetryService;
import software.aws.toolkits.eclipse.amazonq.util.AbapUtil;
import software.aws.toolkits.eclipse.amazonq.util.Constants;
import software.aws.toolkits.eclipse.amazonq.util.ObjectMapperFactory;
import software.aws.toolkits.eclipse.amazonq.util.ThemeDetector;
import software.aws.toolkits.eclipse.amazonq.util.ThreadingUtils;
import software.aws.toolkits.eclipse.amazonq.util.WorkspaceUtils;
import software.aws.toolkits.eclipse.amazonq.views.model.Customization;
import software.aws.toolkits.eclipse.amazonq.views.model.UpdateRedirectUrlCommand;
@SuppressWarnings("restriction")
public class AmazonQLspClientImpl extends LanguageClientImpl implements AmazonQLspClient {
private ThemeDetector themeDetector = new ThemeDetector();
@Override
public final CompletableFuture<ConnectionMetadata> getConnectionMetadata() {
return CompletableFuture.supplyAsync(() -> {
SsoProfileData sso = new SsoProfileData();
String startUrl = Constants.AWS_BUILDER_ID_URL;
AuthState authState = Activator.getLoginService().getAuthState();
if (authState.issuerUrl() != null && !authState.issuerUrl().isBlank()) {
startUrl = authState.issuerUrl();
}
sso.setStartUrl(startUrl);
ConnectionMetadata metadata = new ConnectionMetadata();
metadata.setSso(sso);
return metadata;
});
}
@Override
public final CompletableFuture<List<Object>> configuration(final ConfigurationParams configurationParams) {
if (configurationParams.getItems().size() == 0) {
return CompletableFuture.completedFuture(null);
}
List<Object> output = new ArrayList<>();
configurationParams.getItems().forEach(item -> {
if (item.getSection().equals(Constants.LSP_Q_CONFIGURATION_KEY)) {
Customization storedCustomization = Activator.getPluginStore().getObject(
Constants.CUSTOMIZATION_STORAGE_INTERNAL_KEY,
Customization.class);
Map<String, Object> qConfig = new HashMap<>();
qConfig.put(Constants.LSP_CUSTOMIZATION_CONFIGURATION_KEY, Objects.nonNull(storedCustomization) ? storedCustomization.getArn() : null);
qConfig.put(Constants.LSP_ENABLE_TELEMETRY_EVENTS_CONFIGURATION_KEY, false);
qConfig.put(Constants.LSP_OPT_OUT_TELEMETRY_CONFIGURATION_KEY, !DefaultTelemetryService.telemetryEnabled());
output.add(qConfig);
Activator.getLspProvider().activate(AmazonQLspServer.class);
} else if (item.getSection().equals(Constants.LSP_CW_CONFIGURATION_KEY)) {
Map<String, Boolean> cwConfig = new HashMap<>();
boolean shareContentSetting = Activator.getDefault().getPreferenceStore().getBoolean(AmazonQPreferencePage.Q_DATA_SHARING);
boolean referencesEnabled = Activator.getDefault().getPreferenceStore().getBoolean(AmazonQPreferencePage.CODE_REFERENCE_OPT_IN)
&& Activator.getLoginService().getAuthState().loginType().equals(LoginType.BUILDER_ID);
cwConfig.put(Constants.LSP_CW_OPT_OUT_KEY, shareContentSetting);
cwConfig.put(Constants.LSP_CODE_REFERENCES_OPT_OUT_KEY, referencesEnabled);
output.add(cwConfig);
}
});
return CompletableFuture.completedFuture(output);
}
/*
* Handles the progress notifications received from the LSP server.
* - Process partial results for Chat messages if provided token is maintained by ChatCommunicationManager
* - Other notifications are ignored at this time.
*/
@Override
public final void notifyProgress(final ProgressParams params) {
var chatCommunicationManager = ChatCommunicationManager.getInstance();
ThreadingUtils.executeAsyncTask(() -> {
try {
chatCommunicationManager.handlePartialResultProgressNotification(params);
} catch (Exception e) {
Activator.getLogger().error("Error processing partial result progress notification", e);
}
});
}
@Override
public final void telemetryEvent(final Object event) {
TelemetryEvent telemetryEvent = ObjectMapperFactory.getInstance().convertValue(event, TelemetryEvent.class);
ThreadingUtils.executeAsyncTask(() -> {
switch (telemetryEvent.name()) {
// intercept the feedback telemetry event and re-route to our feedback backend
case "amazonq_sendFeedback":
sendFeedback(telemetryEvent);
break;
default:
Activator.getTelemetryService().emitMetric(telemetryEvent);
}
});
}
private void sendFeedback(final TelemetryEvent telemetryEvent) {
var data = telemetryEvent.data();
if (data.containsKey("sentiment")) {
var sentiment = data.get("sentiment").equals(Sentiment.POSITIVE) ? Sentiment.POSITIVE : Sentiment.NEGATIVE;
var comment = Optional.ofNullable(data.get("comment")).filter(String.class::isInstance)
.map(String.class::cast).orElse("");
try {
Activator.getTelemetryService().emitFeedback(comment, sentiment);
} catch (Exception e) {
Activator.getLogger().error("Error occurred when submitting feedback", e);
}
}
}
@Override
public final CompletableFuture<ShowDocumentResult> showDocument(final ShowDocumentParams params) {
String uri = params.getUri();
Activator.getLogger().info("Opening URI: " + uri);
return CompletableFuture.supplyAsync(() -> {
final boolean[] success = new boolean[1];
if (params.getExternal() != null && params.getExternal()) {
var command = new UpdateRedirectUrlCommand(uri);
Activator.getEventBroker().post(UpdateRedirectUrlCommand.class, command);
Display.getDefault().syncExec(() -> {
try {
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(uri));
success[0] = true;
} catch (Exception e) {
Activator.getLogger().error("Error in UI thread while opening external URI: " + uri, e);
success[0] = false;
}
});
return new ShowDocumentResult(success[0]);
} else {
Display.getDefault().syncExec(() -> {
try {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new URI(uri));
IDE.openEditorOnFileStore(page, fileStore);
success[0] = true;
} catch (Exception e) {
Activator.getLogger().error("Error in UI thread while opening URI: " + uri, e);
success[0] = false;
}
});
return new ShowDocumentResult(success[0]);
}
});
}
@Override
public final void ssoTokenChanged(final SsoTokenChangedParams params) {
SsoTokenChangedKind kind = SsoTokenChangedKind.fromValue(params.kind());
Activator.getLogger().info("Processing " + kind + "ssoTokenChanged notification...");
try {
switch (kind) {
case EXPIRED:
Activator.getLoginService().expire();
return;
case REFRESHED:
boolean loginOnInvalidToken = false;
Activator.getLoginService().reAuthenticate(loginOnInvalidToken);
return;
default:
Activator.getLogger().error("Error processing ssoTokenChanged notification: Unhandled kind " + kind);
}
} catch (IllegalArgumentException ex) {
Activator.getLogger().error("Error processing " + kind + " ssoTokenChanged notification", ex);
}
}
/**
* Handles the language server's generic notification channel. Today the only notification this
* plugin acts on is the report that Amazon Q Developer has refused this identity at sign-in.
*
* <p>Reacting means signing the user out and routing to an explanation. Sign-out happens here
* rather than in the view because the session is already useless: every Q request from this
* identity is refused, so leaving the user signed in would show a working-looking IDE that
* silently does nothing.
*
* <p>Never throws. This runs on the LSP message thread and is shared by every future
* notification, so a failure to classify one must not take the channel down.
*/
@Override
public final void showNotification(final ShowNotificationParams params) {
try {
if (!QDevAccessBlockedNotification.isAccessBlocked(params)) {
return;
}
String message = params.content() == null ? null : params.content().text();
Activator.getLogger().info("Amazon Q Developer access is blocked for this identity: " + message);
// Publish before signing out. Sign-out makes the router re-evaluate, and the blocked
// state has to be in place by then or the router resolves the ordinary login view.
Activator.getEventBroker().post(QDevAccessBlockedState.class, QDevAccessBlockedState.BLOCKED);
Activator.getLoginService().logout();
} catch (Exception e) {
Activator.getLogger().error("Failed to handle showNotification", e);
}
}
@Override
public final void sendContextCommands(final Object params) {
var command = ChatUIInboundCommand.createCommand("aws/chat/sendContextCommands", params);
Activator.getEventBroker().post(ChatUIInboundCommand.class, command);
}
@Override
public final CompletableFuture<Object> openTab(final Object params) {
return CompletableFuture.supplyAsync(() -> {
String requestId = UUID.randomUUID().toString();
var command = ChatUIInboundCommand.createCommand("aws/chat/openTab", params, requestId);
Activator.getEventBroker().post(ChatUIInboundCommand.class, command);
ChatAsyncResultManager manager = ChatAsyncResultManager.getInstance();
manager.createRequestId(requestId);
OpenTabUiResponse response;
try {
Object res = ChatAsyncResultManager.getInstance().getResult(requestId);
response = ObjectMapperFactory.getInstance().convertValue(res, OpenTabUiResponse.class);
} catch (Exception e) {
throw new IllegalStateException("Failed to retrieve new tab response from chat UI", e);
} finally {
manager.removeRequestId(requestId);
}
if (response.result() == null) {
Activator.getLogger().warn("Got null tab response from UI");
return null;
}
return response.result();
});
}
@Override
public final CompletableFuture<ShowSaveFileDialogResult> showSaveFileDialog(final ShowSaveFileDialogParams params) {
CompletableFuture<ShowSaveFileDialogResult> future = new CompletableFuture<>();
Display.getDefault().syncExec(() -> {
String name = "export-chat.md";
String path = "";
try {
URI uri = new URI(params.defaultUri());
File file = new File(uri);
path = file.getParent();
name = file.getName();
} catch (URISyntaxException e) {
Activator.getLogger().warn("Unable to parse file path details from showSaveFileDialog params: " + e.getMessage());
}
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
dialog.setFilterExtensions(new String[] {"*.md", "*.html"});
dialog.setFilterPath(path);
dialog.setFileName(name);
dialog.setFilterNames(new String[] {"Markdown Files (.md)", "HTML Files (.html)"});
dialog.setOverwrite(true);
String filePath = dialog.open();
if (filePath != null) {
future.complete(new ShowSaveFileDialogResult(filePath));
} else {
future.completeExceptionally(new IllegalStateException("User did not provide file path for export"));
}
});
return future;
}
@Override
public final CompletableFuture<SerializedChatResult> getSerializedChat(final GetSerializedChatParams params) {
return CompletableFuture.supplyAsync(() -> {
String requestId = UUID.randomUUID().toString();
var command = ChatUIInboundCommand.createCommand("aws/chat/getSerializedChat", params, requestId);
ChatAsyncResultManager manager = ChatAsyncResultManager.getInstance();
manager.createRequestId(requestId);
Activator.getEventBroker().post(ChatUIInboundCommand.class, command);
SerializedChatResult result;
try {
Object res = ChatAsyncResultManager.getInstance().getResult(requestId);
GetSerializedChatResult serializedChatResult = ObjectMapperFactory.getInstance().convertValue(res, GetSerializedChatResult.class);
result = serializedChatResult.result();
} catch (Exception e) {
throw new IllegalStateException("Failed to retrieve serialized chat from chat UI", e);
} finally {
manager.removeRequestId(requestId);
}
return result;
});
}
@Override
public final void openFileDiff(final OpenFileDiffParams params) {
String annotationAdded = themeDetector.isDarkTheme() ? "diffAnnotation.added.dark" : "diffAnnotation.added";
String annotationDeleted = themeDetector.isDarkTheme() ? "diffAnnotation.deleted.dark"
: "diffAnnotation.deleted";
Display.getDefault().asyncExec(() -> {
try {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IStorageEditorInput input = new InMemoryInput(
new MemoryStorage(new Path(params.originalFileUri()).lastSegment(), ""));
IEditorDescriptor defaultEditor = PlatformUI.getWorkbench().getEditorRegistry()
.getDefaultEditor(".java");
IEditorPart editor = page.openEditor(input,
defaultEditor != null ? defaultEditor.getId() : "org.eclipse.ui.DefaultTextEditor", true,
IWorkbenchPage.MATCH_INPUT);
// Annotation model provides highlighting for the diff additions/deletions
IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider()
.getAnnotationModel(editor.getEditorInput());
var document = ((ITextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput());
// Split original and new code into lines for diff comparison
String[] originalLines = (params.originalFileContent() != null
&& !params.originalFileContent().isEmpty())
? params.originalFileContent().lines().toArray(String[]::new)
: new String[0];
String[] newLines = (params.fileContent() != null && !params.fileContent().isEmpty())
? params.fileContent().lines().toArray(String[]::new)
: new String[0];
// Diff generation --> returns Patch object which contains deltas for each line
Patch<String> patch = DiffUtils.diff(Arrays.asList(originalLines), Arrays.asList(newLines));
StringBuilder resultText = new StringBuilder();
List<TextDiff> currentDiffs = new ArrayList<>();
int currentPos = 0;
int currentLine = 0;
for (AbstractDelta<String> delta : patch.getDeltas()) {
// Continuously copy unchanged lines until we hit a diff
while (currentLine < delta.getSource().getPosition()) {
resultText.append(originalLines[currentLine]).append("\n");
currentPos += originalLines[currentLine].length() + 1;
currentLine++;
}
List<String> originalChangedLines = delta.getSource().getLines();
List<String> newChangedLines = delta.getTarget().getLines();
// Handle deleted lines and mark position
for (String line : originalChangedLines) {
resultText.append(line).append("\n");
currentDiffs.add(new TextDiff(currentPos, line.length(), true));
currentPos += line.length() + 1;
}
// Handle added lines and mark position
for (String line : newChangedLines) {
resultText.append(line).append("\n");
currentDiffs.add(new TextDiff(currentPos, line.length(), false));
currentPos += line.length() + 1;
}
currentLine = delta.getSource().getPosition() + delta.getSource().size();
}
// Loop through remaining unchanged lines
while (currentLine < originalLines.length) {
resultText.append(originalLines[currentLine]).append("\n");
currentPos += originalLines[currentLine].length() + 1;
currentLine++;
}
final String finalText = resultText.toString();
document.replace(0, document.getLength(), finalText);
// Add all annotations after text modifications are complete
for (TextDiff diff : currentDiffs) {
Position position = new Position(diff.offset(), diff.length());
String annotationType = diff.isDeletion() ? annotationDeleted : annotationAdded;
String annotationText = diff.isDeletion() ? "Deleted Code" : "Added Code";
annotationModel.addAnnotation(new Annotation(annotationType, false, annotationText), position);
}
makeEditorReadOnly(editor);
} catch (CoreException | BadLocationException e) {
Activator.getLogger().info("Failed to open file/diff: " + e);
}
});
}
private void makeEditorReadOnly(final IEditorPart editor) {
ITextViewer viewer = editor.getAdapter(ITextViewer.class);
if (viewer != null) {
VerifyKeyListener verifyKeyListener = event -> event.doit = false;
((ITextViewerExtension) viewer).prependVerifyKeyListener(verifyKeyListener);
}
// stop text‑modifying commands
ActionFactory[] ids = {ActionFactory.UNDO, ActionFactory.REDO, ActionFactory.CUT, ActionFactory.PASTE,
ActionFactory.DELETE};
for (ActionFactory id : ids) {
IAction a = ((ITextEditor) editor).getAction(id.getId());
if (a != null) {
a.setEnabled(false);
}
}
IWorkbenchPartSite site = editor.getSite();
if (site == null) {
return;
}
IWorkbenchWindow window = site.getWorkbenchWindow();
if (window == null) {
return;
}
Runnable cleanupEditor = () -> {
Display.getDefault().asyncExec(() -> {
try {
if (editor != null && !editor.isDirty()) {
IWorkbenchPage currentPage = editor.getSite().getPage();
if (currentPage != null) {
// Remove annotations
if (editor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) editor;
IDocumentProvider provider = textEditor.getDocumentProvider();
if (provider != null) {
IAnnotationModel annotationModel = provider
.getAnnotationModel(editor.getEditorInput());
if (annotationModel != null) {
Iterator<?> annotationIterator = annotationModel.getAnnotationIterator();
while (annotationIterator.hasNext()) {
annotationModel.removeAnnotation((Annotation) annotationIterator.next());
}
}
}
}
}
}
} catch (Exception e) {
Activator.getLogger().error("Error during editor cleanup", e);
}
});
};
IPageListener pageListener = new IPageListener() {
@Override
public void pageOpened(final IWorkbenchPage page) {
}
@Override
public void pageClosed(final IWorkbenchPage page) {
cleanupEditor.run();
window.removePageListener(this);
}
@Override
public void pageActivated(final IWorkbenchPage page) {
}
};
window.addPageListener(pageListener);
editor.doSave(new NullProgressMonitor());
}
@Override
public final void sendChatUpdate(final Object params) {
var conversationClickCommand = new ChatUIInboundCommand("aws/chat/sendChatUpdate", null, params,
false, null);
Activator.getEventBroker().post(ChatUIInboundCommand.class, conversationClickCommand);
}
@Override
public final void chatOptionsUpdate(final Object params) {
var chatOptionsUpdateCommand = new ChatUIInboundCommand("aws/chat/chatOptionsUpdate", null, params,
false, null);
Activator.getEventBroker().post(ChatUIInboundCommand.class, chatOptionsUpdateCommand);
}
@Override
public final void didCopyFile(final Object params) {
refreshProjects();
}
@Override
public final void didWriteFile(final Object params) {
var path = extractFilePathFromParams(params);
if (AbapUtil.isAbapFile(path)) {
AbapUtil.updateAdtServer(path);
}
refreshProjects();
}
@Override
public final void didAppendFile(final Object params) {
var path = extractFilePathFromParams(params);
if (AbapUtil.isAbapFile(path)) {
AbapUtil.updateAdtServer(path);
}
refreshProjects();
}
@Override
public final void didRemoveFileOrDirectory(final Object params) {
refreshProjects();
}
@Override
public final void didCreateDirectory(final Object params) {
refreshProjects();
}
private void refreshProjects() {
WorkspaceUtils.refreshAllProjects();
WorkspaceUtils.refreshAdtViews();
}
private boolean isUriInWorkspace(final String uri) {
try {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath workspacePath = workspace.getRoot().getLocation();
URI fileUri = new URI(uri);
File file = new File(fileUri);
String filePath = file.getCanonicalPath();
return filePath.startsWith(workspacePath.toFile().getCanonicalPath());
} catch (Exception e) {
Activator.getLogger().error("Error validating URI location: " + uri, e);
return false;
}
}
private String extractFilePathFromParams(final Object params) {
if (params instanceof Map) {
var map = (Map<?, ?>) params;
Object path = map.get("path");
return path != null ? path.toString() : null;
}
return null;
}
@Override
public final void sendPinnedContext(final Object params) {
Object updatedParams = params;
Optional<String> fileUri = getActiveFileUri();
if (fileUri.isPresent()) {
Map<String, Object> textDocument = Map.of("uri", fileUri.get());
if (params instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> paramsMap = new HashMap<>((Map<String, Object>) params);
paramsMap.put("textDocument", textDocument);
updatedParams = paramsMap;
} else {
updatedParams = Map.of("params", params, "textDocument", textDocument);
}
}
var sendPinnedContextCommand = ChatUIInboundCommand.createCommand(ChatUIInboundCommandName.SendPinnedContext.getValue(), updatedParams);
Activator.getEventBroker().post(ChatUIInboundCommand.class, sendPinnedContextCommand);
}
private Optional<String> getActiveFileUri() {
AtomicReference<Optional<String>> fileUri = new AtomicReference<>();
Display.getDefault().syncExec(() -> {
try {
fileUri.set(getActiveEditorRelativePath());
} catch (Exception e) {
Activator.getLogger().error("Error getting active file URI", e);
fileUri.set(Optional.empty());
}
});
return fileUri.get();
}
private Optional<String> getActiveEditorRelativePath() {
var activeEditor = QEclipseEditorUtils.getActiveTextEditor();
if (activeEditor == null) {
return Optional.empty();
}
return QEclipseEditorUtils.getOpenFileUri(activeEditor.getEditorInput())
.map(this::getRelativePath);
}
private String getRelativePath(final String absoluteUri) {
try {
if (StringUtils.isBlank(absoluteUri)) {
return absoluteUri;
}
var uri = new URI(absoluteUri);
var activeFilePath = new File(uri).getCanonicalPath();
// Get workspace root path
var workspace = ResourcesPlugin.getWorkspace();
var workspacePath = workspace.getRoot().getLocation();
if (workspacePath == null) {
return activeFilePath;
}
var workspaceRoot = workspacePath.toFile().getCanonicalPath();
if (StringUtils.isBlank(workspaceRoot)) {
return activeFilePath;
}
if (StringUtils.startsWithIgnoreCase(activeFilePath, workspaceRoot)) {
var workspaceRootPath = Paths.get(workspaceRoot);
var activeFilePathObj = Paths.get(activeFilePath);
var relativePath = workspaceRootPath.relativize(activeFilePathObj).normalize();
return relativePath.toString().replace('\\', '/');
}
// Not in workspace, return absolute path
return activeFilePath;
} catch (Exception e) {
Activator.getLogger().error("Error occurred when attempting to determine relative path for: " + absoluteUri, e);
return absoluteUri;
}
}
}