Skip to content

Commit

Permalink
Inline vertical indent (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dingfeli authored Sep 24, 2024
1 parent 26eebed commit 8b84484
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 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 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 @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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 {
Expand All @@ -120,8 +123,9 @@ public void invoke() {
AuthUtils.updateToken().get();
}

List<String> newSuggestions = LspProvider.getAmazonQServer().get().inlineCompletionWithReferences(params)
.thenApply(result -> result.getItems().stream().map(InlineCompletionItem::getInsertText).collect(Collectors.toList()))
List<String> newSuggestions = LspProvider.getAmazonQServer().get()
.inlineCompletionWithReferences(params).thenApply(result -> result.getItems().stream()
.map(InlineCompletionItem::getInsertText).collect(Collectors.toList()))
.get();

Display.getDefault().asyncExec(() -> {
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -356,4 +370,3 @@ public void dispose() {
viewer = null;
}
}

0 comments on commit 8b84484

Please sign in to comment.