Skip to content

Commit

Permalink
fix(eclipse): fix rendered tab size in ghost text. (#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored Jul 27, 2024
1 parent 244d842 commit 53c028e
Showing 4 changed files with 81 additions and 43 deletions.
4 changes: 2 additions & 2 deletions clients/eclipse/feature/feature.xml
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<feature
id="com.tabbyml.features.tabby4eclipse"
label="Tabby"
version="0.0.1.4"
version="0.0.1.5"
provider-name="com.tabbyml">

<description url="http://www.example.com/description">
@@ -19,6 +19,6 @@

<plugin
id="com.tabbyml.tabby4eclipse"
version="0.0.1.4"/>
version="0.0.1.5"/>

</feature>
2 changes: 1 addition & 1 deletion clients/eclipse/plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tabby Plugin for Eclipse
Bundle-SymbolicName: com.tabbyml.tabby4eclipse;singleton:=true
Bundle-Version: 0.0.1.4
Bundle-Version: 0.0.1.5
Bundle-Activator: com.tabbyml.tabby4eclipse.Activator
Bundle-Vendor: com.tabbyml
Require-Bundle: org.eclipse.ui,
48 changes: 24 additions & 24 deletions clients/eclipse/plugin/src/com/tabbyml/tabby4eclipse/Logger.java
Original file line number Diff line number Diff line change
@@ -5,41 +5,41 @@

public class Logger {
private String tag;

public Logger(String tag) {
this.tag = tag;
}

public void trace(String message, Object obj) {
System.out.println(tagString(message));
System.out.println(obj);
}
public void trace(String message, Object obj) {
System.out.println(tagString(message));
System.out.println(obj);
}

public void debug(String message) {
System.out.println(tagString(message));
}

public void info(String message) {
logStatus(IStatus.INFO, message, null);
}

public void warn(String message, Throwable throwable) {
logStatus(IStatus.WARNING, message, throwable);
}
public void info(String message) {
logStatus(IStatus.INFO, message, null);
}

public void error(String message) {
logStatus(IStatus.ERROR, message, null);
}
public void warn(String message, Throwable throwable) {
logStatus(IStatus.WARNING, message, throwable);
}

public void error(String message, Throwable throwable) {
logStatus(IStatus.ERROR, message, throwable);
}
public void error(String message) {
logStatus(IStatus.ERROR, message, null);
}

public void error(String message, Throwable throwable) {
logStatus(IStatus.ERROR, message, throwable);
}

private void logStatus(int severity, String message, Throwable throwable) {
Status status = new Status(severity, Activator.PLUGIN_ID, tagString(message), throwable);
Activator.getDefault().getLog().log(status);
}

private void logStatus(int severity, String message, Throwable throwable) {
Status status = new Status(severity, Activator.PLUGIN_ID, tagString(message), throwable);
Activator.getDefault().getLog().log(status);
}

private String tagString(String message) {
return "[" + tag + "] " + message;
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.text.IPaintPositionManager;
import org.eclipse.jface.text.IPainter;
@@ -276,12 +278,15 @@ private void setupPainting() {
private void drawOverwriteText(int offset, String text) {
logger.debug("drawCurrentLineText:" + offset + ":" + text);
StyledText widget = getWidget();
TextWithTabs textWithTabs = splitLeadingTabs(text);

paintFunctions.add((gc) -> {
// Draw ghost text
setStyleToGhostText(gc);
int spaceWidth = gc.textExtent(" ").x;
int tabWidth = textWithTabs.tabs * widget.getTabs() * spaceWidth;
Point location = widget.getLocationAtOffset(offset);
gc.drawString(text, location.x, location.y);
gc.drawString(textWithTabs.text, location.x + tabWidth, location.y);
});
}

@@ -292,6 +297,7 @@ private void drawInsertPartText(int offset, String text) {
private void drawReplacePartText(int offset, String text, String replacedText) {
logger.debug("drawInsertPartText:" + offset + ":" + text + ":" + replacedText);
StyledText widget = getWidget();
TextWithTabs textWithTabs = splitLeadingTabs(text);

int targetOffset = offset + replacedText.length();
String targetChar = widget.getText(targetOffset, targetOffset);
@@ -307,23 +313,30 @@ private void drawReplacePartText(int offset, String text, String replacedText) {
}

paintFunctions.add((gc) -> {
// Draw ghost text
setStyleToGhostText(gc);
int spaceWidth = gc.textExtent(" ").x;
int tabWidth = textWithTabs.tabs * widget.getTabs() * spaceWidth;
int ghostTextWidth = tabWidth + gc.stringExtent(textWithTabs.text).x;
Point location = widget.getLocationAtOffset(offset);
gc.drawString(textWithTabs.text, location.x + tabWidth, location.y);

// Leave the space for the ghost text
setStyle(gc, originStyleRange);

int spaceWidth = gc.stringExtent(text).x - gc.stringExtent(replacedText).x;
int charWidth = gc.stringExtent(targetChar).x;
int shiftWidth = ghostTextWidth - gc.stringExtent(replacedText).x;
int targetCharWidth = gc.stringExtent(targetChar).x;

StyleRange currentStyleRange = widget.getStyleRangeAtOffset(targetOffset);
if (currentStyleRange != null && currentStyleRange.metrics != null
&& currentStyleRange.metrics.width == spaceWidth + charWidth) {
&& currentStyleRange.metrics.width == shiftWidth + targetCharWidth) {
// nothing to do
} else {
StyleRange styleRange = (StyleRange) originStyleRange.clone();
styleRange.start = targetOffset;
styleRange.length = 1;
FontMetrics fontMetrics = gc.getFontMetrics();
GlyphMetrics glyphMetrics = new GlyphMetrics(fontMetrics.getAscent(), fontMetrics.getDescent(),
spaceWidth + charWidth);
shiftWidth + targetCharWidth);
modifiedGlyphMetrics.add(glyphMetrics);
styleRange.metrics = glyphMetrics;
widget.setStyleRange(styleRange);
@@ -332,24 +345,20 @@ private void drawReplacePartText(int offset, String text, String replacedText) {

// Draw the moved char
Point targetCharLocation = widget.getLocationAtOffset(targetOffset);
gc.drawString(targetChar, targetCharLocation.x + spaceWidth, targetCharLocation.y, true);

// Draw ghost text
setStyleToGhostText(gc);
Point location = widget.getLocationAtOffset(offset);
gc.drawString(text, location.x, location.y);
gc.drawString(targetChar, targetCharLocation.x + shiftWidth, targetCharLocation.y, true);
});
}

private void drawSuffixLines(int offset, String text) {
logger.debug("drawSuffixLines:" + offset + ":" + text);
StyledText widget = getWidget();
int lineHeight = widget.getLineHeight();
List<String> lines = text.lines().toList();

// Leave the space for the ghost text
int nextLine = widget.getLineAtOffset(offset) + 1;
if (nextLine < widget.getLineCount()) {
int lineCount = (int) text.lines().count();
int lineCount = lines.size();
int originVerticalIndent = widget.getLineVerticalIndent(nextLine);
Position position = new Position(widget.getOffsetAtLine(nextLine), 0);
positionManager.managePosition(position);
@@ -360,13 +369,22 @@ private void drawSuffixLines(int offset, String text) {
logger.debug("Set LineVerticalIndent:" + nextLine + " -> " + modifiedVerticalIndent);
}

List<TextWithTabs> linesTextWithTab = new ArrayList<>();
for (String line : lines) {
linesTextWithTab.add(splitLeadingTabs(line));
}

paintFunctions.add((gc) -> {
// Draw ghost text
setStyleToGhostText(gc);
int spaceWidth = gc.textExtent(" ").x;
Point location = widget.getLocationAtOffset(offset);
int x = widget.getLeftMargin();
int y = location.y + lineHeight;
gc.drawText(text, x, y, true);
int y = location.y;
for (TextWithTabs textWithTabs : linesTextWithTab) {
int x = widget.getLeftMargin() + textWithTabs.tabs * widget.getTabs() * spaceWidth;
y += lineHeight;
gc.drawString(textWithTabs.text, x, y, true);
}
});
}

@@ -395,6 +413,16 @@ private void setStyleToGhostText(GC gc) {
gc.setFont(font);
}

static final Pattern leadingTabsPattern = Pattern.compile("^(\\t*)(.*)$");
private static TextWithTabs splitLeadingTabs(String text) {
Matcher matcher = leadingTabsPattern.matcher(text);
if (matcher.matches()) {
return new TextWithTabs(matcher.group(1).length(), matcher.group(2));
} else {
return new TextWithTabs(0, text);
}
}

private static class ModifiedLineVerticalIndent {
private Position position;
private int indent;
@@ -406,5 +434,15 @@ public ModifiedLineVerticalIndent(Position position, int indent, int modifiedInd
this.modifiedIndent = modifiedIndent;
}
}

private static class TextWithTabs {
private int tabs;
private String text;

public TextWithTabs(int tabs, String text) {
this.tabs = tabs;
this.text = text;
}
}
}
}

0 comments on commit 53c028e

Please sign in to comment.