简体   繁体   English

Java Swing JeditorPane中的AutoIndent括号

[英]AutoIndent bracket in Java Swing JeditorPane

I am working on a code-editor in java and i want to know how to auto-indent using brackets (open and close) like an actual code editor . 我正在用Java编写代码编辑器,我想知道如何像实际的代码编辑器一样使用方括号(打开和关闭)自动缩进。

like this 1: 像这样1:

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

Editor is a JEditorPane. 编辑器是一个JEditorPane。 I tried some code, but nothing seem to work. 我尝试了一些代码,但似乎没有任何效果。 I have already file contening code, and I want to Re-Indent this file. 我已经有文件竞争代码,并且我想重新缩进该文件。 Code I already tried : 我已经尝试过的代码:

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;




    }

If you expect automatically indentation you won't get such code. 如果您希望自动缩进,则不会获得此类代码。 You should implement it yourself adding \\n spaces (or \\t) chars to format your code. 您应该自己实现,添加\\ n空格(或\\ t)字符来格式化代码。 JEditorPane does not understand your code logic. JEdi​​torPane不了解您的代码逻辑。 You (with your code parser) should define parent/child relation for lines of code you have. 您(使用代码解析器)应该为您拥有的代码行定义父/子关系。

One example for the case when parent/children are defined is XML. 定义父/子的情况的一个示例是XML。 See the XMLEditorKit where nodes are indented. 请参阅XMLEditorKit ,其中缩进了节点。

For the response, What I do is easy. 对于响应,我的工作很容易。 I made a LinkedList, and I use it like a FILO (First in Last out) like this : 我做了一个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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM