Search in sources :

Example 1 with SynchronizedTextFileChange

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;
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Position(org.eclipse.jface.text.Position) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) ICustomMatch(org.python.pydev.shared_ui.search.ICustomMatch) Match(org.eclipse.search.ui.text.Match) TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) PositionTracker(org.eclipse.search2.internal.ui.text.PositionTracker) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with SynchronizedTextFileChange

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;
}
Also used : IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) PyRenameResourceChange(com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange) HashMap(java.util.HashMap) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) PyRefactoringRequest(org.python.pydev.ast.refactoring.PyRefactoringRequest) IPyRefactoringRequest(org.python.pydev.ast.refactoring.IPyRefactoringRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ModuleRenameRefactoringRequest(org.python.pydev.ast.refactoring.ModuleRenameRefactoringRequest) IRefactorRenameProcess(com.python.pydev.analysis.refactoring.wizards.IRefactorRenameProcess) IContainer(org.eclipse.core.resources.IContainer) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) SynchronizedTextFileChange(org.python.pydev.refactoring.core.base.SynchronizedTextFileChange) Tuple(org.python.pydev.shared_core.structure.Tuple) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)2 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)2 SynchronizedTextFileChange (org.python.pydev.refactoring.core.base.SynchronizedTextFileChange)2 PyRenameResourceChange (com.python.pydev.analysis.refactoring.changes.PyRenameResourceChange)1 IRefactorRenameProcess (com.python.pydev.analysis.refactoring.wizards.IRefactorRenameProcess)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 Position (org.eclipse.jface.text.Position)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 TextChange (org.eclipse.ltk.core.refactoring.TextChange)1 TextEditChangeGroup (org.eclipse.ltk.core.refactoring.TextEditChangeGroup)1 Match (org.eclipse.search.ui.text.Match)1