我正在为一个名为 Compilers 的类制作一种自定义语言作为项目。整个项目是用 Java 编写的,使用JFlex作为我的词法分析器,并使用Cup作为我的句法分析器。
我为该语言创建了一个简单的文本编辑器,它基本上由一个 JTextPane 组成,用户可以在其中键入将被解析的自定义代码。这个JTextPane 有一个DefaultStyledDocument,用于设置字符属性,例如为JTextPane 中的代码(文本)更改关键字、注释、字符串、数字等的颜色。
这是我正在使用的代码:
DefaultStyledDocument doc = new DefaultStyledDocument() {
@Override
public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { //cuando se insertan caracteres.
super.insertString(offset, str, a);
String text = getText(0, getLength());
syntax = new SyntaxHighlighter(new java.io.StringReader(text));
Token val;
try {
while ((val = syntax.yylex()) != null) {
switch (val.type) {
case TokenType.KEYWORD:
setCharacterAttributes(val.start, val.length, keyword, true);
break;
case TokenType.COMMENT:
setCharacterAttributes(val.start, val.length, comment, true);
break;
case TokenType.STRING:
setCharacterAttributes(val.start, val.length, string, true);
break;
case TokenType.FUNCTION:
setCharacterAttributes(val.start, val.length, function, true);
break;
case TokenType.NUMBER:
setCharacterAttributes(val.start, val.length, plain, true);
break;
case TokenType.OPERATOR:
setCharacterAttributes(val.start, val.length, operator, true);
break;
case TokenType.READ:
setCharacterAttributes(val.start, val.length, number, true);
break;
default:
setCharacterAttributes(val.start, val.length, plain, true);
break;
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(rootPane, "Oops! Exception triggered\n" + ex.getMessage());
}
}
@Override
//this is the method I want to optimize
public void remove(int offs, int len) throws BadLocationException {
super.remove(offs, len);
String text = getText(0, getLength());
syntax = new SyntaxHighlighter(new java.io.StringReader(text));
Token val;
try {
while ((val = syntax.yylex()) != null) {
switch (val.type) {
case TokenType.KEYWORD:
setCharacterAttributes(val.start, val.length, keyword, true);
break;
case TokenType.COMMENT:
setCharacterAttributes(val.start, val.length, comment, true);
break;
case TokenType.STRING:
setCharacterAttributes(val.start, val.length, string, true);
break;
case TokenType.FUNCTION:
setCharacterAttributes(val.start, val.length, function, true);
break;
case TokenType.NUMBER:
setCharacterAttributes(val.start, val.length, plain, true);
break;
case TokenType.OPERATOR:
setCharacterAttributes(val.start, val.length, operator, true);
break;
case TokenType.READ:
setCharacterAttributes(val.start, val.length, number, true);
break;
default:
setCharacterAttributes(val.start, val.length, plain, true);
break;
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(rootPane, "Oops! Exception triggered\n" + ex.getMessage());
}
}
};
this.codeTextPane.setStyledDocument(doc);
SyntaxHighlighter 类基本上是一个词法分析器(使用 JFlex 制作),仅用作搜索特定文本片段(关键字、字符串等)的一种方式。一切都很完美,但是...
问题:
当 JTextPane 中有大量文本时,按住退格键删除文本会使程序非常难以滞后。我认为发生这种情况的原因可能是因为 SyntaxHighlighter 运行时会删除每个字符,因为按住退格键会为每个被删除的字符调用 remove() 函数。插入文本实际上不是问题,因为您可以从文件中加载代码(该文件中的整个文本将由 SyntaxHighlighter 整体分析),或者您无法快速输入以注意到滞后。
有没有办法可以优化这个?谢谢你们!