Search in sources :

Example 6 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project bmoth by hhu-stups.

the class Highlighter method computeHighlighting.

protected static StyleSpans<Collection<String>> computeHighlighting(String text) {
    String[] groups = new String[] { "START", "KEYWORD", "KEYWORD2", "PAREN", "BRACE", "BRACKET", "SEMICOLON", "STRING", "COMMENT" };
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = null;
        /* never happens */
        for (String group : groups) {
            if (matcher.group(group) != null) {
                styleClass = group.toLowerCase();
                break;
            }
        }
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Matcher(java.util.regex.Matcher) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder)

Example 7 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project ETUmulator by kasirgalabs.

the class SyntaxHighlighter method highlightMisspelled.

private StyleSpans<Collection<String>> highlightMisspelled(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(text);
    int lastIndex = wb.first();
    int lastKeywordEnd = 0;
    while (lastIndex != BreakIterator.DONE) {
        int firstIndex = lastIndex;
        lastIndex = wb.next();
        if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
            String word = text.substring(firstIndex, lastIndex).toLowerCase();
            if (!dictionary.contains(word)) {
                spansBuilder.add(Collections.emptyList(), firstIndex - lastKeywordEnd);
                spansBuilder.add(Collections.singleton("underlined"), lastIndex - firstIndex);
                lastKeywordEnd = lastIndex;
            }
        }
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKeywordEnd);
    return spansBuilder.create();
}
Also used : Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder) BreakIterator(java.text.BreakIterator)

Example 8 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project org.csstudio.display.builder by kasemir.

the class JavaScriptHighlighter method computeHighlighting.

@Override
public StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("PAREN") != null ? "paren" : matcher.group("BRACE") != null ? "brace" : matcher.group("BRACKET") != null ? "bracket" : matcher.group("SEMICOLON") != null ? "semicolon" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : null;
        // It should never happen.
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Matcher(java.util.regex.Matcher) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder)

Example 9 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project RichTextFX by FXMisc.

the class JavaKeywords method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("PAREN") != null ? "paren" : matcher.group("BRACE") != null ? "brace" : matcher.group("BRACKET") != null ? "bracket" : matcher.group("SEMICOLON") != null ? "semicolon" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : null;
        /* never happens */
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Matcher(java.util.regex.Matcher) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder)

Example 10 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project RichTextFX by FXMisc.

the class JavaKeywordsAsync method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("PAREN") != null ? "paren" : matcher.group("BRACE") != null ? "brace" : matcher.group("BRACKET") != null ? "bracket" : matcher.group("SEMICOLON") != null ? "semicolon" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : null;
        /* never happens */
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Matcher(java.util.regex.Matcher) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder)

Aggregations

Collection (java.util.Collection)13 StyleSpansBuilder (org.fxmisc.richtext.model.StyleSpansBuilder)13 Matcher (java.util.regex.Matcher)11 BreakIterator (java.text.BreakIterator)2 ArrayList (java.util.ArrayList)1