use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class ImportChecker method visitImportToken.
/**
* This is so that we can use it without actually being in some visit.
*/
public static ImportInfo visitImportToken(boolean reportUndefinedImports, IToken token, String moduleName, IPythonNature nature, AbstractScopeAnalyzerVisitor visitor, ICompletionCache completionCache) {
// try to find it as a relative import
boolean wasResolved = false;
Tuple3<IModule, String, IToken> modTok = null;
String checkForToken = "";
if (token instanceof SourceToken) {
ICodeCompletionASTManager astManager = nature.getAstManager();
ICompletionState state = CompletionStateFactory.getEmptyCompletionState(token.getRepresentation(), nature, completionCache);
try {
modTok = astManager.findOnImportedMods(new TokensList(token), state, moduleName, visitor.current);
} catch (CompletionRecursionException e1) {
// unable to resolve it
modTok = null;
}
if (modTok != null && modTok.o1 != null) {
checkForToken = modTok.o2;
if (modTok.o2.length() == 0) {
wasResolved = true;
} else {
try {
checkForToken = AbstractASTManager.getTokToSearchInOtherModule(modTok);
if (astManager.getRepInModule(modTok.o1, checkForToken, nature) != null) {
wasResolved = true;
}
} catch (CompletionRecursionException e) {
// not resolved...
}
}
}
if (!wasResolved && moduleName != null && moduleName.length() > 0) {
if (moduleName.equals(token.getRepresentation()) || moduleName.equals(token.getRepresentation() + ".__init__")) {
wasResolved = true;
modTok = new Tuple3<IModule, String, IToken>(visitor.current, "", token);
checkForToken = modTok.o2;
}
}
// if it got here, it was not resolved
if (!wasResolved && reportUndefinedImports) {
visitor.onAddUnresolvedImport(token);
}
}
// might still return a modTok, even if the token we were looking for was not found.
if (modTok != null) {
return new ImportInfo(modTok.o1, checkForToken, modTok.o3, wasResolved);
} else {
return new ImportInfo(null, null, null, wasResolved);
}
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class AbstractInterpreterManager method getBuiltinMod.
@Override
public IModule getBuiltinMod(String projectInterpreterName, IModuleRequestState moduleRequest) {
// Cache with the internal name.
projectInterpreterName = getInternalName(projectInterpreterName);
if (projectInterpreterName == null) {
return null;
}
String cacheName = projectInterpreterName + "_" + moduleRequest.getAcceptTypeshed();
IModule mod = builtinMod.get(cacheName);
if (mod != null) {
return mod;
}
try {
InterpreterInfo interpreterInfo = this.getInterpreterInfo(projectInterpreterName, null);
ISystemModulesManager modulesManager = interpreterInfo.getModulesManager();
mod = modulesManager.getBuiltinModule("__builtin__", false, moduleRequest);
if (mod == null) {
// Python 3.0 has builtins and not __builtin__
mod = modulesManager.getBuiltinModule("builtins", false, moduleRequest);
}
if (mod != null) {
builtinMod.put(cacheName, mod);
}
} catch (MisconfigurationException e) {
Log.log(e);
}
return builtinMod.get(cacheName);
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class PyRefactoringFindDefinition method findActualDefinition.
/**
* @param request OUT: the request to be used in the find definition. It'll be prepared inside this method, and if it's not
* a suitable request for the find definition, the return of this function will be null, otherwise, it was correctly
* prepared for a find definition action.
*
* @param completionCache the completion cache to be used
* @param selected OUT: fills the array with the found definitions
*
* @return an array with 2 strings: the activation token and the qualifier used. The return may be null, in which case
* the refactoring request is not valid for a find definition.
* @throws CompletionRecursionException
* @throws BadLocationException
*/
public static String[] findActualDefinition(RefactoringRequest request, ICompletionState completionCache, List<IDefinition> selected) throws CompletionRecursionException, BadLocationException {
// ok, let's find the definition.
request.getMonitor().beginTask("Find actual definition", 5);
String[] tokenAndQual;
try {
// 1. we have to know what we're looking for (activationToken)
request.communicateWork("Finding Definition");
IModule mod = prepareRequestForFindDefinition(request);
if (mod == null) {
return null;
}
String modName = request.moduleName;
request.communicateWork("Module name found:" + modName);
tokenAndQual = PySelection.getActivationTokenAndQualifier(request.getDoc(), request.ps.getAbsoluteCursorOffset(), true);
String tok = tokenAndQual[0] + tokenAndQual[1];
// 2. check findDefinition (SourceModule)
try {
int beginLine = request.getBeginLine();
int beginCol = request.getBeginCol() + 1;
IPythonNature pythonNature = request.nature;
PyRefactoringFindDefinition.findActualDefinition(request.getMonitor(), request.acceptTypeshed, mod, tok, selected, beginLine, beginCol, pythonNature, completionCache);
} catch (OperationCanceledException e) {
throw e;
} catch (CompletionRecursionException e) {
throw e;
} catch (BadLocationException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
request.getMonitor().done();
}
return tokenAndQual;
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class DjangoNewProjectPage method getNextPage.
@Override
public IWizardPage getNextPage() {
String projectType = this.getProjectType();
IInterpreterManager interpreterManager;
if (IPythonNature.Versions.ALL_JYTHON_VERSIONS.contains(projectType)) {
interpreterManager = InterpreterManagersAPI.getJythonInterpreterManager();
} else if (IPythonNature.Versions.ALL_IRONPYTHON_VERSIONS.contains(projectType)) {
interpreterManager = InterpreterManagersAPI.getIronpythonInterpreterManager();
} else {
// if others fail, consider it python
interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
}
try {
String projectInterpreter = this.getProjectInterpreter();
IInterpreterInfo interpreterInfo;
if (projectInterpreter.toLowerCase().equals("default")) {
interpreterInfo = interpreterManager.getDefaultInterpreterInfo(false);
} else {
interpreterInfo = interpreterManager.getInterpreterInfo(projectInterpreter, new NullProgressMonitor());
}
IModule module = interpreterInfo.getModulesManager().getModuleWithoutBuiltins("django.core.__init__", null, false, new BaseModuleRequest(false));
if (module == null) {
DjangoNotAvailableWizardPage page = new DjangoNotAvailableWizardPage("Django not available", interpreterInfo);
page.setWizard(this.getWizard());
return page;
}
} catch (MisconfigurationException e) {
ErrorWizardPage page = new ErrorWizardPage("Unexpected error.", "An unexpected error happened:\n" + e.getMessage());
page.setWizard(this.getWizard());
return page;
}
return super.getNextPage();
}
use of org.python.pydev.core.IModule in project Pydev by fabioz.
the class ForceCodeAnalysisOnTree method forceCodeAnalysisOnFiles.
public static void forceCodeAnalysisOnFiles(PythonNature nature, IProgressMonitor monitor, List<IFile> filesToVisit, Set<IFile> filesVisited, List<IExternalCodeAnalysisVisitor> externalVisitors) {
if (nature == null) {
return;
}
AnalysisBuilderVisitor visitor = new AnalysisBuilderVisitor();
visitor.visitingWillStart(new NullProgressMonitor(), false, null);
FastStringBuffer buf = new FastStringBuffer();
for (IFile f : filesToVisit) {
if (monitor.isCanceled()) {
break;
}
if (filesVisited.contains(f)) {
continue;
}
filesVisited.add(f);
monitor.setTaskName(buf.clear().append("Scheduling: ").append(f.getName()).toString());
IDocument doc = FileUtilsFileBuffer.getDocFromResource(f);
visitor.memo = new VisitorMemo();
visitor.memo.put(PyDevBuilderVisitor.IS_FULL_BUILD, false);
long documentTime = System.currentTimeMillis();
visitor.memo.put(PyDevBuilderVisitor.DOCUMENT_TIME, documentTime);
String moduleName;
try {
moduleName = nature.resolveModule(f);
} catch (MisconfigurationException e) {
Log.log(e);
continue;
}
if (moduleName == null) {
continue;
}
AnalysisBuilderVisitor.setModuleNameInCache(visitor.memo, f, moduleName);
if (!PythonPathHelper.isValidSourceFile(f)) {
AnalysisRunner.deleteMarkers(f, true);
continue;
}
IModule module = nature.getAstManager().getModule(moduleName, nature, true, new BaseModuleRequest(false));
if (module == null) {
Log.log(IStatus.WARNING, "Unable to get module: " + moduleName + " for resource: " + f, null);
continue;
}
visitor.doVisitChangedResource(nature, f, doc, null, module, new NullProgressMonitor(), true, AnalysisBuilderRunnable.ANALYSIS_CAUSE_PARSER, documentTime, false, externalVisitors);
}
visitor.visitingEnded(new NullProgressMonitor());
}
Aggregations