Java Code Examples for org.eclipse.ltk.core.refactoring.Change#perform()

The following examples show how to use org.eclipse.ltk.core.refactoring.Change#perform() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMultipleFileChanges() throws Exception {
	CompositeChange compositeChange = new CompositeChange("test");
	compositeChange.add(createTextFileChange(file0, 1, 1, "foo"));
	compositeChange.add(createTextFileChange(file0, 2, 1, "bar"));
	CompositeChange compositeChange1 = new CompositeChange("test");
	compositeChange.add(compositeChange1);
	compositeChange1.add(createTextFileChange(file0, 3, 1, "baz"));
	compositeChange1.add(createTextFileChange(file0, 2, 1, "bar"));
	compositeChange1.add(createMultiTextFileChange(file0, 1, 1, "foo", 4, 1, "foo"));
	Change combined = combiner.combineChanges(compositeChange);
	assertTrue(combined instanceof CompositeChange);
	assertEquals(1, ((CompositeChange) combined).getChildren().length);
	Change combinedChild = ((CompositeChange) combined).getChildren()[0];
	assertTrue(combinedChild instanceof DisplayChangeWrapper.Wrapper);
	Change delegate = ((DisplayChangeWrapper.Wrapper) combinedChild).getDelegate();
	assertTextType(delegate);
	Change undo = combined.perform(new NullProgressMonitor());
	assertEquals(MODEL.replace("1234", "foobarbazfoo"), getContents(file0));
	undo.perform(new NullProgressMonitor());
	assertEquals(MODEL, getContents(file0));
}
 
Example 2
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMultipleDocumentChanges() throws Exception {
	ITextEditor editor = openInEditor(file0);
	CompositeChange compositeChange = new CompositeChange("test");
	compositeChange.add(createEditorDocumentChange(editor, 1, 1, "foo"));
	compositeChange.add(createEditorDocumentChange(editor, 2, 1, "bar"));
	CompositeChange compositeChange1 = new CompositeChange("test");
	compositeChange.add(compositeChange1);
	compositeChange1.add(createEditorDocumentChange(editor, 3, 1, "baz"));
	compositeChange1.add(createEditorDocumentChange(editor, 2, 1, "bar"));
	compositeChange1.add(createMultiEditorDocumentChange(editor, 1, 1, "foo", 4, 1, "foo"));
	Change combined = combiner.combineChanges(compositeChange);
	assertTrue(combined instanceof CompositeChange);
	assertEquals(1, ((CompositeChange) combined).getChildren().length);
	assertTrue(((CompositeChange)combined).getChildren()[0] instanceof EditorDocumentChange);
	Change undo = combined.perform(new NullProgressMonitor());
	IDocument document = getDocument(editor);
	assertEquals(MODEL.replace("1234", "foobarbazfoo"), document.get());
	undo.perform(new NullProgressMonitor());
	assertEquals(MODEL, document.get());
}
 
Example 3
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMixedChanges() throws Exception {
	IFile file1 = IResourcesSetupUtil.createFile(PROJECT + "/file1.txt", MODEL);
	ITextEditor editor1 = openInEditor(file1);
	CompositeChange compositeChange = new CompositeChange("test");
	compositeChange.add(createEditorDocumentChange(editor1, 1, 1, "foo"));
	compositeChange.add(createTextFileChange(file0, 1, 1, "foo"));
	CompositeChange compositeChange1 = new CompositeChange("test");
	compositeChange.add(compositeChange1);
	compositeChange1.add(createEditorDocumentChange(editor1, 3, 1, "baz"));
	compositeChange1.add(createTextFileChange(file0, 1, 1, "foo"));
	compositeChange1.add(createTextFileChange(file0, 3, 1, "baz"));
	Change combined = combiner.combineChanges(compositeChange);
	Change undo = combined.perform(new NullProgressMonitor());
	IDocument document1 = getDocument(editor1);
	assertEquals(MODEL.replace("123", "foo2baz"), document1.get());
	assertEquals(MODEL.replace("123", "foo2baz"), getContents(file0));
	undo.perform(new NullProgressMonitor());
	assertEquals(MODEL, document1.get());
	assertEquals(MODEL, getContents(file0));
}
 
Example 4
Source File: AbstractResourceRelocationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void performRefactoring(RefactoringDescriptor descriptor, IProgressMonitor monitor) throws Exception {
	project.refreshLocal(IResource.DEPTH_INFINITE, null);
	project.build(IncrementalProjectBuilder.FULL_BUILD, null);
	RefactoringStatus status = new RefactoringStatus();
	Refactoring refactoring = descriptor.createRefactoring(status);
	refactoring.checkAllConditions(monitor);
	Assert.assertTrue(status.isOK());
	Change change = refactoring.createChange(monitor);
	change.perform(monitor);
	project.refreshLocal(IResource.DEPTH_INFINITE, null);
	project.build(IncrementalProjectBuilder.FULL_BUILD, null);
}
 
