我尝试在项目中使用RichTextFX
来编辑代码。
' String Pattern'似乎不能正常工作。
CodeArea: 如果我在其中输入多个引号,那么之后的代码将与" String" 相同。
我不知道如何修复它。 我的模式或其他什么东西有问题吗?还是匹配者?
这个类扩展了CodeArea,有一个Matcher和荧光笔。
谢谢!
package application.control;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Collections;
import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SqlCodeEditor extends CodeArea {
private static final String PATH_TO_KEYWORDS = "src/richtextfx/keywords.txt";
private static final String PAREN_PATTERN = "\\(|\\)";
private static final String BRACE_PATTERN = "\\{|\\}";
private static final String BRACKET_PATTERN = "\\[|\\]";
private static final String SEMICOLON_PATTERN = "\\;";
private static final String STRING_PATTERN = "\"([^\"]|\\\")*\"" + "|" + "'([^']|\\')*'";
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
private Pattern pattern;
public SqlCodeEditor() {
super();
if (pattern == null) {
makePattern();
}
IntFunction<String> format = (digits -> " %" + digits + "d ");
this.setParagraphGraphicFactory(LineNumberFactory.get(this, format));
this.textProperty()
.addListener(
(observable, oldValue, newValue) ->
this.setStyleSpans(0, computeHighlighting(newValue)));
this.setStyle("-fx-font-family: 'YaHei Consolas Hybrid'; -fx-font-size: 14px;");
this.setStyle("-fx-font-family: 'Monaco'; -fx-font-size: 14px;");
this.getStylesheets().add("/richtextfx/keywords.css");
this.replaceText(0, 0, "CREATE TABLE 'piggy'.'student'\n 'name' varchar(20)");
// new SqlHighlighter(this).highlight();
}
/** For Content highlighting */
private StyleSpans<Collection<String>> computeHighlighting(String text) {
if (pattern == null) {
makePattern();
}
Matcher matcher = pattern.matcher(text.toUpperCase());
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"
: 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();
}
private void makePattern() {
try (final Stream<String> lines =
new BufferedReader(new InputStreamReader(new FileInputStream(PATH_TO_KEYWORDS))).lines()) {
final String keywords = lines.collect(Collectors.joining("|"));
final String patterns = "\\b(" + keywords + ")\\b";
this.pattern =
Pattern.compile(
"(?<KEYWORD>"
+ patterns
+ ")"
+ "|(?<PAREN>"
+ PAREN_PATTERN
+ ")"
+ "|(?<BRACE>"
+ BRACE_PATTERN
+ ")"
+ "|(?<BRACKET>"
+ BRACKET_PATTERN
+ ")"
+ "|(?<SEMICOLON>"
+ SEMICOLON_PATTERN
+ ")"
+ "|(?<STRING>"
+ STRING_PATTERN
+ ")");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}