Search in sources :

Example 1 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project che by eclipse.

the class RenameTypeProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
        String project = null;
        IJavaProject javaProject = fType.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
        try {
            if (!Flags.isPrivate(fType.getFlags()))
                flags |= RefactoringDescriptor.MULTI_CHANGE;
            if (fType.isAnonymous() || fType.isLocal())
                flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
        final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
        final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
        descriptor.setProject(project);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setJavaElement(fType);
        descriptor.setNewName(getNewElementName());
        descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
        descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
        descriptor.setUpdateReferences(fUpdateReferences);
        if (//$NON-NLS-1$
        fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns))
            descriptor.setFileNamePatterns(fFilePatterns);
        descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
        descriptor.setMatchStrategy(fRenamingStrategy);
        final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
        if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
            TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
            if (textChange instanceof TextFileChange) {
                ((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
            }
        }
        result.addAll(fChangeManager.getAllChanges());
        if (willRenameCU()) {
            IResource resource = fType.getCompilationUnit().getResource();
            if (resource != null && resource.isLinked()) {
                String ext = resource.getFileExtension();
                String renamedResourceName;
                if (ext == null)
                    renamedResourceName = getNewElementName();
                else
                    renamedResourceName = getNewElementName() + '.' + ext;
                result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
            } else {
                String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
                result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
            }
        }
        monitor.worked(1);
        return result;
    } finally {
        fChangeManager = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IResource(org.eclipse.core.resources.IResource) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 2 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project che by eclipse.

the class DeleteChangeCreator method addTextEditFromRewrite.

private static TextChange addTextEditFromRewrite(TextChangeManager manager, ICompilationUnit cu, ASTRewrite rewrite) throws CoreException {
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(cu);
        TextEdit resultingEdits = rewrite.rewriteAST(buffer.getDocument(), cu.getJavaProject().getOptions(true));
        TextChange textChange = manager.get(cu);
        if (textChange instanceof TextFileChange) {
            TextFileChange tfc = (TextFileChange) textChange;
            tfc.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
        }
        String message = RefactoringCoreMessages.DeleteChangeCreator_1;
        TextChangeCompatibility.addTextEdit(textChange, message, resultingEdits);
        return textChange;
    } finally {
        RefactoringFileBuffers.release(cu);
    }
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 3 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project che by eclipse.

the class DeletePackageFragmentRootChange method doDelete.

@Override
protected Change doDelete(IProgressMonitor pm) throws CoreException {
    if (!confirmDeleteIfReferenced())
        return new NullChange();
    int resourceUpdateFlags = IResource.KEEP_HISTORY;
    int jCoreUpdateFlags = IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
    //$NON-NLS-1$
    pm.beginTask("", 2);
    IPackageFragmentRoot root = getRoot();
    IResource rootResource = root.getResource();
    CompositeChange result = new CompositeChange(getName());
    ResourceDescription rootDescription = ResourceDescription.fromResource(rootResource);
    IJavaProject[] referencingProjects = JavaElementUtil.getReferencingProjects(root);
    HashMap<IFile, String> classpathFilesContents = new HashMap<IFile, String>();
    for (int i = 0; i < referencingProjects.length; i++) {
        IJavaProject javaProject = referencingProjects[i];
        //$NON-NLS-1$
        IFile classpathFile = javaProject.getProject().getFile(".classpath");
        if (classpathFile.exists()) {
            classpathFilesContents.put(classpathFile, getFileContents(classpathFile));
        }
    }
    root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1));
    rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1));
    for (Iterator<Entry<IFile, String>> iterator = classpathFilesContents.entrySet().iterator(); iterator.hasNext(); ) {
        Entry<IFile, String> entry = iterator.next();
        IFile file = entry.getKey();
        String contents = entry.getValue();
        //Restore time stamps? This should probably be some sort of UndoTextFileChange.
        TextFileChange classpathUndo = new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file);
        classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents));
        result.add(classpathUndo);
    }
    result.add(new UndoDeleteResourceChange(rootDescription));
    pm.done();
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Entry(java.util.Map.Entry) IJavaProject(org.eclipse.jdt.core.IJavaProject) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ResourceDescription(org.eclipse.ui.ide.undo.ResourceDescription) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResource(org.eclipse.core.resources.IResource)

Example 4 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project che by eclipse.

the class QualifiedNameSearchResult method getChange.

public TextChange getChange(IFile file) {
    TextChange result = fChanges.get(file);
    if (result == null) {
        result = new TextFileChange(file.getName(), file);
        fChanges.put(file, result);
    }
    return result;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 5 with TextFileChange

use of org.eclipse.ltk.core.refactoring.TextFileChange in project che by eclipse.

the class ProcessorBasedRefactoring method addToTextChangeMap.

private void addToTextChangeMap(Change change) {
    if (change instanceof TextChange) {
        Object element = ((TextChange) change).getModifiedElement();
        if (element != null) {
            fTextChangeMap.put(element, change);
        }
        // under the file resource into the hash table if possible.
        if (change instanceof TextFileChange && !change.getClass().equals(TextFileChange.class)) {
            IFile file = ((TextFileChange) change).getFile();
            fTextChangeMap.put(file, change);
        }
    } else if (change instanceof CompositeChange) {
        Change[] children = ((CompositeChange) change).getChildren();
        for (int i = 0; i < children.length; i++) {
            addToTextChangeMap(children[i]);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange)

Aggregations

TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)37 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)18 TextChange (org.eclipse.ltk.core.refactoring.TextChange)12 IFile (org.eclipse.core.resources.IFile)11 CoreException (org.eclipse.core.runtime.CoreException)11 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)10 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)9 TextEdit (org.eclipse.text.edits.TextEdit)9 InsertEdit (org.eclipse.text.edits.InsertEdit)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)6 ArrayList (java.util.ArrayList)5 IDocument (org.eclipse.jface.text.IDocument)5 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)4 IPath (org.eclipse.core.runtime.IPath)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)3