|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | + |
| 3 | +package software.aws.toolkits.eclipse.amazonq.util; |
| 4 | + |
| 5 | +import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 6 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 7 | +import org.eclipse.swt.SWT; |
| 8 | +import org.eclipse.swt.custom.StyledText; |
| 9 | +import org.eclipse.swt.custom.VerifyKeyListener; |
| 10 | +import org.eclipse.swt.events.VerifyEvent; |
| 11 | +import org.eclipse.swt.events.VerifyListener; |
| 12 | + |
| 13 | +public final class QInlineInputListener implements VerifyListener, VerifyKeyListener { |
| 14 | + private StyledText widget = null; |
| 15 | + private int distanceTraversed = 0; |
| 16 | + private boolean isAutoClosingEnabled = true; |
| 17 | + private LastKeyStrokeType lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; |
| 18 | + |
| 19 | + private enum LastKeyStrokeType { |
| 20 | + NORMAL_INPUT, BACKSPACE, NORMAL_BRACKET, CURLY_BRACES, OPEN_CURLY, OPEN_CURLY_FOLLOWED_BY_NEW_LINE, |
| 21 | + } |
| 22 | + |
| 23 | + public QInlineInputListener(final StyledText widget) { |
| 24 | + IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode("org.eclipse.jdt.ui"); |
| 25 | + // This needs to be defaulted to true. This key is only present in the |
| 26 | + // preference store if it is set to false. |
| 27 | + // Therefore if you can't find it, it has been set to true. |
| 28 | + this.isAutoClosingEnabled = preferences.getBoolean("closeBrackets", true); |
| 29 | + this.widget = widget; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void verifyKey(final VerifyEvent event) { |
| 34 | + var qInvocationSessionInstance = QInvocationSession.getInstance(); |
| 35 | + if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // We need to provide the reason for the caret movement. This way we can perform |
| 40 | + // subsequent actions accordingly: |
| 41 | + // - If the caret has been moved due to traversals (i.e. arrow keys or mouse |
| 42 | + // click) we would want to end the invocation session since that signifies the |
| 43 | + // user no longer has the intent for text input at its original location. |
| 44 | + if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT |
| 45 | + || event.keyCode == SWT.ARROW_RIGHT) { |
| 46 | + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + qInvocationSessionInstance.setCaretMovementReason(CaretMovementReason.TEXT_INPUT); |
| 51 | + |
| 52 | + // Here we examine all other relevant keystrokes that may be relevant to the |
| 53 | + // preview's lifetime: |
| 54 | + // - CR (new line) |
| 55 | + // - BS (backspace) |
| 56 | + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); |
| 57 | + switch (event.keyCode) { |
| 58 | + case SWT.CR: |
| 59 | + if (lastKeyStrokeType == LastKeyStrokeType.OPEN_CURLY && isAutoClosingEnabled) { |
| 60 | + lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY_FOLLOWED_BY_NEW_LINE; |
| 61 | + // we need to unset the vertical indent prior to new line otherwise the line inserted by |
| 62 | + // eclipse with the closing curly braces would inherit the extra vertical indent. |
| 63 | + int line = widget.getLineAtOffset(widget.getCaretOffset()); |
| 64 | + qInvocationSessionInstance.unsetVerticalIndent(line + 1); |
| 65 | + } else { |
| 66 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; |
| 67 | + } |
| 68 | + return; |
| 69 | + case SWT.BS: |
| 70 | + if (--distanceTraversed < 0) { |
| 71 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 72 | + qInvocationSessionInstance.end(); |
| 73 | + return; |
| 74 | + } |
| 75 | + lastKeyStrokeType = LastKeyStrokeType.BACKSPACE; |
| 76 | + return; |
| 77 | + case SWT.ESC: |
| 78 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 79 | + qInvocationSessionInstance.end(); |
| 80 | + return; |
| 81 | + default: |
| 82 | + } |
| 83 | + |
| 84 | + // If auto closing of brackets are not enabled we can just treat them as normal |
| 85 | + // inputs |
| 86 | + // Another scenario |
| 87 | + if (!isAutoClosingEnabled) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // If auto cloising of brackets are enabled, SWT will treat the open bracket |
| 92 | + // differently. |
| 93 | + // Input of the brackets will not trigger a call to verifyText. |
| 94 | + // Thus we have to do the typeahead verification here. |
| 95 | + // Note that '{' is excluded because |
| 96 | + switch (event.character) { |
| 97 | + case '<': |
| 98 | + if (currentSuggestion.charAt(distanceTraversed++) != '<') { |
| 99 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 100 | + qInvocationSessionInstance.end(); |
| 101 | + return; |
| 102 | + } |
| 103 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 104 | + return; |
| 105 | + case '>': |
| 106 | + if (currentSuggestion.charAt(distanceTraversed++) != '>') { |
| 107 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 108 | + qInvocationSessionInstance.end(); |
| 109 | + return; |
| 110 | + } |
| 111 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 112 | + return; |
| 113 | + case '(': |
| 114 | + if (currentSuggestion.charAt(distanceTraversed++) != '(') { |
| 115 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 116 | + qInvocationSessionInstance.end(); |
| 117 | + return; |
| 118 | + } |
| 119 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 120 | + return; |
| 121 | + case ')': |
| 122 | + if (currentSuggestion.charAt(distanceTraversed++) != ')') { |
| 123 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 124 | + qInvocationSessionInstance.end(); |
| 125 | + return; |
| 126 | + } |
| 127 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 128 | + return; |
| 129 | + case '[': |
| 130 | + if (currentSuggestion.charAt(distanceTraversed++) != '[') { |
| 131 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 132 | + qInvocationSessionInstance.end(); |
| 133 | + return; |
| 134 | + } |
| 135 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 136 | + return; |
| 137 | + case ']': |
| 138 | + if (currentSuggestion.charAt(distanceTraversed++) != ']') { |
| 139 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 140 | + qInvocationSessionInstance.end(); |
| 141 | + return; |
| 142 | + } |
| 143 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 144 | + return; |
| 145 | + case '{': |
| 146 | + if (currentSuggestion.charAt(distanceTraversed++) != '{') { |
| 147 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 148 | + qInvocationSessionInstance.end(); |
| 149 | + return; |
| 150 | + } |
| 151 | + lastKeyStrokeType = LastKeyStrokeType.OPEN_CURLY; |
| 152 | + return; |
| 153 | + case '}': |
| 154 | + if (currentSuggestion.charAt(distanceTraversed++) != '}') { |
| 155 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 156 | + qInvocationSessionInstance.end(); |
| 157 | + return; |
| 158 | + } |
| 159 | + lastKeyStrokeType = LastKeyStrokeType.CURLY_BRACES; |
| 160 | + return; |
| 161 | + case '"': |
| 162 | + if (currentSuggestion.charAt(distanceTraversed++) != '"') { |
| 163 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 164 | + qInvocationSessionInstance.end(); |
| 165 | + return; |
| 166 | + } |
| 167 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_BRACKET; |
| 168 | + default: |
| 169 | + } |
| 170 | + |
| 171 | + lastKeyStrokeType = LastKeyStrokeType.NORMAL_INPUT; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void verifyText(final VerifyEvent event) { |
| 176 | + String input = event.text; |
| 177 | + switch (lastKeyStrokeType) { |
| 178 | + case NORMAL_INPUT: |
| 179 | + break; |
| 180 | + case OPEN_CURLY_FOLLOWED_BY_NEW_LINE: |
| 181 | + input = '\n' + event.text.split("\\R")[1]; |
| 182 | + break; |
| 183 | + default: |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + var qInvocationSessionInstance = QInvocationSession.getInstance(); |
| 188 | + if (qInvocationSessionInstance == null || !qInvocationSessionInstance.isPreviewingSuggestions()) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + String currentSuggestion = qInvocationSessionInstance.getCurrentSuggestion().getInsertText().trim(); |
| 193 | + int currentOffset = widget.getCaretOffset(); |
| 194 | + qInvocationSessionInstance |
| 195 | + .setHasBeenTypedahead(currentOffset - qInvocationSessionInstance.getInvocationOffset() > 0); |
| 196 | + |
| 197 | + boolean isOutOfBounds = distanceTraversed >= currentSuggestion.length() || distanceTraversed < 0; |
| 198 | + if (isOutOfBounds || !isInputAMatch(currentSuggestion, distanceTraversed, input)) { |
| 199 | + qInvocationSessionInstance.transitionToDecisionMade(); |
| 200 | + qInvocationSessionInstance.end(); |
| 201 | + return; |
| 202 | + } |
| 203 | + distanceTraversed += input.length(); |
| 204 | + } |
| 205 | + |
| 206 | + private boolean isInputAMatch(final String currentSuggestion, final int startIdx, final String input) { |
| 207 | + boolean res; |
| 208 | + if (input.length() > 1) { |
| 209 | + res = currentSuggestion.substring(startIdx, startIdx + input.length()).equals(input); |
| 210 | + System.out.println("This is a match: " + res); |
| 211 | + } else { |
| 212 | + res = String.valueOf(currentSuggestion.charAt(startIdx)).equals(input); |
| 213 | + } |
| 214 | + return res; |
| 215 | + } |
| 216 | +} |
0 commit comments