2015-06-23 25 views
1

我正在研究java中的代碼編輯器,我想知道如何像使用實際的代碼編輯器一樣使用括號(打開和關閉)自動縮進。Java Swing中的AutoIndent括號JeditorPane

這樣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; 




    } 

回答

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; 

} 
1

如果您希望自動壓痕,你不會得到這樣的代碼。您應該自己添加\ n空格(或\ t)字符來格式化您的代碼。 JEdi​​torPane不理解你的代碼邏輯。你(用你的代碼解析器)應該爲你有的代碼行定義父/子關係。

定義父/子的情況的一個示例是XML。請參閱the XMLEditorKit其中節點縮進。

+0

我編輯我的職務,我使用一個字符串,以後我把這個字符串中的JEditorPanel。 但代碼不起作用。 – Tumeco