use of org.python.pydev.core.IProjectModulesManager in project Pydev by fabioz.
the class ProjectModulesManager method resolveModule.
/**
* @see org.python.pydev.core.IProjectModulesManager#resolveModule(java.lang.String, boolean)
*/
@Override
public String resolveModule(String full, boolean checkSystemManager) {
IModulesManager[] managersInvolved = this.getManagersInvolved(checkSystemManager);
for (IModulesManager m : managersInvolved) {
String mod;
if (m instanceof IProjectModulesManager) {
IProjectModulesManager pM = (IProjectModulesManager) m;
mod = pM.resolveModuleInDirectManager(full);
} else {
mod = m.resolveModule(full);
}
if (mod != null) {
return mod;
}
}
return null;
}
use of org.python.pydev.core.IProjectModulesManager in project Pydev by fabioz.
the class AbstractRenameWorkspaceRefactorProcess method doCheckInitialOnWorkspace.
/**
* This method is made to be used in the checkInitialOnWorkspace implementation.
*
* It will find files with possible references in the workspace (from the token
* name we're searching) and for each file that maps to a module it will
* call getOccurrencesInOtherModule, and will add those occurrences to
* the map with the file pointing to the entries.
*
* @param status used to add some error status to the refactoring
* @param request the request used for the refactoring
*/
protected void doCheckInitialOnWorkspace(RefactoringStatus status, RefactoringRequest request) {
try {
request.getMonitor().beginTask("Check references on workspace", 100);
List<Tuple<List<ModulesKey>, IPythonNature>> references;
try {
request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 90));
references = findFilesWithPossibleReferences(request);
if (request.getMonitor().isCanceled()) {
return;
}
} finally {
request.popMonitor().done();
}
int total = references.size();
try {
request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 10));
request.getMonitor().beginTask("Analyzing references found", total);
int i = 0;
IModuleRequestState moduleRequest = new BaseModuleRequest(request.acceptTypeshed);
for (Tuple<List<ModulesKey>, IPythonNature> file : references) {
i++;
request.communicateWork(StringUtils.format("Analyzing %s (%s of %s)", file.o2.getProject(), i, total));
PythonNature nature = (PythonNature) file.o2;
if (nature != null) {
if (!nature.startRequests()) {
continue;
}
try {
for (ModulesKey key : file.o1) {
IProjectModulesManager modulesManager = (IProjectModulesManager) nature.getAstManager().getModulesManager();
request.checkCancelled();
String modName = key.name;
if (modName != null) {
if (!request.moduleName.equals(modName)) {
// we've already checked the module from the request...
request.checkCancelled();
IModule module = modulesManager.getModuleInDirectManager(modName, nature, false, moduleRequest);
if (module instanceof SourceModule) {
SourceModule sourceModule = (SourceModule) module;
if (sourceModule.getAst() == null) {
status.addWarning("Unable to get AST for: " + modName);
continue;
}
request.checkCancelled();
List<ASTEntry> entryOccurrences = getOccurrencesInOtherModule(status, request, request.qualifier, (SourceModule) module, nature);
if (entryOccurrences.size() > 0) {
addOccurrences(entryOccurrences, key.file, modName);
}
}
}
}
}
} finally {
nature.endRequests();
}
}
}
} finally {
request.popMonitor().done();
}
} catch (OperationCanceledException e) {
// that's ok
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
request.getMonitor().done();
}
}
use of org.python.pydev.core.IProjectModulesManager in project Pydev by fabioz.
the class ProjectModulesManager method getModuleAndRelatedModulesManager.
/**
* @return a tuple with the IModule requested and the IModulesManager that contained that module.
*/
@Override
public Tuple<IModule, IModulesManager> getModuleAndRelatedModulesManager(String name, IPythonNature nature, boolean checkSystemManager, boolean dontSearchInit, IModuleRequestState moduleRequest) {
IModule module = null;
// only get the system manager here (to avoid recursion)
IModulesManager[] managersInvolved = this.getManagersInvolved(true);
for (IModulesManager m : managersInvolved) {
if (m instanceof ISystemModulesManager) {
module = ((ISystemModulesManager) m).getBuiltinModule(name, dontSearchInit, moduleRequest);
if (module != null) {
if (DEBUG_MODULES) {
System.out.println("Trying to get:" + name + " - " + " returned builtin:" + module + " - " + m.getClass());
}
return new Tuple<IModule, IModulesManager>(module, m);
}
}
}
for (IModulesManager m : managersInvolved) {
if (m instanceof IProjectModulesManager) {
IProjectModulesManager pM = (IProjectModulesManager) m;
module = pM.getModuleInDirectManager(name, nature, dontSearchInit, moduleRequest);
} else if (m instanceof ISystemModulesManager) {
ISystemModulesManager systemModulesManager = (ISystemModulesManager) m;
module = systemModulesManager.getModuleWithoutBuiltins(name, nature, dontSearchInit, moduleRequest);
} else {
throw new RuntimeException("Unexpected: " + m);
}
if (module != null) {
if (DEBUG_MODULES) {
System.out.println("Trying to get:" + name + " - " + " returned:" + module + " - " + m.getClass());
}
return new Tuple<IModule, IModulesManager>(module, m);
}
}
if (DEBUG_MODULES) {
System.out.println("Trying to get:" + name + " - " + " returned:null - " + this.getClass());
}
return null;
}
use of org.python.pydev.core.IProjectModulesManager 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 org.python.pydev.core.IProjectModulesManager in project Pydev by fabioz.
the class PythonBaseModelProvider method getChildrenForIWrappedResource.
/**
* @param wrappedResourceParent: this is the parent that is an IWrappedResource (which means
* that children will also be IWrappedResources)
*
* @return the children (an array of IWrappedResources)
*/
private Object[] getChildrenForIWrappedResource(IWrappedResource wrappedResourceParent) {
// ------------------------------------------------------------------- get python nature
PythonNature nature = null;
Object[] childrenToReturn = null;
Object obj = wrappedResourceParent.getActualObject();
IProject project = null;
if (obj instanceof IResource) {
IResource resource = (IResource) obj;
project = resource.getProject();
if (project != null && project.isOpen()) {
nature = PythonNature.getPythonNature(project);
}
}
// ------------------------------------------------------------------- treat python nodes
if (wrappedResourceParent instanceof PythonNode) {
PythonNode node = (PythonNode) wrappedResourceParent;
childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, node.entry, node.pythonFile);
// ------------------------------------- treat python files (add the classes/methods,etc)
} else if (wrappedResourceParent instanceof PythonFile) {
// if it's a file, we want to show the classes and methods
PythonFile file = (PythonFile) wrappedResourceParent;
if (PythonPathHelper.isValidSourceFile(file.getActualObject())) {
if (nature != null) {
ICodeCompletionASTManager astManager = nature.getAstManager();
// the nature may still not be completely restored...
if (astManager != null) {
IModulesManager modulesManager = astManager.getModulesManager();
if (modulesManager instanceof IProjectModulesManager) {
IProjectModulesManager projectModulesManager = (IProjectModulesManager) modulesManager;
String moduleName = projectModulesManager.resolveModuleInDirectManager(file.getActualObject());
if (moduleName != null) {
BaseModuleRequest moduleRequest = new BaseModuleRequest(true);
IModule module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true, moduleRequest);
if (module == null) {
// ok, something strange happened... it shouldn't be null... maybe empty, but not null at this point
// so, if it exists, let's try to create it...
// TODO: This should be moved to somewhere else.
String resourceOSString = SharedCorePlugin.getIResourceOSString(file.getActualObject());
if (resourceOSString != null) {
File f = new File(resourceOSString);
if (f.exists()) {
projectModulesManager.addModule(new ModulesKey(moduleName, f));
module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true, moduleRequest);
}
}
}
if (module instanceof SourceModule) {
SourceModule sourceModule = (SourceModule) module;
OutlineCreatorVisitor visitor = OutlineCreatorVisitor.create(sourceModule.getAst());
ParsedItem root = new ParsedItem(visitor.getAll().toArray(new ASTEntryWithChildren[0]), null);
childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, root, file);
}
}
}
}
}
}
} else // ------------------------------------------------------------- treat folders and others
{
Object[] children = super.getChildren(wrappedResourceParent.getActualObject());
childrenToReturn = wrapChildren(wrappedResourceParent, wrappedResourceParent.getSourceFolder(), children);
}
return childrenToReturn;
}
Aggregations