Search in sources :

Example 1 with ICodeCompletionASTManager

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

the class AdditionalProjectInterpreterInfo method recreateAllInfo.

public static void recreateAllInfo(IPythonNature nature, IProgressMonitor monitor) {
    try (GlobalFeedbackReporter r = GlobalFeedback.start("Full projects reindex...")) {
        synchronized (additionalNatureInfoLock) {
            // Note: at this point we're 100% certain that the ast manager is there.
            ICodeCompletionASTManager astManager = nature.getAstManager();
            if (astManager == null) {
                return;
            }
            IModulesManager m = astManager.getModulesManager();
            if (m == null) {
                return;
            }
            IProject project = nature.getProject();
            AbstractAdditionalDependencyInfo currInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
            if (currInfo != null) {
                currInfo.clearAllInfo();
                currInfo.dispose();
            }
            String feedback = "(project:" + project.getName() + ")";
            synchronized (m) {
                AbstractAdditionalDependencyInfo info = (AbstractAdditionalDependencyInfo) restoreInfoForModuleManager(monitor, m, feedback, new AdditionalProjectInterpreterInfo(project), nature, nature.getGrammarVersion());
                if (info != null) {
                    // ok, set it and save it
                    additionalNatureInfo.put(FileUtilsFileBuffer.getValidProjectName(project), info);
                    info.save();
                }
            }
        }
    } catch (Exception e) {
        Log.log(e);
        throw new RuntimeException(e);
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModulesManager(org.python.pydev.core.IModulesManager) GlobalFeedbackReporter(org.python.pydev.shared_core.global_feedback.GlobalFeedback.GlobalFeedbackReporter) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 2 with ICodeCompletionASTManager

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

the class TddQuickFixParticipant method addProps.

@Override
public void addProps(MarkerAnnotationAndPosition markerAnnotation, IAnalysisPreferences analysisPreferences, String line, PySelection ps, int offset, IPythonNature nature, PyEdit edit, List<ICompletionProposalHandle> props) throws BadLocationException, CoreException {
    if (nature == null) {
        return;
    }
    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
        return;
    }
    if (markerAnnotation.position == null) {
        return;
    }
    IMarker marker = markerAnnotation.markerAnnotation.getMarker();
    Integer id = (Integer) marker.getAttribute(AnalysisRunner.PYDEV_ANALYSIS_TYPE);
    int start = markerAnnotation.position.offset;
    int end = start + markerAnnotation.position.length;
    ps.setSelection(start, end);
    String markerContents;
    try {
        markerContents = ps.getSelectedText();
    } catch (Exception e1) {
        // Selection may be wrong.
        return;
    }
    IDocument doc = ps.getDoc();
    List<String> parametersAfterCall = ps.getParametersAfterCall(end);
    switch(id) {
        case IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE:
            addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall);
            addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall);
            break;
        case IAnalysisPreferences.TYPE_UNDEFINED_IMPORT_VARIABLE:
            // Say we had something as:
            // import sys
            // sys.Bar
            // in which case 'Bar' is undefined
            // in this situation, the activationTokenAndQual would be "sys." and "Bar"
            // and we want to get the definition for "sys"
            String[] activationTokenAndQual = ps.getActivationTokenAndQualifier(true);
            if (activationTokenAndQual[0].endsWith(".")) {
                ArrayList<IDefinition> selected = findDefinitions(nature, edit, start - 2, doc);
                for (IDefinition iDefinition : selected) {
                    IModule module = iDefinition.getModule();
                    if (module.getFile() != null) {
                        Definition definition = (Definition) iDefinition;
                        File file = module.getFile();
                        if (definition.ast == null) {
                            // if we have no ast in the definition, it means the module itself was found (global scope)
                            // Add option to create class at the given module!
                            addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                            addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                        } else if (definition.ast instanceof ClassDef) {
                            ClassDef classDef = (ClassDef) definition.ast;
                            // Ok, we should create a field or method in this case (accessing a classmethod or staticmethod)
                            PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                            String className = NodeUtils.getNameFromNameTok(classDef.name);
                            pyCreateMethod.setCreateInClass(className);
                            pyCreateMethod.setCreateAs(PyCreateMethodOrField.CLASSMETHOD);
                            addCreateClassmethodOption(ps, edit, props, markerContents, parametersAfterCall, pyCreateMethod, file, className);
                        }
                    }
                }
            }
            break;
        case IAnalysisPreferences.TYPE_UNRESOLVED_IMPORT:
            // This case is the following: from other_module4 import Foo
            // with 'Foo' being undefined.
            // So, we have to suggest creating a Foo class/method in other_module4
            PyImportsHandling importsHandling = new PyImportsHandling(ps.getDoc(), false);
            int offsetLine = ps.getLineOfOffset(start);
            String selectedText = ps.getSelectedText();
            Tuple<IModule, String> found = null;
            String foundFromImportStr = null;
            boolean isImportFrom = false;
            OUT: for (ImportHandle handle : importsHandling) {
                if (handle.startFoundLine == offsetLine || handle.endFoundLine == offsetLine || (handle.startFoundLine < offsetLine && handle.endFoundLine > offsetLine)) {
                    List<ImportHandleInfo> importInfo = handle.getImportInfo();
                    for (ImportHandleInfo importHandleInfo : importInfo) {
                        String fromImportStr = importHandleInfo.getFromImportStr();
                        List<String> importedStr = importHandleInfo.getImportedStr();
                        for (String imported : importedStr) {
                            if (selectedText.equals(imported)) {
                                if (fromImportStr != null) {
                                    foundFromImportStr = fromImportStr + "." + imported;
                                    isImportFrom = true;
                                } else {
                                    // if fromImportStr == null, it's not a from xxx import yyy (i.e.: simple import)
                                    foundFromImportStr = imported;
                                }
                                try {
                                    String currentModule = nature.resolveModule(edit.getEditorFile());
                                    ICompletionState state = CompletionStateFactory.getEmptyCompletionState(nature, new CompletionCache());
                                    found = nature.getAstManager().findModule(foundFromImportStr, currentModule, state, new SourceModule(currentModule, edit.getEditorFile(), edit.getAST(), null, nature));
                                } catch (Exception e) {
                                    Log.log(e);
                                }
                                break OUT;
                            }
                        }
                    }
                    break OUT;
                }
            }
            boolean addOptionToCreateClassOrMethod = isImportFrom;
            if (found != null && found.o1 != null) {
                // or just create a class or method at the end.
                if (found.o1 instanceof SourceModule) {
                    // if all was found, there's nothing left to create.
                    if (found.o2 != null && found.o2.length() > 0) {
                        SourceModule sourceModule = (SourceModule) found.o1;
                        File file = sourceModule.getFile();
                        if (found.o2.indexOf('.') != -1) {
                            // We have to create some intermediary structure.
                            if (!addOptionToCreateClassOrMethod) {
                                // Cannot create class or method from the info (only the module structure).
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    File f = getFileStructure(file.getParentFile(), found.o2);
                                    addCreateModuleOption(ps, edit, props, markerContents, f);
                                }
                            } else {
                                // Ok, the leaf may be a class or method.
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    String moduleName = FullRepIterable.getWithoutLastPart(sourceModule.getName());
                                    String withoutLastPart = FullRepIterable.getWithoutLastPart(found.o2);
                                    moduleName += "." + withoutLastPart;
                                    String classOrMethodName = FullRepIterable.getLastPart(found.o2);
                                    File f = getFileStructure(file.getParentFile(), withoutLastPart);
                                    addCreateClassInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                                    addCreateMethodInNewModuleOption(ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                                }
                            }
                        } else {
                            // Ok, it's all there, we just have to create the leaf.
                            if (!addOptionToCreateClassOrMethod || sourceModule.getName().endsWith(".__init__")) {
                                // Cannot create class or method from the info (only the module structure).
                                if (sourceModule.getName().endsWith(".__init__")) {
                                    File f = new File(file.getParent(), found.o2 + FileTypesPreferences.getDefaultDottedPythonExtension());
                                    addCreateModuleOption(ps, edit, props, markerContents, f);
                                }
                            } else {
                                // Ok, the leaf may be a class or method.
                                addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                                addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                            }
                        }
                    }
                }
            } else if (foundFromImportStr != null) {
                // We couldn't find anything there, so, we have to create the modules structure as needed and
                // maybe create a class or module at the end (but only if it's an import from).
                // Ok, it's all there, we just have to create the leaf.
                // Discover the source folder where we should create the structure.
                File editorFile = edit.getEditorFile();
                String onlyProjectPythonPathStr = nature.getPythonPathNature().getOnlyProjectPythonPathStr(false);
                List<String> split = StringUtils.splitAndRemoveEmptyTrimmed(onlyProjectPythonPathStr, '|');
                for (int i = 0; i < split.size(); i++) {
                    String fullPath = FileUtils.getFileAbsolutePath(split.get(i));
                    fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
                    split.set(i, fullPath);
                }
                HashSet<String> projectSourcePath = new HashSet<String>(split);
                if (projectSourcePath.size() == 0) {
                    // No source folder for editor... this shouldn't happen (code analysis wouldn't even run on it).
                    return;
                }
                String fullPath = FileUtils.getFileAbsolutePath(editorFile);
                fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
                String foundSourceFolderFullPath = null;
                if (projectSourcePath.size() == 1) {
                    foundSourceFolderFullPath = projectSourcePath.iterator().next();
                } else {
                    for (String string : projectSourcePath) {
                        if (fullPath.startsWith(string)) {
                            // Use this as the source folder
                            foundSourceFolderFullPath = string;
                            break;
                        }
                    }
                }
                if (foundSourceFolderFullPath != null) {
                    if (!addOptionToCreateClassOrMethod) {
                        // Cannot create class or method from the info (only the module structure).
                        File f = getFileStructure(new File(foundSourceFolderFullPath), foundFromImportStr);
                        addCreateModuleOption(ps, edit, props, foundFromImportStr, f);
                    } else {
                        // Ok, the leaf may be a class or method.
                        String moduleName = FullRepIterable.getWithoutLastPart(foundFromImportStr);
                        File file = getFileStructure(new File(foundSourceFolderFullPath), moduleName);
                        String lastPart = FullRepIterable.getLastPart(foundFromImportStr);
                        addCreateClassInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
                        addCreateMethodInNewModuleOption(ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
                    }
                }
            }
            break;
    }
}
Also used : IModule(org.python.pydev.core.IModule) ImportHandleInfo(org.python.pydev.core.docutils.ImportHandle.ImportHandleInfo) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ICompletionState(org.python.pydev.core.ICompletionState) ArrayList(java.util.ArrayList) List(java.util.List) ImportHandle(org.python.pydev.core.docutils.ImportHandle) HashSet(java.util.HashSet) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) IDefinition(org.python.pydev.core.IDefinition) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) IMarker(org.eclipse.core.resources.IMarker) File(java.io.File) PyImportsHandling(org.python.pydev.core.docutils.PyImportsHandling) IDocument(org.eclipse.jface.text.IDocument)

