Search in sources :

Example 26 with StyledTextContent

use of org.eclipse.swt.custom.StyledTextContent in project webtools.sourceediting by eclipse.

the class StyledTextContentSpec method test_Replace.

public void test_Replace() {
    StyledTextContent content = getContentInstance();
    String newText;
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 4, "a");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":1a:", newText.equals("This\na test\r"));
    assertWithMessage(":1b:", content.getLineCount() == 3);
    assertWithMessage(":1c:", content.getLine(0).equals("This"));
    assertWithMessage(":1d:", content.getLine(1).equals("a test"));
    assertWithMessage(":1e:", content.getLine(2).equals(""));
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 2, "was");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":2a:", newText.equals("This\nwas a test\r"));
    assertWithMessage(":2b:", content.getLineCount() == 3);
    assertWithMessage(":2c:", content.getLine(0).equals("This"));
    assertWithMessage(":2d:", content.getLine(1).equals("was a test"));
    assertWithMessage(":2e:", content.getLine(2).equals(""));
    content.setText("This is a test\r");
    content.replaceTextRange(5, 2, "was");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":3a:", newText.equals("This was a test\r"));
    assertWithMessage(":3b:", content.getLineCount() == 2);
    assertWithMessage(":3c:", content.getLine(0).equals("This was a test"));
    assertWithMessage(":3d:", content.getLineAtOffset(15) == 0);
    content.setText("Line 1\nLine 2\nLine 3");
    content.replaceTextRange(0, 7, "La\nLb\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":4a:", newText.equals("La\nLb\nLine 2\nLine 3"));
    assertWithMessage(":4b:", content.getLine(0).equals("La"));
    assertWithMessage(":4c:", content.getLine(1).equals("Lb"));
    assertWithMessage(":4d:", content.getLine(2).equals("Line 2"));
    assertWithMessage(":4e:", content.getLine(3).equals("Line 3"));
    content.setText(getTestText());
    newText = content.getTextRange(0, content.getCharCount());
    int start = content.getOffsetAtLine(6);
    int end = content.getOffsetAtLine(11);
    content.replaceTextRange(start, end - start, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":5a:", content.getLineCount() == 12);
    assertWithMessage(":5a:", content.getLine(5).equals(""));
    assertWithMessage(":5a:", content.getLine(6).equals(""));
    start = content.getOffsetAtLine(7);
    content.replaceTextRange(start, content.getCharCount() - start, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":5a:", content.getLineCount() == 8);
    assertWithMessage(":5a:", content.getLine(5).equals(""));
    assertWithMessage(":5a:", content.getLine(6).equals(""));
    assertWithMessage(":5a:", content.getLine(7).equals(""));
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Aggregations

StyledTextContent (org.eclipse.swt.custom.StyledTextContent)26 TextChangeListener (org.eclipse.swt.custom.TextChangeListener)6 Test (org.junit.Test)6 TextChangedEvent (org.eclipse.swt.custom.TextChangedEvent)3 TextChangingEvent (org.eclipse.swt.custom.TextChangingEvent)3 Point (org.eclipse.swt.graphics.Point)3 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1