Search in sources :

Example 1 with IndexableFileSet

use of com.intellij.util.indexing.IndexableFileSet in project intellij-community by JetBrains.

the class LightProjectDescriptor method registerSourceRoot.

protected void registerSourceRoot(Project project, VirtualFile srcRoot) {
    IndexableFileSet indexableFileSet = new IndexableFileSet() {

        @Override
        public boolean isInSet(@NotNull VirtualFile file) {
            return file.getFileSystem() == srcRoot.getFileSystem() && project.isOpen();
        }

        @Override
        public void iterateIndexableFilesIn(@NotNull VirtualFile file, @NotNull ContentIterator iterator) {
            VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

                @Override
                public boolean visitFile(@NotNull VirtualFile file) {
                    iterator.processFile(file);
                    return true;
                }
            });
        }
    };
    FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, null);
    Disposer.register(project, () -> FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) IndexableFileSet(com.intellij.util.indexing.IndexableFileSet) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IndexableFileSet

use of com.intellij.util.indexing.IndexableFileSet in project intellij-plugins by JetBrains.

the class DesignerTests method configureByFiles.

public static VirtualFile[] configureByFiles(@Nullable VirtualFile rawProjectRoot, VirtualFile[] files, @Nullable VirtualFile[] auxiliaryFiles, Module module, @Nullable TripleFunction<ModifiableRootModel, VirtualFile, List<String>, Void> moduleInitializer) throws Exception {
    return WriteAction.compute(() -> {
        VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
        //noinspection ConstantConditions
        dummyRoot.refresh(false, false);
        final VirtualFile sourceDir = dummyRoot.createChildDirectory(DesignerTests.class, "s");
        assert sourceDir != null;
        final IndexableFileSet indexableFileSet = new IndexableFileSet() {

            @Override
            public boolean isInSet(@NotNull final VirtualFile file) {
                return file.getFileSystem() == sourceDir.getFileSystem();
            }

            @Override
            public void iterateIndexableFilesIn(@NotNull final VirtualFile file, @NotNull final ContentIterator iterator) {
                if (file.isDirectory()) {
                    for (VirtualFile child : file.getChildren()) {
                        iterateIndexableFilesIn(child, iterator);
                    }
                } else {
                    iterator.processFile(file);
                }
            }
        };
        FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, module.getProject());
        Disposer.register(module, new Disposable() {

            @Override
            public void dispose() {
                FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet);
                ApplicationManager.getApplication().runWriteAction(() -> {
                    try {
                        sourceDir.delete(null);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        });
        final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
        VirtualFile[] toFiles = copyFiles(files, sourceDir, rawProjectRoot);
        if (auxiliaryFiles != null) {
            copyFiles(auxiliaryFiles, sourceDir, rawProjectRoot);
        }
        rootModel.addContentEntry(sourceDir).addSourceFolder(sourceDir, false);
        final List<String> libs = new ArrayList<>();
        if (moduleInitializer != null) {
            moduleInitializer.fun(rootModel, sourceDir, libs);
        }
        rootModel.commit();
        for (String path : libs) {
            VirtualFile virtualFile = path.charAt(0) != '/' ? getFile("lib", path) : getFile(path);
            FlexTestUtils.addLibrary(module, path, virtualFile.getParent().getPath(), virtualFile.getName(), null, null);
        }
        return toFiles;
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) ContentIterator(com.intellij.openapi.roots.ContentIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IndexableFileSet(com.intellij.util.indexing.IndexableFileSet) NotNull(org.jetbrains.annotations.NotNull) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel)

Aggregations

ContentIterator (com.intellij.openapi.roots.ContentIterator)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IndexableFileSet (com.intellij.util.indexing.IndexableFileSet)2 NotNull (org.jetbrains.annotations.NotNull)2 Disposable (com.intellij.openapi.Disposable)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1 VirtualFileVisitor (com.intellij.openapi.vfs.VirtualFileVisitor)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1