use of org.eclipse.ltk.core.refactoring.TextChange in project webtools.sourceediting by eclipse.
the class BasicRefactorSearchRequestor method createChange.
/**
* </p>If a {@link TextChange} does not already exist for the given {@link JavaSearchDocumentDelegate}
* then a new one will be created with the given {@link TextEdit}. Otherwise the given {@link TextEdit}
* will be added to a new group and added to the existing change and <code>null</code> will be returned.</p>
*
* @param searchDoc the {@link JavaSearchDocumentDelegate} that the <code>edit</code> will be applied to
* @param edit the {@link TextEdit} that needs to be added to a new {@link TextChange} or appended to an
* existing one
* @param participant the {@link RefactoringParticipant} that knows about the existing {@link TextChange}s
* @return a new {@link Change} if there was not one already existing for the document in question,
* else <code>null</code>
*/
private Change createChange(JavaSearchDocumentDelegate searchDoc, TextEdit edit, RefactoringParticipant participant) {
IDocument doc = searchDoc.getJspTranslation().getJspDocument();
String description = getDescription();
TextChange existingChange = participant.getTextChange(searchDoc.getFile());
TextChange change = null;
if (existingChange != null) {
try {
existingChange.addEdit(edit);
} catch (MalformedTreeException e) {
Logger.logException(// $NON-NLS-1$
"MalformedTreeException while adding edit " + edit + " to existing change " + change, // $NON-NLS-1$
e);
}
TextEditGroup group = new TextEditGroup(description, edit);
existingChange.addTextEditGroup(group);
} else {
change = new JSPRenameChange(searchDoc.getFile(), doc, edit, searchDoc.getFile().getName());
TextEditGroup group = new TextEditGroup(description, edit);
change.addTextEditGroup(group);
}
return change;
}
use of org.eclipse.ltk.core.refactoring.TextChange in project webtools.sourceediting by eclipse.
the class BasicRefactorSearchRequestor method getChanges.
/**
* Gets new {@link Change}s created as a result of this {@link SearchRequestor}.
* Any existing {@link TextChange}s that had new edits added to them will not be
* returned.
*
* @param participant {@link RefactoringParticipant} to determine if there are already existing
* {@link TextChange}s for the documents that this {@link SearchRequestor} found.
* If existing
* {@link TextChange}s are found then they will be used for any new edits, else new {@link TextChange}s
* will be created.
*
* @return Any new {@link TextChange}s created by this {@link SearchRequestor}. If edits were
* added to existing {@link TextChange}s then those existing {@link TextChange}s will not be
* returned in this array.
*/
public Change[] getChanges(RefactoringParticipant participant) {
JSPSearchSupport support = JSPSearchSupport.getInstance();
List changes = new ArrayList();
Iterator keys = fSearchDocPath2JavaEditMap.keySet().iterator();
String searchDocPath = null;
SearchDocument delegate = null;
while (keys.hasNext()) {
// create on the fly
searchDocPath = (String) keys.next();
MultiTextEdit javaEdit = (MultiTextEdit) fSearchDocPath2JavaEditMap.get(searchDocPath);
delegate = support.getSearchDocument(searchDocPath);
if (delegate != null && delegate instanceof JavaSearchDocumentDelegate) {
JavaSearchDocumentDelegate javaDelegate = (JavaSearchDocumentDelegate) delegate;
Change change = createChange(javaDelegate, javaDelegate.getJspTranslation().getJspEdit(javaEdit), participant);
changes.add(change);
}
}
return (Change[]) changes.toArray(new Change[changes.size()]);
}
use of org.eclipse.ltk.core.refactoring.TextChange in project webtools.sourceediting by eclipse.
the class JSPMoveParticipant method createChange.
/**
* @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#createChange(
* org.eclipse.core.runtime.IProgressMonitor)
*/
public Change createChange(IProgressMonitor pm) throws CoreException {
// $NON-NLS-1$
this.getTextChange("");
// create one multi change to contain all new created changes
CompositeChange multiChange = new CompositeChange(JSPUIMessages.JSP_changes);
// for each element get the changes for it and add it to the multi change
Iterator iter = fElementAndArgumentPairs.values().iterator();
while (iter.hasNext()) {
ElementAndArgumentsPair elemArgsPair = (ElementAndArgumentsPair) iter.next();
Object dest = elemArgsPair.fArgs.getDestination();
if (dest instanceof IPackageFragment) {
Change[] changes = createChangesFor(elemArgsPair.fElement, ((IPackageFragment) dest).getElementName(), pm);
/* add all new text changes to the local list of text changes so that
* future iterations through the while loop will be aware of already
* existing changes
*/
for (int i = 0; i < changes.length; ++i) {
if (changes[i] instanceof TextChange) {
fLocalTextChanges.put(((TextChange) changes[i]).getModifiedElement(), changes[i]);
}
}
if (changes.length > 0) {
multiChange.addAll(changes);
}
}
}
// unless there are actually new changes return null
Change result = null;
if (multiChange.getChildren().length > 0) {
result = multiChange;
}
return result;
}
use of org.eclipse.ltk.core.refactoring.TextChange in project webtools.sourceediting by eclipse.
the class RenameComponentProcessor method addDeclarationUpdate.
final void addDeclarationUpdate(TextChangeManager manager, IFile file) throws CoreException {
String componentName = selectedComponent.getName();
String componentNamespace = selectedComponent.getNamespaceURI();
QualifiedName elementQName = new QualifiedName(componentNamespace, componentName);
QualifiedName typeQName = selectedComponent.getTypeQName();
SearchScope scope = new WorkspaceSearchScope();
if (file != null) {
scope = new SelectionSearchScope(new IResource[] { file });
}
CollectingSearchRequestor requestor = new CollectingSearchRequestor();
SearchPattern pattern = new XMLComponentDeclarationPattern(file, elementQName, typeQName);
SearchEngine searchEngine = new SearchEngine();
HashMap map = new HashMap();
if (singleFileOnly) {
map.put("searchDirtyContent", Boolean.TRUE);
}
searchEngine.search(pattern, requestor, scope, map, new NullProgressMonitor());
List results = requestor.getResults();
// more than one declaration found, so use offset as additional check
Position offsetPosition = null;
if (results.size() > 1) {
IDOMElement selectedElement = selectedComponent.getElement();
if (selectedElement != null) {
int startOffset = selectedElement.getStartOffset();
offsetPosition = new Position(startOffset, (selectedElement.getEndOffset() - startOffset));
}
}
for (Iterator iter = results.iterator(); iter.hasNext(); ) {
SearchMatch match = (SearchMatch) iter.next();
if (match != null) {
boolean addTextChange = true;
// additional check to verify correct declaration is changed
if (offsetPosition != null) {
addTextChange = offsetPosition.overlapsWith(match.getOffset(), match.getLength());
}
if (addTextChange) {
TextChange textChange = manager.get(match.getFile());
String newName = getNewElementName();
newName = quoteString(newName);
ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName);
String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_declatation");
TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
}
}
}
}
use of org.eclipse.ltk.core.refactoring.TextChange in project webtools.sourceediting by eclipse.
the class RenameTargetNamespaceProcessor method addDeclarationUpdate.
private void addDeclarationUpdate(TextChangeManager manager) {
String fileStr = getModel().getSchemaLocation();
URI uri = URI.createPlatformResourceURI(fileStr);
try {
URL url = new URL(uri.toString());
url = Platform.resolve(url);
if (url != null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile file = root.getFileForLocation(new Path(url.getFile()));
if (file != null) {
TextChange change = manager.get(file);
addDeclarationUpdate(change);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Aggregations