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 db66ffb5..e49c2917 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java @@ -36,7 +36,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(); diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QEclipseEditorUtils.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QEclipseEditorUtils.java index 6edb6435..937469f8 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QEclipseEditorUtils.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QEclipseEditorUtils.java @@ -63,8 +63,8 @@ public static ITextViewer getActiveTextViewer(final ITextEditor editor) { return asTextViewer(editor); } - public static boolean isLastLine(final StyledText textWidget, final int line) { - return line == textWidget.getLineCount() - 1; + public static boolean shouldIndentVertically(final StyledText textWidget, final int zeroIndexedLine) { + return zeroIndexedLine + 1 < textWidget.getLineCount(); } 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 3379aa97..62304a51 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInlineRendererListener.java @@ -3,13 +3,12 @@ package software.aws.toolkits.eclipse.amazonq.util; -//import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; -//import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Point; import static software.aws.toolkits.eclipse.amazonq.util.QConstants.Q_INLINE_HINT_TEXT_COLOR; -//import static software.aws.toolkits.eclipse.amazonq.util.QEclipseEditorUtils.isLastLine; +import static software.aws.toolkits.eclipse.amazonq.util.QEclipseEditorUtils.shouldIndentVertically; import java.util.Arrays; @@ -50,38 +49,38 @@ public final void paintControl(final PaintEvent e) { } int renderHeadIndex = currentOffset - offsetAtCurrentLine; - String[] remainderArray = Arrays.copyOfRange(suggestionParts, currentLineInSuggestion + 1, suggestionParts.length); + String[] remainderArray = Arrays.copyOfRange(suggestionParts, currentLineInSuggestion + 1, + suggestionParts.length); String remainder = String.join("\n", remainderArray); // Draw first line inline - String firstLine = renderHeadIndex >= 0 - ? suggestionParts[currentLineInSuggestion].trim() : suggestionParts[currentLineInSuggestion]; + String firstLine = renderHeadIndex >= 0 ? suggestionParts[currentLineInSuggestion].trim() + : suggestionParts[currentLineInSuggestion]; int xLoc = renderHeadIndex >= 0 ? location.x : widget.getLeftMargin(); if (renderHeadIndex < firstLine.length()) { - gc.drawText(renderHeadIndex >= 0 ? firstLine.substring(renderHeadIndex) : firstLine, xLoc, location.y, true); + gc.drawText(renderHeadIndex >= 0 ? firstLine.substring(renderHeadIndex) : firstLine, xLoc, location.y, + true); } // Draw other lines inline if (!remainder.isEmpty()) { // For last line case doesn't need to indent next line vertically var caretLine = widget.getLineAtOffset(widget.getCaretOffset()); - // Felix: Not really sure what this does and it is throwing under certain - // circumstance - // Will confirm with Andrew before adding this back in / modifying it. -// if (!isLastLine(widget, caretLine + 1)) { -// // when showing the suggestion need to add next line indent -// Point textExtent = gc.stringExtent(" "); -// int height = textExtent.y * suggestionParts[1].split("\\R").length; -// widget.setLineVerticalIndent(caretLine + 1, height); -// } + if (shouldIndentVertically(widget, caretLine) && qInvocationSessionInstance.isPreviewingSuggestions()) { + // when showing the suggestion need to add next line indent + Point textExtent = gc.stringExtent(" "); + int height = textExtent.y * remainder.split("\\R").length; + qInvocationSessionInstance.setVerticalIndent(caretLine + 1, height); + } int lineHt = widget.getLineHeight(); int fontHt = gc.getFontMetrics().getHeight(); int x = widget.getLeftMargin(); int y = location.y + lineHt * 2 - fontHt; gc.drawText(remainder, x, y, true); + } else { + qInvocationSessionInstance.unsetVerticalIndent(); } } } - 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 7397424e..0f353758 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java @@ -48,6 +48,7 @@ public final class QInvocationSession extends QResource { private int[] headOffsetAtLine = new int[500]; private boolean hasBeenTypedahead = false; private CodeReferenceAcceptanceCallback codeReferenceAcceptanceCallback = null; + private Runnable unsetVerticalIndent; // Private constructor to prevent instantiation private QInvocationSession() { @@ -220,14 +221,7 @@ public void transitionToDecisionMade() { } state = QInvocationSessionState.DECISION_MADE; - // Clear previous next line indent in certain cases (always for now?) - // From Felix: Not really sure when or where this is needed, disabling for now. - // This is throwing under certain circumstances with IllegalArgument - // var widget = viewer.getTextWidget(); - // var caretLine = widget.getLineAtOffset(widget.getCaretOffset()); - // if (!isLastLine(widget, caretLine + 1)) { - // widget.setLineVerticalIndent(caretLine + 1, 0); - // } + unsetVerticalIndent(); } public void setCaretMovementReason(final CaretMovementReason reason) { @@ -345,6 +339,22 @@ public void executeCallbackForCodeReference() { codeReferenceAcceptanceCallback.onCallback(selectedSuggestion, startLine); } } + + public void setVerticalIndent(int line, int height) { + var widget = viewer.getTextWidget(); + widget.setLineVerticalIndent(line, height); + unsetVerticalIndent = () -> { + var caretLine = widget.getLineAtOffset(widget.getCaretOffset()); + widget.setLineVerticalIndent(caretLine + 1, 0); + }; + } + + public void unsetVerticalIndent() { + if (unsetVerticalIndent != null) { + unsetVerticalIndent.run(); + unsetVerticalIndent = null; + } + } // Additional methods for the session can be added here @Override