Search in sources :

Example 16 with TextChange

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;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IDocument(org.eclipse.jface.text.IDocument)

Example 17 with TextChange

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()]);
}
Also used : JSPSearchSupport(org.eclipse.jst.jsp.core.internal.java.search.JSPSearchSupport) JavaSearchDocumentDelegate(org.eclipse.jst.jsp.core.internal.java.search.JavaSearchDocumentDelegate) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) SearchDocument(org.eclipse.jdt.core.search.SearchDocument)

Example 18 with TextChange

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;
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Iterator(java.util.Iterator) TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange)

Example 19 with TextChange

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);
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.wst.common.core.search.SearchMatch) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) QualifiedName(org.eclipse.wst.common.core.search.pattern.QualifiedName) SelectionSearchScope(org.eclipse.wst.common.core.search.scope.SelectionSearchScope) TextChange(org.eclipse.ltk.core.refactoring.TextChange) WorkspaceSearchScope(org.eclipse.wst.common.core.search.scope.WorkspaceSearchScope) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) SearchEngine(org.eclipse.wst.common.core.search.SearchEngine) SearchScope(org.eclipse.wst.common.core.search.scope.SearchScope) SelectionSearchScope(org.eclipse.wst.common.core.search.scope.SelectionSearchScope) WorkspaceSearchScope(org.eclipse.wst.common.core.search.scope.WorkspaceSearchScope) SearchPattern(org.eclipse.wst.common.core.search.pattern.SearchPattern) Iterator(java.util.Iterator) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) CollectingSearchRequestor(org.eclipse.wst.common.core.search.util.CollectingSearchRequestor) XMLComponentDeclarationPattern(org.eclipse.wst.xml.core.internal.search.XMLComponentDeclarationPattern) IResource(org.eclipse.core.resources.IResource)

Example 20 with TextChange

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();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL)

Aggregations

TextChange (org.eclipse.ltk.core.refactoring.TextChange)44 TextEdit (org.eclipse.text.edits.TextEdit)14 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)13 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IFile (org.eclipse.core.resources.IFile)7 IDocument (org.eclipse.jface.text.IDocument)7 ArrayList (java.util.ArrayList)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 Change (org.eclipse.ltk.core.refactoring.Change)6 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)6 Iterator (java.util.Iterator)5 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)5 HashMap (java.util.HashMap)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)4 IOException (java.io.IOException)3