Skip to content

Fix replaceTextRange logic for unfocused StyledText with variable line height #2318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1077,31 +1077,50 @@ void calculateTopIndex(int delta) {
}
}
} else {
int lineCount = content.getLineCount();
if (delta >= 0) {
delta -= topIndexY;
int lineIndex = topIndex;
int lineCount = content.getLineCount();
while (lineIndex < lineCount) {
if (delta <= 0) break;
delta -= renderer.getCachedLineHeight(lineIndex++);
if (lineIndex >= 0 && lineIndex < lineCount) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lineIndex < lineCount doesn't have to be repeated.
Also the condition lineIndex >= 0 can be skipped if we initialize lineIndex to Math.max(0, topIndex)

delta -= renderer.getCachedLineHeight(lineIndex);
}
lineIndex++;
}
if (lineIndex < lineCount && -delta + renderer.getCachedLineHeight(lineIndex) <= clientAreaHeight - topMargin - bottomMargin) {
int lineHeight = 0;
if (lineIndex >= 0 && lineIndex < lineCount) {
lineHeight = renderer.getCachedLineHeight(lineIndex);
}
if (lineIndex < lineCount && -delta + lineHeight <= clientAreaHeight - topMargin - bottomMargin) {
topIndex = lineIndex;
topIndexY = -delta;
} else {
topIndex = lineIndex - 1;
topIndexY = -renderer.getCachedLineHeight(topIndex) - delta;
int topIndexHeight = 0;
if (topIndex >= 0 && topIndex < lineCount) {
topIndexHeight = renderer.getCachedLineHeight(topIndex);
}
topIndexY = -topIndexHeight - delta;
Comment on lines +1100 to +1104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be clearer to write it like

if (topIndex >= 0 && topIndex < lineCount) {
   topIndexY = -renderer.getCachedLineHeight(topIndex);
}
topIndexY -= delta;

}
} else {
delta -= topIndexY;
int lineIndex = topIndex;
while (lineIndex > 0) {
int lineHeight = renderer.getCachedLineHeight(lineIndex - 1);
int previousLineIndex = lineIndex - 1;
int lineHeight = 0;
if (previousLineIndex >= 0 && previousLineIndex < lineCount) {
lineHeight = renderer.getCachedLineHeight(previousLineIndex);
}
if (delta + lineHeight > 0) break;
delta += lineHeight;
lineIndex--;
}
if (lineIndex == 0 || -delta + renderer.getCachedLineHeight(lineIndex) <= clientAreaHeight - topMargin - bottomMargin) {
int lineHeight = 0;
if (lineIndex >= 0 && lineIndex < lineCount) {
lineHeight = renderer.getCachedLineHeight(lineIndex);
}
if (lineIndex == 0 || -delta + lineHeight <= clientAreaHeight - topMargin - bottomMargin) {
topIndex = lineIndex;
topIndexY = - delta;
} else {
Expand Down Expand Up @@ -1378,11 +1397,18 @@ int getAvailableHeightAbove(int height) {
if (maxHeight == -1) {
int lineIndex = topIndex - 1;
maxHeight = -topIndexY;
int lineCount = content.getLineCount();
if (topIndexY > 0) {
maxHeight += renderer.getLineHeight(lineIndex--);
if (lineIndex >= 0 && lineIndex < lineCount) {
maxHeight += renderer.getLineHeight(lineIndex);
}
lineIndex--;
}
while (height > maxHeight && lineIndex >= 0) {
maxHeight += renderer.getLineHeight(lineIndex--);
if (lineIndex >= 0 && lineIndex < lineCount) {
maxHeight += renderer.getLineHeight(lineIndex);
}
lineIndex--;
}
}
return Math.min(height, maxHeight);
Expand Down Expand Up @@ -3877,10 +3903,16 @@ public int getLinePixel(int lineIndex) {
int height = topIndexY;
if (lineIndex > topIndex) {
for (int i = topIndex; i < lineIndex; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use

for (int i = Math.max(topIndex, 0); i < Math.min(lineIndex, lineCount); i++) {

here and get rid of next conditions?

if (i < 0 || i >= lineCount) {
continue;
}
height += renderer.getLineHeight(i);
}
} else {
for (int i = topIndex - 1; i >= lineIndex; i--) {
if (i < 0 || i >= lineCount) {
continue;
}
height -= renderer.getLineHeight(i);
}
}
Expand Down Expand Up @@ -3914,10 +3946,18 @@ public int getLineIndex(int y) {
}
} else {
int lineCount = content.getLineCount();
int lineHeight = renderer.getLineHeight(line);
int lineHeight = 0;
if (line>=0 && line<lineCount) {
lineHeight = renderer.getLineHeight(line);
}
while (y - lineHeight >= topIndexY && line < lineCount - 1) {
y -= lineHeight;
lineHeight = renderer.getLineHeight(++line);
++line;
if (line >= 0 && line < lineCount) {
lineHeight = renderer.getLineHeight(line);
}else {
lineHeight = 0;
}
}
}
return line;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.BidiUtil;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.widgets.Caret;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.junit.Assume;
import org.junit.Before;
Expand Down Expand Up @@ -195,6 +197,35 @@ public void test_getTextBounds() {
}
}

@Test
public void test_replaceTextRangeWithVariableHeight() {
shell.setLayout(new FillLayout());
StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL);
Text text = new Text(shell, SWT.BORDER);
try {
String lines = IntStream.range(0, 10)
.collect(StringBuilder::new, (s, i) -> s.append("line " + (i + 1) + "\n"), StringBuilder::append).toString();
styledText.setText(lines);
StyleRange style = new StyleRange();
style.start = 0;
style.length = lines.length();
style.font = styledText.getFont(); // To make the line-height non-fixed
styledText.setStyleRange(style);

shell.setSize(100, 3 * styledText.getLineHeight());
shell.open();

text.setFocus();
styledText.setTopIndex(styledText.getLineCount() - 1);
assertFalse(styledText.isFocusControl());
// ensure no IllegalArgumentException is thrown when styledText control has not the focus and the text is replaced
styledText.replaceTextRange(0, styledText.getCharCount(), "");
}finally {
styledText.dispose();
text.dispose();
}
}

@Test
public void test_addExtendedModifyListenerLorg_eclipse_swt_custom_ExtendedModifyListener() {
final String line = "Line1";
Expand Down
Loading