From 3193b683c258e972cf256283bdfe3586bf6f265e Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Mon, 23 Sep 2024 13:30:35 -0700 Subject: [PATCH 01/12] Separates key listeners from key verify listeners --- .../util/QInlineVerifyKeyListener.java | 173 ---------------- .../amazonq/util/QInlineVerifyListener.java | 188 ++++++++++++++++++ .../amazonq/util/QInvocationSession.java | 17 +- 3 files changed, 197 insertions(+), 181 deletions(-) delete mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java deleted file mode 100644 index 5f5484e8..00000000 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. - -package software.aws.toolkits.eclipse.amazonq.util; - -import java.util.Stack; - -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.custom.VerifyKeyListener; -import org.eclipse.swt.events.VerifyEvent; - -public final class QInlineVerifyKeyListener implements VerifyKeyListener { - private StyledText widget = null; - - public QInlineVerifyKeyListener(final StyledText widget) { - this.widget = widget; - } - - @Override - public void verifyKey(final VerifyEvent event) { - var qInvocationSessionInstance = QInvocationSession.getInstance(); - if (qInvocationSessionInstance == null || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { - return; - } - // We need to provide the reason for the caret movement. This way we can perform - // subsequent actions accordingly: - // - If the caret has been moved due to traversals (i.e. arrow keys or mouse - // click) we would want to end the invocation session since that signifies the - // user no longer has the intent for text input at its original location. - // - And if the caret has been moved due to typing, we will need to determine if - // it's appropriate to perform a "typeahead". - // - // In effect, we would need to fulfill the following responsibilities. - // - We identify whether text is being entered and update a state that - // accessible by other listeners. - // - We shall also examine said text to see if it matches the beginning of the - // suggestion (if there is one) so we can fulfill "typeahead" functionality. - if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT || event.keyCode == SWT.ARROW_RIGHT) { - System.out.println("Movement key registered"); - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); - } else { - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); - - // 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. - // Therefore if you can't find it, it has been set to true. - boolean isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); - - // We are deliberately leaving out curly braces here (i.e. '{') because eclipse - // does not auto close curly braces unless there is a new line entered. - boolean isInputClosingBracket = (event.character == ')' || event.character == ']' || event.character == '"' || event.character == '>'); - boolean isInputOpeningBracket = (event.character == '(' || event.character == '[' || event.character == '"' || event.character == '<'); - Stack closingBrackets = qInvocationSessionInstance.getClosingBrackets(); - - if (isAutoClosingEnabled) { - if (closingBrackets == null) { - closingBrackets = new Stack<>(); - } - if (event.character == '(') { - closingBrackets.push(')'); - } else if (event.character == '[') { - closingBrackets.push(']'); - } else if (event.character == '"') { - closingBrackets.push('"'); - } else if (event.character == '<') { - closingBrackets.push('>'); - } else if (isInputClosingBracket) { - Character topClosingBracket = closingBrackets.pop(); - if (!topClosingBracket.equals(event.character)) { - System.out.println("Session terminated due to mismatching closing bracket"); - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - } - } - } - - int distanceAdjustedForAutoClosingBrackets = (isAutoClosingEnabled && (isInputOpeningBracket || isInputClosingBracket)) ? 1 : 0; - // Note that distanceTraversed here is the zero-based index - // We need to subtract from leading whitespace skipped the indent the editor had - // placed the caret after at the new line. - int newLineOffset = 0; - boolean isLastKeyNewLine = qInvocationSessionInstance.isLastKeyNewLine(); - int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); - int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); - if (isLastKeyNewLine) { - int currentLineInEditor = widget.getLineAtOffset(currentOffset); - newLineOffset = currentOffset - widget.getOffsetAtLine(currentLineInEditor); - leadingWhitespaceSkipped -= newLineOffset; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - qInvocationSessionInstance.setIsLastKeyNewLine(false); - } - int distanceTraversed = currentOffset - invocationOffset + leadingWhitespaceSkipped - distanceAdjustedForAutoClosingBrackets; - - // If we are traversing backwards, we need to undo the adjustments we had done - // for the following items as we come across them: - // - whitespace - // - brackets (?) - if (event.keyCode == SWT.BS && distanceTraversed > 0 - && currentOffset - 1 <= qInvocationSessionInstance.getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset)) - ) { - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); - return; - } -// System.out.println("Distance traversed: " + distanceTraversed); -// System.out.println("Leading whitespace skipped: " + leadingWhitespaceSkipped); -// System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); -// System.out.println("Character typed: " + event.character); -// System.out.println("Current caret offset: " + currentOffset); -// System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); - - // Terminate the session under the right conditions. These are: - // - If what has been typed does not match what has been suggested (we are going - // to assume the user does not want the suggestion) - // We are excluding modifier keys that do not produce text on the screen (e.g. - // shift, ctrl, option). - // - If the caret position has exceeded the invocation offset leftwards. - // - If the caret position has exceeded that of the end of the suggestion. - // - If the user has typehead to the end of the suggest (we would not just - // terminate the - if (event.character == '\0' || event.keyCode == SWT.ESC) { - // We have ESC mapped to reject command. We'll let the command take care of it. - return; - } - if (distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - - char currentCharInSuggestion = currentSuggestion.charAt(distanceTraversed); - if ((distanceTraversed <= 0 && event.keyCode == SWT.BS) || distanceTraversed > currentSuggestion.length() - || ((event.keyCode != SWT.BS && event.keyCode != SWT.CR) && currentCharInSuggestion != event.character) - || (event.keyCode == SWT.CR && (currentCharInSuggestion != '\n' && currentCharInSuggestion != '\r'))) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - } - - if (event.keyCode == SWT.CR) { - qInvocationSessionInstance.setIsLastKeyNewLine(true); - } - - // We would also need to consider scenarios where the suggestion contains - // formatting whitespace. - // Should we come across them, we would need to do the following: - // - Examine if the last key registered was a CR, if it isn't we treat it as - // normal and examine the input verbatim - // - Otherwise, we shall skip ahead until the first non-whitespace character in - // the suggestion and increment `leadingWhitespaceSkipped` accordingly. - if (event.keyCode == SWT.CR && distanceTraversed < currentSuggestion.length() - 1 - && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) - && currentSuggestion.charAt(distanceTraversed + 1) != '\n' && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { - int newWs = 0; - while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { - newWs++; - if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { - break; - } - } - leadingWhitespaceSkipped += newWs; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - } - } - } -} - diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java new file mode 100644 index 00000000..a28712c2 --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java @@ -0,0 +1,188 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +package software.aws.toolkits.eclipse.amazonq.util; + +import java.util.Stack; + +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.custom.VerifyKeyListener; +import org.eclipse.swt.events.VerifyEvent; +import org.eclipse.swt.events.VerifyListener; + +public final class QInlineVerifyListener implements VerifyListener, VerifyKeyListener { + private StyledText widget = null; + + public QInlineVerifyListener(final StyledText widget) { + this.widget = widget; + } + + @Override + public void verifyKey(final VerifyEvent event) { + var qInvocationSessionInstance = QInvocationSession.getInstance(); + if (qInvocationSessionInstance == null + || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { + return; + } + + // We need to provide the reason for the caret movement. This way we can perform + // subsequent actions accordingly: + // - If the caret has been moved due to traversals (i.e. arrow keys or mouse + // click) we would want to end the invocation session since that signifies the + // user no longer has the intent for text input at its original location. + // - And if the caret has been moved due to typing, we will need to determine if + // it's appropriate to perform a "typeahead". + if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT + || event.keyCode == SWT.ARROW_RIGHT) { + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); + } else { + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); + } + } + + @Override + public void verifyText(final VerifyEvent event) { + var qInvocationSessionInstance = QInvocationSession.getInstance(); + if (qInvocationSessionInstance == null + || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { + return; + } + + // In effect, we would need to fulfill the following responsibilities. + // - We identify whether text is being entered and update a state that + // accessible by other listeners. + // - We shall also examine said text to see if it matches the beginning of the + // suggestion (if there is one) so we can fulfill "typeahead" functionality. + // 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. + // Therefore if you can't find it, it has been set to true. + boolean isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); + + // We are deliberately leaving out curly braces here (i.e. '{') because eclipse + // does not auto close curly braces unless there is a new line entered. + boolean isInputClosingBracket = (event.text == ")" || event.text == "]" || event.text == "\"" + || event.text == ">"); + boolean isInputOpeningBracket = (event.text == "(" || event.text == "[" || event.text == "\"" + || event.text == "<"); + Stack closingBrackets = qInvocationSessionInstance.getClosingBrackets(); + + if (isAutoClosingEnabled) { + if (closingBrackets == null) { + closingBrackets = new Stack<>(); + } + if (event.text == "(") { + closingBrackets.push(")"); + } else if (event.text == "[") { + closingBrackets.push("]"); + } else if (event.text == "\"") { + closingBrackets.push("\""); + } else if (event.text == "<") { + closingBrackets.push(">"); + } else if (isInputClosingBracket) { + String topClosingBracket = closingBrackets.pop(); + if (!topClosingBracket.equals(event.text)) { + System.out.println("Session terminated due to mismatching closing bracket"); + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + } + } + } + + int distanceAdjustedForAutoClosingBrackets = (isAutoClosingEnabled + && (isInputOpeningBracket || isInputClosingBracket)) ? 1 : 0; + // Note that distanceTraversed here is the zero-based index + // We need to subtract from leading whitespace skipped the indent the editor had + // placed the caret after at the new line. + int newLineOffset = 0; + boolean isLastKeyNewLine = qInvocationSessionInstance.isLastKeyNewLine(); + int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); + int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); + if (isLastKeyNewLine) { + int currentLineInEditor = widget.getLineAtOffset(currentOffset); + newLineOffset = currentOffset - widget.getOffsetAtLine(currentLineInEditor); + leadingWhitespaceSkipped -= newLineOffset; + qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); + qInvocationSessionInstance.setIsLastKeyNewLine(false); + } + int distanceTraversed = currentOffset - invocationOffset + leadingWhitespaceSkipped + - distanceAdjustedForAutoClosingBrackets; + + // If we are traversing backwards, we need to undo the adjustments we had done + // for the following items as we come across them: + // - whitespace + // - brackets (?) + if (event.keyCode == SWT.BS && distanceTraversed > 0 && currentOffset - 1 <= qInvocationSessionInstance + .getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset))) { + qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); + return; + } + System.out.println("Distance traversed: " + distanceTraversed); + System.out.println("Leading whitespace skipped: " + leadingWhitespaceSkipped); + System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); + System.out.println("text typed: " + event.text); + System.out.println("Current caret offset: " + currentOffset); + System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); + + // Terminate the session under the right conditions. These are: + // - If what has been typed does not match what has been suggested (we are going + // to assume the user does not want the suggestion) + // We are excluding modifier keys that do not produce text on the screen (e.g. + // shift, ctrl, option). + // - If the caret position has exceeded the invocation offset leftwards. + // - If the caret position has exceeded that of the end of the suggestion. + // - If the user has typehead to the end of the suggest (we would not just + // terminate the + if (event.text == "\0" || event.keyCode == SWT.ESC) { + // We have ESC mapped to reject command. We'll let the command take care of it. + return; + } + if (distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0) { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + + String currentCharInSuggestion = String.valueOf(currentSuggestion.charAt(distanceTraversed)); + System.out.println("Current char in suggestion: " + currentCharInSuggestion); + if ((distanceTraversed <= 0 && event.keyCode == SWT.BS) || distanceTraversed > currentSuggestion.length() + || ((event.keyCode != SWT.BS && event.keyCode != SWT.CR) && !currentCharInSuggestion.equals(event.text)) + || (event.keyCode == SWT.CR && (currentCharInSuggestion != "\n" && currentCharInSuggestion != "\r"))) { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + } + + if (event.keyCode == SWT.CR) { + qInvocationSessionInstance.setIsLastKeyNewLine(true); + } + + // We would also need to consider scenarios where the suggestion contains + // formatting whitespace. + // Should we come across them, we would need to do the following: + // - Examine if the last key registered was a CR, if it isn't we treat it as + // normal and examine the input verbatim + // - Otherwise, we shall skip ahead until the first non-whitespace text in + // the suggestion and increment `leadingWhitespaceSkipped` accordingly. + if (event.keyCode == SWT.CR && distanceTraversed < currentSuggestion.length() - 1 + && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) + && currentSuggestion.charAt(distanceTraversed + 1) != '\n' + && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { + int newWs = 0; + while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { + newWs++; + if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { + break; + } + } + leadingWhitespaceSkipped += newWs; + qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); + } + } +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java index 6f7fbec8..b4aa1fee 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java @@ -8,7 +8,6 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CaretListener; import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.widgets.Display; @@ -41,9 +40,9 @@ public final class QInvocationSession extends QResource { private long invocationTimeInMs = -1L; private QInlineRendererListener paintListener = null; private CaretListener caretListener = null; - private VerifyKeyListener verifyKeyListener = null; + private QInlineVerifyListener inputListener = null; private int leadingWhitespaceSkipped = 0; - private Stack closingBrackets = new Stack<>(); + private Stack closingBrackets = new Stack<>(); private boolean isLastKeyNewLine = false; private int[] headOffsetAtLine = new int[500]; private boolean hasBeenTypedahead = false; @@ -94,8 +93,9 @@ public synchronized boolean start(final ITextEditor editor) { widget.addPaintListener(paintListener); } - verifyKeyListener = new QInlineVerifyKeyListener(widget); - widget.addVerifyKeyListener(verifyKeyListener); + inputListener = new QInlineVerifyListener(widget); + widget.addVerifyListener(inputListener); + widget.addVerifyKeyListener(inputListener); caretListener = new QInlineCaretListener(widget); widget.addCaretListener(caretListener); @@ -268,7 +268,7 @@ public CaretMovementReason getCaretMovementReason() { return caretMovementReason; } - public Stack getClosingBrackets() { + public Stack getClosingBrackets() { return closingBrackets; } @@ -360,10 +360,11 @@ public void dispose() { QInvocationSession.getInstance().getViewer().getTextWidget().redraw(); widget.removePaintListener(paintListener); widget.removeCaretListener(caretListener); - widget.removeVerifyKeyListener(verifyKeyListener); + widget.removeVerifyListener(inputListener); + widget.removeVerifyKeyListener(inputListener); paintListener = null; caretListener = null; - verifyKeyListener = null; + inputListener = null; invocationOffset = -1; invocationTimeInMs = -1L; editor = null; From 3156b3f8feb72f9c6dac753a84b77ecf1e76a437 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Mon, 23 Sep 2024 15:41:51 -0700 Subject: [PATCH 02/12] Simplifies key listeners --- ...istener.java => QInlineInputListener.java} | 151 ++++++++++-------- .../amazonq/util/QInvocationSession.java | 4 +- 2 files changed, 89 insertions(+), 66 deletions(-) rename plugin/src/software/aws/toolkits/eclipse/amazonq/util/{QInlineVerifyListener.java => QInlineInputListener.java} (60%) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java similarity index 60% rename from plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java rename to plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index a28712c2..dfcab7a5 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -12,10 +12,12 @@ import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; -public final class QInlineVerifyListener implements VerifyListener, VerifyKeyListener { +public final class QInlineInputListener implements VerifyListener, VerifyKeyListener { private StyledText widget = null; + private int distanceTraversed = 0; + private boolean isLastKeyBackspace = false; - public QInlineVerifyListener(final StyledText widget) { + public QInlineInputListener(final StyledText widget) { this.widget = widget; } @@ -32,18 +34,86 @@ public void verifyKey(final VerifyEvent event) { // - If the caret has been moved due to traversals (i.e. arrow keys or mouse // click) we would want to end the invocation session since that signifies the // user no longer has the intent for text input at its original location. - // - And if the caret has been moved due to typing, we will need to determine if - // it's appropriate to perform a "typeahead". if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT || event.keyCode == SWT.ARROW_RIGHT) { qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); + return; } else { qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); } + + // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: + // - CR (new line) + // - BS (backspace) + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); + switch (event.keyCode) { + case SWT.CR: + distanceTraversed++; + char currentCharInSuggestion = currentSuggestion.charAt(distanceTraversed); + if (currentCharInSuggestion != '\n' && currentCharInSuggestion != '\r') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + qInvocationSessionInstance.setIsLastKeyNewLine(true); + // We would also need to consider scenarios where the suggestion contains + // formatting whitespace. + // Should we come across them, we would need to do the following: + // - Examine if the last key registered was a CR, if it isn't we treat it as + // normal and examine the input verbatim + // - Otherwise, we shall skip ahead until the first non-whitespace text in + // the suggestion and increment `leadingWhitespaceSkipped` accordingly. + if (distanceTraversed < currentSuggestion.length() - 1 + && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) + && currentSuggestion.charAt(distanceTraversed + 1) != '\n' + && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { + int newWs = 0; + while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { + newWs++; + if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { + break; + } + } + int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); + leadingWhitespaceSkipped += newWs; + qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); + } + break; + case SWT.BS: + // If we are traversing backwards, we need to undo the adjustments we had done + // for the following items as we come across them: + // - whitespace + // - brackets (?) + distanceTraversed--; + if (distanceTraversed < -1) { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + isLastKeyBackspace = true; + int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); + int currentOffset = widget.getCaretOffset(); + if (currentOffset - 1 <= qInvocationSessionInstance + .getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset))) { + qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); + return; + } + break; + case SWT.ESC: + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + break; + default: + } } @Override public void verifyText(final VerifyEvent event) { + if (isLastKeyBackspace) { + isLastKeyBackspace = false; + return; + } + var qInvocationSessionInstance = QInvocationSession.getInstance(); if (qInvocationSessionInstance == null || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { @@ -112,77 +182,30 @@ public void verifyText(final VerifyEvent event) { qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); qInvocationSessionInstance.setIsLastKeyNewLine(false); } - int distanceTraversed = currentOffset - invocationOffset + leadingWhitespaceSkipped + int distanceTraversed = currentOffset - invocationOffset - distanceAdjustedForAutoClosingBrackets; + this.distanceTraversed = distanceTraversed; - // If we are traversing backwards, we need to undo the adjustments we had done - // for the following items as we come across them: - // - whitespace - // - brackets (?) - if (event.keyCode == SWT.BS && distanceTraversed > 0 && currentOffset - 1 <= qInvocationSessionInstance - .getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset))) { - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); - return; - } - System.out.println("Distance traversed: " + distanceTraversed); + System.out.println("=========================\nDistance traversed: " + distanceTraversed); System.out.println("Leading whitespace skipped: " + leadingWhitespaceSkipped); System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); System.out.println("text typed: " + event.text); + System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); System.out.println("Current caret offset: " + currentOffset); System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); - // Terminate the session under the right conditions. These are: - // - If what has been typed does not match what has been suggested (we are going - // to assume the user does not want the suggestion) - // We are excluding modifier keys that do not produce text on the screen (e.g. - // shift, ctrl, option). - // - If the caret position has exceeded the invocation offset leftwards. - // - If the caret position has exceeded that of the end of the suggestion. - // - If the user has typehead to the end of the suggest (we would not just - // terminate the - if (event.text == "\0" || event.keyCode == SWT.ESC) { - // We have ESC mapped to reject command. We'll let the command take care of it. - return; - } - if (distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - - String currentCharInSuggestion = String.valueOf(currentSuggestion.charAt(distanceTraversed)); - System.out.println("Current char in suggestion: " + currentCharInSuggestion); - if ((distanceTraversed <= 0 && event.keyCode == SWT.BS) || distanceTraversed > currentSuggestion.length() - || ((event.keyCode != SWT.BS && event.keyCode != SWT.CR) && !currentCharInSuggestion.equals(event.text)) - || (event.keyCode == SWT.CR && (currentCharInSuggestion != "\n" && currentCharInSuggestion != "\r"))) { + boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; + if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, event.text)) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); } - - if (event.keyCode == SWT.CR) { - qInvocationSessionInstance.setIsLastKeyNewLine(true); - } - - // We would also need to consider scenarios where the suggestion contains - // formatting whitespace. - // Should we come across them, we would need to do the following: - // - Examine if the last key registered was a CR, if it isn't we treat it as - // normal and examine the input verbatim - // - Otherwise, we shall skip ahead until the first non-whitespace text in - // the suggestion and increment `leadingWhitespaceSkipped` accordingly. - if (event.keyCode == SWT.CR && distanceTraversed < currentSuggestion.length() - 1 - && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) - && currentSuggestion.charAt(distanceTraversed + 1) != '\n' - && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { - int newWs = 0; - while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { - newWs++; - if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { - break; - } - } - leadingWhitespaceSkipped += newWs; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); + } + + private boolean isInputAMatch(String currentSuggestion, int startIdx, String input) { + if (input.length() > 1) { + return currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); + } else { + return String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); } } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java index b4aa1fee..ccb38d99 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java @@ -40,7 +40,7 @@ public final class QInvocationSession extends QResource { private long invocationTimeInMs = -1L; private QInlineRendererListener paintListener = null; private CaretListener caretListener = null; - private QInlineVerifyListener inputListener = null; + private QInlineInputListener inputListener = null; private int leadingWhitespaceSkipped = 0; private Stack closingBrackets = new Stack<>(); private boolean isLastKeyNewLine = false; @@ -93,7 +93,7 @@ public synchronized boolean start(final ITextEditor editor) { widget.addPaintListener(paintListener); } - inputListener = new QInlineVerifyListener(widget); + inputListener = new QInlineInputListener(widget); widget.addVerifyListener(inputListener); widget.addVerifyKeyListener(inputListener); From 33c942cb028c31f4cad67cdc0a2083b67cc00d8f Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Tue, 24 Sep 2024 10:13:02 -0700 Subject: [PATCH 03/12] Removes logic for skipping leading whitespace --- .../amazonq/util/QInlineInputListener.java | 65 ++++--------------- .../amazonq/util/QInvocationSession.java | 20 ------ 2 files changed, 12 insertions(+), 73 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index dfcab7a5..e6214fa0 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -38,10 +38,10 @@ public void verifyKey(final VerifyEvent event) { || event.keyCode == SWT.ARROW_RIGHT) { qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); return; - } else { - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); } - + + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); + // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: // - CR (new line) // - BS (backspace) @@ -56,34 +56,8 @@ public void verifyKey(final VerifyEvent event) { return; } qInvocationSessionInstance.setIsLastKeyNewLine(true); - // We would also need to consider scenarios where the suggestion contains - // formatting whitespace. - // Should we come across them, we would need to do the following: - // - Examine if the last key registered was a CR, if it isn't we treat it as - // normal and examine the input verbatim - // - Otherwise, we shall skip ahead until the first non-whitespace text in - // the suggestion and increment `leadingWhitespaceSkipped` accordingly. - if (distanceTraversed < currentSuggestion.length() - 1 - && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) - && currentSuggestion.charAt(distanceTraversed + 1) != '\n' - && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { - int newWs = 0; - while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { - newWs++; - if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { - break; - } - } - int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); - leadingWhitespaceSkipped += newWs; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - } - break; + return; case SWT.BS: - // If we are traversing backwards, we need to undo the adjustments we had done - // for the following items as we come across them: - // - whitespace - // - brackets (?) distanceTraversed--; if (distanceTraversed < -1) { qInvocationSessionInstance.transitionToDecisionMade(); @@ -91,20 +65,19 @@ public void verifyKey(final VerifyEvent event) { return; } isLastKeyBackspace = true; - int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); - int currentOffset = widget.getCaretOffset(); - if (currentOffset - 1 <= qInvocationSessionInstance - .getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset))) { - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); - return; - } - break; + return; case SWT.ESC: qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); - break; + return; default: } + + // We also have to process inputs for open brackets. + // Open brackets are special in eclipse SWT in that they *do not* trigger a call to `verifyText`. + // Thus we would have to process them here. + + } @Override @@ -168,26 +141,12 @@ public void verifyText(final VerifyEvent event) { int distanceAdjustedForAutoClosingBrackets = (isAutoClosingEnabled && (isInputOpeningBracket || isInputClosingBracket)) ? 1 : 0; - // Note that distanceTraversed here is the zero-based index - // We need to subtract from leading whitespace skipped the indent the editor had - // placed the caret after at the new line. - int newLineOffset = 0; - boolean isLastKeyNewLine = qInvocationSessionInstance.isLastKeyNewLine(); - int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); - if (isLastKeyNewLine) { - int currentLineInEditor = widget.getLineAtOffset(currentOffset); - newLineOffset = currentOffset - widget.getOffsetAtLine(currentLineInEditor); - leadingWhitespaceSkipped -= newLineOffset; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - qInvocationSessionInstance.setIsLastKeyNewLine(false); - } int distanceTraversed = currentOffset - invocationOffset - distanceAdjustedForAutoClosingBrackets; this.distanceTraversed = distanceTraversed; System.out.println("=========================\nDistance traversed: " + distanceTraversed); - System.out.println("Leading whitespace skipped: " + leadingWhitespaceSkipped); System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); System.out.println("text typed: " + event.text); System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java index ccb38d99..eb60676f 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java @@ -41,9 +41,7 @@ public final class QInvocationSession extends QResource { private QInlineRendererListener paintListener = null; private CaretListener caretListener = null; private QInlineInputListener inputListener = null; - private int leadingWhitespaceSkipped = 0; private Stack closingBrackets = new Stack<>(); - private boolean isLastKeyNewLine = false; private int[] headOffsetAtLine = new int[500]; private boolean hasBeenTypedahead = false; private Runnable unsetVerticalIndent; @@ -229,14 +227,6 @@ public void setCaretMovementReason(final CaretMovementReason reason) { this.caretMovementReason = reason; } - public void setLeadingWhitespaceSkipped(final int numSkipped) { - this.leadingWhitespaceSkipped = numSkipped; - } - - public void setIsLastKeyNewLine(final boolean isLastKeyNewLine) { - this.isLastKeyNewLine = isLastKeyNewLine; - } - public void setHeadOffsetAtLine(final int lineNum, final int offSet) throws IllegalArgumentException { if (lineNum >= headOffsetAtLine.length || lineNum < 0) { throw new IllegalArgumentException("Problematic index given"); @@ -272,14 +262,6 @@ public Stack getClosingBrackets() { return closingBrackets; } - public int getLeadingWhitespaceSkipped() { - return leadingWhitespaceSkipped; - } - - public boolean isLastKeyNewLine() { - return isLastKeyNewLine; - } - public int getHeadOffsetAtLine(final int lineNum) throws IllegalArgumentException { if (lineNum >= headOffsetAtLine.length || lineNum < 0) { throw new IllegalArgumentException("Problematic index given"); @@ -353,8 +335,6 @@ public void dispose() { inlineTextFont.dispose(); inlineTextFont = null; closingBrackets = null; - leadingWhitespaceSkipped = 0; - isLastKeyNewLine = false; caretMovementReason = CaretMovementReason.UNEXAMINED; hasBeenTypedahead = false; QInvocationSession.getInstance().getViewer().getTextWidget().redraw(); From 71754e1ce98ab0c39e95345188750a02a65b362e Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Tue, 24 Sep 2024 12:56:01 -0700 Subject: [PATCH 04/12] Adds logic for checking open brackets --- .../amazonq/util/QInlineInputListener.java | 119 +++++++++--------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index e6214fa0..66f14624 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -16,18 +16,23 @@ public final class QInlineInputListener implements VerifyListener, VerifyKeyList private StyledText widget = null; private int distanceTraversed = 0; private boolean isLastKeyBackspace = false; + private boolean isAutoClosingEnabled = true; public QInlineInputListener(final StyledText widget) { + 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. + // Therefore if you can't find it, it has been set to true. + this.isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); this.widget = widget; } @Override public void verifyKey(final VerifyEvent event) { - var qInvocationSessionInstance = QInvocationSession.getInstance(); - if (qInvocationSessionInstance == null - || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { - return; - } + var qInvocationSessionInstance = QInvocationSession.getInstance(); + if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { + return; + } // We need to provide the reason for the caret movement. This way we can perform // subsequent actions accordingly: @@ -45,21 +50,22 @@ public void verifyKey(final VerifyEvent event) { // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: // - CR (new line) // - BS (backspace) + int distanceTraversed = widget.getCaretOffset() - qInvocationSessionInstance.getInvocationOffset(); String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); switch (event.keyCode) { case SWT.CR: - distanceTraversed++; char currentCharInSuggestion = currentSuggestion.charAt(distanceTraversed); if (currentCharInSuggestion != '\n' && currentCharInSuggestion != '\r') { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } - qInvocationSessionInstance.setIsLastKeyNewLine(true); return; case SWT.BS: - distanceTraversed--; - if (distanceTraversed < -1) { + // The distance traversed is the index at which the key was registered. + // In other words, to get the distance traversed after the fact, we need to subtract one from it. + System.out.println("Distance traversed (from verify key): " + distanceTraversed); + if (distanceTraversed - 1 < 0) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; @@ -73,11 +79,45 @@ public void verifyKey(final VerifyEvent event) { default: } - // We also have to process inputs for open brackets. - // Open brackets are special in eclipse SWT in that they *do not* trigger a call to `verifyText`. - // Thus we would have to process them here. - - + // If auto closing of brackets are not enabled we can just treat them as normal inputs + if (!isAutoClosingEnabled) { + return; + } + + // If auto cloising of brackets are enabled, SWT will treat the open bracket differently. + // Input of the open brackets will not trigger a call to verifyText. + // Thus we have to do the typeahead verification here. + switch (event.character) { + case '<': + if (currentSuggestion.charAt(distanceTraversed) != '<') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case '(': + if (currentSuggestion.charAt(distanceTraversed) != '(') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case '[': + if (currentSuggestion.charAt(distanceTraversed) != '[') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case '{': + if (currentSuggestion.charAt(distanceTraversed) != '{') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + default: + } } @Override @@ -87,11 +127,10 @@ public void verifyText(final VerifyEvent event) { return; } - var qInvocationSessionInstance = QInvocationSession.getInstance(); - if (qInvocationSessionInstance == null - || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { - return; - } + var qInvocationSessionInstance = QInvocationSession.getInstance(); + if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { + return; + } // In effect, we would need to fulfill the following responsibilities. // - We identify whether text is being entered and update a state that @@ -103,51 +142,12 @@ public void verifyText(final VerifyEvent event) { 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. - // Therefore if you can't find it, it has been set to true. - boolean isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); - // We are deliberately leaving out curly braces here (i.e. '{') because eclipse - // does not auto close curly braces unless there is a new line entered. - boolean isInputClosingBracket = (event.text == ")" || event.text == "]" || event.text == "\"" - || event.text == ">"); - boolean isInputOpeningBracket = (event.text == "(" || event.text == "[" || event.text == "\"" - || event.text == "<"); - Stack closingBrackets = qInvocationSessionInstance.getClosingBrackets(); - - if (isAutoClosingEnabled) { - if (closingBrackets == null) { - closingBrackets = new Stack<>(); - } - if (event.text == "(") { - closingBrackets.push(")"); - } else if (event.text == "[") { - closingBrackets.push("]"); - } else if (event.text == "\"") { - closingBrackets.push("\""); - } else if (event.text == "<") { - closingBrackets.push(">"); - } else if (isInputClosingBracket) { - String topClosingBracket = closingBrackets.pop(); - if (!topClosingBracket.equals(event.text)) { - System.out.println("Session terminated due to mismatching closing bracket"); - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - } - } - } - - int distanceAdjustedForAutoClosingBrackets = (isAutoClosingEnabled - && (isInputOpeningBracket || isInputClosingBracket)) ? 1 : 0; int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); - int distanceTraversed = currentOffset - invocationOffset - - distanceAdjustedForAutoClosingBrackets; + int distanceTraversed = currentOffset - invocationOffset; this.distanceTraversed = distanceTraversed; System.out.println("=========================\nDistance traversed: " + distanceTraversed); - System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); System.out.println("text typed: " + event.text); System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); System.out.println("Current caret offset: " + currentOffset); @@ -157,6 +157,7 @@ public void verifyText(final VerifyEvent event) { if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, event.text)) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); + return; } } From bca0631640e33320eb815d14540c5d8df528de92 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Tue, 24 Sep 2024 13:37:05 -0700 Subject: [PATCH 05/12] Adds logic for checking close brackets --- .../amazonq/util/QInlineInputListener.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index 66f14624..14be1164 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -50,22 +50,10 @@ public void verifyKey(final VerifyEvent event) { // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: // - CR (new line) // - BS (backspace) - int distanceTraversed = widget.getCaretOffset() - qInvocationSessionInstance.getInvocationOffset(); String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); switch (event.keyCode) { - case SWT.CR: - char currentCharInSuggestion = currentSuggestion.charAt(distanceTraversed); - if (currentCharInSuggestion != '\n' && currentCharInSuggestion != '\r') { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - return; case SWT.BS: - // The distance traversed is the index at which the key was registered. - // In other words, to get the distance traversed after the fact, we need to subtract one from it. - System.out.println("Distance traversed (from verify key): " + distanceTraversed); - if (distanceTraversed - 1 < 0) { + if (--distanceTraversed < 0) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; @@ -85,32 +73,68 @@ public void verifyKey(final VerifyEvent event) { } // If auto cloising of brackets are enabled, SWT will treat the open bracket differently. - // Input of the open brackets will not trigger a call to verifyText. + // Input of the brackets will not trigger a call to verifyText. // Thus we have to do the typeahead verification here. switch (event.character) { case '<': - if (currentSuggestion.charAt(distanceTraversed) != '<') { + if (currentSuggestion.charAt(distanceTraversed++) != '<') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case '>': + if (currentSuggestion.charAt(distanceTraversed++) != '>') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } return; case '(': - if (currentSuggestion.charAt(distanceTraversed) != '(') { + if (currentSuggestion.charAt(distanceTraversed++) != '(') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case ')': + if (currentSuggestion.charAt(distanceTraversed++) != ')') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } return; case '[': - if (currentSuggestion.charAt(distanceTraversed) != '[') { + if (currentSuggestion.charAt(distanceTraversed++) != '[') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case ']': + if (currentSuggestion.charAt(distanceTraversed++) != ']') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } return; case '{': - if (currentSuggestion.charAt(distanceTraversed) != '{') { + if (currentSuggestion.charAt(distanceTraversed++) != '{') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + return; + case '}': + if (currentSuggestion.charAt(distanceTraversed++) != '}') { + System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; @@ -143,10 +167,6 @@ public void verifyText(final VerifyEvent event) { qInvocationSessionInstance .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); - int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); - int distanceTraversed = currentOffset - invocationOffset; - this.distanceTraversed = distanceTraversed; - System.out.println("=========================\nDistance traversed: " + distanceTraversed); System.out.println("text typed: " + event.text); System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); @@ -162,10 +182,13 @@ public void verifyText(final VerifyEvent event) { } private boolean isInputAMatch(String currentSuggestion, int startIdx, String input) { + boolean res; if (input.length() > 1) { - return currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); + res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); } else { - return String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); + res = String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); } + distanceTraversed += input.length(); + return res; } } From 7ccceda3b18ef348210baaf83551290005986aa1 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Tue, 24 Sep 2024 15:12:31 -0700 Subject: [PATCH 06/12] Loosens caret movement check conditions --- .../toolkits/eclipse/amazonq/util/QInlineCaretListener.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineCaretListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineCaretListener.java index 3646ef01..e9eb6c61 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineCaretListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineCaretListener.java @@ -24,11 +24,7 @@ public void caretMoved(final CaretEvent event) { return; } - // There are instances where the caret movement was induced by sources other than user input - // Under these instances, it is observed that the caret would revert back to a position that was last - // placed by the user in the same rendering cycle. - // We want to preserve the preview state and prevent it from terminating by these non-user instructed movements - if (event.caretOffset != widget.getCaretOffset() && qInvocationSessionInstance.isPreviewingSuggestions()) { + if (qInvocationSessionInstance.isPreviewingSuggestions()) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); } From c4d3ad0c8333376eb7399fcc7634b4feec56bb9c Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Wed, 25 Sep 2024 12:21:42 -0700 Subject: [PATCH 07/12] Refines typeahead logic with regards to auto closing brackets --- .../amazonq/util/QInlineInputListener.java | 72 ++++++++++++++----- .../amazonq/util/QInlineRendererListener.java | 2 +- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index 14be1164..4d59f22a 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -2,8 +2,6 @@ package software.aws.toolkits.eclipse.amazonq.util; -import java.util.Stack; - import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.swt.SWT; @@ -15,8 +13,17 @@ public final class QInlineInputListener implements VerifyListener, VerifyKeyListener { private StyledText widget = null; private int distanceTraversed = 0; - private boolean isLastKeyBackspace = false; private boolean isAutoClosingEnabled = true; + private LastKeyStrokeType lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; + + private enum LastKeyStrokeType { + NORMAL_INPUT, + BACKSPACE, + NORMAL_BRACKET, + CURLY_BRACES, + OPEN_CURLY, + OPEN_CURLY_FOLLOWED_BY_NEW_LINE, + } public QInlineInputListener(final StyledText widget) { IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.jdt.ui"); @@ -52,13 +59,20 @@ public void verifyKey(final VerifyEvent event) { // - BS (backspace) String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); switch (event.keyCode) { + case SWT.CR: + if (lastKeyStrokeType == LastKeyStrokeType.OPEN_CURLY && isAutoClosingEnabled) { + lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY_FOLLOWED_BY_NEW_LINE; + } else { + lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; + } + return; case SWT.BS: if (--distanceTraversed < 0) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } - isLastKeyBackspace = true; + lastKeyStrokeType = LastKeyStrokeType.BACKSPACE; return; case SWT.ESC: qInvocationSessionInstance.transitionToDecisionMade(); @@ -68,6 +82,7 @@ public void verifyKey(final VerifyEvent event) { } // If auto closing of brackets are not enabled we can just treat them as normal inputs + // Another scenario if (!isAutoClosingEnabled) { return; } @@ -75,79 +90,95 @@ public void verifyKey(final VerifyEvent event) { // If auto cloising of brackets are enabled, SWT will treat the open bracket differently. // Input of the brackets will not trigger a call to verifyText. // Thus we have to do the typeahead verification here. + // Note that '{' is excluded because switch (event.character) { case '<': if (currentSuggestion.charAt(distanceTraversed++) != '<') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case '>': if (currentSuggestion.charAt(distanceTraversed++) != '>') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case '(': if (currentSuggestion.charAt(distanceTraversed++) != '(') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case ')': if (currentSuggestion.charAt(distanceTraversed++) != ')') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case '[': if (currentSuggestion.charAt(distanceTraversed++) != '[') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case ']': if (currentSuggestion.charAt(distanceTraversed++) != ']') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; return; case '{': if (currentSuggestion.charAt(distanceTraversed++) != '{') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY; return; case '}': if (currentSuggestion.charAt(distanceTraversed++) != '}') { - System.out.println("bracket mismatched\ntyped: " + event.character + "\nsuggestion: " + currentSuggestion.charAt(distanceTraversed)); qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + lastKeyStrokeType = LastKeyStrokeType.CURLY_BRACES; return; + case '"': + if (currentSuggestion.charAt(distanceTraversed++) != '"') { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; default: } + + lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; } @Override public void verifyText(final VerifyEvent event) { - if (isLastKeyBackspace) { - isLastKeyBackspace = false; + String input = event.text; + switch (lastKeyStrokeType) { + case NORMAL_INPUT: + break; + case OPEN_CURLY_FOLLOWED_BY_NEW_LINE: + input = '\n' + event.text.split("\\R")[1]; + break; + default: return; } @@ -169,26 +200,31 @@ public void verifyText(final VerifyEvent event) { System.out.println("=========================\nDistance traversed: " + distanceTraversed); System.out.println("text typed: " + event.text); - System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); System.out.println("Current caret offset: " + currentOffset); System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; - if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, event.text)) { + if (!isOutOfBounds) { + System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); + } + if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, input)) { qInvocationSessionInstance.transitionToDecisionMade(); qInvocationSessionInstance.end(); return; } + distanceTraversed += input.length(); } - + private boolean isInputAMatch(String currentSuggestion, int startIdx, String input) { boolean res; if (input.length() > 1) { + System.out.println("Suggestion: " + currentSuggestion.substring(startIdx, startIdx + input.length()).replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")); + System.out.println("Adjusted input: " + input.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")); res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); + System.out.println("This is a match: " + res); } else { res = String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); } - distanceTraversed += input.length(); return res; } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java index 05b3534a..bfe9090a 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java @@ -76,7 +76,7 @@ public final void paintControl(final PaintEvent e) { int lineHt = widget.getLineHeight(); int fontHt = gc.getFontMetrics().getHeight(); int x = widget.getLeftMargin(); - int y = location.y + lineHt * 2 - fontHt; + int y = location.y + lineHt * 2 - fontHt; gc.drawText(remainder, x, y, true); } else { qInvocationSessionInstance.unsetVerticalIndent(); From f5e4ea88525d279022ff2c05c158aa0d2831e853 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Wed, 25 Sep 2024 15:03:41 -0700 Subject: [PATCH 08/12] Unsets vertical indent on new line inserted by eclipse --- .../toolkits/eclipse/amazonq/util/QInlineInputListener.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index 4d59f22a..cd685899 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -62,6 +62,9 @@ public void verifyKey(final VerifyEvent event) { case SWT.CR: if (lastKeyStrokeType == LastKeyStrokeType.OPEN_CURLY && isAutoClosingEnabled) { lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY_FOLLOWED_BY_NEW_LINE; + // we need to unset the vertical indent prior to new line otherwise the line inserted by + // eclipse with the closing curly braces would inherit the extra vertical indent. + qInvocationSessionInstance.unsetVerticalIndent(); } else { lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; } From dc518ac3027fa84e1002493d466cba19f22a7318 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Wed, 25 Sep 2024 15:08:19 -0700 Subject: [PATCH 09/12] Comments --- .../amazonq/util/QInlineInputListener.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index cd685899..a59c98c2 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -190,21 +190,15 @@ public void verifyText(final VerifyEvent event) { return; } - // In effect, we would need to fulfill the following responsibilities. - // - We identify whether text is being entered and update a state that - // accessible by other listeners. - // - We shall also examine said text to see if it matches the beginning of the - // suggestion (if there is one) so we can fulfill "typeahead" functionality. - // Here we conduct typeahead logic String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); int currentOffset = widget.getCaretOffset(); qInvocationSessionInstance .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); - System.out.println("=========================\nDistance traversed: " + distanceTraversed); - System.out.println("text typed: " + event.text); - System.out.println("Current caret offset: " + currentOffset); - System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); +// System.out.println("=========================\nDistance traversed: " + distanceTraversed); +// System.out.println("text typed: " + event.text); +// System.out.println("Current caret offset: " + currentOffset); +// System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; if (!isOutOfBounds) { @@ -221,8 +215,6 @@ public void verifyText(final VerifyEvent event) { private boolean isInputAMatch(String currentSuggestion, int startIdx, String input) { boolean res; if (input.length() > 1) { - System.out.println("Suggestion: " + currentSuggestion.substring(startIdx, startIdx + input.length()).replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")); - System.out.println("Adjusted input: " + input.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")); res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); System.out.println("This is a match: " + res); } else { From ae140bebdaeef77ed8c4331539f1e5f445a971af Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Wed, 25 Sep 2024 15:26:06 -0700 Subject: [PATCH 10/12] Fixes merging error --- .../amazonq/util/QInlineInputListener.java | 4 +- .../util/QInlineVerifyKeyListener.java | 173 ------------------ 2 files changed, 2 insertions(+), 175 deletions(-) delete mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index a59c98c2..1645afb5 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -57,7 +57,7 @@ public void verifyKey(final VerifyEvent event) { // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: // - CR (new line) // - BS (backspace) - String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); switch (event.keyCode) { case SWT.CR: if (lastKeyStrokeType == LastKeyStrokeType.OPEN_CURLY && isAutoClosingEnabled) { @@ -190,7 +190,7 @@ public void verifyText(final VerifyEvent event) { return; } - String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().trim(); + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); int currentOffset = widget.getCaretOffset(); qInvocationSessionInstance .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java deleted file mode 100644 index 461f3d31..00000000 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineVerifyKeyListener.java +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. - -package software.aws.toolkits.eclipse.amazonq.util; - -import java.util.Stack; - -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.StyledText; -import org.eclipse.swt.custom.VerifyKeyListener; -import org.eclipse.swt.events.VerifyEvent; - -public final class QInlineVerifyKeyListener implements VerifyKeyListener { - private StyledText widget = null; - - public QInlineVerifyKeyListener(final StyledText widget) { - this.widget = widget; - } - - @Override - public void verifyKey(final VerifyEvent event) { - var qInvocationSessionInstance = QInvocationSession.getInstance(); - if (qInvocationSessionInstance == null || qInvocationSessionInstance.getState() != QInvocationSessionState.SUGGESTION_PREVIEWING) { - return; - } - // We need to provide the reason for the caret movement. This way we can perform - // subsequent actions accordingly: - // - If the caret has been moved due to traversals (i.e. arrow keys or mouse - // click) we would want to end the invocation session since that signifies the - // user no longer has the intent for text input at its original location. - // - And if the caret has been moved due to typing, we will need to determine if - // it's appropriate to perform a "typeahead". - // - // In effect, we would need to fulfill the following responsibilities. - // - We identify whether text is being entered and update a state that - // accessible by other listeners. - // - We shall also examine said text to see if it matches the beginning of the - // suggestion (if there is one) so we can fulfill "typeahead" functionality. - if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT || event.keyCode == SWT.ARROW_RIGHT) { - System.out.println("Movement key registered"); - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); - } else { - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); - - // Here we conduct typeahead logic - String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().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. - // Therefore if you can't find it, it has been set to true. - boolean isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); - - // We are deliberately leaving out curly braces here (i.e. '{') because eclipse - // does not auto close curly braces unless there is a new line entered. - boolean isInputClosingBracket = (event.character == ')' || event.character == ']' || event.character == '"' || event.character == '>'); - boolean isInputOpeningBracket = (event.character == '(' || event.character == '[' || event.character == '"' || event.character == '<'); - Stack closingBrackets = qInvocationSessionInstance.getClosingBrackets(); - - if (isAutoClosingEnabled) { - if (closingBrackets == null) { - closingBrackets = new Stack<>(); - } - if (event.character == '(') { - closingBrackets.push(')'); - } else if (event.character == '[') { - closingBrackets.push(']'); - } else if (event.character == '"') { - closingBrackets.push('"'); - } else if (event.character == '<') { - closingBrackets.push('>'); - } else if (isInputClosingBracket) { - Character topClosingBracket = closingBrackets.pop(); - if (!topClosingBracket.equals(event.character)) { - System.out.println("Session terminated due to mismatching closing bracket"); - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - } - } - } - - int distanceAdjustedForAutoClosingBrackets = (isAutoClosingEnabled && (isInputOpeningBracket || isInputClosingBracket)) ? 1 : 0; - // Note that distanceTraversed here is the zero-based index - // We need to subtract from leading whitespace skipped the indent the editor had - // placed the caret after at the new line. - int newLineOffset = 0; - boolean isLastKeyNewLine = qInvocationSessionInstance.isLastKeyNewLine(); - int leadingWhitespaceSkipped = qInvocationSessionInstance.getLeadingWhitespaceSkipped(); - int invocationOffset = qInvocationSessionInstance.getInvocationOffset(); - if (isLastKeyNewLine) { - int currentLineInEditor = widget.getLineAtOffset(currentOffset); - newLineOffset = currentOffset - widget.getOffsetAtLine(currentLineInEditor); - leadingWhitespaceSkipped -= newLineOffset; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - qInvocationSessionInstance.setIsLastKeyNewLine(false); - } - int distanceTraversed = currentOffset - invocationOffset + leadingWhitespaceSkipped - distanceAdjustedForAutoClosingBrackets; - - // If we are traversing backwards, we need to undo the adjustments we had done - // for the following items as we come across them: - // - whitespace - // - brackets (?) - if (event.keyCode == SWT.BS && distanceTraversed > 0 - && currentOffset - 1 <= qInvocationSessionInstance.getHeadOffsetAtLine(widget.getLineAtOffset(currentOffset)) - ) { - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped - 1); - return; - } -// System.out.println("Distance traversed: " + distanceTraversed); -// System.out.println("Leading whitespace skipped: " + leadingWhitespaceSkipped); -// System.out.println("Distance adjusted for auto closing brackets: " + distanceAdjustedForAutoClosingBrackets); -// System.out.println("Character typed: " + event.character); -// System.out.println("Current caret offset: " + currentOffset); -// System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); - - // Terminate the session under the right conditions. These are: - // - If what has been typed does not match what has been suggested (we are going - // to assume the user does not want the suggestion) - // We are excluding modifier keys that do not produce text on the screen (e.g. - // shift, ctrl, option). - // - If the caret position has exceeded the invocation offset leftwards. - // - If the caret position has exceeded that of the end of the suggestion. - // - If the user has typehead to the end of the suggest (we would not just - // terminate the - if (event.character == '\0' || event.keyCode == SWT.ESC) { - // We have ESC mapped to reject command. We'll let the command take care of it. - return; - } - if (distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - - char currentCharInSuggestion = currentSuggestion.charAt(distanceTraversed); - if ((distanceTraversed <= 0 && event.keyCode == SWT.BS) || distanceTraversed > currentSuggestion.length() - || ((event.keyCode != SWT.BS && event.keyCode != SWT.CR) && currentCharInSuggestion != event.character) - || (event.keyCode == SWT.CR && (currentCharInSuggestion != '\n' && currentCharInSuggestion != '\r'))) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - } - - if (event.keyCode == SWT.CR) { - qInvocationSessionInstance.setIsLastKeyNewLine(true); - } - - // We would also need to consider scenarios where the suggestion contains - // formatting whitespace. - // Should we come across them, we would need to do the following: - // - Examine if the last key registered was a CR, if it isn't we treat it as - // normal and examine the input verbatim - // - Otherwise, we shall skip ahead until the first non-whitespace character in - // the suggestion and increment `leadingWhitespaceSkipped` accordingly. - if (event.keyCode == SWT.CR && distanceTraversed < currentSuggestion.length() - 1 - && Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1)) - && currentSuggestion.charAt(distanceTraversed + 1) != '\n' && currentSuggestion.charAt(distanceTraversed + 1) != '\r') { - int newWs = 0; - while (Character.isWhitespace(currentSuggestion.charAt(distanceTraversed + 1 + newWs))) { - newWs++; - if ((distanceTraversed + 1 + newWs) > currentSuggestion.length()) { - break; - } - } - leadingWhitespaceSkipped += newWs; - qInvocationSessionInstance.setLeadingWhitespaceSkipped(leadingWhitespaceSkipped); - } - } - } -} - From ba40253a1b512fe03a3075dda58fe3622e62f4b8 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Wed, 25 Sep 2024 18:06:14 -0700 Subject: [PATCH 11/12] Moves decision transition ahead in acceptance handler to avoid null pointer exception in caret movement listener --- .../eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java index e49c2917..817a0063 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java @@ -23,6 +23,7 @@ public final boolean isEnabled() { public final Object execute(final ExecutionEvent event) throws ExecutionException { var suggestion = QInvocationSession.getInstance().getCurrentSuggestion(); var widget = QInvocationSession.getInstance().getViewer().getTextWidget(); + QInvocationSession.getInstance().transitionToDecisionMade(); Display display = widget.getDisplay(); display.syncExec(() -> this.insertSuggestion(suggestion.getInsertText())); return null; @@ -36,7 +37,6 @@ private void insertSuggestion(final String suggestion) { var insertOffset = widget.getCaretOffset(); doc.replace(insertOffset, 0, suggestion); widget.setCaretOffset(insertOffset + suggestion.length()); - QInvocationSession.getInstance().transitionToDecisionMade(); QInvocationSession.getInstance().getViewer().getTextWidget().redraw(); QInvocationSession.getInstance().executeCallbackForCodeReference(); QInvocationSession.getInstance().end(); From 53567154b6a8ddac2117d8c2ac9216cd08981b6e Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Thu, 26 Sep 2024 17:06:30 -0700 Subject: [PATCH 12/12] Fixes checkstyle errors --- .../amazonq/util/QInlineInputListener.java | 225 +++++++++--------- .../amazonq/util/QInlineRendererListener.java | 2 +- 2 files changed, 111 insertions(+), 116 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java index 1645afb5..9e6bb8d2 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java @@ -11,90 +11,90 @@ import org.eclipse.swt.events.VerifyListener; public final class QInlineInputListener implements VerifyListener, VerifyKeyListener { - private StyledText widget = null; - private int distanceTraversed = 0; - private boolean isAutoClosingEnabled = true; - private LastKeyStrokeType lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; - - private enum LastKeyStrokeType { - NORMAL_INPUT, - BACKSPACE, - NORMAL_BRACKET, - CURLY_BRACES, - OPEN_CURLY, - OPEN_CURLY_FOLLOWED_BY_NEW_LINE, - } - - public QInlineInputListener(final StyledText widget) { - IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.jdt.ui"); - // This needs to be defaulted to true. This key is only present in the + private StyledText widget = null; + private int distanceTraversed = 0; + private boolean isAutoClosingEnabled = true; + private LastKeyStrokeType lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; + + private enum LastKeyStrokeType { + NORMAL_INPUT, BACKSPACE, NORMAL_BRACKET, CURLY_BRACES, OPEN_CURLY, OPEN_CURLY_FOLLOWED_BY_NEW_LINE, + } + + public QInlineInputListener(final StyledText widget) { + 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. // Therefore if you can't find it, it has been set to true. - this.isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); - this.widget = widget; - } + this.isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); + this.widget = widget; + } - @Override - public void verifyKey(final VerifyEvent event) { + @Override + public void verifyKey(final VerifyEvent event) { var qInvocationSessionInstance = QInvocationSession.getInstance(); if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { return; } - // We need to provide the reason for the caret movement. This way we can perform - // subsequent actions accordingly: - // - If the caret has been moved due to traversals (i.e. arrow keys or mouse - // click) we would want to end the invocation session since that signifies the - // user no longer has the intent for text input at its original location. - if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT - || event.keyCode == SWT.ARROW_RIGHT) { - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); - return; - } - - qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); - - // Here we examine all other relevant keystrokes that may be relevant to the preview's lifetime: - // - CR (new line) - // - BS (backspace) - String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); - switch (event.keyCode) { - case SWT.CR: + // We need to provide the reason for the caret movement. This way we can perform + // subsequent actions accordingly: + // - If the caret has been moved due to traversals (i.e. arrow keys or mouse + // click) we would want to end the invocation session since that signifies the + // user no longer has the intent for text input at its original location. + if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT + || event.keyCode == SWT.ARROW_RIGHT) { + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); + return; + } + + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); + + // Here we examine all other relevant keystrokes that may be relevant to the + // preview's lifetime: + // - CR (new line) + // - BS (backspace) + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); + switch (event.keyCode) { + case SWT.CR: if (lastKeyStrokeType == LastKeyStrokeType.OPEN_CURLY && isAutoClosingEnabled) { lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY_FOLLOWED_BY_NEW_LINE; - // we need to unset the vertical indent prior to new line otherwise the line inserted by - // eclipse with the closing curly braces would inherit the extra vertical indent. + // we need to unset the vertical indent prior to new line otherwise the line + // inserted by + // eclipse with the closing curly braces would inherit the extra vertical + // indent. qInvocationSessionInstance.unsetVerticalIndent(); } else { lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; } - return; - case SWT.BS: - if (--distanceTraversed < 0) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - lastKeyStrokeType = LastKeyStrokeType.BACKSPACE; - return; - case SWT.ESC: - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - default: - } - - // If auto closing of brackets are not enabled we can just treat them as normal inputs - // Another scenario + return; + case SWT.BS: + if (--distanceTraversed < 0) { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + lastKeyStrokeType = LastKeyStrokeType.BACKSPACE; + return; + case SWT.ESC: + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + default: + } + + // If auto closing of brackets are not enabled we can just treat them as normal + // inputs + // Another scenario if (!isAutoClosingEnabled) { return; } - - // If auto cloising of brackets are enabled, SWT will treat the open bracket differently. - // Input of the brackets will not trigger a call to verifyText. - // Thus we have to do the typeahead verification here. - // Note that '{' is excluded because - switch (event.character) { + + // If auto cloising of brackets are enabled, SWT will treat the open bracket + // differently. + // Input of the brackets will not trigger a call to verifyText. + // Thus we have to do the typeahead verification here. + // Note that '{' is excluded because + switch (event.character) { case '<': if (currentSuggestion.charAt(distanceTraversed++) != '<') { qInvocationSessionInstance.transitionToDecisionMade(); @@ -168,58 +168,53 @@ public void verifyKey(final VerifyEvent event) { lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; default: } - - lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; - } - - @Override - public void verifyText(final VerifyEvent event) { - String input = event.text; - switch (lastKeyStrokeType) { - case NORMAL_INPUT: - break; - case OPEN_CURLY_FOLLOWED_BY_NEW_LINE: - input = '\n' + event.text.split("\\R")[1]; - break; - default: - return; - } + + lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; + } + + @Override + public void verifyText(final VerifyEvent event) { + String input = event.text; + switch (lastKeyStrokeType) { + case NORMAL_INPUT: + break; + case OPEN_CURLY_FOLLOWED_BY_NEW_LINE: + input = '\n' + event.text.split("\\R")[1]; + break; + default: + return; + } var qInvocationSessionInstance = QInvocationSession.getInstance(); if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { return; } - String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); - int currentOffset = widget.getCaretOffset(); - qInvocationSessionInstance - .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); - -// System.out.println("=========================\nDistance traversed: " + distanceTraversed); -// System.out.println("text typed: " + event.text); -// System.out.println("Current caret offset: " + currentOffset); -// System.out.println("Is auto closing brackets enabled: " + isAutoClosingEnabled); - - boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; - if (!isOutOfBounds) { - System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); - } - if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, input)) { - qInvocationSessionInstance.transitionToDecisionMade(); - qInvocationSessionInstance.end(); - return; - } - distanceTraversed += input.length(); - } - - private boolean isInputAMatch(String currentSuggestion, int startIdx, String input) { - boolean res; - if (input.length() > 1) { - res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); - System.out.println("This is a match: " + res); - } else { - res = String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); - } - return res; - } + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); + int currentOffset = widget.getCaretOffset(); + qInvocationSessionInstance + .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); + + boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; + if (!isOutOfBounds) { + System.out.println("current char in suggestion: " + currentSuggestion.charAt(distanceTraversed)); + } + if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, input)) { + qInvocationSessionInstance.transitionToDecisionMade(); + qInvocationSessionInstance.end(); + return; + } + distanceTraversed += input.length(); + } + + private boolean isInputAMatch(final String currentSuggestion, final int startIdx, final String input) { + boolean res; + if (input.length() > 1) { + res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); + System.out.println("This is a match: " + res); + } else { + res = String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); + } + return res; + } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java index 15562c98..62304a51 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java @@ -76,7 +76,7 @@ public final void paintControl(final PaintEvent e) { int lineHt = widget.getLineHeight(); int fontHt = gc.getFontMetrics().getHeight(); int x = widget.getLeftMargin(); - int y = location.y + lineHt * 2 - fontHt; + int y = location.y + lineHt * 2 - fontHt; gc.drawText(remainder, x, y, true); } else { qInvocationSessionInstance.unsetVerticalIndent();