2009-07-16 93 views
6

我知道這很愚蠢,但我無法克服我的好奇心。是否可以寫一個shell腳本來格式化一段java代碼?使用shell腳本進行Java代碼格式化

例如,如果用戶寫在代碼:

public class Super{ 
    public static void main(String[] args){ 
    System.out.println("Hello world"); 
    int a=0; 
    if(a==100) 
    { 
    System.out.println("Hello world"); 
    } 
    else 
    { 
    System.out.println("Hello world with else"); 
    } 
    } 
} 

我想編寫一個shell腳本,這將使這樣的代碼。

public class Super 
{ 
    public static void main(String[] args) 
    { 
    System.out.println("Hello world"); 
    int a=0; 
    if(a==100){ 
    System.out.println("Hello world"); 
    } 
    else{ 
    System.out.println("Hello world with else"); 
    } 
} 

準確地說,我們應該改變花括號的格式。如果它是try/catch或控制結構,我們應該將它改爲同一行,如果它是函數/方法/類,它應該進入下一行。我對sed和awk知之甚少,因此可以輕鬆完成此任務。另外我知道這可以使用eclipse完成。

+1

可能值得改變你的問題,你問的格式,而不是驗證。 – 2009-07-16 14:23:49

+0

+1爲「花托」8) – RichieHindle 2009-07-16 14:24:53

+0

改變標題謝謝:) – Harish 2009-07-16 14:36:23

回答

5

好修改壓入深度,我有一些免費所以我決定重溫我的好linux舊日:]

在閱讀了一些關於awk和sed的信息後,我決定使用它們可能會更好,因爲它更容易添加縮進在awk中解析sed中的字符串。

這裏是〜/ sed_script該格式源文件:

 # delete indentation 
    s/^ \+//g 

    # format lines with class 
    s/^\(.\+class.\+\) *\({.*\)$/\1\n\2/g 

    # format lines with methods 
    s/^\(public\|private\)\(\+static\)\?\(\+void\)\? \+\(.\+(.*)\) *\({.*\)$/\1\2\3 \4\n\5/g 

    # format lines with other structures 
    /^\(if\|else\|for\|while\|case\|do\|try\)\([^{]*\)$/,+1 { # get lines not containing '{' 
                  # along with the next line 
     /.*{.*/ d    # delete the next line with '{' 
     s/\([^{]*\)/\1 {/g # and add '{' to the first line 
    }

這裏是〜/ awk_script,增加縮進:

 BEGIN { depth = 0 } 
    /}/ { depth = depth - 1 } 
      { 
       getPrefix(depth) 
       print prefix $0 
      } 
    /{/ { depth = depth + 1 } 

      function getPrefix(depth) { 
       prefix = "" 
       for (i = 0; i < depth; i++) { prefix = prefix " "} 
       return prefix 
      }

而且你使用他們喜歡的是:

 > sed -f ~/sed_script ~/file_to_format > ~/.tmp_sed 
    > awk -f ~/awk_script ~/.tmp_sed

它遠不是正確的格式化工具,但我希望它可以作爲示例腳本以供參考:] Go與你的學習運氣不錯。

0

看一看CLI for Jalopy。 Jalopy是一個非常強大的源格式化程序。

+0

我只是想知道是否可以通過shell腳本來完成。有很多軟件可以對代碼進行格式化,但我問的是awk還是sed – Harish 2009-07-16 14:28:06

+0

。雖然我會在問題的頂部清楚地說明你正在爲sed/awk做一個學習練習,或者你會得到很多答案,比如我的 – 2009-07-16 14:48:40

1

這絕對是可能的......我只是不明白你爲什麼想花時間做這件事? :]有足夠的工具可以做到這一點,任何像樣的IDE都可以重新格式化源代碼(包括Eclipse)。

例如,要在Eclipse 3.4中格式化(在其他版本中應該類似),只需右鍵單擊您的項目或文件,然後從菜單中選擇「源>格式」。

如果您需要更改其格式化代碼的方式,請轉至「首選項> Java>代碼樣式>格式化程序」並更改模板。據我所知,它在JDeveloper和NetBeans中非常相似。

1

我認爲這可以通過一個簡單的'sed'腳本完成。 使用1個變量(bCount)來存儲'{'(開頭括號) - (減去)'}'的數量(右括號) 然後我會瀏覽這個文件並根據實際情況插入'tabs'使用的支架數量。 所以

public class Super{ //bCount=0 
public static void main(String[] args){ //bCount=1 
System.out.println("Hello world"); //bCount=2 
int a=0; //bCount=2 
....and so on 

所以instert在線0

1標籤0的選項卡在第1行

2突出部在第3行和第4 ...等

2

快速,有缺陷的企圖,而是一個對你的樣品輸入工作原理:

BEGIN {depth = 0;} 
/{$/ {depth = depth + 1} 
/^}/ {depth = depth - 1} 
     {prefix = ""; for (i = 0; i < depth; i++) { prefix = prefix " "} print prefix $0 ; } 

這是一個awk腳本:將其放入一個文件,然後執行

awk -f awk_script_file source_file 

與此明顯的缺陷包括:

  • 它不會抓住你想要縮進的地方,如

    if (foo) 
        bar(); 
    
  • 這將基於註釋括號和字符串

  • 它不會檢測到大括號{之後評論
0

考慮使用Jindent,這是一個「使用Emacs的簡單Java縮進工具」。這是一個免費的shell腳本,它是伯克利托勒密項目的一部分。