本文整理汇总了Java中org.fxmisc.richtext.model.StyleSpans类的典型用法代码示例。如果您正苦于以下问题:Java StyleSpans类的具体用法?Java StyleSpans怎么用?Java StyleSpans使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StyleSpans类属于org.fxmisc.richtext.model包,在下文中一共展示了StyleSpans类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compute
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
public void compute(String text, CodeArea parent) {
Matcher matcher = getHighlightingPattern().matcher(text);
while(matcher.find()) {
String styleClass = getStyleClass(matcher);
// For now lets setStyles for base style.
parent.setStyle(matcher.start(), matcher.end(), Collections.singleton(styleClass));
// Then we can grab the style from the document via subView and overlay
for(HighlightStyle style : getChildStyles(styleClass, getText(matcher.start(), matcher.end(), parent))){
StyleSpans spans = parent.getStyleSpans(matcher.start(), matcher.end()).subView(style.start, style.end).overlay(
StyleSpans.singleton(new StyleSpan<>(
Arrays.asList(style.styleClasses),
(style.end - style.start)
)),
(strings, strings2) -> {
Collection<String> coll = Streams.concat(strings.stream(), strings2.stream()).collect(Collectors.toList());
logger.debug(coll.toString());
return coll;
}
);
parent.setStyleSpans(matcher.start(), spans); // Reset styles
}
}
}
示例2: highlight
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
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();
}
示例3: highlightMisspelled
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
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();
}
示例4: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
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();
}
示例5: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
public static StyleSpans<Collection<String>> computeHighlighting(String text, Pattern pattern) {
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("DIGIT") != null ? "digit" :
matcher.group("STRING") != null ? "string" :
matcher.group("COMMENT") != null ? "comment" :
null; /* never happens */
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singletonList(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
示例6: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
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("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();
}
示例7: replaceText
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
void replaceText(String text, StyleSpans<? extends Collection<String>> styleSpans) {
// remember old selection range and scrollY
IndexRange oldSelection = getSelection();
double oldScrollY = getEstimatedScrollY();
// replace text and styles
doReplaceText(text);
if (styleSpans != null)
setStyleSpans(0, styleSpans);
// restore old selection range and scrollY
int newLength = getLength();
selectRange(Math.min(oldSelection.getStart(), newLength), Math.min(oldSelection.getEnd(), newLength));
Platform.runLater(() -> {
estimatedScrollYProperty().setValue(oldScrollY);
});
}
示例8: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
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();
}
示例9: computeHighlightingByReg
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
public static StyleSpans<Collection<String>> computeHighlightingByReg(String text) {
Matcher matcher = PATTERN.matcher(text);
System.out.println(CSS_PATTERN);
System.out.println(MUTI_COMMENT_REGEX);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>();
while(matcher.find()) {
String styleClass =
matcher.group("CSS") != null ? "css" :
matcher.group("COMMENT") != null ? "multicomment" :
matcher.group("ENTRY") != null ? "entries" :
matcher.group("SELECTOR") != null ? "selector" :
// matcher.group("COLON") != null ? "colon" :
// matcher.group("SEMICOLON") != null ? "semicolon" :
// matcher.group("COMMA") != null ? "comma" :
// matcher.group("STRING") != null ? "string" :
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();
}
示例10: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
/**
* Calculate highlighting for the code.
*
* @param pattern the pattern.
* @param text the text.
* @return the highlight styles.
*/
@FXThread
protected @NotNull StyleSpans<Collection<String>> computeHighlighting(@NotNull final Pattern pattern,
@NotNull final String text) {
final Matcher matcher = pattern.matcher(text);
final StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
int lastKwEnd = 0;
while (matcher.find()) {
String styleClass = null;
for (final String[] availableClass : getAvailableClasses()) {
try {
styleClass = matcher.group(availableClass[0]) != null ? availableClass[1] : null;
} catch (final IllegalArgumentException e) {
continue;
}
if(styleClass != null) {
break;
}
}
assert styleClass != null;
spansBuilder.add(singleton("plain-code"), matcher.start() - lastKwEnd);
spansBuilder.add(singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(singleton("plain-code"), text.length() - lastKwEnd);
return spansBuilder.create();
}
示例11: computeHighlightingAsync
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
private Task<StyleSpans<Collection<String>>> computeHighlightingAsync(CodeArea codeArea) {
String text = codeArea.getText();
Task<StyleSpans<Collection<String>>> task = new Task<StyleSpans<Collection<String>>>() {
@Override
protected StyleSpans<Collection<String>> call() throws Exception {
return computeHighlighting(text);
}
};
Executors.newSingleThreadExecutor().execute(task);
return task;
}
示例12: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
private 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 = null;
String s;
if ((s = matcher.group("IDENTIFIER")) != null) {
for (String command : ExpressionInterpreter.getSupportedCommands()) {
if (s.equalsIgnoreCase(command)) {
styleClass = "command";
break;
}
}
if (s.equals("_x")) {
styleClass = "magic-var";
}
} else if (matcher.group("NUMBER") != null) {
styleClass = "number";
} else if (matcher.group("STRING") != null) {
styleClass = "string";
} else if (matcher.group("COMMENT") != null) {
styleClass = "comment";
}
if (styleClass == null) {
continue;
}
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();
}
示例13: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
text = text.replaceAll("%%", "__");//prevent matching %%1 or %%%%1. We can't remove the %% however because that would mess with the indexes
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
while (matcher.find()) {
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton("param"), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
示例14: computeHighlighting
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
SyntaxHighlighter.highlight(text, "html", (length, style) -> {
spansBuilder.add(toSpanStyle(style), length);
});
return spansBuilder.create();
}
示例15: editorSelectionChanged
import org.fxmisc.richtext.model.StyleSpans; //导入依赖的package包/类
@Override
public void editorSelectionChanged(PreviewContext context, IndexRange range) {
if (range.equals(lastEditorSelection))
return;
lastEditorSelection = range;
List<Range> sequences = context.getRenderer().findSequences(range.getStart(), range.getEnd());
// restore old styles
for (Map.Entry<Integer, StyleSpans<Collection<String>>> e : oldSelectionStylesMap.entrySet())
textArea.setStyleSpans(e.getKey(), e.getValue());
oldSelectionStylesMap.clear();
// set new selection styles
String text = textArea.getText();
for (Range sequence : sequences) {
String rangeStr = "[" + sequence.start + ", " + sequence.end;
int index = 0;
while ((index = text.indexOf(rangeStr, index)) >= 0) {
int endIndex = index + rangeStr.length() + 1;
char after = text.charAt(endIndex - 1);
if ((after == ']' || after == ',') && !oldSelectionStylesMap.containsKey(index)) {
oldSelectionStylesMap.put(index, textArea.getStyleSpans(index, endIndex));
textArea.setStyle(index, endIndex, STYLE_SELECTION);
}
index = endIndex;
}
}
}