use of org.python.pydev.parser.jython.SimpleNode 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.SimpleNode in project Pydev by fabioz.
the class IOUtils method addAstInfo.
public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
if (key instanceof ModulesKeyForFolder) {
addModulesKeyForFolderToIndex(key, generateDelta);
return new ArrayList<IInfo>(0);
}
boolean isZipModule = key instanceof ModulesKeyForZip;
ModulesKeyForZip modulesKeyForZip = null;
if (isZipModule) {
modulesKeyForZip = (ModulesKeyForZip) key;
}
Object doc;
if (isZipModule) {
doc = FileUtilsFileBuffer.getCustomReturnFromZip(modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);
} else {
doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
}
char[] charArray;
int len;
if (doc instanceof IDocument) {
IDocument document = (IDocument) doc;
charArray = document.get().toCharArray();
len = charArray.length;
} else if (doc instanceof FastStringBuffer) {
FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
// In this case, we can actually get the internal array without doing any copies (and just specifying the len).
charArray = fastStringBuffer.getInternalCharsArray();
len = fastStringBuffer.length();
} else if (doc instanceof String) {
String str = (String) doc;
charArray = str.toCharArray();
len = charArray.length;
} else if (doc instanceof char[]) {
charArray = (char[]) doc;
len = charArray.length;
} else {
throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
}
SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len, key.file);
if (node == null) {
return null;
}
return addAstInfo(node, key, generateDelta);
}
use of org.python.pydev.parser.jython.SimpleNode 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.SimpleNode in project Pydev by fabioz.
the class DuplicationChecker method endScope.
/**
* we are ending a scope
* @param node
*/
private void endScope(String name, SimpleNode node) {
stack.pop();
Map<String, SimpleNode> currScope = stack.peek();
SimpleNode currNode = currScope.get(name);
if (currNode == null || canReplaceScope(currNode, node)) {
currScope.put(name, node);
}
}
use of org.python.pydev.parser.jython.SimpleNode 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