From 53f14d96ead5c331115065f238015538526e3964 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Thu, 21 Nov 2024 12:43:04 -0800 Subject: [PATCH] Adds command listeners for undo to control flag which signals to auto trigger whether or not to invoke queries (#256) --- .../util/AutoTriggerDocumentListener.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerDocumentListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerDocumentListener.java index 19e0172c..e07ee0a3 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerDocumentListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerDocumentListener.java @@ -3,14 +3,22 @@ package software.aws.toolkits.eclipse.amazonq.util; +import org.eclipse.core.commands.IExecutionListener; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocumentListener; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; import static software.aws.toolkits.eclipse.amazonq.util.QEclipseEditorUtils.getActiveTextEditor; import java.util.concurrent.ExecutionException; public final class AutoTriggerDocumentListener implements IDocumentListener, IAutoTriggerListener { + private static final String UNDO_COMMAND_ID = "org.eclipse.ui.edit.undo"; + + private ThreadLocal isChangeInducedByUndo = ThreadLocal.withInitial(() -> false); + private IExecutionListener commandListener; + public AutoTriggerDocumentListener() { } @@ -45,17 +53,35 @@ private boolean shouldSendQuery(final DocumentEvent e, final QInvocationSession return false; } + if (isChangeInducedByUndo.get()) { + isChangeInducedByUndo.set(false); + return false; + } + // TODO: implement other logic to prevent unnecessary firing return true; } @Override public void onStart() { + ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class); + commandListener = QEclipseEditorUtils.getAutoTriggerExecutionListener((commandId) -> undoCommandListenerCallback(commandId)); + commandService.addExecutionListener(commandListener); return; } @Override public void onShutdown() { + if (commandListener != null) { + ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class); + commandService.removeExecutionListener(commandListener); + } return; } + + private void undoCommandListenerCallback(final String commandId) { + if (commandId.equals(UNDO_COMMAND_ID)) { + isChangeInducedByUndo.set(true); + } + } }