Search in sources :

Example 1 with MatchImportsVisitor

use of com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor in project Pydev by fabioz.

the class MatchImportsVisitorTest method testMatchImports2.

public void testMatchImports2() throws Exception {
    Document doc = new Document("" + // rename a.b.c
    "import a.b.c.d\n" + // rename a.b.c
    "import a.b.c\n" + "import a.b\n" + "");
    IPythonNature nature = new PythonNatureStub();
    ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
    SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);
    MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
    module.getAst().accept(visitor);
    assertEquals(visitor.importsMatchingOnAliasPart.size(), 2);
    assertEquals(visitor.occurrences.size(), 2);
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) IPythonNature(org.python.pydev.core.IPythonNature) PyParser(org.python.pydev.parser.PyParser) MatchImportsVisitor(com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor) Document(org.eclipse.jface.text.Document) PythonNatureStub(org.python.pydev.parser.PythonNatureStub) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 2 with MatchImportsVisitor

use of com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor in project Pydev by fabioz.

the class MatchImportsVisitorTest method testMatchRelativeImports.

public void testMatchRelativeImports() throws Exception {
    // Note: on Python 2.x, we should get the from b import c unless from __future__ import absolute_import is used.
    // In Python 3.x, we'll only get it when actually marked as a relative import (with leading dots).
    Document doc = new Document("" + "from __future__ import absolute_import\n" + "from b import c\n" + // rename a.b.c
    "from .b import c\n" + // rename a.b.c
    "from ..a.b import c\n" + "");
    IPythonNature nature = new PythonNatureStub() {

        @Override
        public int getGrammarVersion() {
            return IPythonNature.GRAMMAR_PYTHON_VERSION_2_7;
        }
    };
    ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
    SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "a.g", null);
    MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
    module.getAst().accept(visitor);
    assertEquals(2, visitor.importFromsMatchingOnAliasPart.size());
    assertEquals(2, visitor.occurrences.size());
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) IPythonNature(org.python.pydev.core.IPythonNature) PyParser(org.python.pydev.parser.PyParser) MatchImportsVisitor(com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor) Document(org.eclipse.jface.text.Document) PythonNatureStub(org.python.pydev.parser.PythonNatureStub) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 3 with MatchImportsVisitor

use of com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor in project Pydev by fabioz.

the class MatchImportsVisitorTest method testMatchImports.

public void testMatchImports() throws Exception {
    Document doc = new Document("" + // rename a.b.c
    "from a.b.c.d import e\n" + // rename a.b.c
    "from a.b.c import d\n" + // rename a.b.c
    "from a.b import c\n" + // rename a.b.c (but not g nor f)
    "from a.b import g, c, f\n" + "from a import b\n" + "from a import *\n" + // rename a.b.c with wild import
    "from a.b.c import *\n" + // rename a.b.c with wild import
    "from a.b.c.d import *\n" + "");
    IPythonNature nature = new PythonNatureStub();
    ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
    SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);
    MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
    module.getAst().accept(visitor);
    assertEquals(visitor.importFromsMatchingOnAliasPart.size(), 2);
    assertEquals(visitor.importFromsMatchingOnModulePart.size(), 4);
    assertEquals(visitor.occurrences.size(), 6);
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) IPythonNature(org.python.pydev.core.IPythonNature) PyParser(org.python.pydev.parser.PyParser) MatchImportsVisitor(com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor) Document(org.eclipse.jface.text.Document) PythonNatureStub(org.python.pydev.parser.PythonNatureStub) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Aggregations

MatchImportsVisitor (com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor)3 Document (org.eclipse.jface.text.Document)3 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)3 IPythonNature (org.python.pydev.core.IPythonNature)3 PyParser (org.python.pydev.parser.PyParser)3 PythonNatureStub (org.python.pydev.parser.PythonNatureStub)3 SimpleNode (org.python.pydev.parser.jython.SimpleNode)3 ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)3