Java 代码优化 - 如何优化这个 remove() 函数?

标签 java parsing optimization jtextpane defaultstyleddocument

我正在制作一种自定义语言作为名为编译器的类的项目。整个项目是用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 作为一个整体进行分析),或者您只是无法快速键入以注意到滞后。

有什么办法可以优化这个吗?谢谢大家!

最佳答案

我的第一直觉是采用窗口策略。在您的代码中维护一个树或一些其他能够表示独立范围的结构。然后调整语法荧光笔和代码的其他部分,使其仅适用于他们知道受影响的树部分(或其他部分)。

抽象地说,你们可能有这样的关系:

class
  |
+----------+
method1    method2
              |
           +--------+--------+
           line1    line2    line3

...这允许在不改变的内容的上下文中理解第3行中的删除

关于Java 代码优化 - 如何优化这个 remove() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194784/

相关文章:

Django Rest Framework,数据库查询优化

java - 您将如何保存改造的列表响应

java - 打包和解包 *.obb 文件

python - 如何更快地实现贪心集覆盖?

javascript - 解析 JSON 失败,JavaScript 中缺少元素

java - 将字符串解析为 JodaTime 时出现无效格式异常

algorithm - 如何找到最长的可能路径?

Java 小程序和 JFrame

java - 请解释这个 put-if-absent 成语中的竞争条件

Python:搜索一对关键字之前和之后的单词