Java Code Examples for com.intellij.openapi.vfs.VirtualFile#findChild()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#findChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlutterUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
public static Module findFlutterGradleModule(@NotNull Project project) {
  String moduleName = AndroidUtils.FLUTTER_MODULE_NAME;
  Module module = findModuleNamed(project, moduleName);
  if (module == null) {
    moduleName = flutterGradleModuleName(project);
    module = findModuleNamed(project, moduleName);
    if (module == null) {
      return null;
    }
  }
  VirtualFile file = locateModuleRoot(module);
  if (file == null) {
    return null;
  }
  file = file.getParent().getParent();
  VirtualFile meta = file.findChild(".metadata");
  if (meta == null) {
    return null;
  }
  VirtualFile android = getFlutterManagedAndroidDir(meta.getParent());
  if (android != null && android.getName().equals(".android")) {
    return module; // Only true for Flutter modules.
  }
  return null;
}
 
Example 2
Source File: OpenInAndroidStudioAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static VirtualFile getProjectForFile(@Nullable VirtualFile file) {
  // Expect true: isProjectFileName(file.getName()), but some flexibility is allowed.
  if (file == null) {
    return null;
  }
  if (file.isDirectory()) {
    return isAndroidWithApp(file) ? file : null;
  }
  VirtualFile dir = file.getParent();
  if (isAndroidWithApp(dir)) {
    // In case someone moves the .iml file, or the project organization gets rationalized.
    return dir;
  }
  VirtualFile project = dir.findChild("android");
  if (project != null && isAndroidWithApp(project)) {
    return project;
  }
  project = dir.findChild(".android");
  if (project != null && isAndroidWithApp(project)) {
    return project;
  }
  return null;
}
 
Example 3
Source File: Platform.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@NotNull
private static Map<Module, VirtualFile> findContentRootsFor(@NotNull Project project, @NotNull String filename) {
    Map<Module, VirtualFile> rootContents = new HashMap<>();

    ModuleManager moduleManager = ModuleManager.getInstance(project);
    for (Module module : moduleManager.getModules()) {
        for (VirtualFile contentRoot : ModuleRootManager.getInstance(module).getContentRoots()) {
            VirtualFile packageJson = contentRoot.findChild(filename);
            if (packageJson != null && !rootContents.containsKey(module)) {
                rootContents.put(module, packageJson);
            }
        }
    }

    return rootContents;
}
 
Example 4
Source File: DifferenceReverterTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testParentRename() throws Exception {
  VirtualFile dir = myRoot.createChildDirectory(this, "dir");
  VirtualFile f = dir.createChildData(this, "foo.txt");
  f.setBinaryContent(new byte[]{123}, -1, 4000);

  dir.rename(this, "dir2");

  revertLastChange();

  assertNull(myRoot.findChild("dir2"));
  dir = myRoot.findChild("dir");
  f = dir.findChild("foo.txt");
  assertNotNull(f);
  assertEquals(123, f.contentsToByteArray()[0]);
  assertEquals(4000, f.getTimeStamp());
}
 
Example 5
Source File: AndroidUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static boolean isVanillaAddToApp(@NotNull Project project, @Nullable VirtualFile file, @NotNull String name) {
  if (file == null) {
    return false;
  }
  VirtualFile dir = file.getParent();
  if (dir.findChild(".ios") == null) {
    dir = dir.getParent();
  }
  if (dir.getName().equals(".android")) {
    dir = dir.getParent();
  }
  if (dir.findChild(".ios") == null && dir.findChild("ios") == null) {
    return false;
  }
  if (doesBuildFileExist(dir)) {
    return true;
  }
  GradleSettingsFile parsedSettings = parseSettings(project);
  if (parsedSettings == null) {
    return false;
  }
  // Check settings for "include :name".
  return findInclude(requireNonNull(parsedSettings), name) == null;
}
 
Example 6
Source File: FilesystemUtil.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Nullable
public static VirtualFile findExtensionRootFolder(@NotNull VirtualFile file) {
    if (file.isDirectory()) {
        VirtualFile child = file.findChild("ext_emconf.php");

        if (child != null) {
            return file;
        }
    }

    // dragons ahead.
    if (file.getParent() != null) {
        return findExtensionRootFolder(file.getParent());
    }

    return null;
}
 
