use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-plugins by JetBrains.
the class PubspecYamlUtil method findPubspecYamlFile.
@Nullable
public static VirtualFile findPubspecYamlFile(@NotNull final Project project, @NotNull final VirtualFile contextFile) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile current = contextFile;
VirtualFile parent = contextFile.isDirectory() ? contextFile : contextFile.getParent();
while (parent != null && (LIB_DIR_NAME.equals(current.getName()) || fileIndex.isInContent(parent))) {
current = parent;
final VirtualFile file = parent.findChild(PUBSPEC_YAML);
if (file != null && !file.isDirectory()) {
return file;
}
parent = current.getParent();
}
return null;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-plugins by JetBrains.
the class DartAnalysisServerService method doConfigureImportedLibraries.
private static void doConfigureImportedLibraries(@NotNull final Project project, @NotNull final Collection<String> filePaths) {
final DartSdk sdk = DartSdk.getDartSdk(project);
if (sdk == null)
return;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final SortedSet<String> folderPaths = new TreeSet<>();
final Collection<String> rootsToAddToLib = new THashSet<>();
for (final String path : filePaths) {
if (path != null) {
folderPaths.add(PathUtil.getParentPath(FileUtil.toSystemIndependentName(path)));
}
}
outer: for (final String path : folderPaths) {
final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(path);
if (!path.startsWith(sdk.getHomePath() + "/") && (vFile == null || !fileIndex.isInContent(vFile))) {
for (String configuredPath : rootsToAddToLib) {
if (path.startsWith(configuredPath + "/")) {
// folderPaths is sorted so subfolders go after parent folder
continue outer;
}
}
rootsToAddToLib.add(path);
}
}
final Processor<? super PsiFileSystemItem> falseProcessor = (Processor<PsiFileSystemItem>) item -> false;
final Condition<Module> moduleFilter = module -> DartSdkLibUtil.isDartSdkEnabled(module) && !FilenameIndex.processFilesByName(PubspecYamlUtil.PUBSPEC_YAML, false, falseProcessor, module.getModuleContentScope(), project, null);
final DartFileListener.DartLibInfo libInfo = new DartFileListener.DartLibInfo(true);
libInfo.addRoots(rootsToAddToLib);
final Library library = DartFileListener.updatePackagesLibraryRoots(project, libInfo);
DartFileListener.updateDependenciesOnDartPackagesLibrary(project, moduleFilter, library);
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-plugins by JetBrains.
the class JavaStepDefinitionCreator method getDefaultStepDefinitionFolder.
@NotNull
@Override
public PsiDirectory getDefaultStepDefinitionFolder(@NotNull final GherkinStep step) {
PsiFile featureFile = step.getContainingFile();
if (featureFile != null) {
PsiDirectory psiDirectory = featureFile.getContainingDirectory();
final Project project = step.getProject();
if (psiDirectory != null) {
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile directory = psiDirectory.getVirtualFile();
if (projectFileIndex.isInContent(directory)) {
VirtualFile sourceRoot = projectFileIndex.getSourceRootForFile(directory);
//noinspection ConstantConditions
final Module module = projectFileIndex.getModuleForFile(featureFile.getVirtualFile());
if (module != null) {
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
if (sourceRoot != null && sourceRoot.getName().equals("resources")) {
final VirtualFile resourceParent = sourceRoot.getParent();
for (VirtualFile vFile : sourceRoots) {
if (vFile.getPath().startsWith(resourceParent.getPath()) && vFile.getName().equals("java")) {
sourceRoot = vFile;
break;
}
}
} else {
if (sourceRoots.length > 0) {
sourceRoot = sourceRoots[sourceRoots.length - 1];
}
}
}
String packageName = "";
if (sourceRoot != null) {
packageName = CucumberJavaUtil.getPackageOfStepDef(step);
}
final String packagePath = packageName.replace('.', '/');
final String path = sourceRoot != null ? sourceRoot.getPath() : directory.getPath();
// ToDo: I shouldn't create directories, only create VirtualFile object.
final Ref<PsiDirectory> resultRef = new Ref<>();
new WriteAction() {
protected void run(@NotNull Result result) throws Throwable {
final VirtualFile packageFile = VfsUtil.createDirectoryIfMissing(path + '/' + packagePath);
if (packageFile != null) {
resultRef.set(PsiDirectoryFactory.getInstance(project).createDirectory(packageFile));
}
}
}.execute();
return resultRef.get();
}
}
}
assert featureFile != null;
return ObjectUtils.assertNotNull(featureFile.getParent());
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-plugins by JetBrains.
the class DartTestSourcesFilter method isTestSource.
@Override
public boolean isTestSource(@NotNull final VirtualFile file, @NotNull final Project project) {
if (!file.isInLocalFileSystem())
return false;
final ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);
if (!fileIndex.isInContent(file))
return false;
if (DartIconProvider.isFolderNearPubspecYaml(file, "test"))
return true;
// quick fail
if (!file.getPath().contains("/test/"))
return false;
VirtualFile parent = file;
while ((parent = parent.getParent()) != null && fileIndex.isInContent(parent)) {
if (DartIconProvider.isFolderNearPubspecYaml(parent, "test"))
return true;
}
return false;
}
use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-plugins by JetBrains.
the class DartBuildFileUtil method findPackageRootBuildFile.
/**
* Return the BUILD build in the root of the package that contains the given context file.
*
* This may be not the closest BUILD file.
* For example it will ignore "examples/BUILD" file, because the enclosing folder contains a "lib" folder and another BUILD file.
*/
@Nullable
public static VirtualFile findPackageRootBuildFile(@NotNull final Project project, @NotNull final VirtualFile contextFile) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile parent = contextFile.isDirectory() ? contextFile : contextFile.getParent();
while (parent != null && fileIndex.isInContent(parent)) {
final VirtualFile file = parent.findChild(BUILD_FILE_NAME);
if (file != null && !file.isDirectory()) {
final VirtualFile parent2 = parent.getParent();
if (parent2 != null && parent2.findChild(LIB_DIR_NAME) == null) {
return file;
}
}
parent = parent.getParent();
}
return null;
}
Aggregations