Java Code Examples for com.intellij.openapi.vfs.VfsUtil#isAncestor()

The following examples show how to use com.intellij.openapi.vfs.VfsUtil#isAncestor() . 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: DirectoryChooser.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
@Nonnull
public Image getIcon() {
  if (myDirectory != null) {
    VirtualFile virtualFile = myDirectory.getVirtualFile();
    List<ContentFolder> contentFolders = ModuleUtilCore.getContentFolders(myDirectory.getProject());
    for (ContentFolder contentFolder : contentFolders) {
      VirtualFile file = contentFolder.getFile();
      if(file == null) {
        continue;
      }
      if(VfsUtil.isAncestor(file, virtualFile, false)) {
        return contentFolder.getType().getIcon(contentFolder.getProperties());
      }
    }
  }
  return AllIcons.Nodes.Folder;
}
 
Example 2
Source File: IgnoredFileBean.java    From consulo with Apache License 2.0 6 votes vote down vote up
public boolean matchesFile(VirtualFile file) {
  if (myType == IgnoreSettingsType.MASK) {
    myMatcher.reset(file.getName());
    return myMatcher.matches();
  } else {
    // quick check for 'file' == exact match pattern
    if (IgnoreSettingsType.FILE.equals(myType) && ! myFilenameIfFile.equals(file.getName())) return false;

    VirtualFile selector = resolve();
    if (Comparing.equal(selector, NullVirtualFile.INSTANCE)) return false;

    if (myType == IgnoreSettingsType.FILE) {
      return Comparing.equal(selector, file);
    }
    else {
      if ("./".equals(myPath)) {
        // special case for ignoring the project base dir (IDEADEV-16056)
        return !file.isDirectory() && Comparing.equal(file.getParent(), selector);
      }
      return VfsUtil.isAncestor(selector, file, false);
    }
  }
}
 
Example 3
Source File: MakeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static VirtualFile getSourceRoot(CompileContext context, Module module, VirtualFile file) {
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(module.getProject()).getFileIndex();
  final VirtualFile root = fileIndex.getSourceRootForFile(file);
  if (root != null) {
    return root;
  }
  // try to find among roots of generated files.
  final VirtualFile[] sourceRoots = context.getSourceRoots(module);
  for (final VirtualFile sourceRoot : sourceRoots) {
    if (fileIndex.isInSourceContent(sourceRoot)) {
      continue; // skip content source roots, need only roots for generated files
    }
    if (VfsUtil.isAncestor(sourceRoot, file, false)) {
      return sourceRoot;
    }
  }
  return null;
}
 
Example 4
Source File: ExcludedEntriesConfiguration.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isExcluded(VirtualFile virtualFile) {
  for (final ExcludeEntryDescription entryDescription : getExcludeEntryDescriptions()) {
    VirtualFile descriptionFile = entryDescription.getVirtualFile();
    if (descriptionFile == null) {
      continue;
    }
    if (entryDescription.isFile()) {
      if (descriptionFile.equals(virtualFile)) {
        return true;
      }
    }
    else if (entryDescription.isIncludeSubdirectories()) {
      if (VfsUtil.isAncestor(descriptionFile, virtualFile, false)) {
        return true;
      }
    }
    else {
      if (virtualFile.isDirectory()) {
        continue;
      }
      if (descriptionFile.equals(virtualFile.getParent())) {
        return true;
      }
    }
  }
  return false;
}
 
Example 5
Source File: FilesForRefresh.java    From consulo with Apache License 2.0 5 votes vote down vote up
public int compare(File o1, File o2) {
  if (Comparing.equal(o1.getAbsolutePath(), o2.getAbsolutePath())) return 0;
  if (VfsUtil.isAncestor(o1, o2, true)) {
    return -1;
  }
  if (VfsUtil.isAncestor(o2, o1, true)) {
    return 1;
  }
  return 0;
}
 
Example 6
Source File: ProjectViewNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean someChildContainsFile(final VirtualFile file, boolean optimizeByCheckingFileRootsFirst) {
  VirtualFile parent = file.getParent();

  boolean mayContain = false;

  if (optimizeByCheckingFileRootsFirst && parent != null) {
    Collection<VirtualFile> roots = getRoots();
    for (VirtualFile eachRoot : roots) {
      if (parent.equals(eachRoot.getParent())) {
        mayContain = true;
        break;
      }

      if (VfsUtil.isAncestor(eachRoot, file, true)) {
        mayContain = true;
        break;
      }
    }
  }
  else {
    mayContain = true;
  }

  if (!mayContain) {
    return false;
  }

  Collection<? extends AbstractTreeNode> kids = getChildren();
  for (final AbstractTreeNode kid : kids) {
    ProjectViewNode node = (ProjectViewNode)kid;
    if (node.contains(file)) return true;
  }
  return false;
}
 
