org.eclipse.ltk.core.refactoring.CompositeChange Java Examples

The following examples show how to use org.eclipse.ltk.core.refactoring.CompositeChange. 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: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws JavaModelException {
	IPackageFragment[] fragments= getPackages();
	pm.beginTask("", fragments.length); //$NON-NLS-1$
	CompositeChange result= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move_package);
	result.markAsSynthetic();
	IPackageFragmentRoot root= getDestinationAsPackageFragmentRoot();
	for (int i= 0; i < fragments.length; i++) {
		if (root == null) {
			result.add(createChange(fragments[i], (IContainer)getResourceDestination()));
		} else {
			result.add(createChange(fragments[i], root));
		}
		pm.worked(1);
		if (pm.isCanceled()) {
			throw new OperationCanceledException();
		}
	}
	pm.done();
	return result;
}
 
Example #2
Source File: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
	if (fQualifiedNameSearchResult != null) {
		CompositeChange parent= (CompositeChange) fRenamePackageChange.getParent();
		try {
			/*
			 * Sneak text changes in before the package rename to ensure
			 * modified files are still at original location (see
			 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=154238)
			 */
			parent.remove(fRenamePackageChange);
			parent.add(fQualifiedNameSearchResult.getSingleChange(Changes.getModifiedFiles(participantChanges)));
		} finally {
			fQualifiedNameSearchResult= null;
			parent.add(fRenamePackageChange);
			fRenamePackageChange= null;
		}
	}
	return null;
}
 
