Search in sources :

Example 1 with AssignDefinition

use of org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition in project Pydev by fabioz.

the class AbstractScopeAnalyzerVisitor method internalGenerateIsDefinitionUnknown.

/**
 * Actually makes the check to see if a given representation is unknown in a given module (without using caches)
 */
private boolean internalGenerateIsDefinitionUnknown(IModule m, String repToCheck) throws Exception {
    if (!(m instanceof SourceModule)) {
        return false;
    }
    repToCheck = FullRepIterable.headAndTail(repToCheck, true)[0];
    if (repToCheck.length() == 0) {
        return false;
    }
    IDefinition[] definitions = m.findDefinition(CompletionStateFactory.getEmptyCompletionState(repToCheck, nature, this.completionCache), -1, -1, nature);
    for (int i = 0; i < definitions.length; i++) {
        IDefinition foundDefinition = definitions[i];
        if (foundDefinition instanceof AssignDefinition) {
            AssignDefinition d = (AssignDefinition) foundDefinition;
            // if the value is currently None, it will be set later on
            if (d.value.equals("None")) {
                return true;
            }
            // ok, go to the definition of whatever is set
            IDefinition[] definitions2 = d.module.findDefinition(CompletionStateFactory.getEmptyCompletionState(d.value, nature, this.completionCache), d.line, d.col, nature);
            if (definitions2.length == 1) {
                // out about its return value
                if (definitions2[0] instanceof Definition) {
                    Definition definition = (Definition) definitions2[0];
                    if (definition.ast instanceof FunctionDef) {
                        return true;
                    } else if (definition.ast instanceof ClassDef) {
                        ClassDef def = (ClassDef) definition.ast;
                        if (isDynamicClass(def)) {
                            return true;
                        }
                    }
                }
            }
        } else if (foundDefinition instanceof Definition) {
            // not Assign definition
            Definition definition = (Definition) foundDefinition;
            if (definition.ast instanceof ClassDef) {
                // direct class access
                ClassDef classDef = (ClassDef) definition.ast;
                if (isDynamicClass(classDef)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition)

Example 2 with AssignDefinition

use of org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition in project Pydev by fabioz.

the class AbstractASTManager method getCompletionsFromAssignDefinition.

public DefinitionAndCompletions getCompletionsFromAssignDefinition(IModule module, ICompletionState state, UnpackInfo unpackPos, AssignDefinition assignDefinition) throws CompletionRecursionException, Exception {
    exprType[] elts = null;
    if (assignDefinition.ast instanceof Assign) {
        Assign assign = (Assign) assignDefinition.ast;
        if (assign.type != null) {
            elts = NodeUtils.getEltsTypedAnnotation(assign.type);
        }
    }
    if (elts == null) {
        elts = NodeUtils.getEltsFromCompoundObject(assignDefinition.nodeValue);
    }
    if (elts != null) {
        // I.e.: something as [1,2,3, Call()]
        TokensList completionsFromUnpackedList = getCompletionsFromUnpackedCompoundObject(module, state, elts, unpackPos);
        if (completionsFromUnpackedList != null) {
            // Completions from that same definition
            return new DefinitionAndCompletions(new Definition[] { assignDefinition }, completionsFromUnpackedList);
        }
    } else {
        ArrayList<IDefinition> found = new ArrayList<>();
        // Pointing to some other place... let's follow it.
        PyRefactoringFindDefinition.findActualDefinition(null, state.getAcceptTypeshed(), assignDefinition.module, assignDefinition.value, found, assignDefinition.line, assignDefinition.col, state.getNature(), state);
        for (IDefinition f : found) {
            if (f instanceof Definition) {
                Definition definition = (Definition) f;
                if (definition.ast != null) {
                    // We're unpacking some class/method we found... something as:
                    // class SomeClass:
                    // def __iter__(self):
                    // x = SomeClass()
                    // for a in x:
                    // a.
                    TokensList ret = getCompletionsUnpackingAST(definition, state, unpackPos);
                    if (ret != null && ret.size() > 0) {
                        // Had to follow reference
                        return new DefinitionAndCompletions(new Definition[] { definition }, ret);
                    }
                }
            }
        }
    }
    return null;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ArrayList(java.util.ArrayList) 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) Assign(org.python.pydev.parser.jython.ast.Assign) TokensList(org.python.pydev.core.TokensList) IDefinition(org.python.pydev.core.IDefinition)

Example 3 with AssignDefinition

use of org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition in project Pydev by fabioz.

the class AssignAnalysis method getAssignCompletions.

/**
 * If we got here, either there really is no definition from the token
 * or it is not looking for a definition. This means that probably
 * it is something like.
 *
 * It also can happen in many scopes, so, first we have to check the current
 * scope and then pass to higher scopes
 *
 * e.g. foo = Foo()
 *      foo. | Ctrl+Space
 *
 * so, first thing is discovering in which scope we are (Storing previous scopes so
 * that we can search in other scopes as well).
 * @param localScope
 */
public DefinitionAndCompletions getAssignCompletions(ICodeCompletionASTManager manager, IModule module, ICompletionState state, ILocalScope localScope) {
    state.pushAssign();
    try {
        TokensList ret = new TokensList();
        Definition[] defs = new Definition[0];
        if (module instanceof SourceModule) {
            SourceModule sourceModule = (SourceModule) module;
            try {
                defs = sourceModule.findDefinition(state, state.getLine() + 1, state.getCol() + 1, state.getNature());
                if (defs.length > 0) {
                    for (int i = 0; i < defs.length; i++) {
                        // go through all definitions found and make a merge of it...
                        Definition definition = defs[i];
                        TokensList completionsFromDefinition = getCompletionsFromDefinition(definition, state, sourceModule, manager);
                        if (completionsFromDefinition != null && completionsFromDefinition.notEmpty()) {
                            if (definition instanceof AssignDefinition) {
                                AssignDefinition assignDefinition = (AssignDefinition) definition;
                                if (assignDefinition.nodeValue instanceof Call) {
                                    completionsFromDefinition.setLookingFor(LookingFor.LOOKING_FOR_INSTANCED_VARIABLE);
                                }
                            }
                            ret.addAll(completionsFromDefinition);
                        }
                    }
                } else {
                    if (localScope != null) {
                        TokensList tokens = searchInLocalTokens(manager, state, true, state.getLine() + 1, state.getCol() + 1, module, localScope, state.getActivationToken());
                        if (tokens != null) {
                            ret.addAll(tokens);
                        }
                    }
                }
            } catch (CompletionRecursionException e) {
            // thats ok
            } catch (Exception e) {
                Log.log(e);
                throw new RuntimeException("Error when getting assign completions for:" + module.getName(), e);
            } catch (Throwable t) {
                throw new RuntimeException("A throwable exception has been detected " + t.getClass());
            }
        }
        return new DefinitionAndCompletions(defs, ret);
    } finally {
        state.popAssign();
    }
}
Also used : AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) Call(org.python.pydev.parser.jython.ast.Call) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) TokensList(org.python.pydev.core.TokensList) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException)

Example 4 with AssignDefinition

use of org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition in project Pydev by fabioz.

the class FindDefinitionModelVisitorTest method testFind.

/**
 * @throws Exception
 */
public void testFind() throws Exception {
    String d = "" + "from testAssist import assist\n" + "ex = assist.ExistingClass()\n" + "ex.newMethod(c,d)";
    Document doc = new Document(d);
    IModule module = AbstractModule.createModuleFromDoc("", null, doc, nature, true);
    Definition[] defs = (Definition[]) module.findDefinition(CompletionStateFactory.getEmptyCompletionState("ex", nature, new CompletionCache()), 3, 3, nature);
    assertEquals(1, defs.length);
    assertEquals("ex", ((AssignDefinition) defs[0]).target);
    assertEquals("assist.ExistingClass", defs[0].value);
    assertSame(module, defs[0].module);
    defs = (Definition[]) module.findDefinition(CompletionStateFactory.getEmptyCompletionState("assist.ExistingClass", nature, new CompletionCache()), 2, 6, nature);
    assertEquals(1, defs.length);
    assertEquals("ExistingClass", defs[0].value);
    assertNotSame(module, defs[0].module);
    assertEquals("testAssist.assist", defs[0].module.getName());
}
Also used : IModule(org.python.pydev.core.IModule) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) Document(org.eclipse.jface.text.Document)

