Search in sources :

Example 1 with StyledTextContent

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

the class WhitespaceCharacterPainter method drawCharRange.

/**
 * Draw characters of content range.
 *
 * @param gc the GC
 * @param startOffset inclusive start index of the drawing range
 * @param endOffset exclusive end index of the drawing range
 * @param lineOffset inclusive start index of the line
 * @param lineEndOffset exclusive end index of the line
 * @param spaceCharsAreSameWidth whether or not all space chars are same width, if <code>true</code>
 *            rendering can be optimized
 */
private void drawCharRange(GC gc, int startOffset, int endOffset, int lineOffset, int lineEndOffset, boolean spaceCharsAreSameWidth) {
    StyledTextContent content = fTextWidget.getContent();
    String lineText = content.getTextRange(lineOffset, lineEndOffset - lineOffset);
    int startOffsetInLine = startOffset - lineOffset;
    int endOffsetInLine = endOffset - lineOffset;
    int textBegin = -1;
    for (int i = 0; i < lineText.length(); ++i) {
        if (!isWhitespaceCharacter(lineText.charAt(i))) {
            textBegin = i;
            break;
        }
    }
    boolean isEmptyLine = textBegin == -1;
    int textEnd = lineText.length() - 1;
    if (!isEmptyLine) {
        for (int i = lineText.length() - 1; i >= 0; --i) {
            if (!isWhitespaceCharacter(lineText.charAt(i))) {
                textEnd = i;
                break;
            }
        }
    }
    StyleRange styleRange = null;
    Color fg = null;
    StringBuilder visibleChar = new StringBuilder(10);
    int delta = 0;
    for (int textOffset = startOffsetInLine; textOffset <= endOffsetInLine; ++textOffset) {
        boolean eol = false;
        delta++;
        if (textOffset < endOffsetInLine) {
            char c = lineText.charAt(textOffset);
            switch(c) {
                case ' ':
                    if (isEmptyLine) {
                        if (fShowLeadingSpaces || fShowEnclosedSpace || fShowTrailingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedSpace) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else {
                        if (fShowTrailingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    }
                    // it can be used only for monospace fonts
                    if (spaceCharsAreSameWidth) {
                        continue;
                    }
                    break;
                case // ideographic whitespace
                '\u3000':
                    if (isEmptyLine) {
                        if (fShowLeadingIdeographicSpaces || fShowEnclosedIdeographicSpaces || fShowTrailingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else {
                        if (fShowTrailingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    }
                    // it can be used only for monospace fonts
                    if (spaceCharsAreSameWidth) {
                        continue;
                    }
                    break;
                case '\t':
                    if (isEmptyLine) {
                        if (fShowLeadingTabs || fShowEnclosedTabs || fShowTrailingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else {
                        if (fShowTrailingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    }
                    break;
                case '\r':
                    if (fShowCarriageReturn) {
                        visibleChar.append(CARRIAGE_RETURN_SIGN);
                    }
                    if (textOffset >= endOffsetInLine - 1 || lineText.charAt(textOffset + 1) != '\n') {
                        eol = true;
                        break;
                    }
                    continue;
                case '\n':
                    if (fShowLineFeed) {
                        visibleChar.append(LINE_FEED_SIGN);
                    }
                    eol = true;
                    break;
                default:
                    break;
            }
        }
        if (visibleChar.length() > 0) {
            int widgetOffset = startOffset + textOffset - startOffsetInLine - delta + 1;
            if (!eol || !isFoldedLine(content.getLineAtOffset(widgetOffset))) {
                /*
					 * Block selection is drawn using alpha and no selection-inverting
					 * takes place, we always draw as 'unselected' in block selection mode.
					 */
                if (!fTextWidget.getBlockSelection() && fIsFullSelectionStyle && isOffsetSelected(fTextWidget, widgetOffset)) {
                    fg = fTextWidget.getSelectionForeground();
                } else if (styleRange == null || styleRange.start + styleRange.length <= widgetOffset) {
                    styleRange = fTextWidget.getStyleRangeAtOffset(widgetOffset);
                    if (styleRange == null || styleRange.foreground == null) {
                        fg = fTextWidget.getForeground();
                    } else {
                        fg = styleRange.foreground;
                    }
                }
                draw(gc, widgetOffset, visibleChar.toString(), fg);
            }
            visibleChar.delete(0, visibleChar.length());
        }
        delta = 0;
    }
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) StyleRange(org.eclipse.swt.custom.StyleRange) Color(org.eclipse.swt.graphics.Color) Point(org.eclipse.swt.graphics.Point)

Example 2 with StyledTextContent

use of org.eclipse.swt.custom.StyledTextContent in project eclipse.platform.swt 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");
    assertTrue(":1a:", content.getLineAtOffset(0) == 0);
    assertTrue(":1b:", content.getLineAtOffset(3) == 0);
    assertTrue(":1c:", content.getLineAtOffset(4) == 0);
    assertTrue(":1d:", content.getLineAtOffset(25) == 3);
    assertTrue(":1e:", content.getLineAtOffset(26) == 4);
    content.setText("This\r\nis a test");
    assertTrue(":2a:", content.getLineAtOffset(5) == 0);
    assertTrue(":2b:", content.getLineAtOffset(6) == 1);
    assertTrue(":2c:", content.getLineAtOffset(10) == 1);
    content.setText("\r\n");
    assertTrue(":3a:", content.getLineAtOffset(0) == 0);
    assertTrue(":3b:", content.getLineAtOffset(1) == 0);
    assertTrue(":3c:", content.getLineAtOffset(2) == 1);
    content.setText("\r\n\n\r\r\n");
    assertTrue(":4a:", content.getLineAtOffset(0) == 0);
    assertTrue(":4b:", content.getLineAtOffset(1) == 0);
    assertTrue(":4c:", content.getLineAtOffset(2) == 1);
    assertTrue(":4d:", content.getLineAtOffset(3) == 2);
    assertTrue(":4e:", content.getLineAtOffset(4) == 3);
    assertTrue(":4f:", content.getLineAtOffset(5) == 3);
    assertTrue(":4g:", content.getLineAtOffset(6) == 4);
    content.setText("\r\n\r\n");
    assertTrue(":5a:", content.getLineAtOffset(0) == 0);
    assertTrue(":5b:", content.getLineAtOffset(1) == 0);
    assertTrue(":5c:", content.getLineAtOffset(2) == 1);
    assertTrue(":5d:", content.getLineAtOffset(3) == 1);
    assertTrue(":5e:", content.getLineAtOffset(4) == 2);
    content.setText("\r\r\r\n\r\n");
    assertTrue(":6a:", content.getLineAtOffset(0) == 0);
    assertTrue(":6b:", content.getLineAtOffset(1) == 1);
    assertTrue(":6c:", content.getLineAtOffset(2) == 2);
    assertTrue(":6d:", content.getLineAtOffset(4) == 3);
    content.setText("");
    assertTrue(":7a:", content.getLineAtOffset(0) == 0);
    content = getContentInstance();
    assertTrue(":8a:", content.getLineAtOffset(0) == 0);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 3 with StyledTextContent

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

the class StyledTextContentSpec method test_Text_Changed_Event.

public void test_Text_Changed_Event() {
    StyledTextContent content = getContentInstance();
    content.addTextChangeListener(this);
    verify = 1;
    content.setText("testing");
    content.replaceTextRange(0, 0, "\n");
    verify = 2;
    content.setText("\n\n");
    content.replaceTextRange(0, 2, "a");
    verify = 3;
    content.setText("a");
    content.replaceTextRange(0, 1, "\n\n");
    verify = 4;
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        content.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        assertTrue(":4:", true);
    }
    verify = 5;
    content.setText("Line 1\r\nLine 2");
    content.replaceTextRange(0, 0, "\r");
    verify = 6;
    content.setText("This\nis a test\nline 3\nline 4");
    content.replaceTextRange(21, 7, "");
    verify = 7;
    content.setText("This\nis a test\r");
    content.replaceTextRange(5, 9, "");
    verify = 8;
    content.setText("\nL1\r\nL2\r\n");
    content.replaceTextRange(7, 2, "");
    verify = 9;
    content.setText("L1\r\n");
    content.replaceTextRange(2, 2, "test");
    verify = 10;
    content.setText("L1\r\n");
    try {
        content.replaceTextRange(3, 1, "");
    } catch (IllegalArgumentException ex) {
        assertTrue(":10:", true);
    }
    verify = 11;
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        content.replaceTextRange(1, 2, "");
    } catch (IllegalArgumentException ex) {
        assertTrue(":11:", true);
    }
    verify = 12;
    content.setText("L1\r");
    content.replaceTextRange(3, 0, "\n");
    verify = 13;
    content.setText("L1\n");
    content.replaceTextRange(2, 0, "\r");
    verify = 14;
    content.setText("L1\r\n");
    try {
        content.replaceTextRange(3, 0, "test");
    } catch (IllegalArgumentException ex) {
        assertTrue(":14:", true);
    }
    verify = 15;
    content.setText("L1\r\n");
    content.replaceTextRange(2, 2, "test\n\n");
    verify = 16;
    content.setText("L1\r\n");
    try {
        content.replaceTextRange(3, 1, "test\r\n");
    } catch (IllegalArgumentException ex) {
        assertTrue(":16:", true);
    }
    verify = 17;
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        content.replaceTextRange(1, 2, "test\n\n");
    } catch (IllegalArgumentException ex) {
        assertTrue(":17:", true);
    }
    verify = 18;
    content.setText("L1\r");
    content.replaceTextRange(3, 0, "\ntest\r\n");
    verify = 19;
    content.setText("L1\n");
    content.replaceTextRange(2, 0, "test\r\r\r");
    verify = 20;
    content.setText("L1\r\nL2\r\nL3\r\nL4\r\n");
    try {
        content.replaceTextRange(3, 1, "test\n");
    } catch (IllegalArgumentException ex) {
        assertTrue(":20:", true);
    }
    verify = 0;
    content.removeTextChangeListener(this);
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 4 with StyledTextContent

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

the class StyledTextContentSpec method test_Empty.

public void test_Empty() {
    StyledTextContent content = getContentInstance();
    assertTrue(":1a:", content.getLineCount() == 1);
    assertTrue(":1b:", content.getLine(0).isEmpty());
    content.setText("test");
    content.replaceTextRange(0, 4, "");
    assertTrue(":2a:", content.getLineCount() == 1);
    assertTrue(":2b:", content.getLine(0).isEmpty());
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent)

Example 5 with StyledTextContent

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

the class StyledTextContentSpec method test_Special_Cases.

public void test_Special_Cases() {
    String newText;
    StyledTextContent content = getContentInstance();
    assertTrue(":0a:", content.getLineCount() == 1);
    assertTrue(":0b:", content.getOffsetAtLine(0) == 0);
    content.setText("This is the input/output text component.");
    content.replaceTextRange(0, 0, "\n");
    assertTrue(":1a:", content.getLine(0).isEmpty());
    content.replaceTextRange(1, 0, "\n");
    assertTrue(":1b:", content.getLine(0).isEmpty());
    content.replaceTextRange(2, 0, "\n");
    assertTrue(":1c:", content.getLine(0).isEmpty());
    content.replaceTextRange(3, 0, "\n");
    assertTrue(":1d:", content.getLine(0).isEmpty());
    content.replaceTextRange(4, 0, "\n");
    assertTrue(":1e:", content.getLine(0).isEmpty());
    content.replaceTextRange(5, 0, "\n");
    assertTrue(":1f:", content.getLine(0).isEmpty());
    content.replaceTextRange(6, 0, "\n");
    assertTrue(":1g:", content.getLine(0).isEmpty());
    content.replaceTextRange(7, 0, "\n");
    assertTrue(":1h:", content.getLine(0).isEmpty());
    content.setText("This is the input/output text component.");
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":2a:", newText.equals("\nThis is the input/output text component."));
    assertTrue(":2b:", content.getLine(0).isEmpty());
    assertTrue(":2c:", content.getLine(1).equals("This is the input/output text component."));
    content.replaceTextRange(1, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":2d:", newText.equals("\n\nThis is the input/output text component."));
    assertTrue(":2e:", content.getLine(0).isEmpty());
    assertTrue(":2f:", content.getLine(1).isEmpty());
    assertTrue(":2g:", content.getLine(2).equals("This is the input/output text component."));
    content.replaceTextRange(2, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":3a:", newText.equals("\n\n\nThis is the input/output text component."));
    assertTrue(":3b:", content.getLine(0).isEmpty());
    assertTrue(":3c:", content.getLine(1).isEmpty());
    assertTrue(":3d:", content.getLine(2).isEmpty());
    assertTrue(":3e:", content.getLine(3).equals("This is the input/output text component."));
    content.replaceTextRange(3, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":3f:", newText.equals("\n\n\n\nThis is the input/output text component."));
    assertTrue(":3g:", content.getLine(0).isEmpty());
    assertTrue(":3h:", content.getLine(1).isEmpty());
    assertTrue(":3i:", content.getLine(2).isEmpty());
    assertTrue(":3j:", content.getLine(3).isEmpty());
    assertTrue(":3k:", content.getLine(4).equals("This is the input/output text component."));
    content.replaceTextRange(3, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":4a:", newText.equals("\n\n\nThis is the input/output text component."));
    assertTrue(":4b:", content.getLine(0).isEmpty());
    assertTrue(":4c:", content.getLine(1).isEmpty());
    assertTrue(":4d:", content.getLine(2).isEmpty());
    assertTrue(":4e:", content.getLine(3).equals("This is the input/output text component."));
    content.replaceTextRange(2, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":4f:", newText.equals("\n\nThis is the input/output text component."));
    assertTrue(":4g:", content.getLine(0).isEmpty());
    assertTrue(":4h:", content.getLine(1).isEmpty());
    assertTrue(":4i:", content.getLine(2).equals("This is the input/output text component."));
    content.replaceTextRange(2, 0, "a");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":5a:", newText.equals("\n\naThis is the input/output text component."));
    assertTrue(":5b:", content.getLine(0).isEmpty());
    assertTrue(":5c:", content.getLine(1).isEmpty());
    assertTrue(":5d:", content.getLine(2).equals("aThis is the input/output text component."));
    content.setText("abc\r\ndef");
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6a:", newText.equals("ac\r\ndef"));
    assertTrue(":6b:", content.getLineCount() == 2);
    assertTrue(":6c:", content.getLine(0).equals("ac"));
    assertTrue(":6d:", content.getLine(1).equals("def"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6e:", newText.equals("a\r\ndef"));
    assertTrue(":6f:", content.getLineCount() == 2);
    assertTrue(":6g:", content.getLine(0).equals("a"));
    assertTrue(":6h:", content.getLine(1).equals("def"));
    content.replaceTextRange(1, 2, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6i:", newText.equals("adef"));
    assertTrue(":6j:", content.getLineCount() == 1);
    assertTrue(":6k:", content.getLine(0).equals("adef"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6l:", newText.equals("aef"));
    assertTrue(":6m:", content.getLineCount() == 1);
    assertTrue(":6n:", content.getLine(0).equals("aef"));
    content.replaceTextRange(1, 1, "");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":6o:", newText.equals("af"));
    assertTrue(":6p:", content.getLineCount() == 1);
    assertTrue(":6q:", content.getLine(0).equals("af"));
    content.setText("abc");
    content.replaceTextRange(0, 1, "1");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":7a:", content.getLineCount() == 1);
    assertTrue(":7b:", newText.equals("1bc"));
    assertTrue(":7c:", content.getLine(0).equals("1bc"));
    content.replaceTextRange(0, 0, "\n");
    newText = content.getTextRange(0, content.getCharCount());
    assertTrue(":7d:", newText.equals("\n1bc"));
    assertTrue(":7e:", content.getLineCount() == 2);
    assertTrue(":7f:", content.getLine(0).isEmpty());
    assertTrue(":7g:", 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());
    assertTrue(":8a:", newText.equals("package test;\n\t/*Line 1\n\t * Line 2\n\t */\npublic class SimpleClass {\n}"));
    assertTrue(":8b:", content.getLineCount() == 6);
    assertTrue(":8c:", content.getLine(0).equals("package test;"));
    assertTrue(":8d:", content.getLine(1).equals("\t/*Line 1"));
    assertTrue(":8e:", content.getLine(2).equals("\t * Line 2"));
    assertTrue(":8f:", content.getLine(3).equals("\t */"));
    assertTrue(":8g:", content.getLine(4).equals("public class SimpleClass {"));
    assertTrue(":8h:", content.getLine(5).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