Search in sources :

Example 76 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonModelProviderTest method testAddSourceFolderToSourceFolder.

public void testAddSourceFolderToSourceFolder() throws Exception {
    final HashSet<String> pythonPathSet = new HashSet<String>();
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
    String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
    File f = new File(source2Folder);
    if (f.exists()) {
        f.delete();
    }
    // still not created!
    pythonPathSet.add(source2Folder);
    PythonNature nature = createNature(pythonPathSet);
    project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
    provider = new PythonModelProvider();
    Object[] children1 = provider.getChildren(project);
    assertEquals(1, children1.length);
    assertTrue(children1[0] instanceof PythonSourceFolder);
    Set set = new HashSet();
    f.mkdir();
    try {
        FolderStub source2FolderFile = new FolderStub(project, f);
        set.add(source2FolderFile);
        provider.interceptAdd(new PipelinedShapeModification(project, set));
        assertEquals(1, set.size());
        assertTrue(set.iterator().next() instanceof PythonSourceFolder);
    } finally {
        f.delete();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) PythonNature(org.python.pydev.plugin.nature.PythonNature) FolderStub(org.python.pydev.shared_core.resource_stubs.FolderStub) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) File(java.io.File) HashSet(java.util.HashSet)

Example 77 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonModelProviderTest method testNullElements.

public void testNullElements() throws Exception {
    final HashSet<String> pythonPathSet = new HashSet<String>();
    // root is the source
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot");
    PythonNature nature = createNature(pythonPathSet);
    WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
    project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
    workspaceRootStub.addChild(project);
    project.setParent(workspaceRootStub);
    provider = new PythonModelProvider();
    HashSet<Object> currentChildren = new HashSet<Object>();
    currentChildren.add(project);
    currentChildren.add(null);
    provider.getPipelinedChildren(workspaceRootStub, currentChildren);
    assertEquals(1, currentChildren.size());
    PythonProjectSourceFolder projectSourceFolder = (PythonProjectSourceFolder) currentChildren.iterator().next();
    currentChildren = new HashSet<Object>();
    currentChildren.add(null);
    currentChildren.add(null);
    provider.getPipelinedChildren(projectSourceFolder, currentChildren);
    assertEquals(1, currentChildren.size());
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) File(java.io.File) HashSet(java.util.HashSet)

Example 78 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonModelProviderTest method testProjectIsRoot.

/**
 * Test if setting the project root as a source folder will return an object from the python model.
 */
public void testProjectIsRoot() throws Exception {
    String pythonpathLoc = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot";
    final HashSet<String> pythonPathSet = new HashSet<String>();
    pythonPathSet.add(pythonpathLoc);
    PythonNature nature = createNature(pythonPathSet);
    WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
    project = new ProjectStub(new File(pythonpathLoc), nature);
    provider = new PythonModelProvider();
    workspaceRootStub.addChild(project);
    workspaceRootStub.addChild(null);
    workspaceRootStub.addChild("other");
    project.setParent(workspaceRootStub);
    Object[] children1 = provider.getChildren(workspaceRootStub);
    assertEquals(2, children1.length);
    int stringsFound = 0;
    int projectSourceFoldersFound = 0;
    for (Object c : children1) {
        if (c instanceof String) {
            stringsFound += 1;
        } else if (c instanceof PythonProjectSourceFolder) {
            projectSourceFoldersFound += 1;
        } else {
            fail("Expecting source folder or string. Received: " + c.getClass().getName());
        }
    }
    assertEquals(1, stringsFound);
    assertEquals(1, projectSourceFoldersFound);
    // now, let's go and change the pythonpath location to a folder within the project and see if it changes...
    pythonPathSet.clear();
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python");
    IResource refreshObject = provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));
    assertTrue("Expecting the refresh object to be the root and not the project", refreshObject instanceof IWorkspaceRoot);
    children1 = provider.getChildren(workspaceRootStub);
    assertEquals(2, children1.length);
    stringsFound = 0;
    int projectsFound = 0;
    for (Object c : children1) {
        if (c instanceof String) {
            stringsFound += 1;
        } else if (c instanceof IProject) {
            projectsFound += 1;
        } else {
            fail("Expecting source folder or string. Received: " + c.getClass().getName());
        }
    }
    assertEquals(1, stringsFound);
    assertEquals(1, projectsFound);
    // set to be the root again
    pythonPathSet.clear();
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot");
    refreshObject = provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));
    assertTrue("Expecting the refresh object to be the root and not the project", refreshObject instanceof IWorkspaceRoot);
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) ArrayList(java.util.ArrayList) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) File(java.io.File) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 79 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonModelProviderTest method createNature.

/**
 * Creates a nature that has the given set as its underlying pythonpath paths. The reference
 * is kept inside as a reference, so, changing that reference will affect the pythonpath
 * that is set in the nature.
 */
private PythonNature createNature(final HashSet<String> pythonPathSet) {
    PythonNature nature = new PythonNature() {

        @Override
        public IPythonPathNature getPythonPathNature() {
            HashSet<String> hashSet = new HashSet<>();
            IPath base = Path.fromOSString(TestDependent.TEST_PYSRC_NAVIGATOR_LOC);
            for (String s : pythonPathSet) {
                if (s.equals("invalid")) {
                    hashSet.add(s);
                } else {
                    IPath p = Path.fromOSString(s);
                    Assert.isTrue(FileUtils.isPrefixOf(base, p), "Expected: " + base + " to be prefix of: " + p);
                    hashSet.add(p.makeRelativeTo(base).toString());
                }
            }
            return new PythonPathNatureStub(hashSet);
        }
    };
    return nature;
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) IPath(org.eclipse.core.runtime.IPath) HashSet(java.util.HashSet)

Example 80 with PythonNature

use of org.python.pydev.plugin.nature.PythonNature in project Pydev by fabioz.

the class PythonModelProviderTest method testInterceptAdd.

/**
 * Test if intercepting an add deep within the pythonpath structure will correctly return an object
 * from the python model.
 */
public void testInterceptAdd() throws Exception {
    PythonNature nature = createNature(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python");
    project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
    file = new FileStub(project, new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python/pack1/pack2/mod2.py"));
    provider = new PythonModelProvider();
    HashSet<Object> files = new HashSet<Object>();
    files.add(file);
    files.add(null);
    files.add("string");
    provider.interceptAdd(new PipelinedShapeModification(file.getParent(), files));
    assertEquals(2, files.size());
    for (Object wrappedResource : files) {
        assertTrue((wrappedResource instanceof IWrappedResource && ((IWrappedResource) wrappedResource).getActualObject() == file) || wrappedResource.equals("string"));
    }
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) FileStub(org.python.pydev.shared_core.resource_stubs.FileStub) File(java.io.File) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) HashSet(java.util.HashSet)

Aggregations

PythonNature (org.python.pydev.plugin.nature.PythonNature)88 IProject (org.eclipse.core.resources.IProject)41 IPythonNature (org.python.pydev.core.IPythonNature)32 File (java.io.File)31 HashSet (java.util.HashSet)20 IFile (org.eclipse.core.resources.IFile)20 ArrayList (java.util.ArrayList)19 CoreException (org.eclipse.core.runtime.CoreException)18 IResource (org.eclipse.core.resources.IResource)17 IPath (org.eclipse.core.runtime.IPath)14 IContainer (org.eclipse.core.resources.IContainer)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)10 HashMap (java.util.HashMap)9 IPythonPathNature (org.python.pydev.core.IPythonPathNature)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)8 IFolder (org.eclipse.core.resources.IFolder)7 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)7 Set (java.util.Set)6