示例#1
0
  @Override
  public void runTest() throws Throwable {
    REF.IN_TESTS = true;

    IDocument document = new Document(data.source);
    ITextSelection selection =
        new TextSelection(
            document, data.sourceSelection.getOffset(), data.sourceSelection.getLength());
    RefactoringInfo info =
        new RefactoringInfo(
            document,
            selection,
            new IGrammarVersionProvider() {

              public int getGrammarVersion() throws MisconfigurationException {
                return IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_2_7;
              }
            });
    InlineLocalRefactoring refactoring = new InlineLocalRefactoring(info);

    NullProgressMonitor monitor = new NullProgressMonitor();
    RefactoringStatus result = refactoring.checkAllConditions(monitor);

    assertTrue(
        "Refactoring is not ok: " + result.getMessageMatchingSeverity(RefactoringStatus.WARNING),
        result.isOK());

    Change change = refactoring.createChange(monitor);
    change.perform(monitor);

    assertEquals(data.result, document.get());

    REF.IN_TESTS = false;
  }
示例#2
0
  /**
   * 執行Quick Fix變更
   *
   * @param activeEditor
   * @param document
   * @throws CoreException
   */
  private void performChange(IEditorPart activeEditor, IDocument document, ASTRewrite rewrite)
      throws CoreException {
    Change change = null;
    IRewriteTarget rewriteTarget = null;
    try {
      change = getChange(actRoot, rewrite);
      if (change != null) {
        if (document != null) {
          LinkedModeModel.closeAllModels(document);
        }
        if (activeEditor != null) {
          rewriteTarget = (IRewriteTarget) activeEditor.getAdapter(IRewriteTarget.class);
          if (rewriteTarget != null) {
            rewriteTarget.beginCompoundChange();
          }
        }

        change.initializeValidationData(new NullProgressMonitor());
        RefactoringStatus valid = change.isValid(new NullProgressMonitor());
        if (valid.hasFatalError()) {
          IStatus status =
              new Status(
                  IStatus.ERROR,
                  JavaPlugin.getPluginId(),
                  IStatus.ERROR,
                  valid.getMessageMatchingSeverity(RefactoringStatus.FATAL),
                  null);
          throw new CoreException(status);
        } else {
          IUndoManager manager = RefactoringCore.getUndoManager();
          manager.aboutToPerformChange(change);
          Change undoChange = change.perform(new NullProgressMonitor());
          manager.changePerformed(change, true);
          if (undoChange != null) {
            undoChange.initializeValidationData(new NullProgressMonitor());
            manager.addUndo("Quick Undo", undoChange);
          }
        }
      }
    } finally {
      if (rewriteTarget != null) {
        rewriteTarget.endCompoundChange();
      }

      if (change != null) {
        change.dispose();
      }
    }
  }