Skip to content

Commit

Permalink
Ghost text alignment fix windows linux (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
dingfeli authored Dec 6, 2024
1 parent b11599a commit e07722f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static List<IQInlineSuggestionSegment> getSegmentsFromSuggestion(final QI
case CLOSE:
if (!unresolvedBrackets.isEmpty()) {
var closeBracket = new QInlineSuggestionCloseBracketSegment(startOffset + j, i,
currentLine.substring(0, j), c);
currentLine.substring(0, j), c, qSes.isMacOS());
var top = unresolvedBrackets.pop();
if (top.isAMatch(closeBracket)) {
top.pairUp(closeBracket);
Expand All @@ -70,7 +70,7 @@ public static List<IQInlineSuggestionSegment> getSegmentsFromSuggestion(final QI
}
distanceTraversed += sb.length() + 1; // plus one because we got rid of a \\R when we split it
endOffset = startOffset + sb.length() - 1;
res.add(new QInlineSuggestionNormalSegment(startOffset, endOffset, i, sb.toString()));
res.add(new QInlineSuggestionNormalSegment(startOffset, endOffset, i, sb.toString(), qSes.isMacOS()));
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;

public final class QInlineSuggestionCloseBracketSegment implements IQInlineSuggestionSegment, IQInlineBracket {
private QInlineSuggestionOpenBracketSegment openBracket;
Expand All @@ -15,16 +17,28 @@ public final class QInlineSuggestionCloseBracketSegment implements IQInlineSugge
private int lineInSuggestion;
private String text;
private Font adjustedTypedFont;
private TextLayout layout;
private TextLayout measureLayout;
private boolean isMacOS;

public QInlineSuggestionCloseBracketSegment(final int caretOffset, final int lineInSuggestion, final String text,
final char symbol) {
final char symbol, final boolean isMacOS) {
this.caretOffset = caretOffset;
this.symbol = symbol;
this.lineInSuggestion = lineInSuggestion;
this.text = text;
this.layout = isMacOS ? null : new TextLayout(Display.getCurrent());
this.isMacOS = isMacOS;

var qInvocationSessionInstance = QInvocationSession.getInstance();
adjustedTypedFont = qInvocationSessionInstance.getBoldInlineFont();
if (!isMacOS) {
int[] tabStops = qInvocationSessionInstance.getViewer().getTextWidget().getTabStops();
measureLayout = new TextLayout(Display.getCurrent());
measureLayout.setText(text);
measureLayout.setFont(qInvocationSessionInstance.getInlineTextFont());
measureLayout.setTabs(tabStops);
}
}

@Override
Expand Down Expand Up @@ -57,25 +71,40 @@ public void render(final GC gc, final int currentCaretOffset) {
int invocationLine = widget.getLineAtOffset(invocationOffset);
int lineHt = widget.getLineHeight();
int fontHt = gc.getFontMetrics().getHeight();
// educated guess:
int endPadding = gc.getAdvanceWidth(symbol) / 4;
y = (invocationLine + lineInSuggestion + 1) * lineHt - fontHt;
x = gc.textExtent(text).x;
x = isMacOS ? gc.textExtent(text).x : (int) measureLayout.getBounds().width;
if (lineInSuggestion == 0) {
x += widget.getLocationAtOffset(invocationOffset).x;
}

int scrollOffsetY = widget.getTopPixel();
y -= scrollOffsetY;
String textToRender = String.valueOf(symbol);
if (currentCaretOffset > openBracket.getRelevantOffset()) {
Color typedColor = widget.getForeground();
gc.setForeground(typedColor);
gc.setFont(adjustedTypedFont);
if (isMacOS) {
gc.setForeground(typedColor);
gc.setFont(adjustedTypedFont);
gc.drawText(textToRender, x, y, false);
} else {
layout.setFont(adjustedTypedFont);
layout.setText(textToRender);
layout.setTabs(widget.getTabStops());
gc.setAlpha(255);
layout.draw(gc, x, y);
}
} else {
gc.setForeground(Q_INLINE_HINT_TEXT_COLOR);
gc.setFont(qInvocationSessionInstance.getInlineTextFont());
if (isMacOS) {
gc.setForeground(Q_INLINE_HINT_TEXT_COLOR);
gc.setFont(qInvocationSessionInstance.getInlineTextFont());
gc.drawText(textToRender, x, y, true);
} else {
layout.setFont(qInvocationSessionInstance.getInlineTextFont());
layout.setText(textToRender);
layout.setTabs(widget.getTabStops());
gc.setAlpha(127);
layout.draw(gc, x, y);
}
}
int scrollOffsetY = widget.getTopPixel();
y -= scrollOffsetY;
gc.drawText(String.valueOf(symbol), x, y, true);
}

@Override
Expand Down Expand Up @@ -112,5 +141,11 @@ public QInlineSuggestionOpenBracketSegment getOpenBracket() {

@Override
public void cleanUp() {
if (layout != null) {
layout.dispose();
}
if (measureLayout != null) {
measureLayout.dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;

import software.aws.toolkits.eclipse.amazonq.plugin.Activator;

Expand All @@ -17,13 +19,17 @@ public final class QInlineSuggestionNormalSegment implements IQInlineSuggestionS
private int lineInSuggestion;
private String text;
private StyleRange styleRange = new StyleRange();
private TextLayout layout;
private boolean isMacOS;

public QInlineSuggestionNormalSegment(final int startCaretPosition, final int endCaretPosition,
final int lineInSuggestion, final String text) {
final int lineInSuggestion, final String text, final boolean isMacOS) {
this.isMacOS = isMacOS;
this.text = text;
this.startCaretOffset = startCaretPosition;
this.endCaretOffset = endCaretPosition;
this.lineInSuggestion = lineInSuggestion;
this.layout = isMacOS ? null : new TextLayout(Display.getCurrent());
}

@Override
Expand Down Expand Up @@ -73,13 +79,24 @@ public void render(final GC gc, final int currentCaretOffset) {
int scrollOffsetY = widget.getTopPixel();
y -= scrollOffsetY;

gc.setForeground(Q_INLINE_HINT_TEXT_COLOR);
gc.setFont(qInvocationSessionInstance.getInlineTextFont());
gc.drawText(textToRender, x, y, true);
if (!isMacOS) {
layout.setText(textToRender);
layout.setFont(qInvocationSessionInstance.getInlineTextFont());
layout.setTabs(widget.getTabStops());
gc.setAlpha(127);
layout.draw(gc, x, y);
} else {
gc.setForeground(Q_INLINE_HINT_TEXT_COLOR);
gc.setFont(qInvocationSessionInstance.getInlineTextFont());
gc.drawText(textToRender, x, y, true);
}
}

@Override
public void cleanUp() {
if (layout != null) {
layout.dispose();
}
QInvocationSession session = QInvocationSession.getInstance();
if (!session.isActive()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class QInvocationSession extends QResource {
private volatile QInvocationSessionState state = QInvocationSessionState.INACTIVE;
private CaretMovementReason caretMovementReason = CaretMovementReason.UNEXAMINED;
private boolean suggestionAccepted = false;
private boolean isMacOS;

private QSuggestionsContext suggestionsContext = null;

Expand All @@ -60,6 +61,7 @@ public final class QInvocationSession extends QResource {
// Private constructor to prevent instantiation
private QInvocationSession() {
// Initialization code here
isMacOS = System.getProperty("os.name").toLowerCase().contains("mac");
}

// Method to get the single instance
Expand Down Expand Up @@ -489,6 +491,10 @@ public Font getBoldInlineFont() {
return inlineTextFontBold;
}

public boolean isMacOS() {
return isMacOS;
}

// Additional methods for the session can be added here
@Override
public void dispose() {
Expand Down

0 comments on commit e07722f

Please sign in to comment.