use of org.python.pydev.ast.refactoring.MultiModuleMoveRefactoringRequest 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.MultiModuleMoveRefactoringRequest in project Pydev by fabioz.
the class PyRenameRefactoringWizard method createInputPage.
protected TextInputWizardPage createInputPage(String message, final String initialSetting) {
return new TextInputWizardPage(message, true, initialSetting) {
private Text textField;
private IFile targetFile;
@Override
protected RefactoringStatus validateTextField(String text) {
RefactoringStatus status = new RefactoringStatus();
boolean acceptPoint = fRequest.isModuleRenameRefactoringRequest();
if (PyStringUtils.isValidIdentifier(text, acceptPoint)) {
fRequest.setInputName(text);
} else {
status.addFatalError("The name: " + text + " is not a valid identifier.");
}
return status;
}
@Override
protected Text createTextInputField(Composite parent, int style) {
Text ret = super.createTextInputField(parent, style);
this.textField = ret;
setTextToFullName();
return ret;
}
private void setTextToResourceName() {
if (targetFile != null) {
String curr = targetFile.getName();
textField.setText(curr);
int i = curr.lastIndexOf('.');
if (i >= 0) {
textField.setSelection(0, i);
} else {
textField.selectAll();
}
}
}
private void setTextToFullName() {
textField.setText(initialSetting);
String text = initialSetting;
int i = text.lastIndexOf('.');
if (i >= 0) {
textField.setSelection(i + 1, text.length());
} else {
textField.selectAll();
}
}
@Override
protected void textModified(String text) {
if (targetFile != null && fRequest.getSimpleResourceRename()) {
if (!isEmptyInputValid() && text.equals("")) {
// $NON-NLS-1$
setPageComplete(false);
setErrorMessage(null);
restoreMessage();
return;
}
if ((!isInitialInputValid()) && text.equals(targetFile.getName())) {
setPageComplete(false);
setErrorMessage(null);
restoreMessage();
return;
}
setPageComplete(validateTextField(text));
}
if (fRequest instanceof MultiModuleMoveRefactoringRequest) {
RefactoringStatus status;
if (text.length() == 0) {
// Accept empty for move!
status = new RefactoringStatus();
status.addInfo("Empty text: move to source folder");
} else {
status = validateTextField(text);
}
if (!status.hasFatalError()) {
fRequest.setInputName(text);
}
setPageComplete(status);
} else {
super.textModified(text);
}
}
@Override
public void createControl(Composite parent) {
Composite superComposite = new Composite(parent, SWT.NONE);
setControl(superComposite);
initializeDialogUnits(superComposite);
superComposite.setLayout(new GridLayout());
Composite composite = new Composite(superComposite, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
GridLayout layout = new GridLayout();
layout.numColumns = 4;
layout.verticalSpacing = 8;
composite.setLayout(layout);
Label label = new Label(composite, SWT.NONE);
label.setText("New &value:");
final Text text = createTextInputField(composite);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint = convertWidthInCharsToPixels(25);
gd.horizontalSpan = 3;
text.setLayoutData(gd);
//
if (fRequest.isModuleRenameRefactoringRequest()) {
Button updateReferencesButton = addOptionalUpdateReferencesCheckbox(composite);
IFile targetFile = fRequest.getIFileResource();
if (targetFile != null) {
this.targetFile = targetFile;
addResourceRenameCheckbox(composite, updateReferencesButton);
}
}
// spacer
new Label(composite, SWT.NONE);
Button bt = new Button(composite, SWT.PUSH);
bt.setText("as_&lower_underscore");
bt.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText(StringUtils.asStyleLowercaseUnderscores(text.getText()));
}
});
Button bt2 = new Button(composite, SWT.PUSH);
bt2.setText("CamelCaseFirst&Upper");
bt2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText(StringUtils.asStyleCamelCaseFirstUpper(text.getText()));
}
});
Button bt3 = new Button(composite, SWT.PUSH);
bt3.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
text.setText(StringUtils.asStyleCamelCaseFirstLower(text.getText()));
}
});
bt3.setText("&camelCaseFirstLower");
// addOptionalUpdateTextualMatches(composite, layouter);
// addOptionalUpdateQualifiedNameComponent(composite, layouter, layout.marginWidth);
Dialog.applyDialogFont(superComposite);
}
protected Button addResourceRenameCheckbox(Composite result, final Button updateReferencesButton) {
final Button resourceRename = new Button(result, SWT.CHECK);
resourceRename.setText("&Simple Resource Rename / Change Extension?");
IPreferenceStore preferences = PyDevUiPrefs.getPreferenceStore();
// Default is always false to rename resources.
preferences.setDefault(SIMPLE_RESOURCE_RENAME, false);
boolean simpleResourceRenameBool = preferences.getBoolean(SIMPLE_RESOURCE_RENAME);
resourceRename.setSelection(simpleResourceRenameBool);
fRequest.setSimpleResourceRename(simpleResourceRenameBool);
resourceRename.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IPreferenceStore preferences = PyDevUiPrefs.getPreferenceStore();
boolean simpleResourceRenameBool = resourceRename.getSelection();
updateReferencesButton.setVisible(!simpleResourceRenameBool);
preferences.setValue(SIMPLE_RESOURCE_RENAME, simpleResourceRenameBool);
fRequest.setSimpleResourceRename(simpleResourceRenameBool);
// Must be the last thing.
if (simpleResourceRenameBool) {
setTextToResourceName();
} else {
setTextToFullName();
}
}
});
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
resourceRename.setLayoutData(gridData);
updateReferencesButton.setVisible(!simpleResourceRenameBool);
if (simpleResourceRenameBool) {
setTextToResourceName();
}
return resourceRename;
}
protected Button addOptionalUpdateReferencesCheckbox(Composite result) {
final Button updateReferences = new Button(result, SWT.CHECK);
updateReferences.setText("&Update References?");
IPreferenceStore preferences = PyDevUiPrefs.getPreferenceStore();
// Default is always true to update references.
preferences.setDefault(UPDATE_REFERENCES, true);
boolean updateRefs = preferences.getBoolean(UPDATE_REFERENCES);
updateReferences.setSelection(updateRefs);
fRequest.setUpdateReferences(updateRefs);
updateReferences.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IPreferenceStore preferences = PyDevUiPrefs.getPreferenceStore();
boolean updateRefs = updateReferences.getSelection();
preferences.setValue(UPDATE_REFERENCES, updateRefs);
fRequest.setUpdateReferences(updateRefs);
}
});
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
updateReferences.setLayoutData(gridData);
return updateReferences;
}
};
}
use of org.python.pydev.ast.refactoring.MultiModuleMoveRefactoringRequest in project Pydev by fabioz.
the class PyResourceDropAdapterAssistant method performResourceMove.
/**
* Performs a resource move
*/
private IStatus performResourceMove(CommonDropAdapter dropAdapter, IResource[] sources) {
MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1, WorkbenchNavigatorMessages.DropAdapter_problemsMoving, null);
mergeStatus(problems, validateTarget(getCurrentTarget(dropAdapter), dropAdapter.getCurrentTransfer(), dropAdapter.getCurrentOperation()));
IContainer target = getActualTarget((IResource) getCurrentTarget(dropAdapter));
ReadOnlyStateChecker checker = new ReadOnlyStateChecker(getShell(), WorkbenchNavigatorMessages.MoveResourceAction_title, WorkbenchNavigatorMessages.MoveResourceAction_checkMoveMessage);
sources = checker.checkReadOnlyResources(sources);
boolean targetInSourceFolder = false;
PythonNature nature;
try {
nature = PythonNature.getPythonNature(target);
Set<String> projectSourcePathSet = nature.getPythonPathNature().getProjectSourcePathSet(true);
for (String string : projectSourcePathSet) {
if (FileUtils.isPrefixOf(new Path(string), target.getFullPath())) {
targetInSourceFolder = true;
break;
}
}
} catch (CoreException e1) {
Log.log(e1);
}
if (targetInSourceFolder) {
try {
int resolved = 0;
List<ModuleRenameRefactoringRequest> requests = new ArrayList<>();
for (IResource s : sources) {
if (!PythonPathHelper.isValidSourceFile(s.getName())) {
// For now this is a limitation: compiled modules cannot be moved updating references :(
continue;
}
nature = PythonNature.getPythonNature(s);
try {
String resolveModule = nature.resolveModule(s);
if (resolveModule != null) {
File file = s.getLocation().toFile();
boolean isDir = file.isDirectory();
File initFile = null;
if (isDir) {
initFile = PythonPathHelper.getFolderInit(file);
}
if (isDir && initFile != null) {
// If it's a directory, and we have an __init__.py, use the __init__.py instead.
file = initFile;
}
resolved += 1;
requests.add(new ModuleRenameRefactoringRequest(file, nature, target));
}
} catch (MisconfigurationException e) {
Log.log(e);
}
}
if (resolved != 0) {
if (resolved != sources.length) {
problems.add(SharedCorePlugin.makeStatus(IStatus.ERROR, "Unable to do refactor action because some of the resources moved are in the PYTHONPATH and some are not.", null));
return problems;
} else {
// Make a refactoring operation
PyRenameRefactoring.rename(new MultiModuleMoveRefactoringRequest(requests, target));
return problems;
}
}
} catch (TargetNotInPythonpathException e) {
// Keep on going through the regular path.
} catch (Exception e) {
Log.log(e);
// Ok, log it but do the regular operation.
}
}
MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(getShell());
IResource[] copiedResources = operation.copyResources(sources, target);
if (copiedResources.length > 0) {
PythonPathHelper.updatePyPath(copiedResources, target, PythonPathHelper.OPERATION_MOVE);
}
return problems;
}
Aggregations