Search in sources :

Example 1 with IToken

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

the class AnalysisPlugin method getDefinitionFromIInfo.

/**
 * @param pointers the list where the pointers will be added (if null, a new one will be created).
 * @param manager the manager to be used to get the definition.
 * @param nature the nature to be used.
 * @param info the info that we are looking for.
 * @param force whether we should force getting the ItemPointer if it's not readily available.
 * @return whether we actually tried to look for a completion or just bailed out due to force being == false.
 */
public static boolean getDefinitionFromIInfo(List<ItemPointer> pointers, ICodeCompletionASTManager manager, IPythonNature nature, IInfo info, ICompletionState completionCache, boolean requireIDefinition, boolean force) {
    if (pointers == null) {
        pointers = new ArrayList<>();
    }
    if (!requireIDefinition) {
        String file = info.getFile();
        if (file != null) {
            File f = new File(file);
            int line = info.getLine();
            int col = info.getCol();
            if (line > 0 && col > 0) {
                // 0 is invalid.
                ItemPointer itemPointer = new ItemPointer(f, new Location(line - 1, col - 1), new Location(line - 1, col - 1), null, null, f.toURI());
                pointers.add(itemPointer);
                return true;
            }
        }
    }
    if (!force) {
        return false;
    }
    IModule mod;
    String tok;
    mod = manager.getModule(info.getDeclaringModuleName(), nature, true, completionCache);
    if (mod != null) {
        if (info.getType() == IInfo.MOD_IMPORT_TYPE) {
            Definition definition = new Definition(1, 1, "", null, null, mod);
            PyRefactoringFindDefinition.getAsPointers(pointers, new Definition[] { definition });
            return true;
        }
        // ok, now that we found the module, we have to get the actual definition
        tok = "";
        String path = info.getPath();
        if (path != null && path.length() > 0) {
            tok = path + ".";
        }
        tok += info.getName();
        try {
            IDefinition[] definitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(tok, nature, completionCache), -1, -1, nature);
            if ((definitions == null || definitions.length == 0) && path != null && path.length() > 0) {
                // this can happen if we have something as an attribute in the path:
                // class Bar(object):
                // def __init__(self):
                // self.xxx = 10
                // 
                // so, we'de get a find definition for Bar.__init__.xxx which is something we won't find
                // for now, let's simply return a match in the correct context (although the correct way of doing
                // it would be analyzing that context to find the match)
                IDefinition[] contextDefinitions = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(path, nature, completionCache), -1, -1, nature);
                if (contextDefinitions != null && contextDefinitions.length > 0) {
                    for (IDefinition iDefinition : contextDefinitions) {
                        if (iDefinition instanceof Definition) {
                            Definition definition = (Definition) iDefinition;
                            if (definition.ast instanceof FunctionDef) {
                                FunctionDef functionDef = (FunctionDef) definition.ast;
                                if (functionDef.args != null) {
                                    exprType[] args = functionDef.args.args;
                                    if (args != null && args.length > 0) {
                                        // I.e.: only analyze functions with at least one argument (for self or cls).
                                        Map<String, SourceToken> repToTokenWithArgs = new HashMap<String, SourceToken>();
                                        HeuristicFindAttrs heuristicFindAttrs = new HeuristicFindAttrs(HeuristicFindAttrs.WHITIN_ANY, HeuristicFindAttrs.IN_ASSIGN, "", definition.module.getName(), null, repToTokenWithArgs, nature);
                                        heuristicFindAttrs.visitFunctionDef(functionDef);
                                        List<IToken> tokens = heuristicFindAttrs.getTokens();
                                        List<IDefinition> newDefs = new ArrayList<>();
                                        for (IToken iToken : tokens) {
                                            if (info.getName().equals(iToken.getRepresentation())) {
                                                newDefs.add(new Definition(iToken, definition.scope, definition.module));
                                            }
                                        }
                                        definitions = newDefs.toArray(new IDefinition[newDefs.size()]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            PyRefactoringFindDefinition.getAsPointers(pointers, definitions);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) IModule(org.python.pydev.core.IModule) HashMap(java.util.HashMap) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition) IToken(org.python.pydev.core.IToken) File(java.io.File) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer) Location(org.python.pydev.shared_core.structure.Location) HeuristicFindAttrs(org.python.pydev.ast.codecompletion.revisited.visitors.HeuristicFindAttrs)

Example 2 with IToken

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

the class AbstractAdditionalDependencyInfo method addAstForCompiledModule.

private void addAstForCompiledModule(IModule module, InterpreterInfo info, ModulesKey newKey, boolean removeFirst) {
    TokensList globalTokens = module.getGlobalTokens();
    PyAstFactory astFactory = new PyAstFactory(new AdapterPrefs("\n", info.getModulesManager().getNature()));
    List<stmtType> body = new ArrayList<>(globalTokens.size());
    for (IterTokenEntry entry : globalTokens) {
        IToken token = entry.getToken();
        switch(token.getType()) {
            case IToken.TYPE_CLASS:
                body.add(astFactory.createClassDef(token.getRepresentation()));
                break;
            case IToken.TYPE_FUNCTION:
                body.add(astFactory.createFunctionDef(token.getRepresentation()));
                break;
            default:
                Name attr = astFactory.createName(token.getRepresentation());
                // assign to itself just for generation purposes.
                body.add(astFactory.createAssign(attr, attr));
                break;
        }
    }
    // System.out.println("Creating info for: " + module.getName());
    if (removeFirst) {
        removeInfoFromModule(newKey.name, false);
    }
    addAstInfo(astFactory.createModule(body), newKey, false);
}
Also used : IToken(org.python.pydev.core.IToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) AdapterPrefs(org.python.pydev.parser.jython.ast.factory.AdapterPrefs) IterTokenEntry(org.python.pydev.core.IterTokenEntry) PyAstFactory(org.python.pydev.parser.jython.ast.factory.PyAstFactory) TokensList(org.python.pydev.core.TokensList) Name(org.python.pydev.parser.jython.ast.Name)

Example 3 with IToken

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

the class TddCodeGenerationQuickFixParticipant method checkCreationBasedOnFoundPointers.

public boolean checkCreationBasedOnFoundPointers(PyEdit edit, PySelection callPs, List<ICompletionProposalHandle> ret, TddPossibleMatches possibleMatch, ItemPointer[] pointers, String methodToCreate, PySelection newSelection, IPythonNature nature) throws MisconfigurationException, Exception {
    CompletionCache completionCache = new CompletionCache();
    for (ItemPointer pointer : pointers) {
        Definition definition = pointer.definition;
        try {
            definition = rebaseToClassDefDefinition(nature, completionCache, definition, null);
        } catch (CompletionRecursionException e) {
            // Just keep going.
            Log.log(e);
        }
        if (definition.ast instanceof ClassDef) {
            ClassDef d = (ClassDef) definition.ast;
            String fullName = NodeUtils.getRepresentationString(d) + "." + methodToCreate;
            IToken repInModule = nature.getAstManager().getRepInModule(definition.module, fullName, nature);
            if (repInModule != null) {
                // System.out.println("Skipping creation of: " + fullName); //We found it, so, don't suggest it.
                continue;
            }
            for (Boolean isCall : new Boolean[] { true, false }) {
                // Give the user a chance to create the method we didn't find.
                PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                List<String> parametersAfterCall = null;
                parametersAfterCall = configCreateAsAndReturnParametersAfterCall(callPs, isCall, pyCreateMethod, parametersAfterCall, methodToCreate);
                String className = NodeUtils.getRepresentationString(d);
                pyCreateMethod.setCreateInClass(className);
                String displayString = StringUtils.format("Create %s %s at %s (%s)", methodToCreate, pyCreateMethod.getCreationStr(), className, definition.module.getName());
                TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule(methodToCreate, tddQuickFixParticipant != null ? tddQuickFixParticipant.imageMethod : null, displayString, null, displayString, IPyCompletionProposal.PRIORITY_CREATE, edit, definition.module.getFile(), parametersAfterCall, pyCreateMethod, newSelection);
                completion.locationStrategy = AbstractPyCreateAction.LOCATION_STRATEGY_END;
                ret.add(completion);
            }
            return true;
        }
    }
    return false;
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) ICompletionCache(org.python.pydev.core.ICompletionCache) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) IToken(org.python.pydev.core.IToken) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 4 with IToken

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

the class GenAndTok method toString.

@Override
public String toString() {
    FastStringBuffer buffer = new FastStringBuffer();
    buffer.append("GenAndTok [ ");
    buffer.append(generator.getRepresentation());
    buffer.append(" - ");
    buffer.append(tok.getRepresentation());
    buffer.append(" (scopeId:");
    buffer.append(scopeId);
    buffer.append(") ");
    if (references.size() > 0) {
        buffer.append(" (references:");
        for (IToken ref : references) {
            buffer.append(ref.getRepresentation());
            buffer.append(",");
        }
        // remove the last comma
        buffer.deleteLast();
        buffer.append(") ");
    }
    buffer.append("]");
    return buffer.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) IToken(org.python.pydev.core.IToken)

Example 5 with IToken

use of org.python.pydev.core.IToken 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)

Aggregations

IToken (org.python.pydev.core.IToken)120 TokensList (org.python.pydev.core.TokensList)48 IterTokenEntry (org.python.pydev.core.IterTokenEntry)35 Document (org.eclipse.jface.text.Document)34 ArrayList (java.util.ArrayList)29 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)25 IModule (org.python.pydev.core.IModule)20 SimpleNode (org.python.pydev.parser.jython.SimpleNode)20 ICompletionState (org.python.pydev.core.ICompletionState)16 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)15 HashSet (java.util.HashSet)11 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)11 List (java.util.List)10 Found (com.python.pydev.analysis.visitors.Found)9 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)9 File (java.io.File)8 HashMap (java.util.HashMap)8 NameTok (org.python.pydev.parser.jython.ast.NameTok)8 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)8