Search in sources :

Example 16 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.

the class PydevConsoleInterpreter method getNatureAndRelatedNatures.

private Set<IPythonNature> getNatureAndRelatedNatures() {
    Set<IPythonNature> ret = new HashSet<IPythonNature>();
    for (IPythonNature iPythonNature : this.initialNatures) {
        try {
            ICodeCompletionASTManager astManager = iPythonNature.getAstManager();
            if (astManager == null) {
                continue;
            }
            IModulesManager modulesManager = astManager.getModulesManager();
            if (modulesManager == null) {
                continue;
            }
            IModulesManager[] managersInvolved = modulesManager.getManagersInvolved(true);
            for (IModulesManager iModulesManager : managersInvolved) {
                ret.add(iModulesManager.getNature());
            }
        } catch (Exception e) {
            Log.log(e);
        }
    }
    return ret;
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IPythonNature(org.python.pydev.core.IPythonNature) IModulesManager(org.python.pydev.core.IModulesManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) HashSet(java.util.HashSet)

Example 17 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.

the class PydevConsoleInterpreter method getCompletions.

@Override
@SuppressWarnings("unchecked")
public ICompletionProposalHandle[] getCompletions(IScriptConsoleViewer viewer, String commandLine, int position, int offset, int whatToShow) throws Exception {
    final String text = commandLine.substring(0, position);
    ActivationTokenAndQualifier tokenAndQual = PySelection.getActivationTokenAndQualifier(new Document(text), text.length(), true, false);
    String textForCompletionInConsole = PySelection.getTextForCompletionInConsole(new Document(text), text.length());
    if (PySelection.isCompletionForLiteralNumber(tokenAndQual.activationToken)) {
        // suppress completions that would be invalid
        return new ICompletionProposalHandle[0];
    }
    // Code-completion for imports
    ImportInfo importsTipper = ImportsSelection.getImportsTipperStr(text, false);
    Set<IPythonNature> natureAndRelatedNatures = getNatureAndRelatedNatures();
    if (importsTipper.importsTipperStr.length() != 0) {
        importsTipper.importsTipperStr = importsTipper.importsTipperStr.trim();
        Set<IToken> tokens = new TreeSet<IToken>();
        final boolean onlyGetDirectModules = true;
        // Check all the natures.
        for (final IPythonNature nature : natureAndRelatedNatures) {
            ICodeCompletionASTManager astManager = nature.getAstManager();
            TokensList importTokens = astManager.getCompletionsForImport(importsTipper, new ICompletionRequest() {

                @Override
                public IPythonNature getNature() {
                    return nature;
                }

                @Override
                public File getEditorFile() {
                    return null;
                }

                @Override
                public IModule getModule() throws MisconfigurationException {
                    return null;
                }
            }, onlyGetDirectModules);
            // only get all modules for the 1st one we analyze (no need to get on the others)
            for (IterTokenEntry entry : importTokens) {
                IToken iToken = entry.getToken();
                tokens.add(iToken);
            }
        }
        int qlen = tokenAndQual.qualifier.length();
        List<ICompletionProposalHandle> ret = new ArrayList<ICompletionProposalHandle>(tokens.size());
        Iterator<IToken> it = tokens.iterator();
        for (int i = 0; i < tokens.size(); i++) {
            IToken t = it.next();
            int replacementOffset = offset - qlen;
            String representation = t.getRepresentation();
            if (representation.startsWith(tokenAndQual.qualifier)) {
                ret.add(CompletionProposalFactory.get().createPyLinkedModeCompletionProposal(representation, replacementOffset, qlen, representation.length(), t, null, null, IPyCompletionProposal.PRIORITY_DEFAULT, IPyCompletionProposal.ON_APPLY_DEFAULT, "", null));
            }
        }
        return ret.toArray(new ICompletionProposalHandle[ret.size()]);
    }
    // END Code-completion for imports
    String actTok = tokenAndQual.activationToken;
    if (tokenAndQual.qualifier != null && tokenAndQual.qualifier.length() > 0) {
        if (actTok.length() > 0 && actTok.charAt(actTok.length() - 1) != '.') {
            actTok += '.';
        }
        actTok += tokenAndQual.qualifier;
    }
    boolean showOnlyTemplates = whatToShow == AbstractCompletionProcessorWithCycling.SHOW_ONLY_TEMPLATES;
    boolean showForTabCompletion = whatToShow == AbstractCompletionProcessorWithCycling.SHOW_FOR_TAB_COMPLETIONS;
    // simple completions (clients)
    ArrayList<ICompletionProposalHandle> results = new ArrayList<ICompletionProposalHandle>();
    if (!showForTabCompletion) {
        for (ISimpleAssistParticipant2 participant : simpleParticipants) {
            results.addAll(participant.computeConsoleProposals(tokenAndQual.activationToken, tokenAndQual.qualifier, offset));
        }
    }
    ProposalsComparator proposalsComparator = new ProposalsComparator(tokenAndQual.qualifier, null);
    ArrayList<ICompletionProposalHandle> results2 = new ArrayList<ICompletionProposalHandle>();
    if (!showOnlyTemplates) {
        // shell completions
        if (consoleCommunication != null) {
            ICompletionProposalHandle[] consoleCompletions = consoleCommunication.getCompletions(text, textForCompletionInConsole, offset, showForTabCompletion);
            // If we're only showing ipython completions, then short-circuit the rest
            if (showForTabCompletion) {
                return consoleCompletions;
            }
            results2.addAll(Arrays.asList(consoleCompletions));
        }
    }
    if (tokenAndQual.activationToken.length() == 0) {
        // templates (only if we have no activation token)
        IPyTemplateCompletionProcessor pyTemplateCompletionProcessor = new PyTemplateCompletionProcessor();
        pyTemplateCompletionProcessor.addTemplateProposals((ITextViewer) viewer, offset, results2);
    }
    Collections.sort(results2, proposalsComparator);
    ArrayList<ICompletionProposalHandle> results3 = new ArrayList<ICompletionProposalHandle>();
    if (!showOnlyTemplates) {
        // other participants
        List<Object> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_COMPLETION);
        for (Object participant : participants) {
            if (participant instanceof IPyDevCompletionParticipant2) {
                IPyDevCompletionParticipant2 participant2 = (IPyDevCompletionParticipant2) participant;
                results3.addAll(participant2.computeConsoleCompletions(tokenAndQual, natureAndRelatedNatures, viewer, offset));
            }
        }
        Collections.sort(results3, proposalsComparator);
    }
    results.addAll(results2);
    results.addAll(results3);
    return results.toArray(new ICompletionProposalHandle[results.size()]);
}
Also used : IModule(org.python.pydev.core.IModule) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ISimpleAssistParticipant2(org.python.pydev.ast.simpleassist.ISimpleAssistParticipant2) IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IToken(org.python.pydev.core.IToken) TreeSet(java.util.TreeSet) IPyDevCompletionParticipant2(org.python.pydev.ast.codecompletion.IPyDevCompletionParticipant2) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) ActivationTokenAndQualifier(org.python.pydev.core.docutils.PySelection.ActivationTokenAndQualifier) PyTemplateCompletionProcessor(org.python.pydev.editor.codecompletion.PyTemplateCompletionProcessor) IPyTemplateCompletionProcessor(org.python.pydev.editor.codecompletion.IPyTemplateCompletionProcessor) TokensList(org.python.pydev.core.TokensList) ICompletionRequest(org.python.pydev.core.ICompletionRequest) IPyTemplateCompletionProcessor(org.python.pydev.editor.codecompletion.IPyTemplateCompletionProcessor) IterTokenEntry(org.python.pydev.core.IterTokenEntry) ProposalsComparator(org.python.pydev.ast.codecompletion.ProposalsComparator) ImportInfo(org.python.pydev.core.ICodeCompletionASTManager.ImportInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) File(java.io.File)

