use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class PyLinkedModeCompletionProposal method getAdditionalProposalInfo.
@Override
public String getAdditionalProposalInfo() {
if (computedInfo != null) {
return computedInfo;
}
if (element != null) {
if (element instanceof SourceToken) {
SourceToken sourceToken = (SourceToken) element;
SimpleNode ast = sourceToken.getAst();
if (ast != null && (ast instanceof FunctionDef || ast instanceof ClassDef)) {
computedInfo = addTipForApplyWithModifiers(NodeUtils.printAst(null, ast));
}
if (computedInfo != null) {
return computedInfo;
}
}
computedInfo = addTipForApplyWithModifiers(element.getDocStr());
return computedInfo;
} else {
return addTipForApplyWithModifiers(super.getAdditionalProposalInfo());
}
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class AbstractScopeAnalyzerVisitor method visitGlobal.
@Override
public Object visitGlobal(Global node) throws Exception {
unhandled_node(node);
for (NameTokType name : node.names) {
Name nameAst = new Name(((NameTok) name).id, Name.Store, false);
nameAst.beginLine = name.beginLine;
nameAst.beginColumn = name.beginColumn;
SourceToken token = AbstractVisitor.makeToken(nameAst, moduleName, nature);
scope.addTokenToGlobalScope(token);
// it is global, so, ignore it...
addToNamesToIgnore(nameAst, false, true);
}
return null;
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class AbstractScopeAnalyzerVisitor method visitNameTok.
/**
* We want to make the name tok a regular name for interpreting purposes.
*/
@Override
public Object visitNameTok(NameTok nameTok) throws Exception {
unhandled_node(nameTok);
if (nameTok.ctx == NameTok.VarArg || nameTok.ctx == NameTok.KwArg || nameTok.ctx == NameTok.PatternName) {
SourceToken token = AbstractVisitor.makeToken(nameTok, moduleName, nature);
scope.addToken(token, token, (nameTok).id);
if (checkCurrentScopeForAssignmentsToBuiltins()) {
if (builtinTokens.contains(token.getRepresentation())) {
// Overriding builtin...
onAddAssignmentToBuiltinMessage(token, token.getRepresentation());
}
}
}
return null;
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken 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.ast.codecompletion.revisited.modules.SourceToken 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;
}
Aggregations