Java Swing JeditorPane 中的 AutoIndent 括号

AutoIndent bracket in Java Swing JeditorPane

我正在 java 开发代码编辑器,我想知道如何像实际代码编辑器一样使用方括号(打开和关闭)自动缩进。

像这样 1:

Array PrincipalVar = (Var => (OtherVar =>  (var3 => 3,
                                            var4 => 8,
                                            var6 => 1) 
                             ), 
                      Var2 => (var => 1))

编辑器是一个 JEditorPane。我尝试了一些代码,但似乎没有任何效果。 我已经有文件内容代码,我想重新缩进这个文件。 我已经尝试过的代码:

public String indentFileTry() throws FileNotFoundException{
        LinkedList<Integer> inBracket = new LinkedList<Integer>();
        String currentLine = "";
        Scanner indent = new Scanner(new FileReader(f));
        String ptu = "";
        while(indent.hasNextLine()) {
            currentLine =   indent.nextLine();
            currentLine = currentLine.trim(); 
            char[] line = currentLine.toCharArray();
            int i = 0;
            while(i < line.length){ //Here I define the position of the Bracet for Indentation
                if(line[i] == '('){

                    inBracket.addFirst(i);
                }

                i++;
            }
            if(!inBracket.isEmpty()){//here I indent with the position of the bracket and I remove the first(First In First Out)
                if(!currentLine.contains(")")){
                    int spaceadded = 0;
                    String space ="";
                    while(spaceadded <= inBracket.getFirst()){
                        spaceadded++; space += " ";
                    }

                        currentLine = space + currentLine;
                        inBracket.removeFirst();

                }else if(currentLine.contains(")")){
                    int spaceadded = 0;
                    String space ="";
                    while(spaceadded <= inBracket.getFirst()){
                        spaceadded++; space += " ";
                    }

                        currentLine =    space + currentLine;

                    inBracket.removeFirst();
                }
            }


            ptu += currentLine +"\n";

        }
        indent.close() ; 
        System.out.println(ptu);
        return ptu;




    }

如果您希望自动缩进,您将不会得到这样的代码。您应该自己实现它,添加 \n 空格(或 \t)字符来格式化您的代码。 JEdi​​torPane 不理解您的代码逻辑。您(使用您的代码解析器)应该为您拥有的代码行定义 parent/child 关系。

定义 parent/children 的情况的一个例子是 XML。请参阅 the XMLEditorKit 节点缩进。

对于响应,我做的很简单。 我制作了一个 LinkedList,我像这样使用它作为 FILO(先进后出):

public String indentFile() throws FileNotFoundException{
    LinkedList<Integer> positionBracket = new LinkedList<Integer>();
    String currentLine = "";
    Scanner indent = new Scanner(new FileReader(f));
    String stringIndented = "";

    while(indent.hasNextLine()) {

        currentLine =   indent.nextLine();
        currentLine = currentLine.trim(); 
        char[] lineInChar = currentLine.toCharArray();
        int i = 0;
        int spaceadded = 0;
        String space ="";
        if(!positionBracket.isEmpty()){

                while(spaceadded <= positionBracket.getFirst()){
                    spaceadded++; 
                    space += " "; // We put same space like the last opened bracket

                }

        }

        while(i < lineInChar.length){

            if(lineInChar[i] == '('){ //If opened bracket I put the position in the top of the Filo


                positionBracket.addFirst(new Integer(i));

            }
            if(lineInChar[i] == ')' && !countCom){
                positionBracket.removeFirst(); //If closed bracket I remove the position on the top of the Filo

            }



            i++;
        }
        stringIndented += space + currentLine +"\n";
        }
    }
    return stringIndented;

}