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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations