public void verifyKey()

in plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineInputListener.java [208:262]


    public void verifyKey(final VerifyEvent event) {
        var session = QInvocationSession.getInstance();
        if (session == null || !session.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) {
            session.setCaretMovementReason(CaretMovementReason.MOVEMENT_KEY);
            return;
        }

        session.setCaretMovementReason(CaretMovementReason.TEXT_INPUT);

        if (event.keyCode == SWT.BS && distanceTraversed == 0) {
            session.transitionToDecisionMade();
            session.end();
            return;
        }
        // Here we want to check for the following:
        // - If the input is a closing bracket
        // - If it is, does it correspond to the most recent open bracket in the
        // unresolved bracket list
        // - If it does, we need to perform the following:
        // - Adjust the caret offset backwards by one. This is because the caret offset
        // in documentChanged is the offset before the change is made. Since this key
        // event will not actually trigger documentChanged under the right condition
        // (and what ends up triggering documentChanged is the doc.replace call), we
        // will need to decrement the offset to prepare for when the documentChanged is
        // triggered.
        // - Insert the closing bracket into the buffer at the decremented position.
        // This is because eclipse will not actually insert closing bracket when auto
        // close is turned on.
        TypeaheadProcessorInstruction instruction = typeaheadProcessor.processVerifyKeyBuffer(distanceTraversed,
                event.character, brackets);
        if (instruction.shouldModifyCaretOffset()) {
            widget.setCaretOffset(instruction.getCaretOffset());
        }
        if (instruction.shouldModifyDocument()) {
            try {
                ITextViewer viewer = session.getViewer();
                IDocument doc = viewer.getDocument();
                int expandedOffset = QEclipseEditorUtils.getOffsetInFullyExpandedDocument(session.getViewer(),
                        instruction.getDocInsertOffset());
                doc.replace(expandedOffset, instruction.getDocInsertLength(), instruction.getDocInsertContent());
            } catch (BadLocationException e) {
                Activator.getLogger().error("Error inserting close bracket during typeahead", e);
            }
        }
    }