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;
}
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;
}
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();
}
}
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());
}
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);
}
Aggregations