Example 5
Source File: DisplayChangeWrapperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test 
public void testDocumentChange() throws CoreException {
	Change change = new DocumentChange("my change", new Document());
	Change wrapped = DisplayChangeWrapper.wrap(change);
	assertTrue(wrapped instanceof TextEditBasedChange);
	assertTrue(wrapped instanceof DisplayChangeWrapper.Wrapper);
	assertEquals(change, ((DisplayChangeWrapper.Wrapper) wrapped).getDelegate());
	Change undo = wrapped.perform(new NullProgressMonitor());
	assertTrue(undo instanceof DisplayChangeWrapper.Wrapper);
}
 
Example 6
Source File: DisplayChangeWrapperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test 
public void testNullChange() throws CoreException {
	Change change = new NullChange("my change");
	Change wrapped = DisplayChangeWrapper.wrap(change);
	assertFalse(wrapped instanceof TextEditBasedChange);
	assertTrue(wrapped instanceof DisplayChangeWrapper.Wrapper);
	assertEquals(change, ((DisplayChangeWrapper.Wrapper) wrapped).getDelegate());
	Change undo = wrapped.perform(new NullProgressMonitor());
	assertTrue(undo instanceof DisplayChangeWrapper.Wrapper);
}
 
Example 7
Source File: DisplayChangeWrapperTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test 
public void testNoUndoChange() throws CoreException {
	Change change = new NullChange("my no undo change") {
		@Override
		public Change perform(IProgressMonitor pm) throws CoreException {
			return null;
		}
	};
	Change wrapped = DisplayChangeWrapper.wrap(change);
	assertFalse(wrapped instanceof TextEditBasedChange);
	assertTrue(wrapped instanceof DisplayChangeWrapper.Wrapper);
	assertEquals(change, ((DisplayChangeWrapper.Wrapper) wrapped).getDelegate());
	Change undo = wrapped.perform(new NullProgressMonitor());
	assertNull(undo);
}
 
Example 8
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSingleFileChange() throws Exception {
	Change textFileChange = createTextFileChange(file0, 1, 1, "foo");
	Change combined = combiner.combineChanges(textFileChange);
	assertTextType(combined);
	assertEquals(textFileChange, combined);
	Change undo = combined.perform(new NullProgressMonitor());
	assertEquals(MODEL.replace("1", "foo"), getContents(file0));
	undo.perform(new NullProgressMonitor());
	assertEquals(MODEL, getContents(file0));
}
 
Example 9
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSingleDocumentChange() throws Exception {
	ITextEditor editor = openInEditor(file0);
	Change docChange = createEditorDocumentChange(editor, 1, 1, "foo");
	Change combined = combiner.combineChanges(docChange);
	assertEquals(docChange, combined);
	assertTextType(combined);
	Change undo = combined.perform(new NullProgressMonitor());
	IDocument document = getDocument(editor);
	assertEquals(MODEL.replace("1", "foo"), document.get());
	undo.perform(new NullProgressMonitor());
	assertEquals(MODEL, document.get());
}
 
Example 10
Source File: ChangeCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Performs the change associated with this proposal.
 * <p>
 * Subclasses may extend, but must call the super implementation.
 *
 * @throws CoreException
 *             when the invocation of the change failed
 */
@Override
protected void performChange() throws CoreException {

	Change change= null;
	try {
		change= getChange();
		if (change != null) {

			change.initializeValidationData(new NullProgressMonitor());
			RefactoringStatus valid= change.isValid(new NullProgressMonitor());
			if (valid.hasFatalError()) {
				IStatus status = new Status(IStatus.ERROR,  IConstants.PLUGIN_ID, IStatus.ERROR,
						valid.getMessageMatchingSeverity(RefactoringStatus.FATAL), null);
				throw new CoreException(status);
			} else {
				IUndoManager manager= RefactoringCore.getUndoManager();
				Change undoChange;
				boolean successful= false;
				try {
					manager.aboutToPerformChange(change);
					undoChange= change.perform(new NullProgressMonitor());
					successful= true;
				} finally {
					manager.changePerformed(change, successful);
				}
				if (undoChange != null) {
					undoChange.initializeValidationData(new NullProgressMonitor());
					manager.addUndo(getName(), undoChange);
				}
			}
		}
	} finally {

		if (change != null) {
			change.dispose();
		}
	}
}
 
