Search in sources :

Example 21 with SourceToken

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]));
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Call(org.python.pydev.parser.jython.ast.Call) Attribute(org.python.pydev.parser.jython.ast.Attribute) ArrayList(java.util.ArrayList) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Str(org.python.pydev.parser.jython.ast.Str) SequencialASTIteratorVisitor(org.python.pydev.parser.visitors.scope.SequencialASTIteratorVisitor) IToken(org.python.pydev.core.IToken) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) TokensList(org.python.pydev.core.TokensList) HashSet(java.util.HashSet)

Example 22 with SourceToken

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;
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 23 with SourceToken

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;
}
Also used : CtxInsensitiveImportComplProposal(org.python.pydev.editor.codecompletion.proposals.CtxInsensitiveImportComplProposal) Import(org.python.pydev.parser.jython.ast.Import) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) Document(org.eclipse.jface.text.Document) IToken(org.python.pydev.core.IToken) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) File(java.io.File) InMemoryEclipsePreferences(org.python.pydev.shared_core.preferences.InMemoryEclipsePreferences) NameTok(org.python.pydev.parser.jython.ast.NameTok) TokensList(org.python.pydev.core.TokensList) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 24 with SourceToken

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");
}
Also used : IToken(org.python.pydev.core.IToken) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 25 with SourceToken

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));
        }
    }
}
Also used : SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Aggregations

SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)50 SimpleNode (org.python.pydev.parser.jython.SimpleNode)26 IToken (org.python.pydev.core.IToken)25 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)13 TokensList (org.python.pydev.core.TokensList)12 ArrayList (java.util.ArrayList)11 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)11 IModule (org.python.pydev.core.IModule)9 NameTok (org.python.pydev.parser.jython.ast.NameTok)9 ICompletionState (org.python.pydev.core.ICompletionState)8 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)8 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)8 Import (org.python.pydev.parser.jython.ast.Import)7 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)7 IterTokenEntry (org.python.pydev.core.IterTokenEntry)6 CompletionRecursionException (org.python.pydev.core.structure.CompletionRecursionException)6 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)6 HashSet (java.util.HashSet)5 Attribute (org.python.pydev.parser.jython.ast.Attribute)5 Found (com.python.pydev.analysis.visitors.Found)4