Search in sources :

Example 6 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class PyLinkedModeCompletionProposal method getAdditionalProposalInfo.

@Override
public String getAdditionalProposalInfo() {
    if (computedInfo != null) {
        return computedInfo;
    }
    if (element != null) {
        if (element instanceof SourceToken) {
            SourceToken sourceToken = (SourceToken) element;
            SimpleNode ast = sourceToken.getAst();
            if (ast != null && (ast instanceof FunctionDef || ast instanceof ClassDef)) {
                computedInfo = addTipForApplyWithModifiers(NodeUtils.printAst(null, ast));
            }
            if (computedInfo != null) {
                return computedInfo;
            }
        }
        computedInfo = addTipForApplyWithModifiers(element.getDocStr());
        return computedInfo;
    } else {
        return addTipForApplyWithModifiers(super.getAdditionalProposalInfo());
    }
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 7 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method visitGlobal.

@Override
public Object visitGlobal(Global node) throws Exception {
    unhandled_node(node);
    for (NameTokType name : node.names) {
        Name nameAst = new Name(((NameTok) name).id, Name.Store, false);
        nameAst.beginLine = name.beginLine;
        nameAst.beginColumn = name.beginColumn;
        SourceToken token = AbstractVisitor.makeToken(nameAst, moduleName, nature);
        scope.addTokenToGlobalScope(token);
        // it is global, so, ignore it...
        addToNamesToIgnore(nameAst, false, true);
    }
    return null;
}
Also used : NameTokType(org.python.pydev.parser.jython.ast.NameTokType) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) Name(org.python.pydev.parser.jython.ast.Name)

Example 8 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method visitNameTok.

/**
 * We want to make the name tok a regular name for interpreting purposes.
 */
@Override
public Object visitNameTok(NameTok nameTok) throws Exception {
    unhandled_node(nameTok);
    if (nameTok.ctx == NameTok.VarArg || nameTok.ctx == NameTok.KwArg || nameTok.ctx == NameTok.PatternName) {
        SourceToken token = AbstractVisitor.makeToken(nameTok, moduleName, nature);
        scope.addToken(token, token, (nameTok).id);
        if (checkCurrentScopeForAssignmentsToBuiltins()) {
            if (builtinTokens.contains(token.getRepresentation())) {
                // Overriding builtin...
                onAddAssignmentToBuiltinMessage(token, token.getRepresentation());
            }
        }
    }
    return null;
}
Also used : SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 9 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class ScopeAnalyzerVisitor method onGetCompleteTokenOccurrences.

/**
 * Called for each found occurrence in the complete token occurrences
 *
 * Is used to add other returns to ret
 */
