Search in sources :

Example 1 with PythonNatureStub

use of org.python.pydev.parser.PythonNatureStub 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 PythonNatureStub

use of org.python.pydev.parser.PythonNatureStub 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 PythonNatureStub

use of org.python.pydev.parser.PythonNatureStub 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)

Example 4 with PythonNatureStub

use of org.python.pydev.parser.PythonNatureStub in project Pydev by fabioz.

the class PyRenameResourceChangeTest method testRenameResource.

public void testRenameResource() throws Exception {
    ProjectStub project = new ProjectStub(tempDir, new PythonNatureStub());
    File dirFile = new File(tempDir, "dir");
    dirFile.mkdirs();
    File file = new File(dirFile, "file.py");
    file.createNewFile();
    FolderStub folderStub = new FolderStub(project, dirFile);
    FileStub fileStub = new FileStub(project, file);
    String tempName = tempDir.getName();
    IContainer container;
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "foo.bar.now", null);
    assertEquals(new Path(tempName + "/foo/bar"), container.getFullPath());
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "foo", null);
    assertEquals(new Path(tempName), container.getFullPath());
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "my.foo", null);
    assertEquals(new Path(tempName + "/my"), container.getFullPath());
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "dir.foo", null);
    assertEquals(new Path(tempName + "/dir"), container.getFullPath());
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "dir", null);
    assertEquals(new Path(tempName + ""), container.getFullPath());
    container = PyRenameResourceChange.getDestination(fileStub, "dir.file", "dir.file.now", null);
    assertEquals(new Path(tempName + "/dir/file"), container.getFullPath());
}
Also used : Path(org.eclipse.core.runtime.Path) ProjectStub(org.python.pydev.shared_core.resource_stubs.ProjectStub) FileStub(org.python.pydev.shared_core.resource_stubs.FileStub) FolderStub(org.python.pydev.shared_core.resource_stubs.FolderStub) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) PythonNatureStub(org.python.pydev.parser.PythonNatureStub)

Aggregations

PythonNatureStub (org.python.pydev.parser.PythonNatureStub)4 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 SimpleNode (org.python.pydev.parser.jython.SimpleNode)3 ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)3 File (java.io.File)1 IContainer (org.eclipse.core.resources.IContainer)1 Path (org.eclipse.core.runtime.Path)1 FileStub (org.python.pydev.shared_core.resource_stubs.FileStub)1 FolderStub (org.python.pydev.shared_core.resource_stubs.FolderStub)1 ProjectStub (org.python.pydev.shared_core.resource_stubs.ProjectStub)1