Example 7
Source File: OpenInAndroidStudioAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static VirtualFile getProjectForFile(@Nullable VirtualFile file) {
  // Expect true: isProjectFileName(file.getName()), but some flexibility is allowed.
  if (file == null) {
    return null;
  }
  if (file.isDirectory()) {
    return isAndroidWithApp(file) ? file : null;
  }
  VirtualFile dir = file.getParent();
  if (isAndroidWithApp(dir)) {
    // In case someone moves the .iml file, or the project organization gets rationalized.
    return dir;
  }
  VirtualFile project = dir.findChild("android");
  if (project != null && isAndroidWithApp(project)) {
    return project;
  }
  project = dir.findChild(".android");
  if (project != null && isAndroidWithApp(project)) {
    return project;
  }
  return null;
}
 
Example 8
Source File: Utils.java    From idea-gitignore with MIT License 6 votes vote down vote up
/**
 * Returns all Ignore files in given {@link Project} that can match current passed file.
 *
 * @param project current project
 * @param file    current file
 * @return collection of suitable Ignore files
 */
public static List<VirtualFile> getSuitableIgnoreFiles(@NotNull Project project, @NotNull IgnoreFileType fileType,
                                                       @NotNull VirtualFile file)
        throws ExternalFileException {
    VirtualFile baseDir = getModuleRootForFile(file, project);
    List<VirtualFile> files = new ArrayList<>();
    if (file.getCanonicalPath() == null || baseDir == null ||
            !VfsUtilCore.isAncestor(baseDir, file, true)) {
        throw new ExternalFileException();
    }
    if (!baseDir.equals(file)) {
        do {
            file = file.getParent();
            VirtualFile ignoreFile = file.findChild(fileType.getIgnoreLanguage().getFilename());
            ContainerUtil.addIfNotNull(files, ignoreFile);
        } while (!file.equals(baseDir));
    }
    return files;
}
 
Example 9
Source File: PubRootCache.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private VirtualFile findPubspecDir(VirtualFile file) {
  if (file == null) {
    return null;
  }

  if (file.isDirectory()) {
    final VirtualFile pubspec = file.findChild("pubspec.yaml");
    if (pubspec != null && pubspec.exists() && !pubspec.isDirectory()) {
      return file;
    }
  }

  return findPubspecDir(file.getParent());
}
 
Example 10
Source File: AndroidModuleLibraryManager.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean hasAndroidDir(Project project) {
  if (FlutterSdkUtil.hasFlutterModules(project)) {
    VirtualFile base = project.getBaseDir();
    VirtualFile dir = base.findChild("android");
    if (dir == null) dir = base.findChild(".android");
    return dir != null;
  }
  else {
    return false;
  }
}
 
