Skip to content

Commit

Permalink
Toggle suggestions (#13)
Browse files Browse the repository at this point in the history
* Enables users to toggle between suggestions

* Disables suggestion toggling when user has typed ahead

* Removes println for incrementing and decrementing

* Fixes formatting

* Makes adjustment for various checkstyle items
  • Loading branch information
dingfeli authored Sep 17, 2024
1 parent 89cc6a6 commit c66a378
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 1 deletion.
30 changes: 30 additions & 0 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@
name="Reject Suggestions"
id="software.aws.toolkits.eclipse.amazonq.commands.rejectSuggestions">
</command>
<command
categoryId="software.aws.toolkits.eclipse.amazonq.commands.category"
id="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsForward"
name="Toggle Suggestions Forward">
</command>
<command
categoryId="software.aws.toolkits.eclipse.amazonq.commands.category"
id="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsBackward"
name="Toggle Suggestions Backward">
</command>
</extension>

<extension point="org.eclipse.ui.handlers">
Expand All @@ -112,6 +122,14 @@
class="software.aws.toolkits.eclipse.amazonq.handlers.QRejectSuggestionsHandler"
commandId="software.aws.toolkits.eclipse.amazonq.commands.rejectSuggestions">
</handler>
<handler
class="software.aws.toolkits.eclipse.amazonq.handlers.QToggleSuggestionsForwardHandler"
commandId="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsForward">
</handler>
<handler
class="software.aws.toolkits.eclipse.amazonq.handlers.QToggleSuggestionsBackwardHandler"
commandId="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsBackward">
</handler>
</extension>

<extension point="org.eclipse.ui.bindings">
Expand All @@ -133,6 +151,18 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="ESC">
</key>
<key
commandId="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsBackward"
contextId="org.eclipse.ui.textEditorScope"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M3+[">
</key>
<key
commandId="software.aws.toolkits.eclipse.amazonq.commands.toggleSuggestionsForward"
contextId="org.eclipse.ui.textEditorScope"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M3+]">
</key>
</extension>

<extension point="org.eclipse.ui.startup">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

package software.aws.toolkits.eclipse.amazonq.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;

import software.aws.toolkits.eclipse.amazonq.util.QInvocationSession;

public abstract class AbstractQToggleSuggestionsHandler extends AbstractHandler {
public enum Direction {
FORWARD, BACKWARD
}

private Direction direction = Direction.FORWARD;

@Override
public final boolean isEnabled() {
QInvocationSession qInvocationSessionInstance = QInvocationSession.getInstance();
return qInvocationSessionInstance != null && !qInvocationSessionInstance.hasBeenTypedahead()
&& qInvocationSessionInstance.isPreviewingSuggestions();
}

/**
* Executes the command when the user triggers the handler.
* <p>
* Subclasses overriding this method should ensure that the following conditions are met:
* </p>
* <ul>
* <li>The method should make sure to set a direction of toggle</li>
* <li>The method should make sure to call the super's counter part of this method</li>
* </ul>
*
* @param event The execution event that triggered the handler.
* @return The result of the execution, or <code>null</code> if there is no result.
*
* @implSpec
* Implementations should call {@code super.execute(event)} at the end to delegate the actual movement.
*/
@Override
public Object execute(final ExecutionEvent event) {
QInvocationSession qInvocationSessionInstance = QInvocationSession.getInstance();

switch (direction) {
case FORWARD:
qInvocationSessionInstance.incrementCurentSuggestionIndex();
break;
case BACKWARD:
qInvocationSessionInstance.decrementCurrentSuggestionIndex();
break;
default:
qInvocationSessionInstance.incrementCurentSuggestionIndex();
}

return null;
}

protected final void setCommandDirection(final Direction direction) {
this.direction = direction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

package software.aws.toolkits.eclipse.amazonq.handlers;

import org.eclipse.core.commands.ExecutionEvent;

public class QToggleSuggestionsBackwardHandler extends AbstractQToggleSuggestionsHandler {
// Actual command handler logic consolidated in parent class
@Override
public final Object execute(final ExecutionEvent event) {
super.setCommandDirection(Direction.BACKWARD);

return super.execute(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

package software.aws.toolkits.eclipse.amazonq.handlers;

import org.eclipse.core.commands.ExecutionEvent;

public class QToggleSuggestionsForwardHandler extends AbstractQToggleSuggestionsHandler {
// Actual command handler logic consolidated in parent class
@Override
public final Object execute(final ExecutionEvent event) {
super.setCommandDirection(Direction.FORWARD);

return super.execute(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void verifyKey(final VerifyEvent event) {
// Here we conduct typeahead logic
String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim();
int currentOffset = widget.getCaretOffset();
qInvocationSessionInstance
.setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0);
IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.jdt.ui");
// This needs to be defaulted to true. This key is only present in the
// preference store if it is set to false.
Expand Down Expand Up @@ -162,7 +164,6 @@ public void verifyKey(final VerifyEvent event) {
break;
}
}

leadingWhitespaceSkipped += newWs;
qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class QInvocationSession extends QResource {
private Stack<Character> closingBrackets = new Stack<>();
private boolean isLastKeyNewLine = false;
private int[] headOffsetAtLine = new int[500];
private boolean hasBeenTypedahead = false;

// Private constructor to prevent instantiation
private QInvocationSession() {
Expand Down Expand Up @@ -307,6 +308,28 @@ public String getCurrentSuggestion() {
return details.get(index).getSuggestion();
}

public void decrementCurrentSuggestionIndex() {
if (suggestionsContext != null) {
suggestionsContext.decrementIndex();
getViewer().getTextWidget().redraw();
}
}

public void incrementCurentSuggestionIndex() {
if (suggestionsContext != null) {
suggestionsContext.incrementIndex();
getViewer().getTextWidget().redraw();
}
}

public void setHasBeenTypedahead(final boolean hasBeenTypedahead) {
this.hasBeenTypedahead = hasBeenTypedahead;
}

public boolean hasBeenTypedahead() {
return hasBeenTypedahead;
}

// Additional methods for the session can be added here
@Override
public void dispose() {
Expand All @@ -319,6 +342,7 @@ public void dispose() {
leadingWhitespaceSkipped = 0;
isLastKeyNewLine = false;
caretMovementReason = CaretMovementReason.UNEXAMINED;
hasBeenTypedahead = false;
QInvocationSession.getInstance().getViewer().getTextWidget().redraw();
widget.removePaintListener(paintListener);
widget.removeCaretListener(caretListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,16 @@ public final void setCurrentIndex(final int index) {
this.currentIndex = index;
}

public final void incrementIndex() {
currentIndex = (currentIndex + 1) % details.size();
}

public final void decrementIndex() {
if (currentIndex - 1 < 0) {
currentIndex = details.size() - 1;
} else {
currentIndex--;
}
}
}

0 comments on commit c66a378

Please sign in to comment.