Search in sources :

Example 1 with FastStack

use of org.python.pydev.shared_core.structure.FastStack 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 FastStack

use of org.python.pydev.shared_core.structure.FastStack in project Pydev by fabioz.

the class PyAstFactory method createOverrideBody.

/**
 * @param functionDef the function for the override body
 * @param currentClassName
 */
public stmtType createOverrideBody(FunctionDef functionDef, String parentClassName, String currentClassName) {
    // create a copy because we do not want to retain the original line/col and we may change the originals here.
    final boolean[] addReturn = new boolean[] { false };
    VisitorBase visitor = new VisitorBase() {

        @Override
        public Object visitClassDef(ClassDef node) throws Exception {
            return null;
        }

        @Override
        public Object visitFunctionDef(FunctionDef node) throws Exception {
            // don't visit internal scopes.
            return null;
        }

        @Override
        protected Object unhandled_node(SimpleNode node) throws Exception {
            if (node instanceof Return) {
                addReturn[0] = true;
                throw stopVisitingException;
            }
            return null;
        }

        @Override
        public void traverse(SimpleNode node) throws Exception {
            node.traverse(this);
        }
    };
    try {
        visitor.traverse(functionDef);
    } catch (Exception e) {
        if (e != stopVisitingException) {
            Log.log(e);
        }
    }
    boolean isClassMethod = false;
    if (functionDef.decs != null) {
        for (decoratorsType dec : functionDef.decs) {
            String rep = NodeUtils.getRepresentationString(dec.func);
            if ("classmethod".equals(rep)) {
                isClassMethod = true;
                break;
            }
        }
    }
    argumentsType args = functionDef.args.createCopy(false);
    List<exprType> params = new ArrayList<exprType>();
    for (exprType expr : args.args) {
        // note: self should be there already!
        params.add(expr);
    }
    exprType starargs = args.vararg != null ? new Name(((NameTok) args.vararg).id, Name.Load, false) : null;
    exprType kwargs = args.kwarg != null ? new Name(((NameTok) args.kwarg).id, Name.Load, false) : null;
    List<keywordType> keywords = new ArrayList<keywordType>();
    if (args.defaults != null) {
        int diff = args.args.length - args.defaults.length;
        FastStack<Integer> removePositions = new FastStack<Integer>(args.defaults.length);
        for (int i = 0; i < args.defaults.length; i++) {
            exprType expr = args.defaults[i];
            if (expr != null) {
                exprType name = params.get(i + diff);
                if (name instanceof Name) {
                    // it's removed backwards, that's why it's a stack
                    removePositions.push(i + diff);
                    keywords.add(new keywordType(new NameTok(((Name) name).id, NameTok.KeywordName), name, false));
                } else {
                    Log.log("Expected: " + name + " to be a Name instance.");
                }
            }
        }
        while (removePositions.size() > 0) {
            Integer pop = removePositions.pop();
            params.remove((int) pop);
        }
    }
    Call call;
    if (isClassMethod && params.size() > 0) {
        // We need to use the super() construct
        // Something as:
        // Expr[value=
        // Call[func=
        // Attribute[value=
        // Call[func=Name[id=super, ctx=Load, reserved=false], args=[Name[id=Current, ctx=Load, reserved=false], Name[id=cls, ctx=Load, reserved=false]], keywords=[], starargs=null, kwargs=null],
        // attr=NameTok[id=test, ctx=Attrib], ctx=Load],
        // args=[], keywords=[], starargs=null, kwargs=null]
        // ]
        exprType firstParam = params.remove(0);
        Call innerCall = createCall("super", currentClassName, NodeUtils.getRepresentationString(firstParam));
        Attribute attr = new Attribute(innerCall, new NameTok(NodeUtils.getRepresentationString(functionDef), NameTok.Attrib), Attribute.Load);
        call = new Call(attr, params.toArray(new Name[params.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
    } else {
        call = createCall(parentClassName + "." + NodeUtils.getRepresentationString(functionDef), params, keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
    }
    if (addReturn[0]) {
        return new Return(call);
    } else {
        return new Expr(call);
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) FastStack(org.python.pydev.shared_core.structure.FastStack) Attribute(org.python.pydev.parser.jython.ast.Attribute) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) VisitorBase(org.python.pydev.parser.jython.ast.VisitorBase) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Expr(org.python.pydev.parser.jython.ast.Expr) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with FastStack

use of org.python.pydev.shared_core.structure.FastStack in project Pydev by fabioz.

the class FastParser method parse.

private List<stmtType> parse() {
    List<stmtType> body = new ArrayList<stmtType>();
    FastStack<stmtType> stack = new FastStack<>(5);
    Map<Integer, List<stmtType>> objectIdToBody = new HashMap<>();
    PySelection ps = new PySelection(doc);
    DocIterator it = new DocIterator(forward, ps, currentLine, false);
    Matcher functionMatcher = FUNCTION_PATTERN.matcher("");
    List<Matcher> cythonMatchers = null;
    if (this.cythonParse) {
        cythonMatchers = new ArrayList<Matcher>();
        cythonMatchers.add(FUNCTION_PATTERN_CYTHON.matcher(""));
        cythonMatchers.add(FUNCTION_PATTERN_CYTHON2.matcher(""));
    }
    Matcher classMatcher = CLASS_PATTERN.matcher("");
    while (it.hasNext()) {
        Matcher functionFound = null;
        String line = it.next();
        // we don't care about empty lines
        if (line.trim().length() == 0) {
            continue;
        }
        if (findGloballyAccessiblePath) {
            int currentFirstCharCol = PySelection.getFirstCharPosition(line);
            if (firstCharCol == -1) {
                firstCharCol = currentFirstCharCol;
            } else {
                // to the global scope.
                if (firstCharCol <= currentFirstCharCol) {
                    // don't check this line as it's not valid in the current context.
                    continue;
                }
            }
        }
        functionMatcher.reset(line);
        if (functionMatcher.find()) {
            functionFound = functionMatcher;
        } else if (cythonMatchers != null) {
            for (Matcher matcher : cythonMatchers) {
                matcher.reset(line);
                if (matcher.find()) {
                    functionFound = matcher;
                    break;
                }
            }
        }
        if (functionFound != null) {
            int lastReturnedLine = it.getLastReturnedLine();
            NameTok nameTok = createNameTok(functionFound, lastReturnedLine, NameTok.FunctionName, ps);
            if (nameTok != null) {
                FunctionDef functionDef = createFunctionDef(lastReturnedLine, nameTok, PySelection.getFirstCharPosition(line));
                if (!addStatement(body, stack, objectIdToBody, functionDef)) {
                    return body;
                }
                if (stopOnFirstMatch) {
                    return body;
                }
            }
            continue;
        }
        classMatcher.reset(line);
        if (classMatcher.find()) {
            int lastReturnedLine = it.getLastReturnedLine();
            NameTok nameTok = createNameTok(classMatcher, lastReturnedLine, NameTok.ClassName, ps);
            if (nameTok != null) {
                ClassDef classDef = createClassDef(lastReturnedLine, nameTok, PySelection.getFirstCharPosition(line));
                if (!addStatement(body, stack, objectIdToBody, classDef)) {
                    return body;
                }
                if (stopOnFirstMatch) {
                    return body;
                }
            }
            continue;
        }
    }
    if (cythonParse) {
        for (stmtType t : body) {
            buildBody(t, objectIdToBody);
        }
    }
    return body;
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) DocIterator(org.python.pydev.shared_core.string.DocIterator) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ArrayList(java.util.ArrayList) List(java.util.List) PySelection(org.python.pydev.core.docutils.PySelection) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 4 with FastStack

use of org.python.pydev.shared_core.structure.FastStack in project Pydev by fabioz.

the class CreateLocalVariableEdit method calculateLineForLocal.

private int calculateLineForLocal() {
    if (lineForLocal == -1) {
        ICoreTextSelection userSelection = info.getUserSelection();
        if (replaceDuplicates) {
            // replace (so that the local created works for all the replaces).
            for (Tuple<ICoreTextSelection, SimpleNode> dup : duplicates) {
                if (dup.o1.getStartLine() < userSelection.getStartLine()) {
                    userSelection = dup.o1;
                }
            }
        }
        PySelection selection = new PySelection(info.getDocument(), userSelection);
        int startLineIndexIndocCoords = selection.getStartLineIndex();
        // from doc to ast
        int startLineIndexInASTCoords = startLineIndexIndocCoords + 1;
        Module module = info.getModuleAdapter().getASTNode();
        SimpleNode currentScope = module;
        try {
            FindScopeVisitor scopeVisitor = new FindScopeVisitor(startLineIndexInASTCoords, selection.getCursorColumn() + 1, info.getNature());
            module.accept(scopeVisitor);
            ILocalScope scope = scopeVisitor.scope;
            FastStack scopeStack = scope.getScopeStack();
            // at least the module should be there if we don't have anything.
            currentScope = (SimpleNode) scopeStack.peek();
        } catch (Exception e1) {
            Log.log(e1);
        }
        GetNodeForExtractLocalVisitor visitor = new GetNodeForExtractLocalVisitor(startLineIndexInASTCoords);
        try {
            currentScope.accept(visitor);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        SimpleNode lastNodeBeforePassedLine = visitor.getLastInContextBeforePassedLine();
        if (lastNodeBeforePassedLine != null) {
            final int[] line = new int[] { Integer.MAX_VALUE };
            try {
                Visitor v = new Visitor() {

                    @Override
                    protected Object unhandled_node(SimpleNode node) throws Exception {
                        if (node.beginLine > 0) {
                            line[0] = Math.min(line[0], node.beginLine - 1);
                        } else {
                            Log.log("Found node with beginLine <= 0:" + node + " line: " + node.beginLine);
                        }
                        return this;
                    }
                };
                lastNodeBeforePassedLine.accept(v);
            } catch (Exception e) {
                Log.log(e);
            }
            if (line[0] != Integer.MAX_VALUE) {
                lineForLocal = line[0];
            } else {
                lineForLocal = lastNodeBeforePassedLine.beginLine - 1;
            }
        } else {
            lineForLocal = startLineIndexIndocCoords;
        }
        // location!
        if (lineForLocal > startLineIndexIndocCoords) {
            lineForLocal = startLineIndexIndocCoords;
        }
    }
    return lineForLocal;
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) FindScopeVisitor(org.python.pydev.ast.codecompletion.revisited.visitors.FindScopeVisitor) FindScopeVisitor(org.python.pydev.ast.codecompletion.revisited.visitors.FindScopeVisitor) Visitor(org.python.pydev.parser.jython.Visitor) GetNodeForExtractLocalVisitor(org.python.pydev.parser.visitors.scope.GetNodeForExtractLocalVisitor) ICoreTextSelection(org.python.pydev.shared_core.string.ICoreTextSelection) SimpleNode(org.python.pydev.parser.jython.SimpleNode) PySelection(org.python.pydev.core.docutils.PySelection) Module(org.python.pydev.parser.jython.ast.Module) GetNodeForExtractLocalVisitor(org.python.pydev.parser.visitors.scope.GetNodeForExtractLocalVisitor) ILocalScope(org.python.pydev.core.ILocalScope)

Example 5 with FastStack

use of org.python.pydev.shared_core.structure.FastStack in project Pydev by fabioz.

the class AbstractASTManager method getCompletionsUnpackingAST.

private TokensList getCompletionsUnpackingAST(IDefinition definition, ICompletionState state, UnpackInfo unpackPos) throws CompletionRecursionException {
    if (definition instanceof Definition) {
        Definition definition2 = (Definition) definition;
        SimpleNode ast = definition2.ast;
        if (ast instanceof Name) {
            Name name = (Name) ast;
            // Found it as a parameter.
            if (name.ctx == Name.Param) {
                if (definition2.scope != null) {
                    FastStack scopeStack = definition2.scope.getScopeStack();
                    if (scopeStack.size() > 0) {
                        Object peek = scopeStack.peek();
                        if (peek instanceof FunctionDef) {
                            String typeForParameterFromDocstring = NodeUtils.getTypeForParameterFromDocstring(name.id, (FunctionDef) peek);
                            if (typeForParameterFromDocstring != null) {
                                String unpackedTypeFromDocstring = NodeUtils.getUnpackedTypeFromTypeDocstring(typeForParameterFromDocstring, unpackPos);
                                if (unpackedTypeFromDocstring != null) {
                                    ICompletionState copyWithActTok = state.getCopyWithActTok(unpackedTypeFromDocstring);
                                    copyWithActTok.setLookingFor(ICompletionState.LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
                                    TokensList completionsForModule = getCompletionsForModule(((Definition) definition).module, copyWithActTok);
                                    if (completionsForModule.size() > 0) {
                                        return completionsForModule;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return getCompletionsUnpackingAST(definition2.ast, definition2.module, state, unpackPos);
    }
    return null;
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) ICompletionState(org.python.pydev.core.ICompletionState) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) TokensList(org.python.pydev.core.TokensList) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) Name(org.python.pydev.parser.jython.ast.Name)

Aggregations

FastStack (org.python.pydev.shared_core.structure.FastStack)8 SimpleNode (org.python.pydev.parser.jython.SimpleNode)6 ArrayList (java.util.ArrayList)4 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)4 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)4 AssignDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition)3 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)3 FindScopeVisitor (org.python.pydev.ast.codecompletion.revisited.visitors.FindScopeVisitor)3 IDefinition (org.python.pydev.core.IDefinition)3 ILocalScope (org.python.pydev.core.ILocalScope)3 TokensList (org.python.pydev.core.TokensList)3 List (java.util.List)2 LocalScope (org.python.pydev.ast.codecompletion.revisited.visitors.LocalScope)2 TypeInfoDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.TypeInfoDefinition)2 ICompletionState (org.python.pydev.core.ICompletionState)2 IModule (org.python.pydev.core.IModule)2 ISourceModule (org.python.pydev.core.ISourceModule)2 IToken (org.python.pydev.core.IToken)2 IterTokenEntry (org.python.pydev.core.IterTokenEntry)2 PySelection (org.python.pydev.core.docutils.PySelection)2