use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class LocalScope method getInterfaceForLocal.
public TokensList getInterfaceForLocal(String activationToken, boolean addAttributeAccess, boolean addLocalsFromHasAttr) {
Set<SourceToken> comps = new HashSet<SourceToken>();
Iterator<ISimpleNode> it = this.scope.topDownIterator();
if (!it.hasNext()) {
return new TokensList();
}
SimpleNode element = (SimpleNode) it.next();
String dottedActTok = activationToken + '.';
// ok, that's the scope we have to analyze
SequencialASTIteratorVisitor visitor = SequencialASTIteratorVisitor.create(element);
ArrayList<Class<? extends SimpleNode>> classes = new ArrayList<>(2);
if (addAttributeAccess) {
classes.add(Attribute.class);
}
if (addLocalsFromHasAttr) {
classes.add(Call.class);
}
Iterator<ASTEntry> iterator = visitor.getIterator(classes.toArray(new Class[classes.size()]));
while (iterator.hasNext()) {
ASTEntry entry = iterator.next();
if (entry.node instanceof Attribute) {
String rep = NodeUtils.getFullRepresentationString(entry.node);
if (rep.startsWith(dottedActTok)) {
rep = rep.substring(dottedActTok.length());
if (NodeUtils.isValidNameRepresentation(rep)) {
// that'd be something that can happen when trying to recreate the parsing
comps.add(new SourceToken(entry.node, FullRepIterable.getFirstPart(rep), "", "", "", IToken.TYPE_OBJECT_FOUND_INTERFACE, this.nature));
}
}
} else if (entry.node instanceof Call) {
Call call = (Call) entry.node;
if ("hasattr".equals(NodeUtils.getFullRepresentationString(call.func)) && call.args != null && call.args.length == 2) {
String rep = NodeUtils.getFullRepresentationString(call.args[0]);
if (rep.equals(activationToken)) {
exprType node = call.args[1];
if (node instanceof Str) {
Str str = (Str) node;
String attrName = str.s;
if (NodeUtils.isValidNameRepresentation(attrName)) {
comps.add(new SourceToken(node, attrName, "", "", "", IToken.TYPE_OBJECT_FOUND_INTERFACE, this.nature));
}
}
}
}
}
}
return new TokensList(comps.toArray(new IToken[0]));
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class AbstractToken method isClassDef.
public static boolean isClassDef(IToken element) {
if (element instanceof SourceToken) {
SourceToken token = (SourceToken) element;
SimpleNode ast = token.getAst();
if (ast instanceof ClassDef) {
return true;
}
}
return false;
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class CompletionParticipantTest method testImportCompletion.
public void testImportCompletion() throws Exception {
participant = new ImportsCompletionParticipant();
// check simple
ICompletionProposalHandle[] proposals = requestCompl("unittest", -1, -1, // the unittest module and testlib.unittest
new String[] { "unittest", "unittest - testlib" });
Document document = new Document("unittest");
ICompletionProposalHandle p0 = null;
ICompletionProposalHandle p1 = null;
for (ICompletionProposalHandle p : proposals) {
String displayString = p.getDisplayString();
if (displayString.equals("unittest")) {
p0 = p;
} else if (displayString.equals("unittest - testlib")) {
p1 = p;
}
}
if (p0 == null) {
fail("Could not find unittest import");
}
if (p1 == null) {
fail("Could not find unittest - testlib import");
}
((CtxInsensitiveImportComplProposal) p0).indentString = " ";
((CtxInsensitiveImportComplProposal) p0).apply(document, ' ', 0, 8);
PySelectionTest.checkStrEquals("import unittest\r\nunittest", document.get());
document = new Document("unittest");
((CtxInsensitiveImportComplProposal) p1).indentString = " ";
((CtxInsensitiveImportComplProposal) p1).apply(document, ' ', 0, 8);
PySelectionTest.checkStrEquals("from testlib import unittest\r\nunittest", document.get());
document = new Document("unittest");
final IEclipsePreferences prefs = new InMemoryEclipsePreferences();
PyCodeCompletionPreferences.getPreferencesForTests = () -> prefs;
document = new Document("unittest");
prefs.putBoolean(PyCodeCompletionPreferences.APPLY_COMPLETION_ON_DOT, false);
((CtxInsensitiveImportComplProposal) p1).indentString = " ";
((CtxInsensitiveImportComplProposal) p1).apply(document, '.', 0, 8);
PySelectionTest.checkStrEquals("unittest.", document.get());
document = new Document("unittest");
prefs.putBoolean(PyCodeCompletionPreferences.APPLY_COMPLETION_ON_DOT, true);
((CtxInsensitiveImportComplProposal) p1).indentString = " ";
((CtxInsensitiveImportComplProposal) p1).apply(document, '.', 0, 8);
PySelectionTest.checkStrEquals("from testlib import unittest\r\nunittest.", document.get());
// for imports, the behavior never changes
AnalysisPreferences.TESTS_DO_IGNORE_IMPORT_STARTING_WITH_UNDER = true;
try {
proposals = requestCompl("_priv3", new String[] { "_priv3 - relative.rel1._priv1._priv2" });
document = new Document("_priv3");
((CtxInsensitiveImportComplProposal) proposals[0]).indentString = " ";
((CtxInsensitiveImportComplProposal) proposals[0]).apply(document, ' ', 0, 6);
PySelectionTest.checkStrEquals("from relative.rel1._priv1._priv2 import _priv3\r\n_priv3", document.get());
} finally {
AnalysisPreferences.TESTS_DO_IGNORE_IMPORT_STARTING_WITH_UNDER = false;
}
// check on actual file
requestCompl(new File(TestDependent.TEST_PYSRC_TESTING_LOC + "/testlib/unittest/guitestcase.py"), "guite", -1, 0, new String[] {});
Import importTok = new Import(new aliasType[] { new aliasType(new NameTok("unittest", NameTok.ImportModule), null) });
this.imports = new TokensList(new IToken[] { new SourceToken(importTok, "unittest", "", "", "", null) });
// none because the import for unittest is already there
requestCompl("import unittest\nunittest", new String[] {});
// the local import for unittest (won't actually show anything because we're only exercising the participant test)
requestCompl("import unittest\nunittes", new String[] {});
this.imports = null;
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class AbstractVisitorTest method testImportCreation2.
public void testImportCreation2() throws Exception {
Iterator<ASTEntry> iterator = createModuleAndGetImports("from os import path, notDefined", ImportFrom.class);
SimpleNode simpleNode = iterator.next().node;
List<IToken> toks = AbstractVisitor.makeImportToken(simpleNode, new ArrayList<IToken>(), MODULE_NAME, true, null);
assertEquals(2, toks.size());
SourceToken token = (SourceToken) toks.get(0);
checkIt(simpleNode, token, "path", "os.path", "os.path");
token = (SourceToken) toks.get(1);
checkIt(simpleNode, token, "notDefined", "os.notDefined", "os.notDefined");
}
use of org.python.pydev.ast.codecompletion.revisited.modules.SourceToken in project Pydev by fabioz.
the class OccurrencesVisitor method onFoundInNamesToIgnore.
@Override
protected void onFoundInNamesToIgnore(IToken token, IToken tokenInNamesToIgnore) {
if (analyzeArgumentsMismatch) {
if (tokenInNamesToIgnore instanceof SourceToken) {
SourceToken sourceToken = (SourceToken) tokenInNamesToIgnore;
// Make a new token because we want the ast to be the FunctionDef or ClassDef, not the name which is the reference.
onPushToRecordedFounds(AbstractVisitor.makeToken(sourceToken.getAst(), token.getRepresentation(), sourceToken.getParentPackage(), nature));
}
}
}
Aggregations