Search in sources :

Example 41 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project eclipse.jdt.ls by eclipse.

the class OrganizeImportsCommand method addWorkspaceEdit.

private void addWorkspaceEdit(ICompilationUnit cu, CUCorrectionProposal proposal, WorkspaceEdit rootEdit) throws CoreException {
    TextChange textChange = proposal.getTextChange();
    TextEdit edit = textChange.getEdit();
    TextEditConverter converter = new TextEditConverter(cu, edit);
    rootEdit.getChanges().put(JDTUtils.toURI(cu), converter.convert());
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextEditConverter(org.eclipse.jdt.ls.core.internal.TextEditConverter)

Example 42 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project bndtools by bndtools.

the class PkgRenameParticipant method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final Map<IFile, TextChange> fileChanges = new HashMap<IFile, TextChange>();
    IResourceProxyVisitor visitor = new IResourceProxyVisitor() {

        @Override
        public boolean visit(IResourceProxy proxy) throws CoreException {
            if ((proxy.getType() == IResource.FOLDER) || (proxy.getType() == IResource.PROJECT)) {
                return true;
            }
            if (!((proxy.getType() == IResource.FILE) && proxy.getName().toLowerCase().endsWith(".bnd"))) {
                return false;
            }
            /* we're dealing with a *.bnd file */
            /* get the proxied file */
            IFile resource = (IFile) proxy.requestResource();
            /* read the file as a single string */
            String bndFileText = null;
            try {
                bndFileText = FileUtils.readFully(resource).get();
            } catch (Exception e) {
                String str = "Could not read file " + proxy.getName();
                logger.logError(str, e);
                throw new OperationCanceledException(str);
            }
            /*
                 * get the previous change for this file if it exists, or otherwise create a new change for it, but do
                 * not store it yet: wait until we know if there are actually changes in the file
                 */
            TextChange fileChange = getTextChange(resource);
            final boolean fileChangeIsNew = (fileChange == null);
            if (fileChange == null) {
                fileChange = new TextFileChange(proxy.getName(), resource);
                fileChange.setEdit(new MultiTextEdit());
            }
            TextEdit rootEdit = fileChange.getEdit();
            BndEditModel model = new BndEditModel();
            Document document = new Document(bndFileText);
            try {
                model.loadFrom(document);
            } catch (IOException e) {
                String str = "Could not load document " + proxy.getName();
                logger.logError(str, e);
                throw new OperationCanceledException(str);
            }
            /* loop over all renames to perform */
            for (Map.Entry<IPackageFragment, RenameArguments> entry : pkgFragments.entrySet()) {
                IPackageFragment pkgFragment = entry.getKey();
                RenameArguments arguments = entry.getValue();
                final String oldName = pkgFragment.getElementName();
                final String newName = arguments.getNewName();
                List<ImportPattern> newImportedPackages = makeNewHeaders(model.getImportPatterns(), oldName, newName);
                if (newImportedPackages != null) {
                    model.setImportPatterns(newImportedPackages);
                }
                List<ExportedPackage> newExportedPackages = makeNewHeaders(model.getExportedPackages(), oldName, newName);
                if (newExportedPackages != null) {
                    model.setExportedPackages(newExportedPackages);
                }
                List<String> newPrivatePackages = makeNewHeaders(model.getPrivatePackages(), oldName, newName);
                if (newPrivatePackages != null) {
                    model.setPrivatePackages(newPrivatePackages);
                }
                Map<String, String> changes = model.getDocumentChanges();
                for (Iterator<Entry<String, String>> iter = changes.entrySet().iterator(); iter.hasNext(); ) {
                    Entry<String, String> change = iter.next();
                    String propertyName = change.getKey();
                    String stringValue = change.getValue();
                    addEdit(document, rootEdit, propertyName, stringValue);
                    iter.remove();
                }
                Pattern pattern = Pattern.compile(/* match start boundary */
                "(^|" + grammarSeparator + ")" + /* match bundle activator */
                "(Bundle-Activator\\s*:\\s*)" + /* match itself / package name */
                "(" + Pattern.quote(oldName) + ")" + /* match class name */
                "(\\.[^\\.]+)" + /* match end boundary */
                "(" + grammarSeparator + "|" + Pattern.quote("\\") + "|$)");
                /* find all matches to replace and add them to the root edit */
                Matcher matcher = pattern.matcher(bndFileText);
                while (matcher.find()) {
                    rootEdit.addChild(new ReplaceEdit(matcher.start(3), matcher.group(3).length(), newName));
                }
            }
            /*
                 * only store the changes when no changes were stored before for this file and when there are actually
                 * changes.
                 */
            if (fileChangeIsNew && rootEdit.hasChildren()) {
                fileChanges.put(resource, fileChange);
            }
            return false;
        }
    };
    /* determine which projects have to be visited */
    Set<IProject> projectsToVisit = new HashSet<IProject>();
    for (IPackageFragment pkgFragment : pkgFragments.keySet()) {
        projectsToVisit.add(pkgFragment.getResource().getProject());
        for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencingProjects()) {
            projectsToVisit.add(projectToVisit);
        }
        for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencedProjects()) {
            projectsToVisit.add(projectToVisit);
        }
    }
    /* visit the projects */
    for (IProject projectToVisit : projectsToVisit) {
        projectToVisit.accept(visitor, IResource.NONE);
    }
    if (fileChanges.isEmpty()) {
        /* no changes at all */
        return null;
    }
    /* build a composite change with all changes */
    CompositeChange cs = new CompositeChange(changeTitle);
    for (TextChange fileChange : fileChanges.values()) {
        cs.add(fileChange);
    }
    return cs;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) HashMap(java.util.HashMap) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) Matcher(java.util.regex.Matcher) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IDocument(aQute.bnd.properties.IDocument) Document(aQute.bnd.properties.Document) Entry(java.util.Map.Entry) IResourceProxy(org.eclipse.core.resources.IResourceProxy) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) BndEditModel(aQute.bnd.build.model.BndEditModel) HashSet(java.util.HashSet) ImportPattern(aQute.bnd.build.model.clauses.ImportPattern) Pattern(java.util.regex.Pattern) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IOException(java.io.IOException) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) ImportPattern(aQute.bnd.build.model.clauses.ImportPattern) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ExportedPackage(aQute.bnd.build.model.clauses.ExportedPackage) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) HashMap(java.util.HashMap) Map(java.util.Map) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 43 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project xtext-eclipse by eclipse.

