CompletableFuture handleDecision()

in plugin/src/software/aws/toolkits/eclipse/amazonq/inlineChat/InlineChatDiffManager.java [191:239]


    CompletableFuture<Void> handleDecision(final boolean userAcceptedChanges) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        var typeToRemove = (userAcceptedChanges) ? annotationDeleted : annotationAdded;

        Display.getDefault().syncExec(() -> {
            try {
                var document = task.getEditor().getDocumentProvider().getDocument(task.getEditor().getEditorInput());
                final IAnnotationModel annotationModel = task.getEditor().getDocumentProvider().getAnnotationModel(task.getEditor().getEditorInput());

                // Collect lines to remove
                List<Position> linesToRemove = new ArrayList<>();
                Iterator<?> annotations = annotationModel.getAnnotationIterator();

                // Iterate over annotations to guarantee editor changes don't cause incorrect
                // code placement and deletions
                while (annotations.hasNext()) {
                    var obj = annotations.next();
                    if (obj instanceof Annotation) {
                        Annotation annotation = (Annotation) obj;
                        Position position = annotationModel.getPosition(annotation);
                        if (position != null && typeToRemove.equals(annotation.getType())) {
                            linesToRemove.add(position);
                        }
                    }
                }

                // Sort in reverse order to maintain valid offsets when removing
                linesToRemove.sort((a, b) -> Integer.compare(b.offset, a.offset));

                // Remove the lines
                for (Position pos : linesToRemove) {
                    int lineNumber = document.getLineOfOffset(pos.offset);
                    int lineStart = document.getLineOffset(lineNumber);
                    int lineLength = document.getLineLength(lineNumber);
                    document.replace(lineStart, lineLength, "");
                }

                clearDiffAnnotations(annotationModel);
                future.complete(null);

            } catch (final Exception e) {
                String action = userAcceptedChanges ? "Accepting" : "Declining";
                Activator.getLogger().error(action + " inline chat results failed with: " + e.getMessage(), e);
                future.completeExceptionally(e);
            }
        });

        return future;
    }