Example 7
Source File: ArtifactUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getRelativePathInSources(@Nonnull VirtualFile file,
                                               final @Nonnull ModuleOutputPackagingElement moduleElement,
                                               @Nonnull PackagingElementResolvingContext context) {
  for (VirtualFile sourceRoot : moduleElement.getSourceRoots(context)) {
    if (VfsUtil.isAncestor(sourceRoot, file, true)) {
      return VfsUtilCore.getRelativePath(file, sourceRoot, '/');
    }
  }
  return null;
}
 
Example 8
Source File: SymfonyBundle.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public boolean isInBundle(@Nullable VirtualFile virtualFile) {
    if(virtualFile == null) {
        return false;
    }

    PsiDirectory psiDirectory =  this.getDirectory();
    return psiDirectory != null && VfsUtil.isAncestor(psiDirectory.getVirtualFile(), virtualFile, false);
}
 
Example 9
Source File: ServicesDefinitionStubIndex.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) {

        String fileName = psiFile.getName();
        if(fileName.startsWith(".") || fileName.endsWith("Test")) {
            return false;
        }

        // container file need to be xml file, eg xsd filetypes are not valid
        String extension = inputData.getFile().getExtension();
        if(extension == null || !(extension.equalsIgnoreCase("xml") || extension.equalsIgnoreCase("yml") || extension.equalsIgnoreCase("yaml") || extension.equalsIgnoreCase("php"))) {
            return false;
        }

        // possible fixture or test file
        // to support also library paths, only filter them on project files
        String relativePath = VfsUtil.getRelativePath(inputData.getFile(), ProjectUtil.getProjectDir(inputData.getProject()), '/');
        if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Tests/") || relativePath.contains("/Fixture/") || relativePath.contains("/Fixtures/"))) {
            return false;
        }

        // dont add configured service paths
        Collection<File> settingsServiceFiles = psiFile.getProject().getComponent(Symfony2ProjectComponent.class).getContainerFiles();
        for(File file: settingsServiceFiles) {
            if(VfsUtil.isAncestor(VfsUtil.virtualToIoFile(inputData.getFile()), file, false)) {
                return false;
            }
        }

        // dont index files larger then files; use 5 MB here
        if(inputData.getFile().getLength() > MAX_FILE_BYTE_SIZE) {
            return false;
        }

        return true;
    }
 
