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();
}
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();
}
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();
}
Aggregations