2012-04-12 84 views

回答

0

我看了一下org.eclise.jdt.ui的聲明。

相關的命令ID是org.eclipse.jdt.ui.edit.text.java.clean.up和實現是org.eclipse.jdt.internal.ui.actions.AllCleanUpsAction。不幸的是,這是一個內部操作,該命令不支持任何參數。

我可以看到三種可能的途徑:

  • 創建AllCleanUpsAction並調用...run(new StructuredSelection(<compilation units>[]))。問題:行動是內部的,所以你可能要創建訪問它的一個片段......

  • 打開包裝導航視圖。選擇對應於編譯單元的正確文件。通過IHandlerService.executeCommand("org.eclipse.jdt.ui.edit.text.java.clean.up")執行命令ID。問題:軟件包導航器已更改...並且您可能沒有在導航器中顯示所有編譯單元。

  • 將您當前選擇的視圖設置爲new StructuredSelection(<compilation units>[])。然後執行如上所述的命令。問題:我不知道該命令被正確啓用..

0

您可以使用RefactoringExecutionStarter.startCleanupRefactoring這需要的ICompilationUnits數組作爲其參數一上來就進行清潔。此方法還允許您指定要執行的ICleanUp,並允許您跳過顯示清理嚮導(如果需要)。

下面是一個例子,其中刪除不必要的括號:

ICleanUp[] cleanUps = new ICleanUp[]{new ExpressionsCleanUp(){ 
    @Override 
    protected boolean isEnabled(String key){ 
     switch(key){ 
     case CleanUpConstants.EXPRESSIONS_USE_PARENTHESES: 
     case CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER: 
      return true; 
     case CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS: 
      return false; 
     default: 
      return super.isEnabled(key); 
     } 
    } 
}}; 
ICompilationUnit[] icus = new ICompilationUnit[]{icu}; 
Shell shell = HandlerUtil.getActiveEditor(event).getSite().getShell(); 
try { 
    RefactoringExecutionStarter.startCleanupRefactoring(
      icus, cleanUps, false, shell, false, ActionMessages.CleanUpAction_actionName); 
} catch (InvocationTargetException e) { 
    throw new AssertionError(e); 
} 
相關問題