2013-04-15 5 views
6

JDTでいくつかのクラスを生成しています。その後、Ctrl + Shift + F(ソース>書式)を選択せず​​に開いたエディタで押すのと同じように、ICompilationUnit全体を書式設定したいと思います。JDTでプログラムでソースコードを書式設定する

JDTのAPIを使用して、プログラムでソースコードを書式設定するためのあらゆるポインタが非常に高く評価されています。

追加:このように試しましたが、コードは変更されません。私は何をしているのですか?

private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException { 
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null); 
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), 0, targetUnit.getSource().length(), 0, null); 
    targetUnit.applyTextEdit(formatEdit, monitor); 
} 

答えて

5

これはバグかもしれませんが、Elcipse 4.2.2でJDKを使用すると、ファイルにTextEditを適用するためにICompilationUnitの作業コピーを作成する必要があります。

public static void formatUnitSourceCode(ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException { 
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null); 
    ISourceRange range = unit.getSourceRange(); 
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null); 
    if (formatEdit != null && formatEdit.hasChildren()) { 
     unit.applyTextEdit(formatEdit, monitor); 
    } else { 
     monitor.done(); 
    } 
} 
2

generating some classes by using JDTの場合、ソースコードに "\ t"を入れることができます。あるいは、コードフォーマッタを使って、あなたがしたことのように。

public static void main(String[] args) 
{ 
    String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}"; 
    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null); 

    TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null); 
    IDocument doc = new Document(code); 
    try { 
     textEdit.apply(doc); 
     System.out.println(doc.get()); 
    } catch (MalformedTreeException e) { 
     e.printStackTrace(); 
    } catch (BadLocationException e) { 
     e.printStackTrace(); 
    } 
} 

apply()方法がここにトリックを行います。私は、次のコードをテストしています。

+0

このことができますファイルをJavaソースをフォーマットするには、以下の方法を使用しますが、それはすべて解決するエレガントdoens't:

targetUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); ... do work on the source file ... formatUnitSourceCode(targetUnit, new SubProgressMonitor(monitor, 1)); targetUnit.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1)); 

フォーマット自体は次のように行われています必要な書式のI.長いメソッド宣言を多くのパラメータで手作業で分割する必要があります。 –

+0

非常に興味深い問題です。私はあなたがしていることを見て、私は自分の答えを編集しました。ありがとう。 – Ryan

+0

こんにちはRyan、私は 'CodeFormatter.K_UNKNOWN'を使うために上記のコードを変更しましたが、それはどちらもうまくいきませんでした。その後、 'targetUnit.applyTextEdit'(' ICompilationUnit'の内部 'IDocument'に' apply()を実行します)の呼び出しの後に 'targetUnit.getSource()'をチェックし、不思議なことに変更が適用されたようです。しかし、ファイルには適用されません。これはバグですか、何か不足していますか? –

0

は私が

public static void formatSource(ICompilationUnit cu, IProgressMonitor progressMonitor) throws JavaModelException{ 
    String source = cu.getSource(); 
    IJavaProject javaProject = cu.getJavaProject(); 

    Map options = javaProject.getOptions(true); 


      // Instantiate the default code formatter with the given options 
      final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); 


      final TextEdit edit = codeFormatter.format(
       CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit 
       source, // source to format 
       0, // starting position 
       source.length(), // length 
       0, // initial indentation 
       System.getProperty("line.separator") // line separator 
      );   

      cu.becomeWorkingCopy(progressMonitor); 
      try { 
       cu.applyTextEdit(edit, progressMonitor); 
       //cu.reconcile(); 
       cu.commitWorkingCopy(true, progressMonitor); 
       //cu.save(progressMonitor, false); 

       JavaUI.openInEditor(cu); 


      } catch (MalformedTreeException e) { 
       e.printStackTrace(); 
      } catch (PartInitException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

} 
関連する問題