use of com.python.pydev.analysis.visitors.Found in project Pydev by fabioz.
the class AbstractScopeAnalyzerVisitor method endScope.
/**
* finalizes the current scope
* @param reportUnused: defines whether we should report unused things found (we may not want to do that
* when we have an abstract method)
*/
protected void endScope(SimpleNode node) {
onBeforeEndScope(node);
// clear the last scope
ScopeItems m = scope.endScope();
for (Iterator<Found> it = probablyNotDefined.iterator(); it.hasNext(); ) {
Found n = it.next();
final GenAndTok probablyNotDefinedFirst = n.getSingle();
IToken tok = probablyNotDefinedFirst.tok;
String rep = tok.getRepresentation();
// we also get a last pass to the unused to see if they might have been defined later on the higher scope
List<Found> foundItems = find(m, rep);
boolean setUsed = false;
for (Found found : foundItems) {
// the scope where it is defined must be an outer scope so that we can say it was defined later...
final GenAndTok foundItemFirst = found.getSingle();
if ((probablyNotDefinedFirst.scopeFound.getScopeType() & Scope.ACCEPTED_METHOD_AND_LAMBDA) != 0 && m.getScopeType() != Scope.SCOPE_TYPE_CLASS) {
if (foundItemFirst.scopeId < probablyNotDefinedFirst.scopeId) {
found.setUsed(true);
setUsed = true;
}
}
}
if (setUsed) {
it.remove();
}
}
// point
if (scope.size() == 0) {
onLastScope(m);
}
onAfterEndScope(node, m);
}
use of com.python.pydev.analysis.visitors.Found in project Pydev by fabioz.
the class ScopeAnalyzerVisitor method onGetCompleteTokenOccurrences.
/**
* Called for each found occurrence in the complete token occurrences
*
* Is used to add other returns to ret
*/
@Override
protected void onGetCompleteTokenOccurrences(Tuple3<Found, Integer, ASTEntry> found, Set<IToken> f, ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> ret) {
// other matches for the imports that we had already found.
Tuple<List<Tuple4<IToken, Integer, ASTEntry, Found>>, List<Tuple4<IToken, Integer, ASTEntry, Found>>> matchingImportEntries = getImportEntries(found, f);
List<Tuple4<IToken, Integer, ASTEntry, Found>> fromModule = matchingImportEntries.o1;
List<Tuple4<IToken, Integer, ASTEntry, Found>> fromImports = matchingImportEntries.o2;
ret.addAll(fromModule);
ret.addAll(fromImports);
// import is different from the context of that import)
for (Tuple4<IToken, Integer, ASTEntry, Found> tuple3 : fromImports) {
try {
if (!(tuple3.o1 instanceof SourceToken)) {
continue;
}
SourceToken tok = (SourceToken) tuple3.o1;
SimpleNode ast = tok.getAst();
int line = 0;
int col = 0;
if (!(ast instanceof Import)) {
continue;
}
Import import1 = (Import) ast;
line = import1.names[0].beginLine - 1;
col = import1.names[0].beginColumn - 1;
PySelection ps = new PySelection(this.document, line, col);
ScopeAnalyzerVisitorWithoutImports analyzerVisitorWithoutImports = new ScopeAnalyzerVisitorWithoutImports(this.nature, this.moduleName, this.current, this.monitor, ps);
SourceModule s = (SourceModule) this.current;
s.getAst().accept(analyzerVisitorWithoutImports);
analyzerVisitorWithoutImports.checkFinished();
// now, let's get the token occurrences for the analyzer that worked without gathering the imports
ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> completeTokenOccurrences = analyzerVisitorWithoutImports.getCompleteTokenOccurrences();
for (Tuple4<IToken, Integer, ASTEntry, Found> oc : completeTokenOccurrences) {
if (!f.contains(oc.o1) && !oc.o1.isImport()) {
// the import should be already added
if (oc.o2 < tuple3.o2) {
oc.o2 = tuple3.o2;
}
f.add(oc.o1);
ret.add(oc);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
use of com.python.pydev.analysis.visitors.Found in project Pydev by fabioz.
the class ScopeAnalyzerVisitorWithoutImports method getEntryOccurrences.
/**
* We get the occurrences as tokens for the name we're looking for. Note that the complete name (may be a dotted name)
* we're looking for may not be equal to the 'partial' name.
*
* This can happen when we're looking for some import such as os.path, and are looking just for the 'path' part.
* So, when this happens, the return is analyzed and only returns names as the one we're looking for (with
* the correct line and col positions).
*/
public List<ASTEntry> getEntryOccurrences() {
checkFinished();
Set<Tuple3<String, Integer, Integer>> s = new HashSet<Tuple3<String, Integer, Integer>>();
ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> complete = getCompleteTokenOccurrences();
ArrayList<ASTEntry> ret = new ArrayList<ASTEntry>();
for (Tuple4<IToken, Integer, ASTEntry, Found> tup : complete) {
IToken token = tup.o1;
if (!(token instanceof SourceToken)) {
// we want only the source tokens for this module
continue;
}
// if it is different, we have to make partial names
SourceToken sourceToken = (SourceToken) tup.o1;
SimpleNode ast = (sourceToken).getAst();
String representation = null;
if (ast instanceof ImportFrom) {
ImportFrom f = (ImportFrom) ast;
// f.names may be empty if it is a wild import
for (aliasType t : f.names) {
NameTok importName = NodeUtils.getNameForAlias(t);
String importRep = NodeUtils.getFullRepresentationString(importName);
if (importRep.equals(nameToFind)) {
ast = importName;
representation = importRep;
break;
}
}
} else if (ast instanceof Import) {
representation = NodeUtils.getFullRepresentationString(ast);
Import f = (Import) ast;
NameTok importName = NodeUtils.getNameForRep(f.names, representation);
if (importName != null) {
ast = importName;
}
} else {
representation = NodeUtils.getFullRepresentationString(ast);
}
if (representation == null) {
// do nothing
// can happen on wild imports
} else if (nameToFind.equals(representation)) {
if (ast instanceof Attribute) {
// it can happen, as we won't go up to the part of the actual call (if there's one).
ast = NodeUtils.getAttributeParts((Attribute) ast).get(0);
ASTEntry entry = new ASTEntry(tup.o3, ast);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
} else {
ASTEntry entry = new ASTEntry(tup.o3, ast);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
}
} else if (FullRepIterable.containsPart(representation, nameToFind)) {
Name nameAst = new Name(nameToFind, Name.Store, false);
List<String> strings = StringUtils.dotSplit(representation);
int plus = 0;
for (String string : strings) {
if (string.equals(nameToFind) && (plus + nameToFind.length() >= tup.o2)) {
break;
}
// len + dot
plus += string.length() + 1;
}
nameAst.beginColumn = AbstractMessage.getStartCol(token, document) + plus;
nameAst.beginLine = AbstractMessage.getStartLine(token, document);
Tuple3<String, Integer, Integer> t = new Tuple3<String, Integer, Integer>(nameToFind, nameAst.beginColumn, nameAst.beginLine);
if (!s.contains(t)) {
s.add(t);
ASTEntry entry = new ASTEntry(tup.o3, nameAst);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
}
}
}
return ret;
}
use of com.python.pydev.analysis.visitors.Found in project Pydev by fabioz.
the class ScopeAnalyzerVisitorWithoutImports method getCompleteTokenOccurrences.
/**
* @return all the occurrences found in a 'complete' way (dotted name).
* The ASTEtries are decorated with the Found here...
*/
protected ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> getCompleteTokenOccurrences() {
// that's because we don't want duplicates
Set<IToken> f = new HashSet<IToken>();
ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> ret = new ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>>();
for (Tuple3<Found, Integer, ASTEntry> found : foundOccurrences) {
List<GenAndTok> all = found.o1.getAll();
for (GenAndTok tok : all) {
Tuple4<IToken, Integer, ASTEntry, Found> tup4 = new Tuple4<IToken, Integer, ASTEntry, Found>(tok.generator, found.o2, found.o3, found.o1);
if (!f.contains(tok.generator)) {
f.add(tok.generator);
ret.add(tup4);
}
for (IToken t : tok.references) {
tup4 = new Tuple4<IToken, Integer, ASTEntry, Found>(t, found.o2, found.o3, found.o1);
if (!f.contains(t)) {
f.add(t);
ret.add(tup4);
}
}
}
onGetCompleteTokenOccurrences(found, f, ret);
}
return ret;
}
use of com.python.pydev.analysis.visitors.Found in project Pydev by fabioz.
the class AbstractScopeAnalyzerVisitor method markRead.
/**
* marks a token as read given its representation
*
* @param token the token to be added
* @param rep the token representation
* @param addToNotDefined determines if it should be added to the 'not defined tokens' stack or not
* @return true if it was found
*/
protected boolean markRead(IToken token, String rep, boolean addToNotDefined, boolean checkIfIsValidImportToken) {
boolean found = false;
Found foundAs = null;
String foundAsStr = null;
int acceptedScopes = 0;
ScopeItems currScopeItems = scope.getCurrScopeItems();
if ((currScopeItems.getScopeType() & Scope.ACCEPTED_METHOD_AND_LAMBDA) != 0) {
acceptedScopes = Scope.ACCEPTED_METHOD_SCOPES;
} else {
acceptedScopes = Scope.ACCEPTED_ALL_SCOPES;
}
if ("locals".equals(rep)) {
// if locals() is accessed, all the tokens currently found are marked as 'used'
// use case:
//
// def f2():
// a = 1
// b = 2
// c = 3
// f1(**locals())
currScopeItems.setAllUsed();
return true;
}
Iterator<String> it = new FullRepIterable(rep, true).iterator();
// search for it
while (found == false && it.hasNext()) {
String nextTokToSearch = it.next();
foundAs = scope.findFirst(nextTokToSearch, true, acceptedScopes);
found = foundAs != null;
if (found) {
foundAsStr = nextTokToSearch;
foundAs.getSingle().references.add(token);
onFoundTokenAs(token, foundAs);
}
}
if (!found) {
// this token might not be defined... (still, might be in names to ignore)
int i;
if ((i = rep.indexOf('.')) != -1) {
// if it is an attribute, we have to check the names to ignore just with its first part
rep = rep.substring(0, i);
}
if (addToNotDefined) {
org.python.pydev.shared_core.structure.Tuple<IToken, Found> foundInNamesToIgnore = findInNamesToIgnore(rep, token);
if (foundInNamesToIgnore == null) {
Found foundForProbablyNotDefined = makeFound(token);
if (scope.size() > 1) {
// if we're not in the global scope, it might be defined later
// we are not in the global scope, so it might be defined later...
probablyNotDefined.add(foundForProbablyNotDefined);
onAddToProbablyNotDefined(token, foundForProbablyNotDefined);
} else {
// it is in the global scope, so, it is undefined.
onAddUndefinedMessage(token, foundForProbablyNotDefined);
}
} else {
IToken tokenInNamesToIgnore = foundInNamesToIgnore.o1;
onFoundInNamesToIgnore(token, tokenInNamesToIgnore);
}
}
} else if (checkIfIsValidImportToken) {
// really exists in xxx, if it was found as an import)
try {
if (foundAs.isImport() && !rep.equals(foundAsStr) && foundAs.importInfo != null && foundAs.importInfo.wasResolved) {
// the foundAsStr equals the module resolved in the Found tok
IModule m = foundAs.importInfo.mod;
String tokToCheck;
if (foundAs.isWildImport()) {
tokToCheck = foundAsStr;
} else {
String tok = foundAs.importInfo.rep;
tokToCheck = rep.substring(foundAsStr.length() + 1);
if (tok.length() > 0) {
tokToCheck = tok + "." + tokToCheck;
}
}
for (String repToCheck : new FullRepIterable(tokToCheck)) {
int inGlobalTokens = m.isInGlobalTokens(repToCheck, nature, true, true, this.completionCache);
if (inGlobalTokens == IModule.NOT_FOUND) {
if (!isDefinitionUnknown(m, repToCheck)) {
// Check if there's some hasattr (if there is, we'll consider that the token which
// had the hasattr checked will actually have it).
TokensList interfaceForLocal = this.currentLocalScope.getInterfaceForLocal(foundAsStr, false, true);
boolean foundInHasAttr = false;
for (IterTokenEntry entry : interfaceForLocal) {
IToken iToken = entry.getToken();
if (iToken.getRepresentation().equals(repToCheck)) {
foundInHasAttr = true;
break;
}
}
if (!foundInHasAttr) {
IToken foundTok = findNameTok(token, repToCheck);
onAddUndefinedVarInImportMessage(foundTok, foundAs);
}
}
// no need to keep checking once one is not defined
break;
} else if (inGlobalTokens == IModule.FOUND_BECAUSE_OF_GETATTR) {
break;
}
}
} else if (foundAs.isImport() && (foundAs.importInfo == null || !foundAs.importInfo.wasResolved)) {
// import was not resolved
onFoundUnresolvedImportPart(token, rep, foundAs);
}
} catch (Exception e) {
Log.log("Error checking for valid tokens (imports) for " + moduleName, e);
}
}
return found;
}
Aggregations