use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.
the class RefactoringLocalTestBase method applyRenameRefactoring.
/**
* Applies a rename refactoring
*/
protected void applyRenameRefactoring(RefactoringRequest request, boolean expectError) throws CoreException {
PyRenameEntryPoint processor = new PyRenameEntryPoint(new PyRefactoringRequest(request));
NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
Change change = processor.createChange(nullProgressMonitor);
if (!expectError) {
// otherwise, if there is an error, the change may be null
change.perform(nullProgressMonitor);
}
}
use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.
the class PyRenameRefactoring method rename.
public static String rename(IPyRefactoringRequest request) {
try {
List<RefactoringRequest> actualRequests = request.getRequests();
if (actualRequests.size() == 1) {
RefactoringRequest req = actualRequests.get(0);
// Note: if it's already a ModuleRenameRefactoringRequest, no need to change anything.
if (!(req.isModuleRenameRefactoringRequest())) {
// Note: if we're renaming an import, we must change to the appropriate req
IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
ItemPointer[] pointers = pyRefactoring.findDefinition(req);
for (ItemPointer pointer : pointers) {
Definition definition = pointer.definition;
if (RefactorProcessFactory.isModuleRename(definition)) {
try {
request = new PyRefactoringRequest(new ModuleRenameRefactoringRequest(definition.module.getFile(), req.nature, null));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
PyRenameEntryPoint entryPoint = new PyRenameEntryPoint(request);
RenameRefactoring renameRefactoring = new RenameRefactoring(entryPoint);
request.fillActivationTokenAndQualifier();
String title = "Rename";
if (request instanceof MultiModuleMoveRefactoringRequest) {
MultiModuleMoveRefactoringRequest multiModuleMoveRefactoringRequest = (MultiModuleMoveRefactoringRequest) request;
title = "Move To package (project: " + multiModuleMoveRefactoringRequest.getTarget().getProject().getName() + ")";
}
final PyRenameRefactoringWizard wizard = new PyRenameRefactoringWizard(renameRefactoring, title, "inputPageDescription", request);
try {
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
op.run(EditorUtils.getShell(), "Rename Refactor Action");
} catch (InterruptedException e) {
// do nothing. User action got cancelled
}
} catch (Exception e) {
Log.log(e);
}
return null;
}
use of org.python.pydev.ast.refactoring.PyRefactoringRequest in project Pydev by fabioz.
the class PyRenameResourceAction method run.
/*
* (non-Javadoc) Method declared on IAction.
*/
@Override
public void run() {
if (!isEnabled()) {
// will also update the list of resources (main change from the DeleteResourceAction)
return;
}
IEditorPart[] dirtyEditors = Helpers.checkValidateState();
List<IResource> resources = getSelectedResources();
if (resources.size() != 1) {
DialogHelpers.openWarning("Can only rename one element.", "One element must be selected for rename.");
return;
}
IResource r = resources.get(0);
if (r instanceof IFile) {
for (IEditorPart iEditorPart : dirtyEditors) {
IEditorInput editorInput = iEditorPart.getEditorInput();
Object input = editorInput.getAdapter(IResource.class);
if (r.equals(input)) {
iEditorPart.doSave(null);
}
}
} else if (r instanceof IFolder) {
try {
renamedFolder = (IFolder) r;
preResources = new ArrayList<IResource>();
IResource[] members = renamedFolder.getParent().members();
for (IResource m : members) {
preResources.add(m);
}
} catch (CoreException e) {
Log.log(IStatus.ERROR, "Unexpected error reading parent properties", e);
renamedFolder = null;
preResources = null;
}
} else {
renamedFolder = null;
preResources = null;
}
IProject project = r.getProject();
PythonNature n = PythonNature.getPythonNature(project);
if (n != null) {
if (r instanceof IFile && !PythonPathHelper.isValidSourceFile((IFile) r)) {
// If it is a file which does not end with .py, don't try to do a regular refactoring.
} else {
try {
String resolveModule = n.resolveModule(r);
if (resolveModule != null && // -- the folder has to be selected to do a package rename
!resolveModule.endsWith(".__init__")) {
IFile file = null;
boolean foundAsInit = false;
if (r instanceof IContainer) {
file = PythonPathHelper.getFolderInit((IContainer) r);
foundAsInit = true;
} else if (r instanceof IFile) {
file = (IFile) r;
}
if (file != null && file.exists()) {
// It's a directory without an __init__.py file, just keep going...
RefactoringRequest request = new ModuleRenameRefactoringRequest(file.getLocation().toFile(), n, null);
if (!foundAsInit) {
// If we have found it as an __init__ when renaming a module, we won't
// set the related IFile (because we don't want to provide a 'simple rename'
// in this case -- as if he did actually select the __init__, only the simple
// rename would be provided in the first place).
request.setFileResource(file);
}
PyRenameRefactoring.rename(new PyRefactoringRequest(request));
// route and do a refactoring request to rename it)!
return;
}
}
} catch (Exception e) {
Log.log(e);
}
}
}
super.run();
updatePyPath();
renamedFolder = null;
preResources = null;
}
Aggregations