use of org.python.pydev.refactoring.core.base.SynchronizedTextFileChange in project Pydev by fabioz.
the class ReplaceRefactoring method createFileChange.
private TextChange createFileChange(IFile file, Pattern pattern, Collection<Match> matches, RefactoringStatus resultingStatus, Collection<MatchGroup> matchGroups) throws PatternSyntaxException, CoreException {
PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
TextFileChange change = new SynchronizedTextFileChange(MessageFormat.format(SearchMessages.ReplaceRefactoring_group_label_change_for_file, file.getName()), file);
change.setEdit(new MultiTextEdit());
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
manager.connect(file.getFullPath(), LocationKind.IFILE, null);
try {
ITextFileBuffer textFileBuffer = manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
if (textFileBuffer == null) {
resultingStatus.addError(MessageFormat.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer, file.getName()));
return null;
}
IDocument document = textFileBuffer.getDocument();
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
for (Iterator<Match> iterator = matches.iterator(); iterator.hasNext(); ) {
Match match = iterator.next();
int offset = match.getOffset();
int length = match.getLength();
Position currentPosition = tracker.getCurrentPosition(match);
if (currentPosition != null) {
offset = currentPosition.offset;
if (length != currentPosition.length) {
resultingStatus.addError(MessageFormat.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
}
String originalText = getOriginalText(document, offset, length);
if (originalText == null) {
resultingStatus.addError(MessageFormat.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
String replacementString = computeReplacementString(pattern, originalText, fReplaceString, lineDelimiter);
if (replacementString == null) {
resultingStatus.addError(MessageFormat.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementString);
change.addEdit(replaceEdit);
TextEditChangeGroup textEditChangeGroup = new TextEditChangeGroup(change, new TextEditGroup(SearchMessages.ReplaceRefactoring_group_label_match_replace, replaceEdit));
change.addTextEditChangeGroup(textEditChangeGroup);
matchGroups.add(new MatchGroup(textEditChangeGroup, match));
}
} finally {
manager.disconnect(file.getFullPath(), LocationKind.IFILE, null);
}
return change;
}
use of org.python.pydev.refactoring.core.base.SynchronizedTextFileChange in project Pydev by fabioz.
the class PyRenameEntryPoint method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
// Clear (will be filled now).
allChanges.clear();
fRequest.pushMonitor(pm);
RefactoringStatus status = new RefactoringStatus();
try {
if (this.fRequest.isModuleRenameRefactoringRequest() && this.fRequest.getSimpleResourceRename() && this.fRequest.getIFileResource() != null) {
// Ok, simple resource change
return status;
}
final Map<IPath, Tuple<TextChange, MultiTextEdit>> fileToChangeInfo = new HashMap<IPath, Tuple<TextChange, MultiTextEdit>>();
final Set<IResource> affectedResources = new HashSet<>();
for (RefactoringRequest request : this.fRequest.getRequests()) {
if (request.isModuleRenameRefactoringRequest()) {
boolean searchInit = true;
IModule module = request.getTargetNature().getAstManager().getModule(request.inputName, request.getTargetNature(), !searchInit, // i.e.: the parameter is dontSearchInit (so, pass in negative form to search)
new BaseModuleRequest(request.acceptTypeshed));
if (module != null) {
String partName = module.getName().endsWith(".__init__") ? "package" : "module";
status.addFatalError("Unable to perform module rename because a " + partName + " named: " + request.inputName + " already exists.");
return status;
}
}
List<IRefactorRenameProcess> processes = pyReferenceSearcher.getProcesses(request);
if (processes == null || processes.size() == 0) {
status.addFatalError("Refactoring Process not defined: the refactoring cycle did not complete correctly.");
return status;
}
request.getMonitor().beginTask("Finding references", processes.size());
try {
pyReferenceSearcher.search(request);
} catch (PyReferenceSearcher.SearchException e) {
status.addFatalError(e.getMessage());
return status;
}
TextEditCreation textEditCreation = new TextEditCreation(request.qualifier, request.inputName, request.getModule().getName(), request.getDoc(), processes, status, request.getIFile()) {
@Override
protected Tuple<TextChange, MultiTextEdit> getTextFileChange(IFile workspaceFile, IDocument doc) {
if (workspaceFile == null) {
// used for tests
TextChange docChange = PyDocumentChange.create("Current module: " + moduleName, doc);
MultiTextEdit rootEdit = new MultiTextEdit();
docChange.setEdit(rootEdit);
docChange.setKeepPreviewEdits(true);
allChanges.add(docChange);
return new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
}
IPath fullPath = workspaceFile.getFullPath();
Tuple<TextChange, MultiTextEdit> tuple = fileToChangeInfo.get(fullPath);
if (tuple == null) {
TextFileChange docChange = new SynchronizedTextFileChange("RenameChange: " + inputName, workspaceFile);
docChange.setTextType("py");
MultiTextEdit rootEdit = new MultiTextEdit();
docChange.setEdit(rootEdit);
docChange.setKeepPreviewEdits(true);
allChanges.add(docChange);
affectedResources.add(workspaceFile);
tuple = new Tuple<TextChange, MultiTextEdit>(docChange, rootEdit);
fileToChangeInfo.put(fullPath, tuple);
}
return tuple;
}
@Override
protected PyRenameResourceChange createResourceChange(IResource resourceToRename, String newName, RefactoringRequest request) {
IContainer target = null;
if (request instanceof ModuleRenameRefactoringRequest) {
target = ((ModuleRenameRefactoringRequest) request).getTarget();
}
PyRenameResourceChange change = new PyRenameResourceChange(resourceToRename, initialName, newName, StringUtils.format("Changing %s to %s", initialName, inputName), target);
allChanges.add(change);
affectedResources.add(resourceToRename);
return change;
}
};
textEditCreation.fillRefactoringChangeObject(request, context);
if (status.hasFatalError() || request.getMonitor().isCanceled()) {
return status;
}
}
checkResourcesToBeChanged(affectedResources, context, status);
} catch (OperationCanceledException e) {
// OK
} finally {
fRequest.popMonitor().done();
}
return status;
}
Aggregations