Search in sources :

Example 1 with DirectoryIndex

use of com.intellij.openapi.roots.impl.DirectoryIndex in project intellij-community by JetBrains.

the class MavenClasspathsAndSearchScopesTest method checkDirIndexTestModulesWithCompileOrRuntimeScope.

// Checks that the DirectoryIndex#getOrderEntries() returns the expected values
// for the dependency graph set up by setupDirIndexTestModulesWithScope().
// The result is the same for "compile" and "runtime" scopes.
private void checkDirIndexTestModulesWithCompileOrRuntimeScope(List<Module> modules) {
    assertEquals(6, modules.size());
    DirectoryIndex index = DirectoryIndex.getInstance(myProject);
    VirtualFile m3JavaDir = VfsUtil.findFileByIoFile(new File(getProjectPath(), "m3/src/main/java"), true);
    assertNotNull(m3JavaDir);
    // Should be: m1 -> m3, m2 -> m3, m3 -> source, and m4 -> m3
    List<OrderEntry> orderEntries = index.getOrderEntries(index.getInfoForFile(m3JavaDir));
    assertEquals(4, orderEntries.size());
    List<Module> ownerModules = orderEntriesToOwnerModules(orderEntries);
    List<Module> depModules = orderEntriesToDepModules(orderEntries);
    assertOrderedElementsAreEqual(ownerModules, Arrays.asList(modules.get(0), modules.get(1), modules.get(2), modules.get(3)));
    assertOrderedElementsAreEqual(depModules, Arrays.asList(modules.get(2), modules.get(2), null, modules.get(2)));
    // m3 -> source
    OrderEntry m3E2 = orderEntries.get(2);
    assertInstanceOf(m3E2, ModuleSourceOrderEntry.class);
    VirtualFile m6javaDir = VfsUtil.findFileByIoFile(new File(getProjectPath(), "m6/src/main/java"), true);
    assertNotNull(m6javaDir);
    // Should be m1 -> m6, m2 -> m6, m5 -> m6, m6 -> source
    List<OrderEntry> m6OrderEntries = index.getOrderEntries(index.getInfoForFile(m6javaDir));
    assertEquals(4, m6OrderEntries.size());
    List<Module> m6OwnerModules = orderEntriesToOwnerModules(m6OrderEntries);
    List<Module> m6DepModules = orderEntriesToDepModules(m6OrderEntries);
    assertOrderedElementsAreEqual(m6OwnerModules, Arrays.asList(modules.get(0), modules.get(1), modules.get(4), modules.get(5)));
    assertOrderedElementsAreEqual(m6DepModules, Arrays.asList(modules.get(5), modules.get(5), modules.get(5), null));
    // m6 -> source
    OrderEntry m6E3 = m6OrderEntries.get(3);
    assertInstanceOf(m6E3, ModuleSourceOrderEntry.class);
    VirtualFile jmockDir = VfsUtil.findFileByIoFile(new File(getRepositoryPath(), "jmock/jmock/1.0/jmock-1.0.jar"), true);
    assertNotNull(jmockDir);
    VirtualFile jmockJar = JarFileSystem.getInstance().getJarRootForLocalFile(jmockDir);
    assertNotNull(jmockJar);
    // m2 -> jmock, m3 -> jmock
    List<OrderEntry> jmockOrderEntries = index.getOrderEntries(index.getInfoForFile(jmockJar));
    assertEquals(2, jmockOrderEntries.size());
    OrderEntry jmockE0 = jmockOrderEntries.get(0);
    assertEquals(modules.get(1), jmockE0.getOwnerModule());
    assertInstanceOf(jmockE0, LibraryOrderEntry.class);
    OrderEntry jmockE1 = jmockOrderEntries.get(1);
    assertEquals(modules.get(2), jmockE1.getOwnerModule());
    assertInstanceOf(jmockE1, LibraryOrderEntry.class);
}
Also used : DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) Module(com.intellij.openapi.module.Module) File(java.io.File)

Example 2 with DirectoryIndex

use of com.intellij.openapi.roots.impl.DirectoryIndex in project intellij-community by JetBrains.

the class MavenClasspathsAndSearchScopesTest method testDirIndexOrderEntriesTransitiveTestScope.

