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

The following examples show how to use org.eclipse.ltk.core.refactoring.TextEditBasedChange. 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: TextEditBasedChangeManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns all text changes managed by this instance.
 *
 * @return all text changes managed by this instance
 */
public TextEditBasedChange[] getAllChanges(){
	Set<ICompilationUnit> cuSet= fMap.keySet();
	ICompilationUnit[] cus= cuSet.toArray(new ICompilationUnit[cuSet.size()]);
	// sort by cu name:
	Arrays.sort(cus, new Comparator<ICompilationUnit>() {
		public int compare(ICompilationUnit o1, ICompilationUnit o2) {
			String name1= o1.getElementName();
			String name2= o2.getElementName();
			return name1.compareTo(name2);
		}
	});

	TextEditBasedChange[] textChanges= new TextEditBasedChange[cus.length];
	for (int i= 0; i < cus.length; i++) {
		textChanges[i]= fMap.get(cus[i]);
	}
	return textChanges;
}
 
Example #2
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 #3
Source File: RefactoringAdapterFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object getAdapter(Object object, Class key) {
	if (!TextEditChangeNode.class.equals(key))
		return null;
	if (!(object instanceof CompilationUnitChange) && !(object instanceof MultiStateCompilationUnitChange))
		return null;
	return new CompilationUnitChangeNode((TextEditBasedChange)object);
}
 
Example #4
Source File: CompilationUnitChangeNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private TextEditBasedChangeGroup[] getSortedChangeGroups(TextEditBasedChange change) {
	TextEditBasedChangeGroup[] edits= change.getChangeGroups();
	List<TextEditBasedChangeGroup> result= new ArrayList<TextEditBasedChangeGroup>(edits.length);
	for (int i= 0; i < edits.length; i++) {
		if (!edits[i].getTextEditGroup().isEmpty())
			result.add(edits[i]);
	}
	Comparator<TextEditBasedChangeGroup> comparator= new OffsetComparator();
	Collections.sort(result, comparator);
	return result.toArray(new TextEditBasedChangeGroup[result.size()]);
}
 
Example #5
Source File: CompilationUnitChangeNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ChildNode[] createChildNodes() {
	TextEditBasedChange change= getTextEditBasedChange();
	if (change instanceof MultiStateTextFileChange) {
		return new ChildNode[0]; // no edit preview & edit disabling possible in the MultiStateTextFileChange (edits must be applied in sequence)
	}

	ICompilationUnit cunit= (ICompilationUnit) change.getAdapter(ICompilationUnit.class);
	if (cunit != null) {
		List<ChildNode> children= new ArrayList<ChildNode>(5);
		Map<IJavaElement, JavaLanguageNode> map= new HashMap<IJavaElement, JavaLanguageNode>(20);
		TextEditBasedChangeGroup[] changes= getSortedChangeGroups(change);
		for (int i= 0; i < changes.length; i++) {
			TextEditBasedChangeGroup tec= changes[i];
			try {
				IJavaElement element= getModifiedJavaElement(tec, cunit);
				if (element.equals(cunit)) {
					children.add(createTextEditGroupNode(this, tec));
				} else {
					JavaLanguageNode pjce= getChangeElement(map, element, children, this);
					pjce.addChild(createTextEditGroupNode(pjce, tec));
				}
			} catch (JavaModelException e) {
				children.add(createTextEditGroupNode(this, tec));
			}
		}
		return children.toArray(new ChildNode[children.size()]);
	} else {
		return EMPTY_CHILDREN;
	}
}
 
Example #6
Source File: TextEditUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the text edit group which has the given text edit as a child.
 * 
 * @param change the change that contains the groups
 * @param edit the text edit to find
 * @return the group that has the text edit as a child, or null
 */
public static TextEditGroup findTextEditGroup(TextEditBasedChange change, TextEdit edit) {
  for (TextEditGroup group : getTextEditGroups(change.getChangeGroups())) {
    for (TextEdit curEdit : group.getTextEdits()) {
      if (edit == curEdit) {
        return group;
      }
    }
  }

  return null;
}
 
