public void beforeRemoval()

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


    public void beforeRemoval() {
        var session = QInvocationSession.getInstance();
        IDocument doc = session.getViewer().getDocument();
        doc.removeDocumentListener(this);
        if (session == null || !session.isActive() || brackets == null || session.getSuggestionAccepted()) {
            return;
        }

        String toAppend = "";
        for (int i = brackets.length - 1; i >= 0; i--) {
            var bracket = brackets[i];
            if (bracket == null) {
                continue;
            }
            boolean isBracketsSetToAutoClose = typeaheadProcessor.isBracesSetToAutoClose();
            boolean isAngleBracketsSetToAutoClose = typeaheadProcessor.isAngleBracketsSetToAutoClose();
            boolean isBracesSetToAutoClose = typeaheadProcessor.isBracesSetToAutoClose();
            boolean isStringSetToAutoClose = typeaheadProcessor.isStringSetToAutoClose();
            String autoCloseContent = bracket.getAutoCloseContent(isBracketsSetToAutoClose,
                    isAngleBracketsSetToAutoClose, isBracesSetToAutoClose, isStringSetToAutoClose);
            if (autoCloseContent != null) {
                toAppend += autoCloseContent;
            }
        }
        toAppend += rightCtxBuf;
        suggestionSegments.stream().forEach((segment) -> segment.cleanUp());
        final String toAppendFinal = toAppend;
        int idx = distanceTraversed;
        if (!toAppend.isEmpty()) {
                try {
                    int currentOffset = session.getInvocationOffset() + idx;
                    int expandedCurrentOffset = QEclipseEditorUtils.getOffsetInFullyExpandedDocument(session.getViewer(),
                            currentOffset);
                    int lineNumber = doc.getLineOfOffset(expandedCurrentOffset);
                    int startLineOffset = doc.getLineOffset(lineNumber);
                    int curLineInDoc = widget.getLineAtOffset(currentOffset);
                    int lineIdx = expandedCurrentOffset - startLineOffset;
                    String contentInLine = widget.getLine(curLineInDoc);
                    String currentRightCtx = contentInLine.substring(lineIdx);
                    int distanceToNewLine = currentRightCtx.length();
                    Display.getCurrent().asyncExec(() -> {
                        try {
                            doc.replace(expandedCurrentOffset, distanceToNewLine, toAppendFinal);
                        } catch (BadLocationException e) {
                            Activator.getLogger().error("Error appending right context", e);
                        }
                    });
                } catch (BadLocationException e) {
                    Activator.getLogger().error("Error retrieving line information for appending right context", e);
                }
        }
    }