Search in sources :

Example 1 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class IOUtils method addAstInfo.

/**
 * Adds ast info information for a module.
 *
 * @param m the module we want to add to the info
 */
public List<IInfo> addAstInfo(SimpleNode node, ModulesKey key, boolean generateDelta) {
    List<IInfo> createdInfos = new ArrayList<IInfo>();
    if (node == null || key.name == null) {
        return createdInfos;
    }
    try {
        Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>> tup = getInnerEntriesForAST(node);
        if (DebugSettings.DEBUG_ANALYSIS_REQUESTS) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Adding ast info to: " + key.name);
        }
        try {
            Iterator<ASTEntry> entries = tup.o2;
            FastStack<SimpleNode> tempStack = new FastStack<SimpleNode>(10);
            synchronized (this.lock) {
                synchronized (ObjectsInternPool.lock) {
                    final String file = key.file != null ? ObjectsInternPool.internUnsynched(key.file.toString()) : null;
                    key.name = ObjectsInternPool.internUnsynched(key.name);
                    while (entries.hasNext()) {
                        ASTEntry entry = entries.next();
                        IInfo infoCreated = null;
                        if (entry.parent == null) {
                            // we only want those that are in the global scope
                            if (entry.node instanceof ClassDef) {
                                // no intern construct (locked in this loop)
                                NameTok name = (NameTok) ((ClassDef) entry.node).name;
                                ClassInfo info = new ClassInfo(ObjectsInternPool.internUnsynched(name.id), key.name, null, false, getNature(), file, name.beginLine, name.beginColumn);
                                add(info, TOP_LEVEL);
                                infoCreated = info;
                            } else if (entry.node instanceof FunctionDef) {
                                // no intern construct (locked in this loop)
                                NameTok name = (NameTok) ((FunctionDef) entry.node).name;
                                FuncInfo info2 = new FuncInfo(ObjectsInternPool.internUnsynched(name.id), key.name, null, false, getNature(), file, name.beginLine, name.beginColumn);
                                add(info2, TOP_LEVEL);
                                infoCreated = info2;
                            } else {
                                // it is an assign
                                infoCreated = this.addAssignTargets(entry, key.name, TOP_LEVEL, null, false, file);
                            }
                        } else {
                            if (entry.node instanceof ClassDef || entry.node instanceof FunctionDef) {
                                // ok, it has a parent, so, let's check to see if the path we got only has class definitions
                                // as the parent (and get that path)
                                Tuple<String, Boolean> pathToRoot = this.getPathToRoot(entry, false, false, tempStack);
                                if (pathToRoot != null && pathToRoot.o1 != null && pathToRoot.o1.length() > 0) {
                                    if (entry.node instanceof ClassDef) {
                                        NameTok name = ((NameTok) ((ClassDef) entry.node).name);
                                        ClassInfo info = new ClassInfo(ObjectsInternPool.internUnsynched(name.id), key.name, ObjectsInternPool.internUnsynched(pathToRoot.o1), false, getNature(), file, name.beginLine, name.beginColumn);
                                        add(info, INNER);
                                        infoCreated = info;
                                    } else {
                                        // FunctionDef
                                        NameTok name = ((NameTok) ((FunctionDef) entry.node).name);
                                        FuncInfo info2 = new FuncInfo(ObjectsInternPool.internUnsynched(name.id), key.name, ObjectsInternPool.internUnsynched(pathToRoot.o1), false, getNature(), file, name.beginLine, name.beginColumn);
                                        add(info2, INNER);
                                        infoCreated = info2;
                                    }
                                }
                            } else {
                                // it is an assign
                                Tuple<String, Boolean> pathToRoot = this.getPathToRoot(entry, true, false, tempStack);
                                if (pathToRoot != null && pathToRoot.o1 != null && pathToRoot.o1.length() > 0) {
                                    infoCreated = this.addAssignTargets(entry, key.name, INNER, pathToRoot.o1, pathToRoot.o2, file);
                                }
                            }
                        }
                        if (infoCreated != null) {
                            createdInfos.add(infoCreated);
                        }
                    }
                // end while
                }
            // end lock ObjectsPool.lock
            }
        // end this.lock
        } catch (Exception e) {
            Log.log(e);
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return createdInfos;
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) SimpleNode(org.python.pydev.parser.jython.SimpleNode) IInfo(org.python.pydev.core.IInfo) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Iterator(java.util.Iterator) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 2 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class IOUtils method getInnerEntriesForAST.

/**
 * @return an iterator that'll get the outline entries for the given ast.
 */
public static Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>> getInnerEntriesForAST(SimpleNode node) throws Exception {
    DefinitionsASTIteratorVisitor visitor = new DefinitionsASTIteratorVisitor();
    node.accept(visitor);
    Iterator<ASTEntry> entries = visitor.getOutline();
    return new Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>>(visitor, entries);
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class TddCodeGenerationQuickFixParticipant method checkInitCreation.

private boolean checkInitCreation(PyEdit edit, PySelection callPs, ItemPointer[] pointers, List<ICompletionProposalHandle> ret) {
    for (ItemPointer pointer : pointers) {
        Definition definition = pointer.definition;
        if (definition != null && definition.ast instanceof ClassDef) {
            ClassDef d = (ClassDef) definition.ast;
            ASTEntry initEntry = findInitInClass(d);
            if (initEntry == null) {
                // Give the user a chance to create the __init__.
                PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                pyCreateMethod.setCreateAs(PyCreateMethodOrField.BOUND_METHOD);
                String className = NodeUtils.getRepresentationString(d);
                pyCreateMethod.setCreateInClass(className);
                List<String> parametersAfterCall = callPs.getParametersAfterCall(callPs.getAbsoluteCursorOffset());
                String displayString = StringUtils.format("Create %s __init__ (%s)", className, definition.module.getName());
                TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule("__init__", tddQuickFixParticipant.imageMethod, displayString, null, displayString, IPyCompletionProposal.PRIORITY_CREATE, edit, definition.module.getFile(), parametersAfterCall, pyCreateMethod, callPs);
                completion.locationStrategy = AbstractPyCreateAction.LOCATION_STRATEGY_FIRST_METHOD;
                ret.add(completion);
                return true;
            }
        }
    }
    return false;
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 4 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class AbstractRenameRefactorProcessTest method createString.

private void createString(List<ASTEntry> initial) {
    ASTEntry entry = new ASTEntry(null);
    entry.setAdditionalInfo(AstEntryScopeAnalysisConstants.AST_ENTRY_FOUND_LOCATION, AstEntryScopeAnalysisConstants.AST_ENTRY_FOUND_IN_STRING);
    add(entry, initial);
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry)

Example 5 with ASTEntry

use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.

the class AbstractRenameRefactorProcessTest method createComment.

private void createComment(List<ASTEntry> initial) {
    ASTEntry entry = new ASTEntry(null);
    entry.setAdditionalInfo(AstEntryScopeAnalysisConstants.AST_ENTRY_FOUND_LOCATION, AstEntryScopeAnalysisConstants.AST_ENTRY_FOUND_IN_COMMENT);
    add(entry, initial);
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry)

Aggregations

ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)81 ArrayList (java.util.ArrayList)30 SimpleNode (org.python.pydev.parser.jython.SimpleNode)30 HashSet (java.util.HashSet)13 SequencialASTIteratorVisitor (org.python.pydev.parser.visitors.scope.SequencialASTIteratorVisitor)13 Tuple (org.python.pydev.shared_core.structure.Tuple)13 IToken (org.python.pydev.core.IToken)11 File (java.io.File)9 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)9 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)9 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)8 Str (org.python.pydev.parser.jython.ast.Str)8 CoreException (org.eclipse.core.runtime.CoreException)7 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)7 PySelection (org.python.pydev.core.docutils.PySelection)7 Name (org.python.pydev.parser.jython.ast.Name)7 Found (com.python.pydev.analysis.visitors.Found)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Attribute (org.python.pydev.parser.jython.ast.Attribute)6