Example 11
Source File: ExtractVariableIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertAfterExtract(final CharSequence input, final CharSequence expected, final boolean isFinal) {
  try {
    final String inputString = input.toString();
    final String model = inputString.replace("$", "");
    final IFile file = this.workbenchTestHelper.createFile("Foo", model);
    final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
    try {
      final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
        Change _xblockexpression = null;
        {
          final int offset = inputString.indexOf("$");
          int _lastIndexOf = inputString.lastIndexOf("$");
          int _minus = (_lastIndexOf - 1);
          final int length = (_minus - offset);
          final TextSelection textSelection = new TextSelection(offset, length);
          final XExpression selection = this.util.findSelectedExpression(it, textSelection);
          final ExtractVariableRefactoring refactoring = this.refactoringProvider.get();
          refactoring.setFinal(isFinal);
          refactoring.initialize(editor, selection);
          NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
          final RefactoringStatus status = refactoring.checkAllConditions(_nullProgressMonitor);
          Assert.assertTrue(status.toString(), status.isOK());
          NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
          Change _createChange = refactoring.createChange(_nullProgressMonitor_1);
          NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
          _xblockexpression = _createChange.perform(_nullProgressMonitor_2);
        }
        return _xblockexpression;
      };
      editor.getDocument().<Change>readOnly(_function);
      Assert.assertEquals(expected.toString(), editor.getDocument().get());
    } finally {
      editor.close(false);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 12
Source File: ExtractLocalTestCase.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void runTest() throws Throwable {
    FileUtils.IN_TESTS = true;

    IDocument document = new Document(data.source);
    ICoreTextSelection selection = new CoreTextSelection(document, data.sourceSelection.getOffset(),
            data.sourceSelection.getLength());
    IGrammarVersionProvider versionProvider = createVersionProvider();
    RefactoringInfo info = new RefactoringInfo(document, selection, versionProvider);
    ExtractLocalRefactoring refactoring = new ExtractLocalRefactoring(info);

    ExtractLocalRequestProcessor requestProcessor = refactoring.getRequestProcessor();
    requestProcessor.setVariableName("extracted_variable");
    requestProcessor.setReplaceDuplicates(true);

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

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

    FileUtils.IN_TESTS = false;
}
 
Example 13
Source File: InlineLocalTestCase.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void runTest() throws Throwable {
    FileUtils.IN_TESTS = true;

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

        @Override
        public int getGrammarVersion() throws MisconfigurationException {
            return IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_2_7;
        }

        @Override
        public AdditionalGrammarVersionsToCheck getAdditionalGrammarVersions() throws MisconfigurationException {
            return null;
        }
    });
    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());

    FileUtils.IN_TESTS = false;
}
 
Example 14
Source File: RefactoringLocalTestBase.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/** Applies a rename refactoring 
 */
protected void applyRenameRefactoring(RefactoringRequest request, boolean expectError) throws CoreException {
    PyRenameEntryPoint processor = new PyRenameEntryPoint(new PyRefactoringRequest(request));
    NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
    checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
    checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
    Change change = processor.createChange(nullProgressMonitor);
    if (!expectError) {
        //otherwise, if there is an error, the change may be null
        change.perform(nullProgressMonitor);
    }
}
 
Example 15
Source File: RefactoringDocumentProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Change checkEdit(IRefactoringDocument document, TextEdit textEdit) throws CoreException {
	Change change = document.createChange("change", textEdit);
	assertNotNull(change);
	Change undoChange = change.perform(new NullProgressMonitor());
	return undoChange;
}
 
Example 16
Source File: ExtractMethodIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void assertAfterExtract(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final CharSequence expected) {
  try {
    final String inputString = input.toString();
    final IFile file = this.workbenchTestHelper.createFile("Foo", inputString.replace("$", ""));
    final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
    try {
      final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
        Change _xblockexpression = null;
        {
          int _indexOf = inputString.indexOf("$");
          int _lastIndexOf = inputString.lastIndexOf("$");
          int _indexOf_1 = inputString.indexOf("$");
          int _minus = (_lastIndexOf - _indexOf_1);
          int _minus_1 = (_minus - 1);
          TextSelection _textSelection = new TextSelection(_indexOf, _minus_1);
          final List<XExpression> selection = this.util.findSelectedSiblingExpressions(it, _textSelection);
          final ExtractMethodRefactoring refactoring = this.refactoringProvider.get();
          refactoring.initialize(editor, selection, true);
          refactoring.setExplicitlyDeclareReturnType(false);
          refactoring.setVisibility(JvmVisibility.PUBLIC);
          refactoring.setMethodName("bar");
          NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
          RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
          Assert.assertTrue(status.toString(), status.isOK());
          initializer.apply(refactoring);
          NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
          status = refactoring.checkFinalConditions(_nullProgressMonitor_1);
          Assert.assertTrue(status.toString(), status.isOK());
          NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
          Change _createChange = refactoring.createChange(_nullProgressMonitor_2);
          NullProgressMonitor _nullProgressMonitor_3 = new NullProgressMonitor();
          _xblockexpression = _createChange.perform(_nullProgressMonitor_3);
        }
        return _xblockexpression;
      };
      editor.getDocument().<Change>readOnly(_function);
      Assert.assertEquals(expected.toString(), editor.getDocument().get());
    } finally {
      editor.close(false);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}