the class TextChangeCombiner method visitChange.

protected void visitChange(Change sourceChange, Map<Object, TextChange> resource2textChange, List<Change> otherChanges, Set<IEditorPart> editorsToSave) {
    if (sourceChange instanceof DisplayChangeWrapper.Wrapper)
        visitChange(((DisplayChangeWrapper.Wrapper) sourceChange).getDelegate(), resource2textChange, otherChanges, editorsToSave);
    else if (sourceChange instanceof CompositeChange) {
        visitCompositeChange((CompositeChange) sourceChange, resource2textChange, otherChanges, editorsToSave);
    } else if (sourceChange instanceof TextChange) {
        if (sourceChange instanceof EditorDocumentChange)
            editorsToSave.add(((EditorDocumentChange) sourceChange).getEditor());
        Object key = getKey((TextChange) sourceChange);
        if (key != null) {
            TextChange textChange = resource2textChange.get(key);
            if (textChange == null) {
                textChange = createTextChange(key, ((TextChange) sourceChange).getTextType());
                resource2textChange.put(key, textChange);
            }
            MultiTextEdit combinedEdits = (MultiTextEdit) textChange.getEdit();
            TextEdit newEdit = ((TextChange) sourceChange).getEdit().copy();
            if (newEdit instanceof MultiTextEdit) {
                for (TextEdit newTextEdit : ((MultiTextEdit) newEdit).getChildren()) {
                    addIfNotDuplicate(combinedEdits, newTextEdit);
                }
            } else {
                addIfNotDuplicate(combinedEdits, newEdit);
            }
        }
    } else {
        CompositeChange parent = (CompositeChange) sourceChange.getParent();
        if (parent != null)
            parent.remove(sourceChange);
        otherChanges.add(sourceChange);
    }
}
Also used : DisplayChangeWrapper(org.eclipse.xtext.ui.refactoring.impl.DisplayChangeWrapper) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 44 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project xtext-eclipse by eclipse.

the class TextChangeCombiner method combineChanges.

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;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IEditorPart(org.eclipse.ui.IEditorPart) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

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