2013-04-15 22 views
6

Tôi đang tạo một số lớp với JDT. Sau đó, tôi muốn định dạng toàn bộ ICompilationUnit, giống như khi tôi nhấn Ctrl + Shift + F (Nguồn> Định dạng) trong Trình chỉnh sửa mở mà không có lựa chọn.Mã định dạng nguồn lập trình với JDT

Bất kỳ con trỏ nào cho API trong JDT để định dạng mã nguồn được lập trình đều được đánh giá cao.

Ngoài ra: Tôi đã thử nó như thế này, nhưng mã không bị thay đổi. Tôi đang làm gì?

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); 
} 

Trả lời

5

Đây có thể là lỗi, nhưng khi sử dụng JDK trong Hình 4.2.2, cần tạo bản sao làm việc của ICompilationUnit để áp dụng TextEdit cho tệp.

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)); 

Các định dạng riêng của mình được thực hiện như thế này:

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

Khi generating some classes by using JDT, bạn có thể đặt "\ t" vào mã nguồn của mình. Hoặc giống như những gì bạn đã làm, sử dụng trình định dạng mã. Tôi đã thử nghiệm mã sau:

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(); 
    } 
} 

Phương pháp apply() thực hiện thủ thuật tại đây.

+0

này giúp, nhưng nó doens't tao nhã giải quyết tất cả của định dạng mong muốn. I E. bạn sẽ cần phải hướng dẫn sử dụng phá vỡ khai báo phương thức dài với nhiều tham số. –

+0

Vấn đề rất thú vị. Tôi thấy những gì bạn đang làm, và tôi chỉ chỉnh sửa câu trả lời của tôi. Cảm ơn. – Ryan

+0

Hi Ryan, tôi đã thay đổi mã của mình ở trên để sử dụng 'CodeFormatter.K_UNKNOWN', nhưng điều đó cũng không hoạt động. Sau đó, tôi đã kiểm tra 'targetUnit.getSource()' sau khi gọi tới 'targetUnit.applyTextEdit' (mà có' apply() 'trên' IDocument' bên trong của 'ICompilationUnit'), và những thay đổi dường như được áp dụng một cách kỳ lạ. Nhưng chúng không được áp dụng cho tệp. Đây có phải là một lỗi, hoặc tôi đang thiếu một cái gì đó? –

0

tôi sử dụng phương pháp sau đây để định dạng một nguồn Java nộp

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(); 
      } 

} 
Các vấn đề liên quan