Example #7
Source File: ChangeUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Attempts to merge all the text changes rooted at <code>rootChange</code>
 * into the refactoring's shared text change for each file (via
 * {@link RefactoringParticipant#getTextChange(Object)}). Any text changes
 * that are merged will be removed from their parents.
 * 
 * 
 * @param participant the participant which will be used to get the shared
 *          text change for each file
 * @param rootChange the root of the change tree that will be searched for
 *          text changes to merge into the shared text changes
 * @return true if the entire tree was merged and the root change is no longer
 *         valid
 */
public static boolean mergeParticipantTextChanges(
    final RefactoringParticipant participant, Change rootChange) {
  final List<Change> dupChanges = new ArrayList<Change>();
  ChangeUtilities.acceptOnChange(rootChange, new ChangeVisitor() {
    public void visit(Change change) {
      if (change instanceof TextEditBasedChange) {
        TextEditBasedChange textChange = (TextEditBasedChange) change;
        TextChange existingTextChange = participant.getTextChange(textChange.getModifiedElement());
        if (existingTextChange == null) {
          return;
        }

        // Merge all changes from text change into the existing
        TextEditUtilities.moveTextEditGroupsIntoChange(
            textChange.getChangeGroups(), existingTextChange);

        dupChanges.add(textChange);
      }
    }
  });

  for (Change dupChange : dupChanges) {
    ChangeUtilities.removeChange(dupChange,
        (CompositeChange) dupChange.getParent());
  }

  return dupChanges.contains(rootChange);
}
 
Example #8
Source File: DisplayChangeWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public static Change wrap(Change delegate) {
	if(delegate instanceof TextEditBasedChange) {
		return new TextEditBased((TextEditBasedChange) delegate);
	} else if (delegate != null) {
		return new Generic(delegate);
	} else {
		return null;
	}
}
 
Example #9
Source File: TextChangeCombinerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertTextType(Change change) {
	if (change instanceof CompositeChange) {
		for (Change child : ((CompositeChange) change).getChildren()) {
			assertTextType(child);
		}
	} else {
		assertTrue(change.getClass().getName(), change instanceof TextEditBasedChange);
		assertEquals(TEXT_TYPE, ((TextEditBasedChange) change).getTextType());
	}
}
 
Example #10
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 #11
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 #12
Source File: DisplayChangeWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public TextEditBasedChange getDelegate() {
	return delegate;
}
 
Example #13
Source File: DisplayChangeWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected TextEditBased(TextEditBasedChange delegate) {
	super(delegate.getName());
	this.delegate = delegate;
}
 
Example #14
Source File: TextEditUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Removes a text edit from a group, optionally updating its owner change. If
 * the edit is the root edit of the owner change, the change will be removed
 * from its parent.
 * 
 * @param edit the text edit
 * @param group the text edit group to update, optional
 * @param change the change to update, optional
 * @return true if the text edit was removed
 */
public static boolean removeTextEdit(TextEdit edit, TextEditGroup group,
    TextEditBasedChange change) {
  boolean removed = false;
  boolean removeChange = false;

  // First remove this edit from its parent, if it has one
  TextEdit parentEdit = edit.getParent();
  if (parentEdit != null) {
    removed |= parentEdit.removeChild(edit);
    if (!parentEdit.hasChildren()) {
      // This parent edit is now empty, so remove it from the change and group
      edit = parentEdit;
    }
  }

  // Remove the edit from the group
  if (group != null) {
    removed |= group.removeTextEdit(edit);
    if (group.getTextEdits().length == 0) {
      // The group has no more edits. We'd like to remove it from the change,
      // but there is no API. Instead, see if this group is the only group in
      // the change and trigger removing the change altogether.
      if (change != null) {
        TextEditBasedChangeGroup[] changeGroups = change.getChangeGroups();
        if (changeGroups.length == 1
            && changeGroups[0].getTextEditGroup().equals(group)) {
          // This is the only group in the change, remove the change
          removeChange = true;
        }
      }
    }
  }

  // Remove the change if this was its root edit
  if (!removeChange && change != null && change instanceof TextFileChange) {
    TextFileChange textFileChange = (TextFileChange) change;
    if (edit.equals(textFileChange.getEdit())) {
      removeChange = true;
    }
  }

  // Execute change removal
  if (removeChange && change != null) {
    Change parentChange = change.getParent();
    if (parentChange instanceof CompositeChange) {
      removed |= ((CompositeChange) parentChange).remove(change);
    }
  }

  return removed;
}
 
Example #15
Source File: UseSuperTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final Change createChange(final IProgressMonitor monitor) throws CoreException, OperationCanceledException {
	Assert.isNotNull(monitor);
	try {
		fChanges= 0;
		monitor.beginTask("", 1); //$NON-NLS-1$
		monitor.setTaskName(RefactoringCoreMessages.ExtractInterfaceProcessor_creating);
		final TextEditBasedChange[] changes= fChangeManager.getAllChanges();
		if (changes != null && changes.length != 0) {
			fChanges= changes.length;
			IJavaProject project= null;
			if (!fSubType.isBinary())
				project= fSubType.getJavaProject();
			int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
			try {
				if (fSubType.isLocal() || fSubType.isAnonymous())
					flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
			} catch (JavaModelException exception) {
				JavaPlugin.log(exception);
			}
			final String name= project != null ? project.getElementName() : null;
			final String description= Messages.format(RefactoringCoreMessages.UseSuperTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fSuperType.getElementName()));
			final String header= Messages.format(RefactoringCoreMessages.UseSuperTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fSuperType, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(fSubType, JavaElementLabels.ALL_FULLY_QUALIFIED) });
			final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(name, this, header);
			comment.addSetting(Messages.format(RefactoringCoreMessages.UseSuperTypeProcessor_refactored_element_pattern, JavaElementLabels.getElementLabel(fSuperType, JavaElementLabels.ALL_FULLY_QUALIFIED)));
			addSuperTypeSettings(comment, false);
			final UseSupertypeDescriptor descriptor= RefactoringSignatureDescriptorFactory.createUseSupertypeDescriptor();
			descriptor.setProject(name);
			descriptor.setDescription(description);
			descriptor.setComment(comment.asString());
			descriptor.setFlags(flags);
			descriptor.setSubtype(getSubType());
			descriptor.setSupertype(getSuperType());
			descriptor.setReplaceInstanceof(fInstanceOf);
			return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.UseSupertypeWherePossibleRefactoring_name, fChangeManager.getAllChanges());
		}
		monitor.worked(1);
	} finally {
		monitor.done();
	}
	return null;
}
 
Example #16
Source File: CompilationUnitChangeNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public CompilationUnitChangeNode(TextEditBasedChange change) {
	super(change);
}
 
Example #17
Source File: TextEditBasedChangeManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Adds an association between the given compilation unit and the passed
 * change to this manager.
 *
 * @param cu the compilation unit (key)
 * @param change the change associated with the compilation unit
 */
public void manage(ICompilationUnit cu, TextEditBasedChange change) {
	fMap.put(cu, change);
}
 
Example #18
Source File: TextEditBasedChangeManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Removes the <tt>TextEditBasedChange</tt> managed under the given key
 * <code>unit<code>.
 *
 * @param unit the key determining the <tt>TextEditBasedChange</tt> to be removed.
 * @return the removed <tt>TextEditBasedChange</tt>.
 */
public TextEditBasedChange remove(ICompilationUnit unit) {
	return fMap.remove(unit);
}