eclipse.platform.swt icon indicating copy to clipboard operation
eclipse.platform.swt copied to clipboard

StyledText: internal error invoking replaceTextRange for \n \r mixed text

Open tmssngr opened this issue 3 months ago • 2 comments

Describe the bug Replacing text with \r and \n characters caused internal error.

To Reproduce

  • run following snippet:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class StyledTextReplaceTest {

    public static void main(String[] args) {
        Display display = new Display();

        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.MULTI);
        styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Push me");
        button.addListener(SWT.Selection, e -> styledText.replaceTextRange(0, styledText.getCharCount(), "abc\n\n\r\rdef\n"));

        shell.setSize( 500, 400);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }
}
  • click "Push me"
  • delete the "def" texts (not the whole line!)
  • click "Push me"
  • you get this exception:
Exception in thread "main" java.lang.IllegalArgumentException: Argument not valid
	at org.eclipse.swt.SWT.error(SWT.java:4913)
	at org.eclipse.swt.SWT.error(SWT.java:4847)
	at org.eclipse.swt.SWT.error(SWT.java:4818)
	at org.eclipse.swt.custom.DefaultContent.error(DefaultContent.java:691)
	at org.eclipse.swt.custom.DefaultContent.getOffsetAtLine(DefaultContent.java:661)
	at org.eclipse.swt.custom.DefaultContent.insert(DefaultContent.java:288)
	at org.eclipse.swt.custom.DefaultContent.replaceTextRange(DefaultContent.java:809)
	at org.eclipse.swt.custom.StyledText.modifyContent(StyledText.java:7131)
	at org.eclipse.swt.custom.StyledText.replaceTextRange(StyledText.java:7780)
	at StyledTextReplaceTest.lambda$main$0(StyledTextReplaceTest.java:22)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:5857)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1652)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:5067)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4519)
	at StyledTextReplaceTest.main(StyledTextReplaceTest.java:28)

Expected behavior The whole text will be replaced.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • [x] All OS
    • [ ] Windows
    • [ ] Linux
    • [ ] macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc) Tried on MX Linux with XFCE4.20/X11 and Windows 11.

tmssngr avatar Sep 25 '25 08:09 tmssngr

It would be nice to have such snippets being added as to the junit tests . Would you have the time to do so?

akurtakov avatar Sep 25 '25 08:09 akurtakov

@akurtakov Unfortunately, not at the moment. We are in release phase. Beside that, adding breaking unit tests without a fix wouldn't be very polite. ;)

tmssngr avatar Sep 25 '25 09:09 tmssngr