Example 5 with AssignDefinition

use of org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition in project Pydev by fabioz.

the class FindDefinitionModelVisitorTest method testFind2.

/**
 * @throws Exception
 */
public void testFind2() throws Exception {
    String d;
    d = "class C:            \n" + "    def met1(self): \n" + "        pass        \n" + "                    \n" + "class B:            \n" + "    def met2(self): \n" + "        self.c = C()\n" + "                    \n" + "    def met3(self): \n" + "        self.c.";
    Document doc = new Document(d);
    IModule module = AbstractModule.createModuleFromDoc("", null, doc, nature, true);
    // self.c is found as an assign
    Definition[] defs = (Definition[]) module.findDefinition(CompletionStateFactory.getEmptyCompletionState("self.c", nature, new CompletionCache()), 10, 9, nature);
    assertEquals(1, defs.length);
    assertEquals("self.c", ((AssignDefinition) defs[0]).target);
    assertEquals("C", defs[0].value);
    assertSame(module, defs[0].module);
    defs = (Definition[]) module.findDefinition(CompletionStateFactory.getEmptyCompletionState("C", nature, new CompletionCache()), 7, 18, nature);
    assertEquals(1, defs.length);
    assertEquals("C", defs[0].value);
    assertSame(module, defs[0].module);
}
Also used : IModule(org.python.pydev.core.IModule) CompletionCache(org.python.pydev.ast.codecompletion.revisited.CompletionCache) AssignDefinition(org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) Document(org.eclipse.jface.text.Document)

Aggregations

AssignDefinition (org.python.pydev.ast.codecompletion.revisited.visitors.AssignDefinition)11 Definition (org.python.pydev.ast.codecompletion.revisited.visitors.Definition)10 TokensList (org.python.pydev.core.TokensList)7 IDefinition (org.python.pydev.core.IDefinition)6 ArrayList (java.util.ArrayList)5 IModule (org.python.pydev.core.IModule)5 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)5 ITypeInfo (org.python.pydev.core.ITypeInfo)4 Assign (org.python.pydev.parser.jython.ast.Assign)4 Document (org.eclipse.jface.text.Document)3 CompletionCache (org.python.pydev.ast.codecompletion.revisited.CompletionCache)3 PyRefactoringFindDefinition (org.python.pydev.ast.refactoring.PyRefactoringFindDefinition)3 ICompletionState (org.python.pydev.core.ICompletionState)3 SimpleNode (org.python.pydev.parser.jython.SimpleNode)3 Call (org.python.pydev.parser.jython.ast.Call)3 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)3 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)3 IOException (java.io.IOException)2 List (java.util.List)2 CoreException (org.eclipse.core.runtime.CoreException)2