Search in sources :

Example 11 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class JavaVfsSourceRootDetectionUtil method suggestRoots.

/**
   * Scan directory and detect java source roots within it. The source root is detected as the following:
   * <ol>
   * <li>It contains at least one Java file.</li>
   * <li>Java file is located in the sub-folder that matches package statement in the file.</li>
   * </ol>
   *
   * @param dir a directory to scan
   * @param progressIndicator
   * @return a list of found source roots within directory. If no source roots are found, a empty list is returned.
   */
@NotNull
public static List<VirtualFile> suggestRoots(@NotNull VirtualFile dir, @NotNull final ProgressIndicator progressIndicator) {
    if (!dir.isDirectory()) {
        return ContainerUtil.emptyList();
    }
    final FileTypeManager typeManager = FileTypeManager.getInstance();
    final ArrayList<VirtualFile> foundDirectories = new ArrayList<>();
    try {
        VfsUtilCore.visitChildrenRecursively(dir, new VirtualFileVisitor() {

            @NotNull
            @Override
            public Result visitFileEx(@NotNull VirtualFile file) {
                progressIndicator.checkCanceled();
                if (file.isDirectory()) {
                    if (typeManager.isFileIgnored(file) || StringUtil.startsWithIgnoreCase(file.getName(), "testData")) {
                        return SKIP_CHILDREN;
                    }
                } else {
                    FileType type = typeManager.getFileTypeByFileName(file.getName());
                    if (StdFileTypes.JAVA == type) {
                        VirtualFile root = suggestRootForJavaFile(file);
                        if (root != null) {
                            foundDirectories.add(root);
                            return skipTo(root);
                        }
                    }
                }
                return CONTINUE;
            }
        });
    } catch (ProcessCanceledException ignore) {
    }
    return foundDirectories;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileType(com.intellij.openapi.fileTypes.FileType) ArrayList(java.util.ArrayList) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class FrameworkDetectionProcessor method collectSuitableFiles.

private void collectSuitableFiles(@NotNull VirtualFile file) {
    try {
        VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                // Since this code is invoked from New Project Wizard it's very possible that VFS isn't loaded to memory yet, so we need to do it
                // manually, otherwise refresh will do nothing
                myProgressIndicator.checkCanceled();
                return true;
            }
        });
        file.refresh(false, true);
        VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                myProgressIndicator.checkCanceled();
                if (!myProcessedFiles.add(file)) {
                    return false;
                }
                if (!file.isDirectory()) {
                    final FileType fileType = file.getFileType();
                    if (myDetectorsByFileType.containsKey(fileType)) {
                        myProgressIndicator.setText2(file.getPresentableUrl());
                        try {
                            final FileContent fileContent = new FileContentImpl(file, file.contentsToByteArray(false));
                            for (FrameworkDetectorData detector : myDetectorsByFileType.get(fileType)) {
                                if (detector.myFilePattern.accepts(fileContent)) {
                                    detector.mySuitableFiles.add(file);
                                }
                            }
                        } catch (IOException e) {
                            LOG.info(e);
                        }
                    }
                }
                return true;
            }
        });
    } catch (ProcessCanceledException ignored) {
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileContent(com.intellij.util.indexing.FileContent) FileType(com.intellij.openapi.fileTypes.FileType) FileContentImpl(com.intellij.util.indexing.FileContentImpl) IOException(java.io.IOException) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 13 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class AbstractConvertLineSeparatorsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent event) {
    final DataContext dataContext = event.getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        return;
    }
    final VirtualFile[] virtualFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    if (virtualFiles == null) {
        return;
    }
    VirtualFile projectVirtualDirectory = ProjectKt.getStateStore(project).getDirectoryStoreFile();
    final FileTypeRegistry fileTypeManager = FileTypeRegistry.getInstance();
    for (VirtualFile file : virtualFiles) {
        VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

            @NotNull
            @Override
            public Result visitFileEx(@NotNull VirtualFile file) {
                if (shouldProcess(file, project)) {
                    changeLineSeparators(project, file, mySeparator);
                }
                return file.isDirectory() && (file.equals(projectVirtualDirectory) || fileTypeManager.isFileIgnored(file)) ? SKIP_CHILDREN : CONTINUE;
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) FileTypeRegistry(com.intellij.openapi.fileTypes.FileTypeRegistry) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 14 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-plugins by JetBrains.

the class FlexSwcFoldersRootDetector method collectRoots.

private static void collectRoots(VirtualFile file, final List<VirtualFile> result, final ProgressIndicator progressIndicator) {
    if (!file.isDirectory() || file.getFileSystem() instanceof JarFileSystem)
        return;
    VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

        @NotNull
        @Override
        public Result visitFileEx(@NotNull VirtualFile child) {
            progressIndicator.checkCanceled();
            if (child.isDirectory()) {
                progressIndicator.setText2(child.getPresentableUrl());
            } else if ("swc".equalsIgnoreCase(child.getExtension()) || "ane".equalsIgnoreCase(child.getExtension())) {
                final VirtualFile dir = child.getParent();
                result.add(dir);
                return skipTo(dir);
            }
            return CONTINUE;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-plugins by JetBrains.

the class KarmaWatchPattern method watchRoot.

@Nullable
private LocalFileSystem.WatchRequest watchRoot(@NotNull final VirtualFile dir, final boolean reportChildren) {
    VfsUtilCore.visitChildrenRecursively(dir, new VirtualFileVisitor() {

        @Override
        public boolean visitFile(@NotNull VirtualFile file) {
            if (reportChildren && !file.isDirectory()) {
                myChangedFileManager.onFileAdded(file.getPath());
            }
            file.getChildren();
            return true;
        }
    });
    LocalFileSystem.WatchRequest watchRequest = myFileSystem.addRootToWatch(dir.getPath(), true);
    if (watchRequest == null) {
        LOG.warn("Can not watch valid directory " + dir.getPath());
    }
    return watchRequest;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 VirtualFileVisitor (com.intellij.openapi.vfs.VirtualFileVisitor)23 NotNull (org.jetbrains.annotations.NotNull)12 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)4 File (java.io.File)4 FileType (com.intellij.openapi.fileTypes.FileType)3 IOException (java.io.IOException)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 Result (com.intellij.openapi.application.Result)1 PatchApplier (com.intellij.openapi.diff.impl.patch.formove.PatchApplier)1 FileTypeRegistry (com.intellij.openapi.fileTypes.FileTypeRegistry)1 Module (com.intellij.openapi.module.Module)1 ModuleImpl (com.intellij.openapi.module.impl.ModuleImpl)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1