Skip to content

Commit

Permalink
Merge branch 'main' into code-reference
Browse files Browse the repository at this point in the history
  • Loading branch information
dingfeli authored Sep 24, 2024
2 parents cdf57f1 + 8b84484 commit 5e7fefb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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 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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e7fefb

Please sign in to comment.