use of com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher in project Pydev by fabioz.
the class Refactorer method findAllOccurrences.
@Override
public Map<Tuple<String, File>, HashSet<ASTEntry>> findAllOccurrences(RefactoringRequest req) throws OperationCanceledException, CoreException {
PyReferenceSearcher pyReferenceSearcher = new PyReferenceSearcher(req);
// to see if a new request was not created in the meantime (in which case this one will be cancelled)
req.checkCancelled();
IProgressMonitor monitor = req.getMonitor();
Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesInOtherFiles;
try {
monitor.beginTask("Find all occurrences", 100);
monitor.setTaskName("Find all occurrences");
try {
req.pushMonitor(new SubProgressMonitor(monitor, 10));
pyReferenceSearcher.prepareSearch(req);
} catch (PyReferenceSearcher.SearchException | BadLocationException e) {
return null;
} finally {
req.popMonitor().done();
}
req.checkCancelled();
try {
req.pushMonitor(new SubProgressMonitor(monitor, 85));
pyReferenceSearcher.search(req);
} catch (PyReferenceSearcher.SearchException e) {
return null;
} finally {
req.popMonitor().done();
}
req.checkCancelled();
occurrencesInOtherFiles = pyReferenceSearcher.getWorkspaceReferences(req);
HashSet<ASTEntry> occurrences = pyReferenceSearcher.getLocalReferences(req);
occurrencesInOtherFiles.put(new Tuple<String, File>(req.moduleName, req.pyEdit != null ? req.pyEdit.getEditorFile() : req.file), occurrences);
req.getMonitor().worked(5);
} finally {
monitor.done();
}
return occurrencesInOtherFiles;
}
use of com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher in project Pydev by fabioz.
the class MarkOccurrencesJob method createRequest.
/**
* @return a tuple with the refactoring request, the processor and a boolean indicating if all pre-conditions succedded.
* @throws MisconfigurationException
*/
@Override
protected MarkOccurrencesRequest createRequest(BaseEditor baseEditor, IDocumentProvider documentProvider, IProgressMonitor monitor) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
if (!MarkOccurrencesPreferencesPage.useMarkOccurrences()) {
return new PyMarkOccurrencesRequest(false, null, null);
}
PyEdit pyEdit = (PyEdit) baseEditor;
// ok, the editor is still there wit ha document... move on
PyRefactorAction pyRefactorAction = getRefactorAction(pyEdit);
String currToken = this.ps.getCurrToken().o1;
if (LOCAL_TEXT_SEARCHES_ON.contains(currToken) && IDocument.DEFAULT_CONTENT_TYPE.equals(ParsingUtils.getContentType(this.ps.getDoc(), this.ps.getAbsoluteCursorOffset()))) {
return new TextBasedLocalMarkOccurrencesRequest(currToken);
}
final RefactoringRequest req = getRefactoringRequest(pyEdit, pyRefactorAction, PySelection.fromTextSelection(this.ps));
if (req == null || !req.nature.getRelatedInterpreterManager().isConfigured()) {
// we check if it's configured because it may still be a stub...
return new PyMarkOccurrencesRequest(false, null, null);
}
PyReferenceSearcher searcher = new PyReferenceSearcher(req);
// to see if a new request was not created in the meantime (in which case this one will be cancelled)
if (monitor.isCanceled()) {
return new PyMarkOccurrencesRequest(false, null, null);
}
try {
searcher.prepareSearch(req);
if (monitor.isCanceled()) {
return new PyMarkOccurrencesRequest(false, null, null);
}
searcher.search(req);
if (monitor.isCanceled()) {
return new PyMarkOccurrencesRequest(false, null, null);
}
// Ok, search succeeded.
return new PyMarkOccurrencesRequest(true, req, searcher);
} catch (PyReferenceSearcher.SearchException | BadLocationException e) {
// Suppress search failures.
return new PyMarkOccurrencesRequest(false, null, null);
} catch (Throwable e) {
throw new RuntimeException("Error in occurrences while analyzing modName:" + req.moduleName + " initialName:" + req.qualifier + " line (start at 0):" + req.ps.getCursorLine(), e);
}
}
use of com.python.pydev.analysis.refactoring.wizards.rename.PyReferenceSearcher in project Pydev by fabioz.
the class FindActualDefinitionTest method testOccurrencesForDefinition.
public void testOccurrencesForDefinition() throws Exception {
int usedGrammar = GRAMMAR_TO_USE_FOR_PARSING;
GRAMMAR_TO_USE_FOR_PARSING = PythonNature.LATEST_GRAMMAR_PY3_VERSION;
try {
File file = new File(TestDependent.TEST_PYSRC_TESTING_LOC + "occurrencesForDefinition/foo.py");
PySelection selection = new PySelection(new Document(FileUtils.getFileContents(file)), 0, 19, 4);
Refactorer.setPyRefactoring(new Refactorer());
RefactoringRequest req = new RefactoringRequest(file, selection, nature);
req.fillActivationTokenAndQualifier();
req.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
req.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, true);
PyReferenceSearcher pyReferenceSearcher = new PyReferenceSearcher(req);
pyReferenceSearcher.prepareSearch(req);
pyReferenceSearcher.search(req);
HashSet<ASTEntry> ref = pyReferenceSearcher.getLocalReferences(req);
assertEquals(4, ref.size());
} finally {
GRAMMAR_TO_USE_FOR_PARSING = usedGrammar;
}
}
Aggregations