@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; }
/* * @see ICompletionProposal#getAdditionalProposalInfo() */ public String getAdditionalProposalInfo() { StringBuffer buf = new StringBuffer(); buf.append("<p>"); // $NON-NLS-1$ try { Change change = getChange(); if (change != null) { String name = change.getName(); if (name.length() == 0) { return null; } buf.append(name); } else { return null; } } catch (CoreException e) { buf.append("Unexpected error when accessing this proposal:<p><pre>"); // $NON-NLS-1$ buf.append(e.getLocalizedMessage()); buf.append("</pre>"); // $NON-NLS-1$ } buf.append("</p>"); // $NON-NLS-1$ return buf.toString(); }
/** * 執行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(); } } }
protected final RefactoringStatus performRefactoring(Refactoring ref, boolean providesUndo) throws Exception { performDummySearch(); IUndoManager undoManager = getUndoManager(); if (DESCRIPTOR_TEST) { final CreateChangeOperation create = new CreateChangeOperation( new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL); create.run(new NullProgressMonitor()); RefactoringStatus checkingStatus = create.getConditionCheckingStatus(); if (!checkingStatus.isOK()) return checkingStatus; Change change = create.getChange(); ChangeDescriptor descriptor = change.getDescriptor(); if (descriptor instanceof RefactoringChangeDescriptor) { RefactoringChangeDescriptor rcd = (RefactoringChangeDescriptor) descriptor; RefactoringDescriptor refactoringDescriptor = rcd.getRefactoringDescriptor(); if (refactoringDescriptor instanceof JavaRefactoringDescriptor) { JavaRefactoringDescriptor jrd = (JavaRefactoringDescriptor) refactoringDescriptor; RefactoringStatus validation = jrd.validateDescriptor(); if (!validation.isOK()) return validation; RefactoringStatus refactoringStatus = new RefactoringStatus(); Class expected = jrd.getClass(); RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(jrd.getID()); jrd = (JavaRefactoringDescriptor) contribution.createDescriptor( jrd.getID(), jrd.getProject(), jrd.getDescription(), jrd.getComment(), contribution.retrieveArgumentMap(jrd), jrd.getFlags()); assertEquals(expected, jrd.getClass()); ref = jrd.createRefactoring(refactoringStatus); if (!refactoringStatus.isOK()) return refactoringStatus; TestRenameParticipantSingle.reset(); TestCreateParticipantSingle.reset(); TestMoveParticipantSingle.reset(); TestDeleteParticipantSingle.reset(); } } } final CreateChangeOperation create = new CreateChangeOperation( new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL); final PerformChangeOperation perform = new PerformChangeOperation(create); perform.setUndoManager(undoManager, ref.getName()); IWorkspace workspace = ResourcesPlugin.getWorkspace(); if (fIsPreDeltaTest) { IResourceChangeListener listener = new IResourceChangeListener() { public void resourceChanged(IResourceChangeEvent event) { if (create.getConditionCheckingStatus().isOK() && perform.changeExecuted()) { TestModelProvider.assertTrue(event.getDelta()); } } }; try { TestModelProvider.clearDelta(); workspace.checkpoint(false); workspace.addResourceChangeListener(listener); executePerformOperation(perform, workspace); } finally { workspace.removeResourceChangeListener(listener); } } else { executePerformOperation(perform, workspace); } RefactoringStatus status = create.getConditionCheckingStatus(); if (!status.isOK()) return status; assertTrue("Change wasn't executed", perform.changeExecuted()); Change undo = perform.getUndoChange(); if (providesUndo) { assertNotNull("Undo doesn't exist", undo); assertTrue("Undo manager is empty", undoManager.anythingToUndo()); } else { assertNull("Undo manager contains undo but shouldn't", undo); } return null; }