Search in sources :

Example 6 with IModule

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);
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModule(org.python.pydev.core.IModule) IToken(org.python.pydev.core.IToken) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) ICompletionState(org.python.pydev.core.ICompletionState) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) TokensList(org.python.pydev.core.TokensList)

Example 7 with IModule

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);
}
Also used : IModule(org.python.pydev.core.IModule) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Example 8 with IModule

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;
}
Also used : IModule(org.python.pydev.core.IModule) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) BadLocationException(org.eclipse.jface.text.BadLocationException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException)

Example 9 with IModule

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();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 10 with IModule

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());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) IFile(org.eclipse.core.resources.IFile) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) MisconfigurationException(org.python.pydev.core.MisconfigurationException) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) VisitorMemo(org.python.pydev.ast.builder.VisitorMemo) AnalysisBuilderVisitor(com.python.pydev.analysis.additionalinfo.builders.AnalysisBuilderVisitor) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IModule (org.python.pydev.core.IModule)87 TokensList (org.python.pydev.core.TokensList)28 Document (org.eclipse.jface.text.Document)22 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)21 ICompletionState (org.python.pydev.core.ICompletionState)21 BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)19 IToken (org.python.pydev.core.IToken)19 ArrayList (java.util.ArrayList)18 File (java.io.File)17 SimpleNode (org.python.pydev.parser.jython.SimpleNode)17 CompletionCache (org.python.pydev.ast.codecompletion.revisited.CompletionCache)15 IDefinition (org.python.pydev.core.IDefinition)15 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)15 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)13 IterTokenEntry (org.python.pydev.core.IterTokenEntry)13 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)12 AssignDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition)12 IPythonNature (org.python.pydev.core.IPythonNature)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)12 IFile (org.eclipse.core.resources.IFile)9