Example 11
Source File: BuckBuildUtil.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Return the virtual file of the BUCK file of the given target. TODO(#7908675): Use Buck's
 * BuildTargetFactory and deprecate this class.
 */
public static VirtualFile getBuckFileFromAbsoluteTarget(Project project, String target) {
  if (!isValidAbsoluteTarget(target)) {
    return null;
  }
  VirtualFile buckDir =
      project.getBaseDir().findFileByRelativePath(extractAbsoluteTarget(target));
  return buckDir != null ? buckDir.findChild(BuckFileUtil.getBuildFileName()) : null;
}
 
Example 12
Source File: Workspace.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the closest WORKSPACE file within or above the given directory, or null if not found.
 */
@Nullable
private static VirtualFile findContainingWorkspaceFile(@NotNull VirtualFile dir) {
  while (dir != null) {
    final VirtualFile child = dir.findChild("WORKSPACE");
    if (child != null && child.exists() && !child.isDirectory()) {
      return child;
    }
    dir = dir.getParent();
  }

  // not found
  return null;
}
 
Example 13
Source File: IntelliJIDEA.java    From intellijcoder with MIT License 5 votes vote down vote up
private VirtualFile findFileInModule(Module module, String fileName) {
    // module name matches class, so we can look at a specific module
    VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
    if (sourceRoots.length == 0) {
        return null;
    }
    for (VirtualFile root : sourceRoots) {
        VirtualFile file = root.findChild(fileName);
        if (file != null) return file;
    }
    return null;
}
 
Example 14
Source File: FileUtils.java    From JHelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nullable
private static VirtualFile findChild(VirtualFile file, @NotNull String child) {
	if (child.equals(".")) {
		return file;
	}
	if (child.equals("..")) {
		return file.getParent();
	}
	return file.findChild(child);
}
 
Example 15
Source File: ProjectKt.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile getDirectoryStoreFile(Project project) {
  VirtualFile baseDir = project.getBaseDir();
  if(baseDir == null) {
    return null;
  }
  return baseDir.findChild(Project.DIRECTORY_STORE_FOLDER);
}
 
Example 16
Source File: OpenInAndroidStudioAction.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isAndroidWithApp(@NotNull VirtualFile file) {
  return FlutterExternalIdeActionGroup.isAndroidDirectory(file) && (file.findChild("app") != null || file.findChild("src") != null);
}
 
Example 17
Source File: FlutterUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
private static VirtualFile getFlutterManagedAndroidDir(VirtualFile dir) {
  VirtualFile meta = dir.findChild(".metadata");
  if (meta != null) {
    try {
      Properties properties = new Properties();
      properties.load(new InputStreamReader(meta.getInputStream(), Charsets.UTF_8));
      String value = properties.getProperty("project_type");
      if (value == null) {
        return null;
      }
      switch (value) {
        case "app":
          return dir.findChild("android");
        case "module":
          return dir.findChild(".android");
        case "package":
          return null;
        case "plugin":
          return dir.findFileByRelativePath("example/android");
      }
    }
    catch (IOException e) {
      // fall thru
    }
  }
  VirtualFile android;
  android = dir.findChild(".android");
  if (android != null) {
    return android;
  }
  android = dir.findChild("android");
  if (android != null) {
    return android;
  }
  android = dir.findFileByRelativePath("example/android");
  if (android != null) {
    return android;
  }
  return null;
}
 
Example 18
Source File: FlutterUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static VirtualFile getAndroidProjectDir(VirtualFile dir) {
  return (dir.findChild("app") == null) ? null : dir;
}
 
Example 19
Source File: RootIndex.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public Query<VirtualFile> getDirectoriesByPackageName(@Nonnull final String packageName, final boolean includeLibrarySources) {
  List<VirtualFile> result = myDirectoriesByPackageNameCache.get(packageName);
  if (result == null) {
    if (myNonExistentPackages.contains(packageName)) return EmptyQuery.getEmptyQuery();

    result = ContainerUtil.newSmartList();

    if (StringUtil.isNotEmpty(packageName) && !StringUtil.startsWithChar(packageName, '.')) {
      int i = packageName.lastIndexOf('.');
      while (true) {
        String shortName = packageName.substring(i + 1);
        String parentPackage = i > 0 ? packageName.substring(0, i) : "";
        for (VirtualFile parentDir : getDirectoriesByPackageName(parentPackage, true)) {
          VirtualFile child = !parentDir.isValid() ? null : parentDir.findChild(shortName);
          if (child != null && child.isDirectory() && getInfoForFile(child).isInProject() && packageName.equals(getPackageName(child))) {
            result.add(child);
          }
        }
        if (i < 0) break;
        i = packageName.lastIndexOf('.', i - 1);
      }
    }

    for (VirtualFile file : myPackagePrefixRoots.get(packageName)) {
      if (file.isDirectory()) {
        result.add(file);
      }
    }

    if (!result.isEmpty()) {
      myDirectoriesByPackageNameCache.put(packageName, result);
    }
    else {
      myNonExistentPackages.add(packageName);
    }
  }

  if (!includeLibrarySources) {
    result = ContainerUtil.filter(result, file -> {
      DirectoryInfo info = getInfoForFile(file);
      return info.isInProject() && (!info.isInLibrarySource() || info.isInModuleSource() || info.hasLibraryClassRoot());
    });
  }
  return new CollectionQuery<>(result);
}
 
Example 20
Source File: FlutterSdkVersion.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public static FlutterSdkVersion readFromSdk(@NotNull VirtualFile sdkHome) {
  final VirtualFile file = sdkHome.findChild("version");

  return readFromFile(file);
}