Example 3 with ICodeCompletionASTManager

use of org.python.pydev.core.ICodeCompletionASTManager 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 4 with ICodeCompletionASTManager

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

the class AbstractPythonNature method startRequests.

/**
 * Start a request for an ast manager (start caching things)
 */
@Override
public boolean startRequests() {
    ICodeCompletionASTManager astManager = this.getAstManager();
    if (astManager == null) {
        return false;
    }
    IModulesManager modulesManager = astManager.getModulesManager();
    if (modulesManager == null) {
        return false;
    }
    synchronized (modulesManagerStackLock) {
        modulesManagerStack.push(modulesManager);
        return modulesManager.startCompletionCache();
    }
}
Also used : ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IModulesManager(org.python.pydev.core.IModulesManager)

Example 5 with ICodeCompletionASTManager

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

the class GlobalsTwoPanelElementSelector2 method fillContentProvider.

/**
 * This is the place where we put all the info in the content provider. Note that here we must add
 * ALL the info -- later, we'll filter it based on the active working set.
 */
@Override
protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
    if (itemsFilter instanceof InfoFilter) {
        if (progressMonitor != null) {
            progressMonitor.beginTask("Searching...", this.additionalInfo.size());
        }
        for (AbstractAdditionalTokensInfo additionalInfo : this.additionalInfo) {
            if (progressMonitor != null) {
                if (progressMonitor.isCanceled()) {
                    return;
                } else {
                    progressMonitor.worked(1);
                }
            }
            // no duplicates
            Collection<IInfo> allTokens = new HashSet<IInfo>(additionalInfo.getAllTokens());
            for (IInfo iInfo : allTokens) {
                contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, iInfo), itemsFilter);
            }
            // Also show to the user the modules available as globals (2.2.3)
            IModulesManager modulesManager = null;
            try {
                if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                    AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                    IProject project = projectInterpreterInfo.getProject();
                    PythonNature nature = PythonNature.getPythonNature(project);
                    if (nature != null) {
                        ICodeCompletionASTManager astManager = nature.getAstManager();
                        if (astManager != null) {
                            modulesManager = astManager.getModulesManager();
                        }
                    }
                } else if (additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                    AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additionalInfo;
                    IInterpreterInfo defaultInterpreterInfo = systemInterpreterInfo.getManager().getDefaultInterpreterInfo(false);
                    modulesManager = defaultInterpreterInfo.getModulesManager();
                }
            } catch (Throwable e) {
                Log.log(e);
            }
            if (modulesManager != null) {
                SortedMap<ModulesKey, ModulesKey> allDirectModulesStartingWith = modulesManager.getAllDirectModulesStartingWith("");
                Collection<ModulesKey> values = allDirectModulesStartingWith.values();
                for (ModulesKey modulesKey : values) {
                    contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, new ModInfo(modulesKey.name, modulesManager.getNature(), modulesKey.file != null ? modulesKey.file.toString() : null, 1, 1)), itemsFilter);
                }
            }
        }
    }
    if (progressMonitor != null) {
        progressMonitor.done();
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IModulesManager(org.python.pydev.core.IModulesManager) IProject(org.eclipse.core.resources.IProject) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) IInfo(org.python.pydev.core.IInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) ModInfo(com.python.pydev.analysis.additionalinfo.ModInfo) HashSet(java.util.HashSet)

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