use of org.python.pydev.parser.jython.ast.FunctionDef 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;
}
use of org.python.pydev.parser.jython.ast.FunctionDef 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;
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class TddCodeGenerationQuickFixParticipant method getTddProps.
public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, PyEdit edit, int offset, List<ICompletionProposalHandle> ret) {
if (ret == null) {
ret = new ArrayList<ICompletionProposalHandle>();
}
// Additional option: Generate markers for 'self.' accesses
int lineOfOffset = ps.getLineOfOffset(offset);
String lineContents = ps.getLine(lineOfOffset);
// Additional option: Generate methods for function calls
List<TddPossibleMatches> callsAtLine = ps.getTddPossibleMatchesAtLine();
if (callsAtLine.size() > 0) {
// Make sure we don't check the same thing twice.
Map<String, TddPossibleMatches> callsToCheck = new HashMap<String, TddPossibleMatches>();
for (TddPossibleMatches call : callsAtLine) {
String callString = call.initialPart + call.secondPart;
callsToCheck.put(callString, call);
}
CONTINUE_FOR: for (Map.Entry<String, TddPossibleMatches> entry : callsToCheck.entrySet()) {
// we have at least something as SomeClass(a=2,c=3) or self.bar or self.foo.bar() or just foo.bar, etc.
IPyRefactoring pyRefactoring = AbstractPyRefactoring.getPyRefactoring();
try {
TddPossibleMatches possibleMatch = entry.getValue();
String callWithoutParens = entry.getKey();
ItemPointer[] pointers = null;
PySelection callPs = null;
TddPossibleMatches lastPossibleMatchNotFound = possibleMatch;
for (int i = 0; i < 10; i++) {
// more than 10 attribute accesses in a line? No way!
lastPossibleMatchNotFound = possibleMatch;
if (i > 0) {
// We have to take 1 level out of the match... i.e.: if it was self.foo.get(), search now for self.foo.
String line = FullRepIterable.getWithoutLastPart(possibleMatch.full);
List<TddPossibleMatches> tddPossibleMatchesAtLine = ps.getTddPossibleMatchesAtLine(line);
if (tddPossibleMatchesAtLine.size() > 0) {
possibleMatch = tddPossibleMatchesAtLine.get(0);
callWithoutParens = possibleMatch.initialPart + possibleMatch.secondPart;
} else {
continue CONTINUE_FOR;
}
}
String full = possibleMatch.full;
int indexOf = lineContents.indexOf(full);
if (indexOf < 0) {
Log.log("Did not expect index < 0.");
continue CONTINUE_FOR;
}
callPs = new PySelection(ps.getDoc(), ps.getLineOffset() + indexOf + callWithoutParens.length());
RefactoringRequest request = new RefactoringRequest(f, callPs, null, nature, edit);
// Don't look in additional info.
request.setAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, false);
pointers = pyRefactoring.findDefinition(request);
if (((pointers != null && pointers.length > 0) || StringUtils.count(possibleMatch.full, '.') <= 1)) {
break;
}
}
if (pointers == null || callPs == null) {
continue CONTINUE_FOR;
}
if (lastPossibleMatchNotFound != null && lastPossibleMatchNotFound != possibleMatch && pointers.length >= 1) {
// Ok, as we were analyzing a string as self.bar.foo, we didn't find something in a pass
// i.e.: self.bar.foo, but we found it in a second pass
// as self.bar, so, this means we have to open the chance to create the 'foo' in self.bar.
String methodToCreate = FullRepIterable.getLastPart(lastPossibleMatchNotFound.secondPart);
int absoluteCursorOffset = callPs.getAbsoluteCursorOffset();
// +1 for the dot removed too.
absoluteCursorOffset = absoluteCursorOffset - (1 + methodToCreate.length());
PySelection newSelection = new PySelection(callPs.getDoc(), absoluteCursorOffset);
checkCreationBasedOnFoundPointers(edit, callPs, ret, possibleMatch, pointers, methodToCreate, newSelection, nature);
continue CONTINUE_FOR;
}
if (pointers.length >= 1) {
// the __init__ or something at the class level).
if (!checkInitCreation(edit, callPs, pointers, ret)) {
// This was called only when isCall == false
// Ok, if it's not a call and we found a field, it's still possible that we may want to create
// a field if it wasn't found in the __init__
boolean foundInInit = false;
for (ItemPointer p : pointers) {
Definition definition = p.definition;
try {
Object peek = definition.scope.getScopeStack().peek();
if (peek instanceof FunctionDef) {
FunctionDef functionDef = (FunctionDef) peek;
String rep = NodeUtils.getRepresentationString(functionDef);
if (rep != null && rep.equals("__init__")) {
foundInInit = true;
break;
}
}
} catch (Exception e) {
}
}
if (!foundInInit) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret, lineContents, possibleMatch, f, nature);
}
}
} else if (pointers.length == 0) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret, lineContents, possibleMatch, f, nature);
}
} catch (Exception e) {
if (onGetTddPropsError != null) {
onGetTddPropsError.call(e);
}
Log.log(e);
}
}
}
return ret;
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class ArgumentsChecker method checkNameFound.
/*default*/
void checkNameFound(Call callNode, SourceToken sourceToken) throws Exception {
FunctionDef functionDefinitionReferenced;
boolean callingBoundMethod = false;
SimpleNode ast = sourceToken.getAst();
if (ast instanceof FunctionDef) {
functionDefinitionReferenced = (FunctionDef) ast;
analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
} else if (ast instanceof ClassDef) {
ClassDef classDef = (ClassDef) ast;
SimpleNode initNode = defToConsideredInit.get(classDef);
callingBoundMethod = true;
if (initNode == null) {
String className = ((NameTok) classDef.name).id;
Definition foundDef = sourceToken.getDefinition();
IModule mod = this.current;
if (foundDef != null) {
mod = foundDef.module;
}
SimpleNode n = NodeUtils.getNodeFromPath(classDef, "__init__");
if (n instanceof FunctionDef) {
initNode = n;
} else {
IDefinition[] definition = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(className + ".__init__", this.nature, this.completionCache), -1, -1, this.nature);
for (IDefinition iDefinition : definition) {
Definition d = (Definition) iDefinition;
if (d.ast instanceof FunctionDef) {
initNode = d.ast;
defToConsideredInit.put(classDef, initNode);
break;
}
}
}
}
if (initNode instanceof FunctionDef) {
functionDefinitionReferenced = (FunctionDef) initNode;
analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
}
}
}
use of org.python.pydev.parser.jython.ast.FunctionDef in project Pydev by fabioz.
the class MessagesManager method addUnusedMessage.
/**
* adds a message for something that was not used
*
* @param node the node representing the scope being closed when adding the
* unused message
*/
public void addUnusedMessage(SimpleNode node, Found f) {
List<GenAndTok> all = f.getAll();
int len = all.size();
for (int i = 0; i < len; i++) {
GenAndTok g = all.get(i);
if (g.generator instanceof SourceToken) {
SimpleNode ast = ((SourceToken) g.generator).getAst();
// it can be an unused import
boolean isFromImport = ast instanceof ImportFrom;
if (isFromImport || ast instanceof Import) {
if (isFromImport && AbstractVisitor.isWildImport((ImportFrom) ast)) {
addMessage(IAnalysisPreferences.TYPE_UNUSED_WILD_IMPORT, g.generator, g.tok);
} else if (!(g.generator instanceof ImportPartSourceToken)) {
addMessage(IAnalysisPreferences.TYPE_UNUSED_IMPORT, g.generator, g.tok);
}
// finish it...
continue;
}
}
// we have to check if this is a name we should ignore
if (startsWithNamesToIgnore(g)) {
int type = IAnalysisPreferences.TYPE_UNUSED_VARIABLE;
if (g.tok instanceof SourceToken) {
SourceToken t = (SourceToken) g.tok;
SimpleNode ast = t.getAst();
if (ast instanceof NameTok) {
NameTok n = (NameTok) ast;
if (n.ctx == NameTok.KwArg || n.ctx == NameTok.VarArg || n.ctx == NameTok.KeywordName) {
type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
}
} else if (ast instanceof Name) {
Name n = (Name) ast;
if (n.ctx == Name.Param || n.ctx == Name.KwOnlyParam) {
type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
}
}
}
boolean addMessage = true;
if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER) {
if (node instanceof FunctionDef) {
addMessage = false;
FunctionDef def = (FunctionDef) node;
for (stmtType b : def.body) {
if (b instanceof Pass) {
continue;
}
if (b instanceof Expr) {
Expr expr = (Expr) b;
if (expr.value instanceof Str) {
continue;
}
}
addMessage = true;
break;
}
}
}
if (addMessage) {
addMessage(type, g.generator, g.tok);
}
}
}
}
Aggregations