Example 10
Source File: LoadedChangeFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean ok(final File vf) {
  for (File root : myRoots) {
    if (VfsUtil.isAncestor(root, vf, false)) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: IdeaUtils.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
public static boolean isInProjectContentRoot(@Nonnull final Project project, @Nonnull final VirtualFile file) {
  for (final VirtualFile root : ProjectRootManager.getInstance(project).getContentRoots()) {
    if (VfsUtil.isAncestor(root, file, false)) {
      return true;
    }
  }
  return false;
}
 
Example 12
Source File: BasicDefaultVcsRootPolicy.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean matchesDefaultMapping(@Nonnull final VirtualFile file, final Object matchContext) {
  return VfsUtil.isAncestor(myBaseDir, file, false);
}
 
Example 13
Source File: RootsCollection.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
@Override
protected boolean isAncestor(@NotNull VirtualFile parent, @NotNull VirtualFile child) {
    return VfsUtil.isAncestor(parent, child, false);
}
 
Example 14
Source File: UnityNamespaceGeneratePolicy.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public String calculateDirtyNamespace(@Nonnull PsiDirectory psiDirectory)
{
	Project project = psiDirectory.getProject();
	VirtualFile baseDir = project.getBaseDir();
	if(baseDir == null)
	{
		return myNamespacePrefix;
	}

	VirtualFile currentDirectory = psiDirectory.getVirtualFile();

	VirtualFile assetsDirectory = baseDir.findChild(Unity3dProjectImportUtil.ASSETS_DIRECTORY);
	if(assetsDirectory != null)
	{
		VirtualFile targetDirectory = assetsDirectory;

		VirtualFile temp = currentDirectory;
		while(temp != null && !temp.equals(targetDirectory))
		{
			if("Editor".equals(temp.getName()))
			{
				targetDirectory = temp;
			}
			temp = temp.getParent();
		}

		// if path is not changed
		if(targetDirectory.equals(assetsDirectory))
		{
			temp = currentDirectory;
			while(temp != null && !temp.equals(targetDirectory))
			{
				if("Scripts".equals(temp.getName()))
				{
					targetDirectory = temp;
				}
				temp = temp.getParent();
			}
		}

		// if path is not changed
		if(targetDirectory.equals(assetsDirectory))
		{
			for(String path : Unity3dProjectImportUtil.FIRST_PASS_PATHS)
			{
				VirtualFile child = baseDir.findFileByRelativePath(path);
				if(child != null && VfsUtil.isAncestor(child, currentDirectory, false))
				{
					targetDirectory = child;
					break;
				}
			}
		}

		String relativePath = VfsUtil.getRelativePath(currentDirectory, targetDirectory, '.');
		if(relativePath != null)
		{
			if(!StringUtil.isEmpty(myNamespacePrefix))
			{
				if(StringUtil.isEmpty(relativePath))
				{
					return myNamespacePrefix;
				}
				return myNamespacePrefix + "." + relativePath;
			}
			return relativePath;
		}
	}
	return myNamespacePrefix;
}
 
Example 15
Source File: DefaultFileIndexFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isInContent(@Nonnull final VirtualFile file) {
  return VfsUtil.isAncestor(getBaseDir(), file, false);
}
 
Example 16
Source File: DescindingFilesFilter.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static FilePath[] filterDescindingFiles(@Nonnull FilePath[] roots, Project project) {
  final List<FilePath> result = new LinkedList<FilePath>();
  ProjectLevelVcsManager manager = ProjectLevelVcsManager.getInstance(project);

  Arrays.sort(roots, FilePathComparator.getInstance());
  final Map<VcsKey, List<FilePath>> chains = new HashMap<VcsKey, List<FilePath>>();
  for (FilePath root : roots) {
    final AbstractVcs vcs = manager.getVcsFor(root);
    if (vcs == null) continue;
    if (vcs.allowsNestedRoots()) {
      // just put into result: nested roots are allowed
      result.add(root);
      continue;
    }
    //if (pathsFilter != null && (! pathsFilter.convert(new Pair<FilePath, AbstractVcs>(root, vcs)))) continue;
    
    final List<FilePath> chain = chains.get(vcs.getKeyInstanceMethod());
    if (chain == null) {
      final LinkedList<FilePath> newList = new LinkedList<FilePath>();
      newList.add(root);
      chains.put(vcs.getKeyInstanceMethod(), newList);
    } else {
      boolean failed = false;
      for (FilePath chainedPath : chain) {
        if (VfsUtil.isAncestor(chainedPath.getIOFile(), root.getIOFile(), false)) {
          // do not take this root
          failed = true;      
          break;
        }
      }
      if (! failed) {
        chain.add(root);
      }
    }
  }

  for (List<FilePath> filePaths : chains.values()) {
    result.addAll(filePaths);
  }

  return result.toArray(new FilePath[result.size()]);
}
 
Example 17
Source File: ProjectFilesViewProjectNode.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean contains(@NotNull VirtualFile file) {
  final Optional<VirtualFile> projectBuildRoot = PantsUtil.findBuildRoot(myProject.getBaseDir());
  return super.contains(file) ||
         (projectBuildRoot.isPresent() && VfsUtil.isAncestor(projectBuildRoot.get(), file, true));
}
 
Example 18
Source File: FilterDescendantVirtualFileConvertible.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isAncestor(final T parent, final T child) {
  return VfsUtil.isAncestor(myConvertor.apply(parent), myConvertor.apply(child), false);
}
 
Example 19
Source File: DefaultFileIndexFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValidAncestor(@Nonnull final VirtualFile baseDir, @Nonnull final VirtualFile childDir) {
  return VfsUtil.isAncestor(baseDir, childDir, false);
}
 
Example 20
Source File: VirtualFileTreeNode.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean contains(@NotNull VirtualFile file) {
  final VirtualFile myFile = getValue();
  return myFile.isDirectory() && VfsUtil.isAncestor(myFile, file, true);
}