Search in sources :

Example 46 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project titan.EclipsePlug-ins by eclipse.

the class MinimizeScopeRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (fileSelection != null) {
        // resource(s) selected
        final CompositeChange cchange = new CompositeChange("MinimizeScopeRefactoring");
        final Iterator<?> it = fileSelection.iterator();
        while (it.hasNext()) {
            final Object o = it.next();
            if (!(o instanceof IResource)) {
                continue;
            }
            final IResource res = (IResource) o;
            final ResourceVisitor vis = new ResourceVisitor();
            res.accept(vis);
            cchange.add(vis.getChange());
        }
        this.affectedObjects = cchange.getAffectedObjects();
        return cchange;
    } else {
        // a single definition selected
        final CompositeChange cchange = new CompositeChange("MinimizeScopeRefactoring");
        final IResource file = defSelection.getLocation().getFile();
        if (!(file instanceof IFile)) {
            ErrorReporter.logError("MinimizeScopeRefactoring.createChange(): File container of defSelection is not an IFile! ");
        }
        final ChangeCreator chCreator = new ChangeCreator((IFile) file, defSelection, settings);
        chCreator.perform();
        final Change ch = chCreator.getChange();
        if (ch != null) {
            cchange.add(ch);
            this.affectedObjects = ch.getAffectedObjects();
        } else {
            this.affectedObjects = new Object[] {};
        }
        return cchange;
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IResource(org.eclipse.core.resources.IResource)

Example 47 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project titan.EclipsePlug-ins by eclipse.

the class UngroupModuleparRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (selection == null) {
        return null;
    }
    final CompositeChange cchange = new CompositeChange("UngroupModuleparRefactoring");
    final Iterator<?> it = selection.iterator();
    while (it.hasNext()) {
        final Object o = it.next();
        if (!(o instanceof IResource)) {
            continue;
        }
        final IResource res = (IResource) o;
        final ResourceVisitor vis = new ResourceVisitor();
        res.accept(vis);
        cchange.add(vis.getChange());
    }
    affectedObjects = cchange.getAffectedObjects();
    return cchange;
}
Also used : CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IResource(org.eclipse.core.resources.IResource)

Example 48 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project titan.EclipsePlug-ins by eclipse.

the class RenameRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException {
    CompositeChange result = new CompositeChange(getName());
    // add the change of all found identifiers grouped by module
    boolean isAsnRename = module.getModuletype() == Module.module_type.ASN_MODULE;
    String newTtcnIdentifierName = newIdentifierName;
    if (isAsnRename) {
        newTtcnIdentifierName = Identifier.getTtcnNameFromAsnName(newIdentifierName);
    }
    List<IFile> filesToProcess = new ArrayList<IFile>(idsMap.size());
    for (Module m : idsMap.keySet()) {
        List<Hit> hitList = idsMap.get(m);
        boolean isTtcnModule = m.getModuletype() == Module.module_type.TTCN3_MODULE;
        IFile file = (IFile) hitList.get(0).identifier.getLocation().getFile();
        TextFileChange tfc = new TextFileChange(file.getName(), file);
        result.add(tfc);
        MultiTextEdit rootEdit = new MultiTextEdit();
        tfc.setEdit(rootEdit);
        for (Hit hit : hitList) {
            int offset = hit.identifier.getLocation().getOffset();
            int length = hit.identifier.getLocation().getEndOffset() - offset;
            String newName = isTtcnModule ? newTtcnIdentifierName : newIdentifierName;
            // reference.
            if (rf.fieldId == null && hit.reference != null && hit.reference.getModuleIdentifier() == null && rf.assignment.getMyScope().getModuleScope() != hit.reference.getMyScope().getModuleScope() && hit.reference.getMyScope().hasAssignmentWithId(CompilationTimeStamp.getBaseTimestamp(), new Identifier(isTtcnModule ? Identifier_type.ID_TTCN : Identifier_type.ID_ASN, newIdentifierName))) {
                newName = rf.assignment.getMyScope().getModuleScope().getName() + "." + newName;
            }
            rootEdit.addChild(new ReplaceEdit(offset, length, newName));
        }
        filesToProcess.add((IFile) m.getLocation().getFile());
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) Identifier(org.eclipse.titan.designer.AST.Identifier) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Module(org.eclipse.titan.designer.AST.Module) ASN1Module(org.eclipse.titan.designer.AST.ASN1.definitions.ASN1Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 49 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project titan.EclipsePlug-ins by eclipse.

the class OrganizeImportsOp method run.

@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    final Set<IProject> projects = new HashSet<IProject>();
    for (final IFile f : files) {
        projects.add(f.getProject());
    }
    monitor.beginTask("Organize imports", files.size() + projects.size() * 20);
    for (final IProject project : projects) {
        final ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
        parser.reportOutdating(files);
        final WorkspaceJob job = parser.analyzeAll();
        monitor.subTask("Waiting for semantic analysis on project " + project.getName());
        try {
            job.join();
        } catch (InterruptedException e) {
            ErrorReporter.logExceptionStackTrace("Error while waiting for the analysis to finish", e);
        }
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }
        monitor.worked(20);
    }
    final CompositeChange compChange = new CompositeChange("Organize imports");
    for (final IFile f : files) {
        monitor.subTask("Organizing " + f.getName());
        try {
            final TextFileChange change = OrganizeImports.organizeImportsChange(f);
            compChange.add(change);
            compChange.perform(new SubProgressMonitor(monitor, 1));
            final MultiTextEdit edit = (MultiTextEdit) change.getEdit();
            if (edit != null && edit.getChildrenSize() > 0) {
                final WorkspaceJob job = GlobalParser.getProjectSourceParser(f.getProject()).reportOutdating(f);
                try {
                    job.join();
                } catch (InterruptedException e) {
                    ErrorReporter.logExceptionStackTrace("Error while waiting for the outdating report to finish", e);
                }
            }
        } catch (CoreException e) {
            ErrorReporter.logExceptionStackTrace("Error while organizing file " + f.getName(), e);
        }
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }
        monitor.worked(1);
    }
    for (final IProject project : projects) {
        GlobalParser.getProjectSourceParser(project).analyzeAll();
    }
    monitor.done();
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) HashSet(java.util.HashSet)

Aggregations

CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)49 Change (org.eclipse.ltk.core.refactoring.Change)22 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)14 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)9 TextChange (org.eclipse.ltk.core.refactoring.TextChange)9 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)7 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)6 CoreException (org.eclipse.core.runtime.CoreException)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 TextEditBasedChange (org.eclipse.ltk.core.refactoring.TextEditBasedChange)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)3 TextEdit (org.eclipse.text.edits.TextEdit)3 IOException (java.io.IOException)2