@Override
protected void onGetCompleteTokenOccurrences(Tuple3<Found, Integer, ASTEntry> found, Set<IToken> f, ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> ret) {
    // other matches for the imports that we had already found.
    Tuple<List<Tuple4<IToken, Integer, ASTEntry, Found>>, List<Tuple4<IToken, Integer, ASTEntry, Found>>> matchingImportEntries = getImportEntries(found, f);
    List<Tuple4<IToken, Integer, ASTEntry, Found>> fromModule = matchingImportEntries.o1;
    List<Tuple4<IToken, Integer, ASTEntry, Found>> fromImports = matchingImportEntries.o2;
    ret.addAll(fromModule);
    ret.addAll(fromImports);
    // import is different from the context of that import)
    for (Tuple4<IToken, Integer, ASTEntry, Found> tuple3 : fromImports) {
        try {
            if (!(tuple3.o1 instanceof SourceToken)) {
                continue;
            }
            SourceToken tok = (SourceToken) tuple3.o1;
            SimpleNode ast = tok.getAst();
            int line = 0;
            int col = 0;
            if (!(ast instanceof Import)) {
                continue;
            }
            Import import1 = (Import) ast;
            line = import1.names[0].beginLine - 1;
            col = import1.names[0].beginColumn - 1;
            PySelection ps = new PySelection(this.document, line, col);
            ScopeAnalyzerVisitorWithoutImports analyzerVisitorWithoutImports = new ScopeAnalyzerVisitorWithoutImports(this.nature, this.moduleName, this.current, this.monitor, ps);
            SourceModule s = (SourceModule) this.current;
            s.getAst().accept(analyzerVisitorWithoutImports);
            analyzerVisitorWithoutImports.checkFinished();
            // now, let's get the token occurrences for the analyzer that worked without gathering the imports
            ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> completeTokenOccurrences = analyzerVisitorWithoutImports.getCompleteTokenOccurrences();
            for (Tuple4<IToken, Integer, ASTEntry, Found> oc : completeTokenOccurrences) {
                if (!f.contains(oc.o1) && !oc.o1.isImport()) {
                    // the import should be already added
                    if (oc.o2 < tuple3.o2) {
                        oc.o2 = tuple3.o2;
                    }
                    f.add(oc.o1);
                    ret.add(oc);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) Import(org.python.pydev.parser.jython.ast.Import) Found(com.python.pydev.analysis.visitors.Found) BadLocationException(org.eclipse.jface.text.BadLocationException) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Tuple4(org.python.pydev.shared_core.structure.Tuple4) IToken(org.python.pydev.core.IToken) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ArrayList(java.util.ArrayList) List(java.util.List) PySelection(org.python.pydev.core.docutils.PySelection) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 10 with SourceToken

use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.

the class ScopeAnalyzerVisitorWithoutImports method getEntryOccurrences.

/**
 * We get the occurrences as tokens for the name we're looking for. Note that the complete name (may be a dotted name)
 * we're looking for may not be equal to the 'partial' name.
 *
 * This can happen when we're looking for some import such as os.path, and are looking just for the 'path' part.
 * So, when this happens, the return is analyzed and only returns names as the one we're looking for (with
 * the correct line and col positions).
 */
public List<ASTEntry> getEntryOccurrences() {
    checkFinished();
    Set<Tuple3<String, Integer, Integer>> s = new HashSet<Tuple3<String, Integer, Integer>>();
    ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> complete = getCompleteTokenOccurrences();
    ArrayList<ASTEntry> ret = new ArrayList<ASTEntry>();
    for (Tuple4<IToken, Integer, ASTEntry, Found> tup : complete) {
        IToken token = tup.o1;
        if (!(token instanceof SourceToken)) {
            // we want only the source tokens for this module
            continue;
        }
        // if it is different, we have to make partial names
        SourceToken sourceToken = (SourceToken) tup.o1;
        SimpleNode ast = (sourceToken).getAst();
        String representation = null;
        if (ast instanceof ImportFrom) {
            ImportFrom f = (ImportFrom) ast;
            // f.names may be empty if it is a wild import
            for (aliasType t : f.names) {
                NameTok importName = NodeUtils.getNameForAlias(t);
                String importRep = NodeUtils.getFullRepresentationString(importName);
                if (importRep.equals(nameToFind)) {
                    ast = importName;
                    representation = importRep;
                    break;
                }
            }
        } else if (ast instanceof Import) {
            representation = NodeUtils.getFullRepresentationString(ast);
            Import f = (Import) ast;
            NameTok importName = NodeUtils.getNameForRep(f.names, representation);
            if (importName != null) {
                ast = importName;
            }
        } else {
            representation = NodeUtils.getFullRepresentationString(ast);
        }
        if (representation == null) {
        // do nothing
        // can happen on wild imports
        } else if (nameToFind.equals(representation)) {
            if (ast instanceof Attribute) {
                // it can happen, as we won't go up to the part of the actual call (if there's one).
                ast = NodeUtils.getAttributeParts((Attribute) ast).get(0);
                ASTEntry entry = new ASTEntry(tup.o3, ast);
                entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
                ret.add(entry);
            } else {
                ASTEntry entry = new ASTEntry(tup.o3, ast);
                entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
                ret.add(entry);
            }
        } else if (FullRepIterable.containsPart(representation, nameToFind)) {
            Name nameAst = new Name(nameToFind, Name.Store, false);
            List<String> strings = StringUtils.dotSplit(representation);
            int plus = 0;
            for (String string : strings) {
                if (string.equals(nameToFind) && (plus + nameToFind.length() >= tup.o2)) {
                    break;
                }
                // len + dot
                plus += string.length() + 1;
            }
            nameAst.beginColumn = AbstractMessage.getStartCol(token, document) + plus;
            nameAst.beginLine = AbstractMessage.getStartLine(token, document);
            Tuple3<String, Integer, Integer> t = new Tuple3<String, Integer, Integer>(nameToFind, nameAst.beginColumn, nameAst.beginLine);
            if (!s.contains(t)) {
                s.add(t);
                ASTEntry entry = new ASTEntry(tup.o3, nameAst);
                entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
                ret.add(entry);
            }
        }
    }
    return ret;
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) Attribute(org.python.pydev.parser.jython.ast.Attribute) ArrayList(java.util.ArrayList) Found(com.python.pydev.analysis.visitors.Found) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Tuple4(org.python.pydev.shared_core.structure.Tuple4) IToken(org.python.pydev.core.IToken) Tuple3(org.python.pydev.shared_core.structure.Tuple3) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok) HashSet(java.util.HashSet)

Aggregations

SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)50 SimpleNode (org.python.pydev.parser.jython.SimpleNode)26 IToken (org.python.pydev.core.IToken)25 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)13 TokensList (org.python.pydev.core.TokensList)12 ArrayList (java.util.ArrayList)11 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)11 IModule (org.python.pydev.core.IModule)9 NameTok (org.python.pydev.parser.jython.ast.NameTok)9 ICompletionState (org.python.pydev.core.ICompletionState)8 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)8 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)8 Import (org.python.pydev.parser.jython.ast.Import)7 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)7 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)6 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)6 HashSet (java.util.HashSet)5 Attribute (org.python.pydev.parser.jython.ast.Attribute)5 Found (com.python.pydev.analysis.visitors.Found)4