Example #3
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries copyQueries) {
	NewNameProposer nameProposer= new NewNameProposer();
	IPackageFragmentRoot[] roots= getPackageFragmentRoots();
	pm.beginTask("", roots.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_source_folder);
	composite.markAsSynthetic();
	IJavaProject destination= getDestinationJavaProject();
	for (int i= 0; i < roots.length; i++) {
		if (destination == null) {
			composite.add(createChange(roots[i], (IContainer) getResourceDestination(), nameProposer, copyQueries));
		} else {
			composite.add(createChange(roots[i], destination, nameProposer, copyQueries));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #4
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 #5
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm, INewNameQueries newNameQueries) throws JavaModelException {
	NewNameProposer nameProposer= new NewNameProposer();
	IPackageFragment[] fragments= getPackages();
	pm.beginTask("", fragments.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_package);
	composite.markAsSynthetic();
	IPackageFragmentRoot root= getDestinationAsPackageFragmentRoot();
	for (int i= 0; i < fragments.length; i++) {
		if (root == null) {
			composite.add(createChange(fragments[i], (IContainer) getResourceDestination(), nameProposer, newNameQueries));
		} else {
			composite.add(createChange(fragments[i], root, nameProposer, newNameQueries));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #6
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 #7
Source File: GetterSetterCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public TextFileChange getChange(IFile file) throws CoreException {
	final SelfEncapsulateFieldRefactoring refactoring= new SelfEncapsulateFieldRefactoring(fField);
	refactoring.setVisibility(Flags.AccPublic);
	refactoring.setConsiderVisibility(false);//private field references are just searched in local file
	refactoring.checkInitialConditions(new NullProgressMonitor());
	refactoring.checkFinalConditions(new NullProgressMonitor());
	Change createdChange= refactoring.createChange(new NullProgressMonitor());
	if (createdChange instanceof CompositeChange) {
		Change[] children= ((CompositeChange) createdChange).getChildren();
		for (int i= 0; i < children.length; i++) {
			Change curr= children[i];
			if (curr instanceof TextFileChange && ((TextFileChange) curr).getFile().equals(file)) {
				return (TextFileChange) curr;
			}
		}
	}
	return null;
}
 
Example #8
Source File: UpdateAcceptorTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testAddTextEdit() throws Exception {

		updateAcceptor.accept(resourceURI0, new ReplaceEdit(0, 1, "foo"));
		updateAcceptor.accept(resourceURI1, new ReplaceEdit(1, 2, "bar"));
		updateAcceptor.accept(resourceURI0, new ReplaceEdit(3, 4, "baz"));

		Change change = updateAcceptor.createCompositeChange(CHANGE_NAME, new NullProgressMonitor());
		assertTrue(change instanceof CompositeChange);
		Change[] children = ((CompositeChange) change).getChildren();
		assertEquals(2, children.length);
		assertTrue(children[0] instanceof MockChange);
		MockChange change0 = (MockChange) children[0];
		assertEquals(CHANGE_NAME, change0.getName());
		assertTrue(children[1] instanceof MockChange);
		MockChange change1 = (MockChange) children[1];
		assertEquals(CHANGE_NAME, change1.getName());
		assertTrue(change0.getTextEdit() instanceof MultiTextEdit);
		assertTrue(change1.getTextEdit() instanceof MultiTextEdit);
		assertEquals(
				3,
				((MultiTextEdit) change0.getTextEdit()).getChildrenSize()
						+ ((MultiTextEdit) change1.getTextEdit()).getChildrenSize());
	}
 
Example #9
Source File: TextChangeCombiner.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public Change combineChanges(Change masterChange) {
	if (!(masterChange instanceof CompositeChange))
		return masterChange;
	Map<Object, TextChange> resource2textChange = newLinkedHashMap();
	List<Change> otherChanges = newArrayList();
	Set<IEditorPart> editorsToSave = newHashSet();
	visitCompositeChange((CompositeChange) masterChange, resource2textChange, otherChanges, editorsToSave);
	CompositeChange compositeChange = new FilteringCompositeChange(masterChange.getName());
	for (TextChange combinedTextChange : resource2textChange.values()) {
		if(((MultiTextEdit) combinedTextChange.getEdit()).getChildrenSize() >0) {
			if(combinedTextChange instanceof EditorDocumentChange) {
				((EditorDocumentChange) combinedTextChange).setDoSave(editorsToSave.contains(((EditorDocumentChange) combinedTextChange).getEditor()));
				compositeChange.add(combinedTextChange);
			}
			else
				compositeChange.add(DisplayChangeWrapper.wrap(combinedTextChange));
		}
	}
	for(Change otherChange: otherChanges) 
		compositeChange.add(DisplayChangeWrapper.wrap(otherChange));
	if(compositeChange.getChildren().length == 0)
		return null;
	return compositeChange;
}
 
Example #10
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	try {
		pm.beginTask("Creating change...", 1);
		final Collection<Change> changes = new ArrayList<Change>();
		changes.addAll(compilationUnitChanges.values());
		changes.addAll(createCompilationUnitChanges.values());
		CompositeChange change = new CompositeChange(getName(), changes.toArray(new Change[changes.size()])) {
			@Override
			public ChangeDescriptor getDescriptor() {
				ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
				String project = sourceICompilationUnit.getJavaProject().getElementName();
				String description = MessageFormat.format("Replace Type Code with State/Strategy in method ''{0}''", new Object[] { typeCheckElimination.getTypeCheckMethod().getName().getIdentifier()});
				String comment = null;
				return new RefactoringChangeDescriptor(new ReplaceTypeCodeWithStateStrategyDescriptor(project, description, comment,
						sourceFile, sourceCompilationUnit, sourceTypeDeclaration, typeCheckElimination));
			}
		};
		return change;
	} finally {
		pm.done();
	}
}
 
Example #11
Source File: ReplaceConditionalWithPolymorphism.java    From JDeodorant with MIT License 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	try {
		pm.beginTask("Creating change...", 1);
		final Collection<CompilationUnitChange> changes = compilationUnitChanges.values();
		CompositeChange change = new CompositeChange(getName(), changes.toArray(new Change[changes.size()])) {
			@Override
			public ChangeDescriptor getDescriptor() {
				ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
				String project = sourceICompilationUnit.getJavaProject().getElementName();
				String description = MessageFormat.format("Replace Conditional with Polymorphism in method ''{0}''", new Object[] { typeCheckElimination.getTypeCheckMethod().getName().getIdentifier()});
				String comment = null;
				return new RefactoringChangeDescriptor(new ReplaceConditionalWithPolymorphismDescriptor(project, description, comment,
						sourceFile, sourceCompilationUnit, sourceTypeDeclaration, typeCheckElimination));
			}
		};
		return change;
	} finally {
		pm.done();
	}
}
 
Example #12
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	try {
		pm.beginTask("Creating change...", 1);
		final Collection<CompilationUnitChange> changes = fChanges.values();
		CompositeChange change = new CompositeChange(getName(), changes.toArray(new Change[changes.size()])) {
			@Override
			public ChangeDescriptor getDescriptor() {
				ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
				String project = sourceICompilationUnit.getJavaProject().getElementName();
				String description = MessageFormat.format("Move method ''{0}''", new Object[] { sourceMethod.getName().getIdentifier()});
				String comment = MessageFormat.format("Move method ''{0}'' to ''{1}''", new Object[] { sourceMethod.getName().getIdentifier(), targetTypeDeclaration.getName().getIdentifier()});
				return new RefactoringChangeDescriptor(new MoveMethodRefactoringDescriptor(project, description, comment,
						sourceCompilationUnit, targetCompilationUnit,
						sourceTypeDeclaration, targetTypeDeclaration, sourceMethod,
						additionalMethodsToBeMoved, leaveDelegate, movedMethodName));
			}
		};
		return change;
	} finally {
		pm.done();
	}
}
 
Example #13
Source File: ExtractMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	try {
		pm.beginTask("Creating change...", 1);
		final Collection<TextFileChange> changes = new ArrayList<TextFileChange>();
		changes.add(compilationUnitChange);
		CompositeChange change = new CompositeChange(getName(), changes.toArray(new Change[changes.size()])) {
			@Override
			public ChangeDescriptor getDescriptor() {
				ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
				String project = sourceICompilationUnit.getJavaProject().getElementName();
				String description = MessageFormat.format("Extract from method ''{0}''", new Object[] { sourceMethodDeclaration.getName().getIdentifier()});
				String comment = MessageFormat.format("Extract from method ''{0}'' variable ''{1}''",
						new Object[] { sourceMethodDeclaration.getName().getIdentifier(), slice.getLocalVariableCriterion().toString()});
				return new RefactoringChangeDescriptor(new ExtractMethodRefactoringDescriptor(project, description, comment,
						sourceCompilationUnit, slice));
			}
		};
		return change;
	} finally {
		pm.done();
	}
}
 
Example #14
Source File: QualifiedNameSearchResult.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public Change getSingleChange(IFile[] alreadyTouchedFiles) {
	Collection<TextChange> values= fChanges.values();
	if (values.size() == 0) {
		return null;
	}

	CompositeChange result= new CompositeChange(RefactoringCoreMessages.QualifiedNameSearchResult_change_name);
	result.markAsSynthetic();
	List<IFile> files= Arrays.asList(alreadyTouchedFiles);
	for (Iterator<TextChange> iter= values.iterator(); iter.hasNext();) {
		TextFileChange change= (TextFileChange)iter.next();
		if (!files.contains(change.getFile())) {
			result.add(change);
		}
	}
	return result;
}
 
Example #15
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws JavaModelException {
	IPackageFragmentRoot[] roots= getPackageFragmentRoots();
	pm.beginTask("", roots.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move_source_folder);
	composite.markAsSynthetic();
	IJavaProject destination= getDestinationJavaProject();
	for (int i= 0; i < roots.length; i++) {
		if (destination == null) {
			composite.add(new MovePackageFragmentRootChange(roots[i], (IContainer) getResourceDestination(), null));
		} else {
			composite.add(createChange(roots[i], destination));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #16
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 #17
Source File: DeleteChangeCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static Change createPackageFragmentRootDeleteChange(IPackageFragmentRoot root) throws JavaModelException {
	IResource resource= root.getResource();
	if (resource != null && resource.isLinked()){
		//XXX using this code is a workaround for jcore bug 31998
		//jcore cannot handle linked stuff
		//normally, we should always create DeletePackageFragmentRootChange
		CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.DeleteRefactoring_delete_package_fragment_root);

		ClasspathChange change= ClasspathChange.removeEntryChange(root.getJavaProject(), root.getRawClasspathEntry());
		if (change != null) {
			composite.add(change);
		}
		Assert.isTrue(! Checks.isClasspathDelete(root));//checked in preconditions
		composite.add(createDeleteChange(resource));

		return composite;
	} else {
		Assert.isTrue(! root.isExternal());
		// TODO remove the query argument
		return new DeletePackageFragmentRootChange(root, true, null);
	}
}
 
Example #18
Source File: QualifiedNameSearchResult.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Change getSingleChange(IFile[] alreadyTouchedFiles) {
	Collection<TextChange> values= fChanges.values();
	if (values.size() == 0)
		return null;

	CompositeChange result= new CompositeChange(RefactoringCoreMessages.QualifiedNameSearchResult_change_name);
	result.markAsSynthetic();
	List<IFile> files= Arrays.asList(alreadyTouchedFiles);
	for (Iterator<TextChange> iter= values.iterator(); iter.hasNext();) {
		TextFileChange change= (TextFileChange)iter.next();
		if (!files.contains(change.getFile())) {
			result.add(change);
		}
	}
	return result;
}
 
Example #19
Source File: DeleteChangeCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Change createPackageFragmentRootDeleteChange(IPackageFragmentRoot root) throws JavaModelException {
	IResource resource= root.getResource();
	if (resource != null && resource.isLinked()){
		//XXX using this code is a workaround for jcore bug 31998
		//jcore cannot handle linked stuff
		//normally, we should always create DeletePackageFragmentRootChange
		CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.DeleteRefactoring_delete_package_fragment_root);

		ClasspathChange change= ClasspathChange.removeEntryChange(root.getJavaProject(), root.getRawClasspathEntry());
		if (change != null) {
			composite.add(change);
		}
		Assert.isTrue(! Checks.isClasspathDelete(root));//checked in preconditions
		composite.add(createDeleteChange(resource));

		return composite;
	} else {
		Assert.isTrue(! root.isExternal());
		// TODO remove the query argument
		return new DeletePackageFragmentRootChange(root, true, null);
	}
}
 
Example #20
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Change createChange(IProgressMonitor pm) throws JavaModelException {
	IPackageFragment[] fragments= getPackages();
	pm.beginTask("", fragments.length); //$NON-NLS-1$
	CompositeChange result= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move_package);
	result.markAsSynthetic();
	IPackageFragmentRoot root= getDestinationAsPackageFragmentRoot();
	for (int i= 0; i < fragments.length; i++) {
		if (root == null) {
			result.add(createChange(fragments[i], (IContainer)getResourceDestination()));
		} else {
			result.add(createChange(fragments[i], root));
		}
		pm.worked(1);
		if (pm.isCanceled())
			throw new OperationCanceledException();
	}
	pm.done();
	return result;
}
 
Example #21
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Change createChange(IProgressMonitor pm) throws JavaModelException {
	IPackageFragmentRoot[] roots= getPackageFragmentRoots();
	pm.beginTask("", roots.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move_source_folder);
	composite.markAsSynthetic();
	IJavaProject destination= getDestinationJavaProject();
	for (int i= 0; i < roots.length; i++) {
		if (destination == null) {
			composite.add(new MovePackageFragmentRootChange(roots[i], (IContainer) getResourceDestination(), null));
		} else {
			composite.add(createChange(roots[i], destination));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #22
Source File: ChangeUtilTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testConvertSimpleCompositeChange() throws CoreException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
	CompositeChange change = new CompositeChange("simple composite change");

	RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java");
	change.add(resourceChange);
	CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu);
	textChange.setEdit(new InsertEdit(0, "// some content"));
	change.add(textChange);

	WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
	assertEquals(edit.getDocumentChanges().size(), 2);
	assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile);
	assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit);
}
 
