Search in sources :

Example 11 with TextFileChange

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

the class ExtractToFunctionRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    createFunctionText();
    createFunctionCallText();
    // 
    final TextFileChange tfc = new TextFileChange(selectionFinder.getSelectedFile().getName(), selectionFinder.getSelectedFile());
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    // replace selection with function call & new declarations
    final int offset = selectionFinder.getSelectedStatements().getLocation().getOffset();
    final int len = selectionFinder.getSelectedStatements().getLocation().getEndOffset() - offset;
    rootEdit.addChild(new ReplaceEdit(offset, len, functionCallTextReady));
    // add new function after the one in which the selection is
    if (parentFunc != null && selectionFinder.getInsertLoc() >= 0) {
        rootEdit.addChild(new InsertEdit(selectionFinder.getInsertLoc(), functionTextReady));
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 12 with TextFileChange

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

the class ChangeCreator method createFileChange.

private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null) {
        return null;
    }
    final DefinitionVisitor vis = new DefinitionVisitor();
    module.accept(vis);
    final List<FormalParameter> nodes = vis.getLocations();
    // Calculate edit locations
    final List<Location> locations = new ArrayList<Location>();
    try {
        final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
        job1.join();
    } catch (InterruptedException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
    } catch (CoreException ce) {
        ErrorReporter.logError("LazyficationRefactoring: " + "CoreException while calculating edit locations in " + toVisit.getName() + ".");
        ErrorReporter.logExceptionStackTrace(ce);
    }
    if (locations.isEmpty()) {
        return null;
    }
    // Create a change for each edit location
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    for (Location l : locations) {
        rootEdit.addChild(new InsertEdit(l.getOffset(), "@lazy "));
    }
    return tfc;
}
Also used : FormalParameter(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameter) InsertEdit(org.eclipse.text.edits.InsertEdit) ArrayList(java.util.ArrayList) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

Example 13 with TextFileChange

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

the class ChangeCreator method createFileChange.

/**
 * Creates the {@link #change} object, which contains all the inserted and edited texts
 * in the selected resources.
 */
private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null) {
        return null;
    }
    // 
    // collect functions
    Set<Definition> funcs;
    final FunctionCollector vis = new FunctionCollector();
    if (defSelection == null) {
        module.accept(vis);
        funcs = vis.getResult();
    } else {
        if (defSelection instanceof Def_Function || defSelection instanceof Def_Testcase) {
            // TODO any other possibilities for the type of 'defSelection'?
            funcs = new HashSet<Definition>();
            funcs.add(defSelection);
        } else {
            ErrorReporter.logError("Variable scope reduction called for " + defSelection.getIdentifier().getDisplayName() + ", but it is only supported for functions and testcases. ");
            return null;
        }
    }
    // create edits
    final List<Edit> allEdits = new ArrayList<Edit>();
    for (Definition def : funcs) {
        final List<Edit> edits = analyzeFunction(def);
        if (edits == null) {
            continue;
        }
        allEdits.addAll(edits);
    }
    if (allEdits.isEmpty()) {
        return null;
    }
    final String fileContents = loadFileContent(toVisit);
    // create text edits
    // 
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    // TODO this is an O(n^2) algorithm
    // merge overlapping DeleteEdits
    // used, when removing all parts of a multi-declaration statement:
    // the DeleteEdit for removing the last part covers all the DeleteEdits for the other parts
    // WARNING merging edits might make debugging more difficult, since the overlapping edit errors are avoided
    final List<TextEdit> allTes = new LinkedList<TextEdit>();
    // collect processed (insert) edits with their created insert edit
    final Map<Edit, InsertEdit> editsDone = new HashMap<Edit, InsertEdit>();
    for (Edit e : allEdits) {
        final TextEdit[] tes = createTextEdit(toVisit, fileContents, e, editsDone);
        for (TextEdit te : tes) {
            if (!(te instanceof DeleteEdit)) {
                allTes.add(te);
                // System.err.println("$ nonde added: " + te.getOffset() + "-" + te.getExclusiveEnd());
                continue;
            }
            DeleteEdit dte = (DeleteEdit) te;
            final ListIterator<TextEdit> it = allTes.listIterator();
            while (it.hasNext()) {
                final TextEdit currTe = it.next();
                if (!(currTe instanceof DeleteEdit)) {
                    continue;
                }
                final DeleteEdit currDte = (DeleteEdit) currTe;
                // if the new edit (dte) overlaps currDte, merge them
                if (doesDeleteEditsOverlap(dte, currDte)) {
                    // System.err.println("$ de removed: " + currDte.getOffset() + "-" + currDte.getExclusiveEnd());
                    it.remove();
                    dte = mergeDeleteEdits(dte, currDte);
                // System.err.println("$ merged des: " + dte.getOffset() + "-" + dte.getExclusiveEnd());
                }
            }
            // System.err.println("$ de added: " + dte.getOffset() + "-" + dte.getExclusiveEnd());
            allTes.add(dte);
        }
    }
    Collections.reverse(allTes);
    for (TextEdit te : allTes) {
        rootEdit.addChild(te);
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) Def_Testcase(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Testcase) HashMap(java.util.HashMap) Definition(org.eclipse.titan.designer.AST.TTCN3.definitions.Definition) ArrayList(java.util.ArrayList) Edit(org.eclipse.titanium.refactoring.scope.nodes.Edit) InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) Def_Function(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function) LinkedList(java.util.LinkedList) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 14 with TextFileChange

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

the class TextChangeManager method get.

/**
 * Returns the <code>TextChange</code> associated with the given file.
 * If the manager does not already manage an association it creates a one.
 *
 * @param file the file for which the text buffer change is requested
 * @return the text change associated with the given file.
 */
public TextChange get(IFile file) {
    TextChange result = (TextChange) fMap.get(file);
    if (result == null) {
        result = new TextFileChange(file.toString(), file);
        result.setKeepPreviewEdits(fKeepExecutedTextEdits);
        result.initializeValidationData(new NullProgressMonitor());
        fMap.put(file, result);
    }
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 15 with TextFileChange

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

the class MakeTypeGlobalChange method getChange.

public TextChange getChange(IFile file) {
    TextChange result = (TextChange) 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)

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 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)10 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 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)3 IProject (org.eclipse.core.resources.IProject)3