use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint 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 com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.
the class PyRenameInFileAction method fillWithOccurrences.
/**
* Puts the found positions referente to the occurrences in the group
*
* @param document the document that will contain this positions
* @param group the group that will contain this positions
* @param ps the selection used
* @return
*
* @throws BadLocationException
* @throws OperationCanceledException
* @throws CoreException
* @throws MisconfigurationException
*/
private boolean fillWithOccurrences(IDocument document, LinkedPositionGroup group, IProgressMonitor monitor, PySelection ps) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
RefactoringRequest req = MarkOccurrencesJob.getRefactoringRequest(pyEdit, MarkOccurrencesJob.getRefactorAction(pyEdit), ps);
if (monitor.isCanceled()) {
return false;
}
PyRenameEntryPoint processor = new PyRenameEntryPoint(req);
// process it to get what we need
processor.checkInitialConditions(monitor);
processor.checkFinalConditions(monitor, null);
HashSet<ASTEntry> occurrences = processor.getOccurrences();
if (monitor.isCanceled()) {
return false;
}
// used so that we don't add duplicates
Set<Tuple<Integer, Integer>> found = new HashSet<Tuple<Integer, Integer>>();
List<ProposalPosition> groupPositions = new ArrayList<ProposalPosition>();
if (occurrences != null) {
// first, just sort by position (line, col)
ArrayList<ASTEntry> sortedOccurrences = new ArrayList<ASTEntry>(occurrences);
Collections.sort(sortedOccurrences, new Comparator<ASTEntry>() {
@Override
public int compare(ASTEntry o1, ASTEntry o2) {
int thisVal = o1.node.beginLine;
int anotherVal = o2.node.beginLine;
int ret;
if (thisVal == anotherVal) {
// if it's in the same line, let's sort by column
thisVal = o1.node.beginColumn;
anotherVal = o2.node.beginColumn;
ret = (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
} else {
ret = (thisVal < anotherVal ? -1 : 1);
}
return ret;
}
});
// now, gather positions to add to the group
int i = 0;
int firstPosition = -1;
int absoluteCursorOffset = ps.getAbsoluteCursorOffset();
for (ASTEntry entry : sortedOccurrences) {
try {
IRegion lineInformation = document.getLineInformation(entry.node.beginLine - 1);
int colDef = NodeUtils.getClassOrFuncColDefinition(entry.node) - 1;
int offset = lineInformation.getOffset() + colDef;
int len = req.qualifier.length();
Tuple<Integer, Integer> foundAt = new Tuple<Integer, Integer>(offset, len);
if (!found.contains(foundAt)) {
i++;
ProposalPosition proposalPosition = new ProposalPosition(document, offset, len, i, new ICompletionProposal[0]);
found.add(foundAt);
groupPositions.add(proposalPosition);
if (offset <= absoluteCursorOffset && absoluteCursorOffset < offset + len) {
firstPosition = i;
}
}
} catch (Exception e) {
Log.log(e);
return false;
}
}
if (firstPosition != -1) {
ArrayList<ProposalPosition> newGroupPositions = new ArrayList<ProposalPosition>();
// add from current to end
for (i = firstPosition - 1; i < groupPositions.size(); i++) {
newGroupPositions.add(groupPositions.get(i));
}
// and now from the start up to the current
for (i = 0; i < firstPosition - 1; i++) {
newGroupPositions.add(groupPositions.get(i));
}
groupPositions = newGroupPositions;
}
for (ProposalPosition proposalPosition : groupPositions) {
group.addPosition(proposalPosition);
}
}
return groupPositions.size() > 0;
}
use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint 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 com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.
the class RefactoringRenameTestBase method getReferencesForRename.
/**
* Goes through all the workspace (in this case the refactoring project) and gathers the references
* for the current selection.
*
* @param moduleName the name of the module we're currently in
* @param line the line we're in
* @param col the col we're in
* @param expectError whether we are expecting some error or not
* @return a map with the name of the module and the file representing it pointing to the
* references found in that module.
*/
protected Map<Tuple<String, File>, HashSet<ASTEntry>> getReferencesForRename(String moduleName, int line, int col, boolean expectError) {
Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesToReturn = null;
try {
IProjectModulesManager modulesManager = (IProjectModulesManager) natureRefactoring.getAstManager().getModulesManager();
IModule module = modulesManager.getModuleInDirectManager(moduleName, natureRefactoring, true, new BaseModuleRequest(true));
if (module == null) {
throw new RuntimeException("Unable to get source module for module:" + moduleName);
}
String strDoc = FileUtils.getFileContents(module.getFile());
Document doc = new Document(strDoc);
PySelection ps = new PySelection(doc, line, col);
RefactoringRequest request = new RefactoringRequest(null, ps, natureRefactoring);
request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, false);
request.moduleName = moduleName;
request.inputName = "new_name";
request.fillActivationTokenAndQualifier();
PyRenameEntryPoint processor = new PyRenameEntryPoint(request);
NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
lastProcessorUsed = processor;
checkProcessors();
checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
occurrencesToReturn = processor.getOccurrencesInOtherFiles();
occurrencesToReturn.put(new Tuple<String, File>(moduleName, module.getFile()), processor.getOccurrences());
} catch (Exception e) {
throw new RuntimeException(e);
}
return occurrencesToReturn;
}
use of com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint in project Pydev by fabioz.
the class RenameModuleRefactoringTest method getReferencesForModuleRename.
protected Map<Tuple<String, File>, HashSet<ASTEntry>> getReferencesForModuleRename(String moduleName, String newName, boolean expectError) {
Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesToReturn = null;
try {
IProjectModulesManager modulesManager = (IProjectModulesManager) natureRefactoring.getAstManager().getModulesManager();
BaseModuleRequest moduleRequest = new BaseModuleRequest(true);
IModule module = modulesManager.getModuleInDirectManager(moduleName, natureRefactoring, true, moduleRequest);
if (module == null) {
if (!moduleName.endsWith("__init__")) {
module = modulesManager.getModuleInDirectManager(moduleName + ".__init__", natureRefactoring, true, moduleRequest);
}
if (module == null) {
throw new RuntimeException("Unable to get source module for module:" + moduleName);
}
}
ModuleRenameRefactoringRequest request = new ModuleRenameRefactoringRequest(module.getFile(), natureRefactoring, null);
request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, false);
request.moduleName = moduleName;
request.fillActivationTokenAndQualifier();
request.inputName = newName;
PyRenameEntryPoint processor = new PyRenameEntryPoint(request);
NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
lastProcessorUsed = processor;
checkProcessors();
checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
occurrencesToReturn = processor.getOccurrencesInOtherFiles();
occurrencesToReturn.put(new Tuple<String, File>(CURRENT_MODULE_IN_REFERENCES, null), processor.getOccurrences());
} catch (Exception e) {
throw new RuntimeException(e);
}
return occurrencesToReturn;
}
Aggregations