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 1e82fe68..0ac2dd36 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/handlers/QAcceptSuggestionsHandler.java @@ -36,9 +36,7 @@ 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().end(); } catch (BadLocationException e) { PluginLogger.error(e.toString()); 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 6d921d8b..05b3534a 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 9de065f3..6f7fbec8 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/QInvocationSession.java @@ -47,6 +47,7 @@ public final class QInvocationSession extends QResource { private boolean isLastKeyNewLine = false; private int[] headOffsetAtLine = new int[500]; private boolean hasBeenTypedahead = false; + private Runnable unsetVerticalIndent; // Private constructor to prevent instantiation private QInvocationSession() { @@ -84,7 +85,8 @@ public synchronized boolean start(final ITextEditor editor) { invocationTimeInMs = System.currentTimeMillis(); System.out.println("Session started."); - var listeners = widget.getTypedListeners(SWT.Paint, QInlineRendererListener.class).collect(Collectors.toList()); + var listeners = widget.getTypedListeners(SWT.Paint, QInlineRendererListener.class) + .collect(Collectors.toList()); System.out.println("Current listeners for " + widget); listeners.forEach(System.out::println); if (listeners.isEmpty()) { @@ -109,7 +111,8 @@ public void invoke() { var session = QInvocationSession.getInstance(); try { - var params = InlineCompletionUtils.cwParamsFromContext(session.getEditor(), session.getViewer(), session.getInvocationOffset()); + var params = InlineCompletionUtils.cwParamsFromContext(session.getEditor(), session.getViewer(), + session.getInvocationOffset()); ThreadingUtils.executeAsyncTask(() -> { try { @@ -120,8 +123,9 @@ public void invoke() { AuthUtils.updateToken().get(); } - List newSuggestions = LspProvider.getAmazonQServer().get().inlineCompletionWithReferences(params) - .thenApply(result -> result.getItems().stream().map(InlineCompletionItem::getInsertText).collect(Collectors.toList())) + List newSuggestions = LspProvider.getAmazonQServer().get() + .inlineCompletionWithReferences(params).thenApply(result -> result.getItems().stream() + .map(InlineCompletionItem::getInsertText).collect(Collectors.toList())) .get(); Display.getDefault().asyncExec(() -> { @@ -130,7 +134,8 @@ public void invoke() { return; } - suggestionsContext.getDetails().addAll(newSuggestions.stream().map(QSuggestionContext::new).collect(Collectors.toList())); + suggestionsContext.getDetails().addAll( + newSuggestions.stream().map(QSuggestionContext::new).collect(Collectors.toList())); suggestionsContext.setCurrentIndex(0); @@ -217,14 +222,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) { @@ -330,6 +328,22 @@ public boolean hasBeenTypedahead() { return hasBeenTypedahead; } + 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 public void dispose() { @@ -356,4 +370,3 @@ public void dispose() { viewer = null; } } -