Example 18 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.

the class NatureGroup method calculateChildren.

@Override
protected void calculateChildren() throws MisconfigurationException {
    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
        addLeaf("AST manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    IModulesManager projectModulesManager = astManager.getModulesManager();
    if (projectModulesManager == null) {
        addLeaf("Modules manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    PythonPathHelper pythonPathHelper = (PythonPathHelper) projectModulesManager.getPythonPathHelper();
    if (pythonPathHelper == null) {
        addLeaf("PythonPathHelper == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    List<String> pythonpath = pythonPathHelper.getPythonpath();
    for (String s : pythonpath) {
        addLeaf("PYTHONPATH: " + s);
    }
    HashSet<ModulesKey> expectedModuleNames = new HashSet<ModulesKey>();
    for (String string : pythonpath) {
        File file = new File(string);
        if (file.isDirectory()) {
            // TODO: Handle zip file modules!
            Collection<PyFileInfo> modulesBelow = PythonPathHelper.getModulesBelow(file, new NullProgressMonitor(), pythonpath).getFoundPyFileInfos();
            for (PyFileInfo fileInfo : modulesBelow) {
                File moduleFile = fileInfo.getFile();
                String modName = pythonPathHelper.resolveModule(FileUtils.getFileAbsolutePath(moduleFile), true, nature.getProject());
                if (modName != null) {
                    expectedModuleNames.add(new ModulesKey(modName, moduleFile));
                } else {
                    if (PythonPathHelper.isValidModuleLastPart(StringUtils.stripExtension((moduleFile.getName())))) {
                        addLeaf(StringUtils.format("Unable to resolve module: %s (gotten null module name)", moduleFile));
                    }
                }
            }
        } else {
            if (!file.exists()) {
                addLeaf(StringUtils.format("File %s is referenced in the pythonpath but does not exist.", file));
            } else {
                addLeaf(org.python.pydev.shared_core.string.StringUtils.format("File %s not handled (TODO: Fix zip files support in the viewer).", file));
            }
        }
    }
    IntegrityInfo info = new IntegrityInfo();
    info.nature = nature;
    ModulesKey[] onlyDirectModules = projectModulesManager.getOnlyDirectModules();
    TreeSet<ModulesKey> inModulesManager = new TreeSet<ModulesKey>(Arrays.asList(onlyDirectModules));
    Set<String> allAdditionalInfoModuleNames = new TreeSet<String>();
    List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> additionalInfoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(nature, false, false, false);
    AbstractAdditionalTokensInfo additionalProjectInfo;
    if (additionalInfoAndNature.size() == 0) {
        addChild(new LeafElement(this, "No additional infos found (1 expected) -- skipping other checks."));
        return;
    } else {
        if (additionalInfoAndNature.size() > 1) {
            addChild(new LeafElement(this, StringUtils.format("%s additional infos found (only 1 expected) -- continuing checks but analysis may be wrong.", additionalInfoAndNature.size())));
        }
        additionalProjectInfo = additionalInfoAndNature.get(0).o1;
        allAdditionalInfoModuleNames.addAll(additionalProjectInfo.getAllModulesWithTokens());
    }
    for (ModulesKey key : inModulesManager) {
        if (!expectedModuleNames.contains(key)) {
            info.modulesNotInDisk.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in memory but not in the disk.", key)));
        }
    }
    ModulesKey tempKey = new ModulesKey(null, null);
    for (String s : allAdditionalInfoModuleNames) {
        tempKey.name = s;
        if (!expectedModuleNames.contains(tempKey)) {
            info.additionalModulesNotInDisk.add(s);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the additional info but not in the disk.", s)));
        }
    }
    for (ModulesKey key : expectedModuleNames) {
        boolean isInModulesManager = inModulesManager.contains(key);
        if (!isInModulesManager) {
            info.modulesNotInMemory.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the disk but not in memory.", key)));
        }
        if (!allAdditionalInfoModuleNames.contains(key.name)) {
            try {
                AbstractModule mod = AbstractModule.createModule(key.name, key.file, info.nature, true);
                if (!(mod instanceof SourceModule)) {
                    continue;
                }
                SourceModule module = (SourceModule) mod;
                if (module == null || module.getAst() == null) {
                    addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
                } else {
                    try {
                        Iterator<ASTEntry> innerEntriesForAST = AbstractAdditionalDependencyInfo.getInnerEntriesForAST(module.getAst()).o2;
                        if (innerEntriesForAST.hasNext()) {
                            info.moduleNotInAdditionalInfo.add(module);
                            addChild(new LeafElement(this, StringUtils.format("The additional info index of the module: %s is not updated.", key.name)));
                        }
                    } catch (Exception e) {
                        addChild(new LeafElement(this, StringUtils.format("Unexpected error happened on: %s - %s: %s", key.name, key.file, e.getMessage())));
                    }
                }
            } catch (IOException e) {
                // OK, it cannot be parsed, so, we cannot generate its info
                addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
            }
        }
    }
    // modules manager
    if (info.modulesNotInDisk.size() > 0) {
        for (ModulesKey m : info.modulesNotInDisk) {
            addChild(new LeafElement(this, StringUtils.format("FIX: Removing from modules manager: %s", m)));
        }
        projectModulesManager.removeModules(info.modulesNotInDisk);
    }
    for (ModulesKey key : info.modulesNotInMemory) {
        addChild(new LeafElement(this, "FIX: Adding to modules manager: " + key));
        projectModulesManager.addModule(key);
    }
    // additional info
    for (String s : info.additionalModulesNotInDisk) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Removing from additional info: %s", s)));
        additionalProjectInfo.removeInfoFromModule(s, true);
    }
    for (SourceModule mod : info.moduleNotInAdditionalInfo) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Adding to additional info: %s", mod.getName())));
        additionalProjectInfo.addAstInfo(mod.getAst(), mod.getModulesKey(), true);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TreeSet(java.util.TreeSet) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PyFileInfo(org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo) HashSet(java.util.HashSet) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModulesManager(org.python.pydev.core.IModulesManager) IOException(java.io.IOException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 19 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.

the class PythonCompletionWithBuiltinsTest method testRecursion.

public void testRecursion() throws FileNotFoundException, Exception, CompletionRecursionException {
    String file = TestDependent.TEST_PYSRC_TESTING_LOC + "testrec3/rec.py";
    String strDoc = "RuntimeError.";
    File f = new File(file);
    try {
        ICodeCompletionASTManager astManager = nature.getAstManager();
        ICompletionState state = CompletionStateFactory.getEmptyCompletionState("RuntimeError", nature, new CompletionCache());
        IModule module = AbstractASTManager.createModule(f, new Document(FileUtils.getFileContents(f)), nature);
        astManager.getCompletionsForModule(module, state, true, true);
    } catch (CompletionRecursionException e) {
    // that's ok... we're asking for it here...
    }
    requestCompl(f, strDoc, strDoc.length(), -1, new String[] { "__doc__", "__getitem__()", "__init__()", "__str__()" });
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModule(org.python.pydev.core.IModule) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) ICompletionState(org.python.pydev.core.ICompletionState) Document(org.eclipse.jface.text.Document) File(java.io.File)

Example 20 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager in project Pydev by fabioz.

the class CtxParticipant method findItemPointerFromPyTestFixture.

private ItemPointer findItemPointerFromPyTestFixture(IPythonNature nature, ICompletionState completionCache, String fixtureName) {
    try {
        ICodeCompletionASTManager astManager = nature.getAstManager();
        if (astManager != null) {
            List<IInfo> tokensEqualTo = AdditionalProjectInterpreterInfo.getTokensEqualTo(fixtureName, nature, AdditionalProjectInterpreterInfo.TOP_LEVEL);
            for (IInfo iInfo : tokensEqualTo) {
                List<ItemPointer> pointers = new LinkedListWarningOnSlowOperations<>();
                AnalysisPlugin.getDefinitionFromIInfo(pointers, astManager, nature, iInfo, completionCache, true, true);
                for (ItemPointer itemPointer : pointers) {
                    if (itemPointer.definition.ast instanceof FunctionDef) {
                        FunctionDef functionDef = (FunctionDef) itemPointer.definition.ast;
                        if (functionDef.decs != null) {
                            for (decoratorsType dec : functionDef.decs) {
                                String decoratorFuncName = NodeUtils.getRepresentationString(dec.func);
                                if (decoratorFuncName != null) {
                                    if (FIXTURE_PATTERN.matcher(decoratorFuncName).find() || YIELD_FIXTURE_PATTERN.matcher(decoratorFuncName).find()) {
                                        return itemPointer;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (MisconfigurationException e1) {
        Log.log(e1);
    }
    return null;
}
Also used : IInfo(org.python.pydev.core.IInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) MisconfigurationException(org.python.pydev.core.MisconfigurationException) LinkedListWarningOnSlowOperations(org.python.pydev.shared_core.structure.LinkedListWarningOnSlowOperations) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Aggregations

ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)40 IPythonNature (org.python.pydev.core.IPythonNature)16 ICompletionState (org.python.pydev.core.ICompletionState)15 TokensList (org.python.pydev.core.TokensList)14 IModule (org.python.pydev.core.IModule)13 IModulesManager (org.python.pydev.core.IModulesManager)13 ArrayList (java.util.ArrayList)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 File (java.io.File)10 CompletionState (org.python.pydev.ast.codecompletion.revisited.CompletionState)9 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)9 IToken (org.python.pydev.core.IToken)8 PythonNature (org.python.pydev.plugin.nature.PythonNature)8 IProject (org.eclipse.core.resources.IProject)7 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)6 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 List (java.util.List)5 IFile (org.eclipse.core.resources.IFile)5 CoreException (org.eclipse.core.runtime.CoreException)5 IInfo (org.python.pydev.core.IInfo)5