Skip to content

Commit

Permalink
Unsets vertical indent upon preview termination
Browse files Browse the repository at this point in the history
  • Loading branch information
dingfeli committed Sep 20, 2024
1 parent c20132d commit 30786a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static ITextViewer getActiveTextViewer(final ITextEditor editor) {
return asTextViewer(editor);
}

public static boolean shouldIndentNextLineVertically(final StyledText textWidget, final int line) {
return line + 1 < textWidget.getLineCount() - 1;
public static boolean shouldIndentVertically(final StyledText textWidget, final int zeroIndexedLine) {
return zeroIndexedLine + 1 < textWidget.getLineCount();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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.shouldIndentNextLineVertically;
import static software.aws.toolkits.eclipse.amazonq.util.QEclipseEditorUtils.shouldIndentVertically;

import java.util.Arrays;

Expand Down Expand Up @@ -60,22 +60,25 @@ public final void paintControl(final PaintEvent e) {
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());
if (shouldIndentNextLineVertically(widget, caretLine)) {
// 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, textExtent.y);
}
// 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());
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 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,17 @@ public void transitionToDecisionMade() {
return;
}
state = QInvocationSessionState.DECISION_MADE;

unsetVerticalIndent();

// 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);
// }
// var widget = viewer.getTextWidget();
// var caretLine = widget.getLineAtOffset(widget.getCaretOffset());
// if (!isLastLine(widget, caretLine + 1)) {
// widget.setLineVerticalIndent(caretLine + 1, 0);
// }
}

public void setCaretMovementReason(final CaretMovementReason reason) {
Expand Down Expand Up @@ -333,10 +335,21 @@ public boolean hasBeenTypedahead() {
return hasBeenTypedahead;
}

public void setVerticalIndent(int line, int height, int originalHeight) {
public void setVerticalIndent(int line, int height) {
System.out.println("set vertical indent called for line " + line);
var widget = viewer.getTextWidget();
widget.setLineVerticalIndent(line, height);
unsetVerticalIndent = () -> { widget.setLineVerticalIndent(line, originalHeight); };
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
Expand All @@ -356,10 +369,6 @@ public void dispose() {
widget.removePaintListener(paintListener);
widget.removeCaretListener(caretListener);
widget.removeVerifyKeyListener(verifyKeyListener);
if (unsetVerticalIndent != null) {
unsetVerticalIndent.run();
unsetVerticalIndent = null;
}
paintListener = null;
caretListener = null;
verifyKeyListener = null;
Expand Down

0 comments on commit 30786a5

Please sign in to comment.