Example #23
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Change createSimpleMoveChange(IProgressMonitor pm) {
	CompositeChange result= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move);
	result.markAsSynthetic();
	IFile[] files= getFiles();
	IFolder[] folders= getFolders();
	ICompilationUnit[] cus= getCus();
	pm.beginTask("", files.length + folders.length + cus.length); //$NON-NLS-1$
	for (int i= 0; i < files.length; i++) {
		result.add(createChange(files[i]));
		pm.worked(1);
	}
	if (pm.isCanceled())
		throw new OperationCanceledException();
	for (int i= 0; i < folders.length; i++) {
		result.add(createChange(folders[i]));
		pm.worked(1);
	}
	if (pm.isCanceled())
		throw new OperationCanceledException();
	for (int i= 0; i < cus.length; i++) {
		result.add(createChange(cus[i]));
		pm.worked(1);
	}
	pm.done();
	return result;
}
 
Example #24
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Change createChange(IProgressMonitor pm, INewNameQueries newNameQueries) throws JavaModelException {
	NewNameProposer nameProposer= new NewNameProposer();
	IPackageFragment[] fragments= getPackages();
	pm.beginTask("", fragments.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_package);
	composite.markAsSynthetic();
	IPackageFragmentRoot root= getDestinationAsPackageFragmentRoot();
	for (int i= 0; i < fragments.length; i++) {
		if (root == null) {
			composite.add(createChange(fragments[i], (IContainer) getResourceDestination(), nameProposer, newNameQueries));
		} else {
			composite.add(createChange(fragments[i], root, nameProposer, newNameQueries));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #25
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Change createChange(IProgressMonitor pm, INewNameQueries copyQueries) {
	NewNameProposer nameProposer= new NewNameProposer();
	IPackageFragmentRoot[] roots= getPackageFragmentRoots();
	pm.beginTask("", roots.length); //$NON-NLS-1$
	CompositeChange composite= new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_copy_source_folder);
	composite.markAsSynthetic();
	IJavaProject destination= getDestinationJavaProject();
	for (int i= 0; i < roots.length; i++) {
		if (destination == null) {
			composite.add(createChange(roots[i], (IContainer) getResourceDestination(), nameProposer, copyQueries));
		} else {
			composite.add(createChange(roots[i], destination, nameProposer, copyQueries));
		}
		pm.worked(1);
	}
	pm.done();
	return composite;
}
 
Example #26
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
	if (fQualifiedNameSearchResult != null) {
		CompositeChange parent= (CompositeChange) fRenamePackageChange.getParent();
		try {
			/*
			 * Sneak text changes in before the package rename to ensure
			 * modified files are still at original location (see
			 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=154238)
			 */
			parent.remove(fRenamePackageChange);
			parent.add(fQualifiedNameSearchResult.getSingleChange(Changes.getModifiedFiles(participantChanges)));
		} finally {
			fQualifiedNameSearchResult= null;
			parent.add(fRenamePackageChange);
			fRenamePackageChange= null;
		}
	}
	return null;
}
 
Example #27
Source File: GWTRefactoringSupportTest.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public void testCreateChange() {
  GWTRefactoringSupport support = new DummyGWTRefactoringSupport();
  support.setUpdateReferences(true);

  IType refactorTestType = refactorTestClass.getCompilationUnit().findPrimaryType();
  support.setOldElement(refactorTestType);

  RefactoringParticipant participant = new DummyRefactoringParticipant();
  IRefactoringChangeFactory changeFactory = new DefaultChangeFactory();
  CompositeChange change = support.createChange(participant, changeFactory);

  // Return value should contain one child change
  Change[] changeChildren = change.getChildren();
  assertEquals(1, changeChildren.length);

  // Root edit should contain two child edits, one for each JSNI ref
  TextChange childChange = (TextChange) changeChildren[0];
  TextEdit changeEdit = childChange.getEdit();
  assertEquals(2, changeEdit.getChildrenSize());
}
 
Example #28
Source File: GWTRefactoringSupportTest.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public void testCreateChangeWithoutEdits() throws JavaModelException {
  GWTRefactoringSupport support = new DummyGWTRefactoringSupport();
  support.setUpdateReferences(true);

  IType entryPointType = getTestProject().findType(
      AbstractGWTPluginTestCase.TEST_PROJECT_SRC_PACKAGE,
      AbstractGWTPluginTestCase.TEST_PROJECT_ENTRY_POINT);
  support.setOldElement(entryPointType);

  // There are no references to the entry point class, so the Change returned
  // should be null
  RefactoringParticipant participant = new DummyRefactoringParticipant();
  IRefactoringChangeFactory changeFactory = new DefaultChangeFactory();
  CompositeChange change = support.createChange(participant, changeFactory);
  assertNull(change);
}
 
Example #29
Source File: ChangeUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Inserts a change at the specified index.
 * 
 * @param change the change to insert
 * @param insertIndex the index to insert at (if >= the number of children, it
 *          will be added to the end)
 * @param parentChange the new parent of the change
 */
public static void insertChange(Change change, int insertIndex,
    CompositeChange parentChange) {
  Change[] changes = parentChange.getChildren();

  if (insertIndex >= changes.length) {
    parentChange.add(change);
  } else {
    // CompositeChange.clear does not clear the parent field on the removed
    // changes, but CompositeChange.remove does
    for (Change curChange : changes) {
      parentChange.remove(curChange);
    }

    for (int i = 0; i < changes.length; i++) {
      if (i == insertIndex) {
        parentChange.add(change);
      }
      parentChange.add(changes[i]);
    }
  }
}
 
Example #30
Source File: ChangeUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Replaces a change with another change created by the given factory.
 * 
 * @param originalChange the original change that will be replaced in its
 *          hierarchy (no children will be copied)
 * @param replacementChangeFactory the factory that creates the replacement
 *          change
 * @return true if a replacement occurred
 */
public static boolean replaceChange(Change originalChange,
    ReplacementChangeFactory replacementChangeFactory) {
  // The old change must not have a parent when we wrap it
  Change parentChange = originalChange.getParent();
  if (parentChange == null || !(parentChange instanceof CompositeChange)) {
    return false;
  }

  int oldIndex = ChangeUtilities.removeChange(originalChange,
      (CompositeChange) parentChange);
  if (oldIndex == -1) {
    return false;
  }

  Change newChange = replacementChangeFactory.createChange(originalChange);
  if (newChange == null) {
    return false;
  }

  ChangeUtilities.insertChange(newChange, oldIndex,
      (CompositeChange) parentChange);

  return true;
}