Skip to content

Commit

Permalink
Adds command listeners for undo to control flag which signals to auto…
Browse files Browse the repository at this point in the history
… trigger whether or not to invoke queries (#256)
  • Loading branch information
dingfeli authored Nov 21, 2024
1 parent 028e27b commit 53f14d9
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> isChangeInducedByUndo = ThreadLocal.withInitial(() -> false);
private IExecutionListener commandListener;

public AutoTriggerDocumentListener() {
}

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 53f14d9

Please sign in to comment.