use of org.python.pydev.shared_core.structure.Tuple4 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 org.python.pydev.shared_core.structure.Tuple4 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 org.python.pydev.shared_core.structure.Tuple4 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 org.python.pydev.shared_core.structure.Tuple4 in project Pydev by fabioz.
the class ScopeAnalyzerVisitor method checkImportEntries.
/**
* Checks the import entries for imports that are the same as the one that should be already found.
*/
private void checkImportEntries(List<Tuple4<IToken, Integer, ASTEntry, Found>> ret, Set<IToken> f, List<Tuple3<Found, Integer, ASTEntry>> importEntries, int colDelta) {
if (importEntries != null) {
for (Tuple3<Found, Integer, ASTEntry> foundInFromModule : importEntries) {
IToken generator = foundInFromModule.o1.getSingle().generator;
Tuple4<IToken, Integer, ASTEntry, Found> tup3 = new Tuple4<IToken, Integer, ASTEntry, Found>(generator, colDelta > foundInFromModule.o2 ? colDelta : foundInFromModule.o2, foundInFromModule.o3, foundInFromModule.o1);
if (!f.contains(generator)) {
f.add(generator);
ret.add(tup3);
}
}
}
}
use of org.python.pydev.shared_core.structure.Tuple4 in project Pydev by fabioz.
the class ImageCache method getImageDecorated.
/**
* @param key the key of the image that should be decorated (relative path to the plugin directory)
* @param decoration the key of the image that should be decorated (relative path to the plugin directory)
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public IImageHandle getImageDecorated(String key, String decoration, int decorationLocation, String secondDecoration, int secondDecorationLocation) {
Display display = Display.getCurrent();
if (display == null) {
Log.log("This method should only be called in a UI thread.");
}
Object cacheKey = new Tuple4(key, decoration, decorationLocation, "imageDecoration");
if (secondDecoration != null) {
// Also add the second decoration to the cache key.
cacheKey = new Tuple3(cacheKey, secondDecoration, secondDecorationLocation);
}
Image image = getFromImageHash(cacheKey);
if (image == null) {
// Note that changing the image data gotten here won't affect the original image.
ImageData baseImageData = (ImageData) get(key).getImageData();
image = decorateImage(decoration, decorationLocation, display, baseImageData);
if (secondDecoration != null) {
image = decorateImage(secondDecoration, secondDecorationLocation, display, image.getImageData());
}
image = putOnImageHash(cacheKey, image);
}
final Image computed = image;
return new IImageHandle() {
@Override
public Object getImageData() {
return computed.getImageData();
}
@Override
public Object getImage() {
return computed;
}
};
}
Aggregations