-
Notifications
You must be signed in to change notification settings - Fork 176
Description
Describe the bug
An exception is thrown when clearing the text of a non-focused StyledText
with non-fixed line-height that is scrolled so that the first line is not visible.
To Reproduce
- Run the following snippet:
import java.util.stream.*;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
public class StyledTextBug {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StyledTextBug");
shell.setLayout(new FillLayout());
String lines = IntStream.range(0, 10)
.collect(StringBuilder::new, (s,i) -> s.append("line " + (i+1) + "\n"), StringBuilder::append)
.toString();
StyledText styledText = new StyledText (shell, SWT.BORDER | SWT.V_SCROLL);
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);
Text text = new Text (shell, SWT.BORDER);
shell.setSize(100, 3 * styledText.getLineHeight());
shell.open();
text.setFocus();
styledText.setTopIndex(styledText.getLineCount() - 1);
styledText.replaceTextRange(0, styledText.getCharCount(), "");
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
- Note that you get an exception:
Exception in thread "main" java.lang.IllegalArgumentException: Index out of bounds
at org.eclipse.swt.SWT.error(SWT.java:4927)
at org.eclipse.swt.SWT.error(SWT.java:4861)
at org.eclipse.swt.SWT.error(SWT.java:4832)
at org.eclipse.swt.custom.StyledText.getOffsetAtLine(StyledText.java:4064)
at org.eclipse.swt.custom.StyledTextRenderer.getStylesForLine(StyledTextRenderer.java:821)
at org.eclipse.swt.custom.StyledTextRenderer.isVariableHeight(StyledTextRenderer.java:782)
at org.eclipse.swt.custom.StyledTextRenderer.getLineHeight(StyledTextRenderer.java:756)
at org.eclipse.swt.custom.StyledTextRenderer.getLineHeight(StyledTextRenderer.java:749)
at org.eclipse.swt.custom.StyledText.getLinePixel(StyledText.java:3884)
at org.eclipse.swt.custom.StyledText.handleTextChanged(StyledText.java:6066)
at org.eclipse.swt.custom.StyledText$5.textChanged(StyledText.java:5406)
at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:75)
at org.eclipse.swt.custom.DefaultContent.sendTextEvent(DefaultContent.java:820)
at org.eclipse.swt.custom.DefaultContent.replaceTextRange(DefaultContent.java:813)
at org.eclipse.swt.custom.StyledText.modifyContent(StyledText.java:7133)
at org.eclipse.swt.custom.StyledText.replaceTextRange(StyledText.java:7782)
at StyledTextBug.main(StyledTextBug.java:32)
Expected behavior
No exception thrown and the StyledText
should be empty.
Environment:
- Select the platform(s) on which the behavior is seen:
-
- All OS
-
- Windows
-
- Linux
-
- macOS
-
Additional OS info (e.g. OS version, Linux Desktop, etc)
Tested on Windows 10 x64 (19045.5737), macOS 15.5 M2, and Ubuntu 20.04 LTS x64. -
JRE/JDK version
Tested with OpenJDK 21.0.7+6.
Version since
It works in SWT 3.128 (Eclipse 4.34) and fails in SWT 3.129+ (Eclipse 4.35+).
Workaround (or) Additional context
This seems to be a regression from #1617, which short-circuited the behavior added in f6d78f0 so that it didn't apply when the StyledText
was not focused, thus making it possible to reproduce the original issue from bug 122379 by doing a programmatic change with the widget not in focus.
The problem can be worked around by making the line-height of the StyledText
fixed, or the problem can be "fixed" in the same way as Test_org_eclipse_swt_custom_StyledText.test_notFixedLineHeightDoesntChangeLinePixelIfUnnecessary
was in 49fd6b0 by forcing the StyledText
to be focused before modifying its content.