Search in sources :

Example 21 with StyledTextContent

use of org.eclipse.swt.custom.StyledTextContent in project eclipse.platform.swt by eclipse.

the class StyledTextContentSpec method test_Insert.

public void test_Insert() {
    StyledTextContent content = getContentInstance();
    String newText;
    content.setText("This\nis a test\r");
    content.replaceTextRange(0, 0, "test\n ");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":1a:", newText.equals("test\n This\nis a test\r"));
    assertTrue(":1b:", content.getLineCount() == 4);
    assertTrue(":1c:", content.getLine(0).equals("test"));
    assertTrue(":1d:", content.getLine(1).equals(" This"));
    assertTrue(":1e:", content.getLine(2).equals("is a test"));
    assertTrue(":1f:", content.getLine(3).isEmpty());
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 0, "*** ");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":2a:", newText.equals("This\n*** is a test\r"));
    assertTrue(":2b:", content.getLineCount() == 3);
    assertTrue(":2c:", content.getLine(0).equals("This"));
    assertTrue(":2d:", content.getLine(1).equals("*** is a test"));
    assertTrue(":2e:", content.getLine(2).isEmpty());
    content.setText("Line 1\r\nLine 2");
    content.replaceTextRange(0, 0, "\r");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":3a:", newText.equals("\rLine 1\r\nLine 2"));
    assertTrue(":3b:", content.getLineCount() == 3);
    assertTrue(":3c:", content.getLine(0).isEmpty());
    assertTrue(":3d:", content.getLine(1).equals("Line 1"));
    assertTrue(":3e:", content.getLine(2).equals("Line 2"));
    content.replaceTextRange(9, 0, "\r");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":3f:", newText.equals("\rLine 1\r\n\rLine 2"));
    assertTrue(":3g:", content.getLineCount() == 4);
    assertTrue(":3h:", content.getLine(0).isEmpty());
    assertTrue(":3i:", content.getLine(1).equals("Line 1"));
    assertTrue(":3j:", content.getLine(2).isEmpty());
    assertTrue(":3k:", content.getLine(3).equals("Line 2"));
    content.setText("This\nis a test\r");
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":4a:", newText.equals("\nThis\nis a test\r"));
    assertTrue(":4b:", content.getLineCount() == 4);
    assertTrue(":4c:", content.getLine(0).isEmpty());
    assertTrue(":4d:", content.getLine(1).equals("This"));
    assertTrue(":4e:", content.getLine(2).equals("is a test"));
    assertTrue(":4f:", content.getLine(3).isEmpty());
    content.setText("This\nis a test\r");
    content.replaceTextRange(7, 0, "\r\nnewLine");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":5a:", newText.equals("This\nis\r\nnewLine a test\r"));
    assertTrue(":5b:", content.getLineCount() == 4);
    assertTrue(":5c:", content.getLine(0).equals("This"));
    assertTrue(":5d:", content.getLine(1).equals("is"));
    assertTrue(":5e:", content.getLine(2).equals("newLine a test"));
    assertTrue(":5f:", content.getLine(3).isEmpty());
    content.setText("");
    content.replaceTextRange(0, 0, "This\nis\r\nnewLine a test\r");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6a:", newText.equals("This\nis\r\nnewLine a test\r"));
    assertTrue(":6b:", content.getLineCount() == 4);
    assertTrue(":6c:", content.getLine(0).equals("This"));
    assertTrue(":6d:", content.getLine(1).equals("is"));
    assertTrue(":6e:", content.getLine(2).equals("newLine a test"));
    assertTrue(":6f:", content.getLine(3).isEmpty());
    // insert at end
    content.setText("This");
    content.replaceTextRange(4, 0, "\n ");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":7a:", newText.equals("This\n "));
    assertTrue(":7b:", content.getLineCount() == 2);
    assertTrue(":7c:", content.getLine(0).equals("This"));
    assertTrue(":7d:", content.getLine(1).equals(" "));
    content.setText("This\n");
    content.replaceTextRange(5, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":7e:", newText.equals("This\n\n"));
    assertTrue(":7f:", content.getLineCount() == 3);
    assertTrue(":7g:", content.getLine(0).equals("This"));
    assertTrue(":7h:", content.getLine(1).isEmpty());
    assertTrue(":7i:", content.getLine(2).isEmpty());
    // insert at beginning
    content.setText("This");
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":8a:", newText.equals("\nThis"));
    assertTrue(":8b:", content.getLineCount() == 2);
    assertTrue(":8c:", content.getLine(0).isEmpty());
    assertTrue(":8d:", content.getLine(1).equals("This"));
    // insert text
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 0, "*** ");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":9a:", newText.equals("This\n*** is a test\r"));
    assertTrue(":9b:", content.getLineCount() == 3);
    assertTrue(":9c:", content.getLine(0).equals("This"));
    assertTrue(":9d:", content.getLine(1).equals("*** is a test"));
    assertTrue(":9e:", content.getLine(2).isEmpty());
    content.setText("This\n");
    content.replaceTextRange(5, 0, "line");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":10a:", newText.equals("This\nline"));
    assertTrue(":10b:", content.getLineCount() == 2);
    assertTrue(":10c:", content.getLine(0).equals("This"));
    assertTrue(":10d:", content.getLine(1).equals("line"));
    assertTrue(":10e:", content.getLineAtOffset(8) == 1);
    assertTrue(":10f:", content.getLineAtOffset(9) == 1);
    // insert at beginning
    content.setText("This\n");
    content.replaceTextRange(0, 0, "line\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":11a:", newText.equals("line\nThis\n"));
    assertTrue(":11b:", content.getLineCount() == 3);
    assertTrue(":11c:", content.getLine(0).equals("line"));
    assertTrue(":11d:", content.getLine(1).equals("This"));
    assertTrue(":11e:", content.getLineAtOffset(5) == 1);
    content.setText("Line 1\r\nLine 2\r\nLine 3");
    content.replaceTextRange(0, 0, "\r");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":12a:", newText.equals("\rLine 1\r\nLine 2\r\nLine 3"));
    assertTrue(":12b:", content.getLineCount() == 4);
    assertTrue(":12c:", content.getLine(0).isEmpty());
    assertTrue(":12d:", content.getLine(1).equals("Line 1"));
    assertTrue(":12e:", content.getLine(2).equals("Line 2"));
    assertTrue(":12f:", content.getLine(3).equals("Line 3"));
    content.setText("Line 1\nLine 2\nLine 3");
    content.replaceTextRange(7, 0, "Line1a\nLine1b\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":13a:", newText.equals("Line 1\nLine1a\nLine1b\nLine 2\nLine 3"));
    assertTrue(":13b:", content.getLineCount() == 5);
    assertTrue(":13c:", content.getLine(0).equals("Line 1"));
    assertTrue(":13d:", content.getLine(1).equals("Line1a"));
    assertTrue(":13e:", content.getLine(2).equals("Line1b"));
    assertTrue(":13f:", content.getLine(3).equals("Line 2"));
    assertTrue(":13g:", content.getLine(4).equals("Line 3"));
    content.setText("Line 1\nLine 2\nLine 3");
    content.replaceTextRange(11, 0, "l1a");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":14a:", newText.equals("Line 1\nLinel1a 2\nLine 3"));
    assertTrue(":14b:", content.getLineCount() == 3);
    assertTrue(":14c:", content.getLine(0).equals("Line 1"));
    assertTrue(":14d:", content.getLine(1).equals("Linel1a 2"));
    assertTrue(":14e:", content.getLine(2).equals("Line 3"));
    content.setText("Line 1\nLine 2 is a very long line that spans many words\nLine 3");
    content.replaceTextRange(19, 0, "very, very, ");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":15a:", newText.equals("Line 1\nLine 2 is a very, very, very long line that spans many words\nLine 3"));
    assertTrue(":15b:", content.getLineCount() == 3);
    assertTrue(":15c:", content.getLine(0).equals("Line 1"));
    assertTrue(":15d:", content.getLine(1).equals("Line 2 is a very, very, very long line that spans many words"));
    assertTrue(":15e:", content.getLine(2).equals("Line 3"));
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 22 with StyledTextContent

