Search in sources :

Example 11 with StyleSpansBuilder

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

the class SpellChecking method computeHighlighting.

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    BreakIterator wb = BreakIterator.getWordInstance();
    wb.setText(text);
    int lastIndex = wb.first();
    int lastKwEnd = 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 - lastKwEnd);
                spansBuilder.add(Collections.singleton("underlined"), lastIndex - firstIndex);
                lastKwEnd = lastIndex;
            }
            System.err.println();
        }
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}
Also used : Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder) BreakIterator(java.text.BreakIterator)

Example 12 with StyleSpansBuilder

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

the class SyntaxHighlighter method highlight.

public StyleSpans<Collection<String>> highlight(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKeywordEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : matcher.group("LABEL") != null ? "label" : null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKeywordEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKeywordEnd = matcher.end();
    }
    if (lastKeywordEnd == 0) {
        return highlightMisspelled(text);
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKeywordEnd);
    return spansBuilder.create();
}
Also used : Matcher(java.util.regex.Matcher) Collection(java.util.Collection) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder)

Example 13 with StyleSpansBuilder

use of org.fxmisc.richtext.model.StyleSpansBuilder in project pmd by pmd.

the class SimpleRegexSyntaxHighlighter method computeHighlighting.

@Override
public StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> builder = new StyleSpansBuilder<>();
    Matcher matcher = grammar.getMatcher(text);
    int lastKwEnd = 0;
    final Set<String> onlyLang = Collections.singleton(languageName);
    try {
        while (matcher.find()) {
            Set<String> styleClasses = grammar.getCssClassesOfLastGroup(matcher);
            builder.add(onlyLang, matcher.start() - lastKwEnd);
            builder.add(styleClasses, matcher.end() - matcher.start());
            lastKwEnd = matcher.end();
        }
    } catch (StackOverflowError ignored) {
    // matcher.find overflowed, might happen when coloring ginormous files with incorrect language
    }
    if (lastKwEnd == 0) {
        // no spans found/ no text
        builder.add(onlyLang, text.length());
    }
    return builder.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