Search in sources :

Example 1 with DefinitionsASTIteratorVisitor

use of org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor 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 DefinitionsASTIteratorVisitor

use of org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor 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 DefinitionsASTIteratorVisitor

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

the class PyOutlineSelectionDialog method calculateHierarchy.

@Override
protected void calculateHierarchy() {
    if (root != null) {
        return;
    }
    if (this.ast == null && pyEdit != null) {
        this.ast = pyEdit.getAST();
    }
    if (ast == null) {
        return;
    }
    DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.create(ast);
    if (visitor == null) {
        return;
    }
    Map<ASTEntry, DataAndImageTreeNode<Object>> entryToTreeNode = new HashMap<ASTEntry, DataAndImageTreeNode<Object>>();
    // Step 1: create 'regular' tree structure from the nodes.
    DataAndImageTreeNode<Object> root = new DataAndImageTreeNode<Object>(null, null, null);
    for (Iterator<ASTEntry> it = visitor.getOutline(); it.hasNext(); ) {
        ASTEntry next = it.next();
        DataAndImageTreeNode<Object> n;
        if (next.parent != null) {
            DataAndImageTreeNode<Object> parent = entryToTreeNode.get(next.parent);
            if (parent == null) {
                Log.log("Unexpected condition: child found before parent!");
                parent = root;
            }
            n = new DataAndImageTreeNode<Object>(parent, new OutlineEntry(next), null);
        } else {
            n = new DataAndImageTreeNode<Object>(root, new OutlineEntry(next), null);
        }
        if (((OutlineEntry) n.data).node.beginLine <= startLineIndex) {
            initialSelection = n;
        }
        entryToTreeNode.put(next, n);
    }
    this.root = root;
}
Also used : HashMap(java.util.HashMap) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode)

Example 4 with DefinitionsASTIteratorVisitor

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

the class HierarchyViewer method onClick.

private void onClick(final HierarchyNodeModel model, int clickCount) {
    if (clickCount == 2) {
        if (model != null) {
            IModule m = model.module;
            if (m != null && model.ast != null) {
                ItemPointer pointer = new ItemPointer(m.getFile(), model.ast.name);
                new PyOpenAction().run(pointer);
            }
        }
    } else {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                synchronized (lock) {
                    if (treeMembers.getItemCount() > 0) {
                        treeMembers.removeAll();
                    }
                    if (model == null) {
                        return;
                    }
                    ClassDef ast = model.ast;
                    if (ast != null && treeMembers != null) {
                        DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.create(ast);
                        Iterator<ASTEntry> outline = visitor.getOutline();
                        HashMap<SimpleNode, TreeItem> c = new HashMap<SimpleNode, TreeItem>();
                        boolean first = true;
                        while (outline.hasNext()) {
                            ASTEntry entry = outline.next();
                            if (first) {
                                // Don't add the class itself.
                                first = false;
                                continue;
                            }
                            TreeItem item = null;
                            if (entry.node instanceof FunctionDef) {
                                item = createTreeItem(c, entry);
                                item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.METHOD_ICON)));
                                if (model.module != null) {
                                    item.setData(new ItemPointer(model.module.getFile(), ((FunctionDef) entry.node).name));
                                }
                            } else if (entry.node instanceof ClassDef) {
                                item = createTreeItem(c, entry);
                                item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.CLASS_ICON)));
                                if (model.module != null) {
                                    item.setData(new ItemPointer(model.module.getFile(), ((ClassDef) entry.node).name));
                                }
                            } else {
                                item = createTreeItem(c, entry);
                                item.setImage(ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PUBLIC_ATTR_ICON)));
                                if (model.module != null) {
                                    item.setData(new ItemPointer(model.module.getFile(), entry.node));
                                }
                            }
                            item.setText(entry.getName());
                            item.setExpanded(true);
                        }
                    }
                }
            }

            private TreeItem createTreeItem(HashMap<SimpleNode, TreeItem> c, ASTEntry entry) {
                TreeItem parent = null;
                ASTEntry par = entry.parent;
                if (par != null) {
                    parent = c.get(par.node);
                }
                TreeItem item;
                if (parent == null) {
                    item = new TreeItem(treeMembers, 0);
                } else {
                    item = new TreeItem(parent, 0);
                }
                c.put(entry.node, item);
                return item;
            }
        };
        Display.getDefault().asyncExec(r);
    }
}
Also used : IModule(org.python.pydev.core.IModule) TreeItem(org.eclipse.swt.widgets.TreeItem) HashMap(java.util.HashMap) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 5 with DefinitionsASTIteratorVisitor

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

the class PyOutlineSelectionDialog method addMethods.

private void addMethods(DataAndImageTreeNode<Object> nextEntry, HierarchyNodeModel model) {
    if (model == null || model.parents == null) {
        return;
    }
    for (HierarchyNodeModel parent : model.parents) {
        DefinitionsASTIteratorVisitor visitor = DefinitionsASTIteratorVisitor.createForChildren(parent.ast);
        if (visitor == null) {
            continue;
        }
        Iterator<ASTEntry> outline = visitor.getOutline();
        while (outline.hasNext()) {
            ASTEntry entry = outline.next();
            if (entry.parent == null) {
                // only direct children...
                new DataAndImageTreeNode<Object>(nextEntry, new OutlineEntry(entry, parent), null);
            }
        }
        addMethods(nextEntry, parent);
    }
}
Also used : ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) HierarchyNodeModel(org.python.pydev.ast.refactoring.HierarchyNodeModel)

Aggregations

ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)5 DefinitionsASTIteratorVisitor (org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor)5 HashMap (java.util.HashMap)2 SimpleNode (org.python.pydev.parser.jython.SimpleNode)2 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)2 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)2 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 TreeItem (org.eclipse.swt.widgets.TreeItem)1 ItemPointer (org.python.pydev.ast.item_pointer.ItemPointer)1 HierarchyNodeModel (org.python.pydev.ast.refactoring.HierarchyNodeModel)1 IInfo (org.python.pydev.core.IInfo)1 IModule (org.python.pydev.core.IModule)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 PyOpenAction (org.python.pydev.editor.actions.PyOpenAction)1 NameTok (org.python.pydev.parser.jython.ast.NameTok)1 FastStack (org.python.pydev.shared_core.structure.FastStack)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1