use of org.eclipse.swt.custom.StyledTextContent in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_TextChangeListener method test_textChangedLorg_eclipse_swt_custom_TextChangedEvent.

@Test
public void test_textChangedLorg_eclipse_swt_custom_TextChangedEvent() {
    StyledTextContent content = styledText.getContent();
    TextChangeListener listener = new TextChangeListener() {

        @Override
        public void textChanged(TextChangedEvent event) {
            switch(verify) {
                case 1:
                    {
                        assertTrue(":1:", styledText.getText().equals("\ntesting"));
                        break;
                    }
                case 2:
                    {
                        assertTrue(":2:", styledText.getText().equals("a"));
                        break;
                    }
                case 3:
                    {
                        assertTrue(":3:", styledText.getText().equals("\n\n"));
                        break;
                    }
                case 4:
                    {
                        assertTrue(":4:", false);
                        break;
                    }
                case 5:
                    {
                        assertTrue(":5:", styledText.getText().equals("\rLine 1\r\nLine 2"));
                        break;
                    }
                case 6:
                    {
                        assertTrue(":6:", styledText.getText().equals("This\nis a test\nline 3"));
                        break;
                    }
                case 7:
                    {
                        assertTrue(":7:", styledText.getText().equals("This\n\r"));
                        break;
                    }
                case 8:
                    {
                        assertTrue(":8:", styledText.getText().equals("\nL1\r\nL2"));
                        break;
                    }
                case 9:
                    {
                        assertTrue(":9:", styledText.getText().equals("L1test"));
                        break;
                    }
                case 10:
                    {
                        assertTrue(":10:", false);
                        break;
                    }
                case 11:
                    {
                        assertTrue(":11:", false);
                        break;
                    }
                case 12:
                    {
                        assertTrue(":12:", styledText.getText().equals("L1\r\n"));
                        break;
                    }
                case 13:
                    {
                        assertTrue(":13:", styledText.getText().equals("L1\r\n"));
                        break;
                    }
                case 14:
                    {
                        assertTrue(":14:", false);
                        break;
                    }
                case 15:
                    {
                        assertTrue(":15:", styledText.getText().equals("L1test\n\n"));
                        break;
                    }
                case 16:
                    {
                        assertTrue(":16:", false);
                        break;
                    }
                case 17:
                    {
                        assertTrue(":17:", false);
                        break;
                    }
                case 18:
                    {
                        assertTrue(":18:", styledText.getText().equals("L1\r\ntest\r\n"));
                        break;
                    }
                case 19:
                    {
                        assertTrue(":19:", styledText.getText().equals("L1test\r\r\r\n"));
                        break;
                    }
                case 20:
                    {
                        assertTrue(":20:", false);
                        break;
                    }
            }
        }

        @Override
        public void textChanging(TextChangingEvent event) {
        }

        @Override
        public void textSet(TextChangedEvent event) {
        }
    };
    content.addTextChangeListener(listener);
    boolean exceptionHandled = false;
    verify = 0;
    styledText.setText("testing");
    verify = 1;
    styledText.replaceTextRange(0, 0, "\n");
    verify = 0;
    styledText.setText("\n\n");
    verify = 2;
    styledText.replaceTextRange(0, 2, "a");
    verify = 0;
    styledText.setText("a");
    verify = 3;
    styledText.replaceTextRange(0, 1, "\n\n");
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 4;
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":4: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("Line 1\r\nLine 2");
    verify = 5;
    styledText.replaceTextRange(0, 0, "\r");
    verify = 0;
    styledText.setText("This\nis a test\nline 3\nline 4");
    verify = 6;
    styledText.replaceTextRange(21, 7, "");
    verify = 0;
    styledText.setText("This\nis a test\r");
    verify = 7;
    styledText.replaceTextRange(5, 9, "");
    verify = 0;
    styledText.setText("\nL1\r\nL2\r\n");
    verify = 8;
    styledText.replaceTextRange(7, 2, "");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 9;
    styledText.replaceTextRange(2, 2, "test");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 10;
    try {
        styledText.replaceTextRange(3, 1, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":10: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 11;
    try {
        styledText.replaceTextRange(1, 2, "");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":11: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r");
    verify = 12;
    styledText.replaceTextRange(3, 0, "\n");
    verify = 0;
    styledText.setText("L1\n");
    verify = 13;
    styledText.replaceTextRange(2, 0, "\r");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 14;
    try {
        styledText.replaceTextRange(3, 0, "test");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":14: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 15;
    styledText.replaceTextRange(2, 2, "test\n\n");
    verify = 0;
    styledText.setText("L1\r\n");
    verify = 16;
    try {
        styledText.replaceTextRange(3, 1, "test\r\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":16: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 17;
    try {
        styledText.replaceTextRange(1, 2, "test\n\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":17: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    verify = 0;
    styledText.setText("L1\r");
    verify = 18;
    styledText.replaceTextRange(3, 0, "\ntest\r\n");
    verify = 0;
    styledText.setText("L1\n");
    verify = 19;
    styledText.replaceTextRange(2, 0, "test\r\r\r");
    verify = 0;
    styledText.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    verify = 20;
    try {
        styledText.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        exceptionHandled = true;
    }
    assertTrue(":20: exception not thrown", exceptionHandled);
    exceptionHandled = false;
    content.removeTextChangeListener(listener);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) TextChangingEvent(org.eclipse.swt.custom.TextChangingEvent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) TextChangedEvent(org.eclipse.swt.custom.TextChangedEvent) Test(org.junit.Test)

Example 23 with StyledTextContent

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

the class StyledTextContentSpec method test_Special_Cases.

public void test_Special_Cases() {
    String newText;
    StyledTextContent content = getContentInstance();
    assertWithMessage(":0a:", content.getLineCount() == 1);
    assertWithMessage(":0b:", content.getOffsetAtLine(0) == 0);
    content.setText("This is the input/output text component.");
    content.replaceTextRange(0, 0, "\n");
    assertWithMessage(":1a:", content.getLine(0).equals(""));
    content.replaceTextRange(1, 0, "\n");
    assertWithMessage(":1b:", content.getLine(0).equals(""));
    content.replaceTextRange(2, 0, "\n");
    assertWithMessage(":1c:", content.getLine(0).equals(""));
    content.replaceTextRange(3, 0, "\n");
    assertWithMessage(":1d:", content.getLine(0).equals(""));
    content.replaceTextRange(4, 0, "\n");
    assertWithMessage(":1e:", content.getLine(0).equals(""));
    content.replaceTextRange(5, 0, "\n");
    assertWithMessage(":1f:", content.getLine(0).equals(""));
    content.replaceTextRange(6, 0, "\n");
    assertWithMessage(":1g:", content.getLine(0).equals(""));
    content.replaceTextRange(7, 0, "\n");
    assertWithMessage(":1h:", content.getLine(0).equals(""));
    content.setText("This is the input/output text component.");
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":2a:", newText.equals("\nThis is the input/output text component."));
    assertWithMessage(":2b:", content.getLine(0).equals(""));
    assertWithMessage(":2c:", content.getLine(1).equals("This is the input/output text component."));
    content.replaceTextRange(1, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":2d:", newText.equals("\n\nThis is the input/output text component."));
    assertWithMessage(":2e:", content.getLine(0).equals(""));
    assertWithMessage(":2f:", content.getLine(1).equals(""));
    assertWithMessage(":2g:", content.getLine(2).equals("This is the input/output text component."));
    content.replaceTextRange(2, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":3a:", newText.equals("\n\n\nThis is the input/output text component."));
    assertWithMessage(":3b:", content.getLine(0).equals(""));
    assertWithMessage(":3c:", content.getLine(1).equals(""));
    assertWithMessage(":3d:", content.getLine(2).equals(""));
    assertWithMessage(":3e:", content.getLine(3).equals("This is the input/output text component."));
    content.replaceTextRange(3, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":3f:", newText.equals("\n\n\n\nThis is the input/output text component."));
    assertWithMessage(":3g:", content.getLine(0).equals(""));
    assertWithMessage(":3h:", content.getLine(1).equals(""));
    assertWithMessage(":3i:", content.getLine(2).equals(""));
    assertWithMessage(":3j:", content.getLine(3).equals(""));
    assertWithMessage(":3k:", content.getLine(4).equals("This is the input/output text component."));
    content.replaceTextRange(3, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":4a:", newText.equals("\n\n\nThis is the input/output text component."));
    assertWithMessage(":4b:", content.getLine(0).equals(""));
    assertWithMessage(":4c:", content.getLine(1).equals(""));
    assertWithMessage(":4d:", content.getLine(2).equals(""));
    assertWithMessage(":4e:", content.getLine(3).equals("This is the input/output text component."));
    content.replaceTextRange(2, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":4f:", newText.equals("\n\nThis is the input/output text component."));
    assertWithMessage(":4g:", content.getLine(0).equals(""));
    assertWithMessage(":4h:", content.getLine(1).equals(""));
    assertWithMessage(":4i:", content.getLine(2).equals("This is the input/output text component."));
    content.replaceTextRange(2, 0, "a");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":5a:", newText.equals("\n\naThis is the input/output text component."));
    assertWithMessage(":5b:", content.getLine(0).equals(""));
    assertWithMessage(":5c:", content.getLine(1).equals(""));
    assertWithMessage(":5d:", content.getLine(2).equals("aThis is the input/output text component."));
    // delete only part of a delimiter
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    content.replaceTextRange(0, 3, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":5e:", newText.equals("\nL2\r\nL3\r\nL4\r\n"));
    assertWithMessage(":5f:", content.getLineCount() == 5);
    assertWithMessage(":5g:", content.getLine(0).equals(""));
    assertWithMessage(":5h:", content.getLine(1).equals("L2"));
    assertWithMessage(":5i:", content.getLine(2).equals("L3"));
    assertWithMessage(":5j:", content.getLine(3).equals("L4"));
    assertWithMessage(":5k:", content.getLine(4).equals(""));
    // replace part of a delimiter
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    content.replaceTextRange(3, 1, "test\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":6a:", newText.equals("L1\rtest\nL2\r\nL3\r\nL4\r\n"));
    assertWithMessage(":6b:", content.getLineCount() == 6);
    assertWithMessage(":6c:", content.getLine(0).equals("L1"));
    assertWithMessage(":6d:", content.getLine(1).equals("test"));
    assertWithMessage(":6e:", content.getLine(2).equals("L2"));
    assertWithMessage(":6f:", content.getLine(3).equals("L3"));
    assertWithMessage(":6g:", content.getLine(4).equals("L4"));
    assertWithMessage(":6h:", content.getLine(5).equals(""));
    content.setText("abc\r\ndef");
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":7a:", newText.equals("ac\r\ndef"));
    assertWithMessage(":7b:", content.getLineCount() == 2);
    assertWithMessage(":7c:", content.getLine(0).equals("ac"));
    assertWithMessage(":7d:", content.getLine(1).equals("def"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":7e:", newText.equals("a\r\ndef"));
    assertWithMessage(":7f:", content.getLineCount() == 2);
    assertWithMessage(":7g:", content.getLine(0).equals("a"));
    assertWithMessage(":7h:", content.getLine(1).equals("def"));
    content.replaceTextRange(1, 2, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":7i:", newText.equals("adef"));
    assertWithMessage(":7j:", content.getLineCount() == 1);
    assertWithMessage(":7k:", content.getLine(0).equals("adef"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":7l:", newText.equals("aef"));
    assertWithMessage(":7m:", content.getLineCount() == 1);
    assertWithMessage(":7n:", content.getLine(0).equals("aef"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":7o:", newText.equals("af"));
    assertWithMessage(":7p:", content.getLineCount() == 1);
    assertWithMessage(":7q:", content.getLine(0).equals("af"));
    content.setText("abc");
    content.replaceTextRange(0, 1, "1");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":8a:", content.getLineCount() == 1);
    assertWithMessage(":8b:", newText.equals("1bc"));
    assertWithMessage(":8c:", content.getLine(0).equals("1bc"));
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":8d:", newText.equals("\n1bc"));
    assertWithMessage(":8e:", content.getLineCount() == 2);
    assertWithMessage(":8f:", content.getLine(0).equals(""));
    assertWithMessage(":8g:", content.getLine(1).equals("1bc"));
    content = getContentInstance();
    content.replaceTextRange(0, 0, "a");
    content.setText("package test;\n/* Line 1\n * Line 2\n */\npublic class SimpleClass {\n}");
    content.replaceTextRange(14, 23, "\t/*Line 1\n\t * Line 2\n\t */");
    newText = content.getTextRange(0, content.getCharCount());
    assertWithMessage(":9a:", newText.equals("package test;\n\t/*Line 1\n\t * Line 2\n\t */\npublic class SimpleClass {\n}"));
    assertWithMessage(":9b:", content.getLineCount() == 6);
    assertWithMessage(":9c:", content.getLine(0).equals("package test;"));
    assertWithMessage(":9d:", content.getLine(1).equals("\t/*Line 1"));
    assertWithMessage(":9e:", content.getLine(2).equals("\t * Line 2"));
    assertWithMessage(":9f:", content.getLine(3).equals("\t */"));
    assertWithMessage(":9g:", content.getLine(4).equals("public class SimpleClass {"));
    assertWithMessage(":9h:", content.getLine(5).equals("}"));
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 24 with StyledTextContent

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

the class StyledTextContentSpec method test_Line_Conversion.

public void test_Line_Conversion() {
    StyledTextContent content = getContentInstance();
    content.setText("This\nis a test\rrepeat\nend\r");
    assertWithMessage(":1a:", content.getLineCount() == 5);
    assertWithMessage(":1b:", content.getLine(0).equals("This"));
    assertWithMessage(":1c:", content.getOffsetAtLine(0) == 0);
    assertWithMessage(":1d:", content.getLine(1).equals("is a test"));
    assertWithMessage(":1e:", content.getLineAtOffset(4) == 0);
    assertWithMessage(":1f:", content.getOffsetAtLine(1) == 5);
    assertWithMessage(":1g:", content.getLine(2).equals("repeat"));
    assertWithMessage(":1h:", content.getOffsetAtLine(2) == 15);
    assertWithMessage(":1i:", content.getLine(3).equals("end"));
    assertWithMessage(":1j:", content.getOffsetAtLine(3) == 22);
    assertWithMessage(":1k:", content.getLine(4).equals(""));
    assertWithMessage(":1l:", content.getOffsetAtLine(4) == 26);
    content.setText("This\r\nis a test");
    assertWithMessage(":2a:", content.getLineCount() == 2);
    assertWithMessage(":2b:", content.getLine(1).equals("is a test"));
    assertWithMessage(":2c:", content.getLineAtOffset(4) == 0);
    assertWithMessage(":2d:", content.getLineAtOffset(5) == 0);
    content.setText("This\r\nis a test\r");
    assertWithMessage(":3a:", content.getLineCount() == 3);
    assertWithMessage(":3b:", content.getLine(1).equals("is a test"));
    assertWithMessage(":3c:", content.getLineAtOffset(15) == 1);
    content.setText("\r\n");
    assertWithMessage(":4a:", content.getLineCount() == 2);
    assertWithMessage(":4b:", content.getLine(0).equals(""));
    assertWithMessage(":4c:", content.getLine(1).equals(""));
    assertWithMessage(":4d:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":4e:", content.getLineAtOffset(1) == 0);
    assertWithMessage(":4f:", content.getLineAtOffset(2) == 1);
    content.setText("\r\n\n\r\r\n");
    assertWithMessage(":5a:", content.getLineCount() == 5);
    assertWithMessage(":5b:", content.getLine(0).equals(""));
    assertWithMessage(":5c:", content.getOffsetAtLine(0) == 0);
    assertWithMessage(":5d:", content.getLine(1).equals(""));
    assertWithMessage(":5e:", content.getOffsetAtLine(1) == 2);
    assertWithMessage(":5f:", content.getLine(2).equals(""));
    assertWithMessage(":5g:", content.getOffsetAtLine(2) == 3);
    assertWithMessage(":5h:", content.getLine(3).equals(""));
    assertWithMessage(":5i:", content.getOffsetAtLine(3) == 4);
    assertWithMessage(":5j:", content.getLine(4).equals(""));
    assertWithMessage(":5k:", content.getOffsetAtLine(4) == 6);
    content.setText("test\r\rtest2\r\r");
    assertWithMessage(":6a:", content.getLineCount() == 5);
    assertWithMessage(":6b:", content.getLine(0).equals("test"));
    assertWithMessage(":6c:", content.getOffsetAtLine(0) == 0);
    assertWithMessage(":6d:", content.getLine(1).equals(""));
    assertWithMessage(":6e:", content.getOffsetAtLine(1) == 5);
    assertWithMessage(":6f:", content.getLine(2).equals("test2"));
    assertWithMessage(":6g:", content.getOffsetAtLine(2) == 6);
    assertWithMessage(":6h:", content.getLine(3).equals(""));
    assertWithMessage(":6i:", content.getOffsetAtLine(3) == 12);
    assertWithMessage(":6j:", content.getLine(4).equals(""));
    assertWithMessage(":6k:", content.getOffsetAtLine(4) == 13);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 25 with StyledTextContent

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

the class StyledTextContentSpec method test_Offset_To_Line.

public void test_Offset_To_Line() {
    StyledTextContent content = getContentInstance();
    content.setText("This\nis a test\rrepeat\nend\r");
    assertWithMessage(":1a:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":1b:", content.getLineAtOffset(3) == 0);
    assertWithMessage(":1c:", content.getLineAtOffset(4) == 0);
    assertWithMessage(":1d:", content.getLineAtOffset(25) == 3);
    assertWithMessage(":1e:", content.getLineAtOffset(26) == 4);
    content.setText("This\r\nis a test");
    assertWithMessage(":2a:", content.getLineAtOffset(5) == 0);
    assertWithMessage(":2b:", content.getLineAtOffset(6) == 1);
    assertWithMessage(":2c:", content.getLineAtOffset(10) == 1);
    content.setText("\r\n");
    assertWithMessage(":3a:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":3b:", content.getLineAtOffset(1) == 0);
    assertWithMessage(":3c:", content.getLineAtOffset(2) == 1);
    content.setText("\r\n\n\r\r\n");
    assertWithMessage(":4a:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":4b:", content.getLineAtOffset(1) == 0);
    assertWithMessage(":4c:", content.getLineAtOffset(2) == 1);
    assertWithMessage(":4d:", content.getLineAtOffset(3) == 2);
    assertWithMessage(":4e:", content.getLineAtOffset(4) == 3);
    assertWithMessage(":4f:", content.getLineAtOffset(5) == 3);
    assertWithMessage(":4g:", content.getLineAtOffset(6) == 4);
    content.setText("\r\n\r\n");
    assertWithMessage(":5a:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":5b:", content.getLineAtOffset(1) == 0);
    assertWithMessage(":5c:", content.getLineAtOffset(2) == 1);
    assertWithMessage(":5d:", content.getLineAtOffset(3) == 1);
    assertWithMessage(":5e:", content.getLineAtOffset(4) == 2);
    content.setText("\r\r\r\n\r\n");
    assertWithMessage(":6a:", content.getLineAtOffset(0) == 0);
    assertWithMessage(":6b:", content.getLineAtOffset(1) == 1);
    assertWithMessage(":6c:", content.getLineAtOffset(2) == 2);
    assertWithMessage(":6d:", content.getLineAtOffset(4) == 3);
    content.setText("");
    assertWithMessage(":7a:", content.getLineAtOffset(0) == 0);
    content = getContentInstance();
    assertWithMessage(":8a:", content.getLineAtOffset(0) == 0);
}
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