Java Swing JeditorPane中的AutoIndent括号

时间:2015-06-23 14:44:58

标签: 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;




    }

2 个答案:

答案 0 :(得分:1)

如果您希望自动缩进,则不会获得此类代码。您应该自己添加\ n空格(或\ t)字符来格式化代码。 JEdi​​torPane不了解您的代码逻辑。您(使用代码解析器)应该为您拥有的代码行定义父/子关系。

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

答案 1 :(得分:0)

对于回复,我做的很容易。 我创建了一个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;

}