public void testDirIndexOrderEntriesTransitiveTestScope() throws IOException {
    // This test is a bit different from the above tests of compile or runtime scope,
    // because test scope does not propagate transitive dependencies.
    List<Module> modules = setupDirIndexTestModulesWithScope("test");
    assertEquals(6, modules.size());
    DirectoryIndex index = DirectoryIndex.getInstance(myProject);
    VirtualFile m3JavaDir = VfsUtil.findFileByIoFile(new File(getProjectPath(), "m3/src/main/java"), true);
    assertNotNull(m3JavaDir);
    // Should be no transitive deps: m2 -> m3, m3 -> source, and m4 -> m3
    List<OrderEntry> orderEntries = index.getOrderEntries(index.getInfoForFile(m3JavaDir));
    assertEquals(3, orderEntries.size());
    List<Module> ownerModules = orderEntriesToOwnerModules(orderEntries);
    List<Module> depModules = orderEntriesToDepModules(orderEntries);
    assertOrderedElementsAreEqual(ownerModules, Arrays.asList(modules.get(1), modules.get(2), modules.get(3)));
    assertOrderedElementsAreEqual(depModules, Arrays.asList(modules.get(2), null, modules.get(2)));
    // m3 -> source
    OrderEntry m3E1 = orderEntries.get(1);
    assertInstanceOf(m3E1, ModuleSourceOrderEntry.class);
    VirtualFile m6javaDir = VfsUtil.findFileByIoFile(new File(getProjectPath(), "m6/src/main/java"), true);
    assertNotNull(m6javaDir);
    // Still has some transitive deps because m5 -> m6 is hardcoded to be compile scope
    // m2 -> m6, m5 -> m6, m6 -> source
    List<OrderEntry> m6OrderEntries = index.getOrderEntries(index.getInfoForFile(m6javaDir));
    assertEquals(3, m6OrderEntries.size());
    List<Module> m6OwnerModules = orderEntriesToOwnerModules(m6OrderEntries);
    List<Module> m6DepModules = orderEntriesToDepModules(m6OrderEntries);
    assertOrderedElementsAreEqual(m6OwnerModules, Arrays.asList(modules.get(1), modules.get(4), modules.get(5)));
    assertOrderedElementsAreEqual(m6DepModules, Arrays.asList(modules.get(5), modules.get(5), null));
    // m6 -> source
    OrderEntry m6E2 = m6OrderEntries.get(2);
    assertInstanceOf(m6E2, ModuleSourceOrderEntry.class);
    VirtualFile jmockDir = VfsUtil.findFileByIoFile(new File(getRepositoryPath(), "jmock/jmock/1.0/jmock-1.0.jar"), true);
    assertNotNull(jmockDir);
    VirtualFile jmockJar = JarFileSystem.getInstance().getJarRootForLocalFile(jmockDir);
    assertNotNull(jmockJar);
    // m2 -> jmock, m3 -> jmock
    List<OrderEntry> jmockOrderEntries = index.getOrderEntries(index.getInfoForFile(jmockJar));
    assertEquals(2, jmockOrderEntries.size());
    OrderEntry jmockE0 = jmockOrderEntries.get(0);
    assertEquals(modules.get(1), jmockE0.getOwnerModule());
    assertInstanceOf(jmockE0, LibraryOrderEntry.class);
    OrderEntry jmockE1 = jmockOrderEntries.get(1);
    assertEquals(modules.get(2), jmockE1.getOwnerModule());
    assertInstanceOf(jmockE1, LibraryOrderEntry.class);
}
Also used : DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) Module(com.intellij.openapi.module.Module) File(java.io.File)

Example 3 with DirectoryIndex

use of com.intellij.openapi.roots.impl.DirectoryIndex in project intellij-plugins by JetBrains.

the class FlexDirectoryAsPackageRenameHandler method getPackage.

@Override
protected FlexPackageImpl getPackage(PsiDirectory psiDirectory) {
    Project project = psiDirectory.getProject();
    DirectoryIndex index = DirectoryIndex.getInstance(project);
    String packageName = index.getPackageName(psiDirectory.getVirtualFile());
    if (packageName == null)
        return null;
    Query<VirtualFile> dirs = index.getDirectoriesByPackageName(packageName, false);
    if (dirs.findFirst() == null)
        return null;
    return new FlexPackageImpl(PsiManager.getInstance(project), packageName);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex)

Example 4 with DirectoryIndex

use of com.intellij.openapi.roots.impl.DirectoryIndex in project intellij-community by JetBrains.

the class MarkRootActionBase method findParentModule.

@Nullable
private static Module findParentModule(@Nullable Project project, @NotNull VirtualFile[] files) {
    if (project == null)
        return null;
    Module result = null;
    DirectoryIndex index = DirectoryIndex.getInstance(project);
    for (VirtualFile file : files) {
        Module module = index.getInfoForFile(file).getModule();
        if (module == null)
            return null;
        if (result == null) {
            result = module;
        } else if (!result.equals(module)) {
            return null;
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with DirectoryIndex

use of com.intellij.openapi.roots.impl.DirectoryIndex in project intellij-community by JetBrains.

the class DumpDirectoryInfoAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final DirectoryIndex index = DirectoryIndex.getInstance(project);
    if (project != null) {
        final VirtualFile root = e.getData(CommonDataKeys.VIRTUAL_FILE);
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            final ContentIterator contentIterator = new ContentIterator() {

                @Override
                public boolean processFile(VirtualFile fileOrDir) {
                    LOG.info(fileOrDir.getPath());
                    LOG.info(index.getInfoForFile(fileOrDir).toString());
                    return true;
                }
            };
            if (root != null) {
                ProjectRootManager.getInstance(project).getFileIndex().iterateContentUnderDirectory(root, contentIterator);
            } else {
                ProjectRootManager.getInstance(project).getFileIndex().iterateContent(contentIterator);
            }
        }, "Dumping directory index", true, project);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ContentIterator(com.intellij.openapi.roots.ContentIterator) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex)

Aggregations

DirectoryIndex (com.intellij.openapi.roots.impl.DirectoryIndex)7 Module (com.intellij.openapi.module.Module)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 File (java.io.File)3 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiManager (com.intellij.psi.PsiManager)1