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

The following examples show how to use com.intellij.openapi.vfs.VfsUtilCore#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: LocalChangesUnderRoots.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
private VirtualFile getRootForPath(@Nonnull FilePath file, @Nonnull Collection<VirtualFile> rootsToSave) {
  final VirtualFile vf = ChangesUtil.findValidParentUnderReadAction(file);
  if (vf == null) {
    return null;
  }
  VirtualFile rootCandidate = null;
  for (VcsRoot root : myRoots) {
    if (VfsUtilCore.isAncestor(root.getPath(), vf, false)) {
      if (rootCandidate == null || VfsUtil.isAncestor(rootCandidate, root.getPath(), true)) { // in the case of nested roots choose the closest root
        rootCandidate = root.getPath();
      }
    }
  }
  if (! rootsToSave.contains(rootCandidate)) return null;
  return rootCandidate;
}
 
Example 2
Source File: PatchProjectUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void processIncluded(final ContentEntry contentEntry, final Set<VirtualFile> included) {
  if (included.isEmpty()) return;
  final Set<VirtualFile> parents = new HashSet<VirtualFile>();
  for (VirtualFile file : included) {
    if (Comparing.equal(file, contentEntry.getFile())) return;
    final VirtualFile parent = file.getParent();
    if (parent == null || parents.contains(parent)) continue;
    parents.add(parent);
    for (VirtualFile toExclude : parent.getChildren()) {  // if it will ever dead-loop on symlink blame anna.kozlova
      boolean toExcludeSibling = true;
      for (VirtualFile includeRoot : included) {
        if (VfsUtilCore.isAncestor(toExclude, includeRoot, false)) {
          toExcludeSibling = false;
        }
      }
      if (toExcludeSibling) {
        contentEntry.addFolder(toExclude, ExcludedContentFolderTypeProvider.getInstance());
      }
    }
  }
  processIncluded(contentEntry, parents);
}
 
Example 3
Source File: SelectionManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
private TreeNodeState getStateImpl(final StateWorker stateWorker) {
  if (stateWorker.getVf() == null) return TreeNodeState.CLEAR;

  final TreeNodeState stateSelf = myState.get(stateWorker.getVf());
  if (stateSelf != null) return stateSelf;

  final Ref<TreeNodeState> result = new Ref<>();
  stateWorker.iterateParents(myState, (virtualFile, state) -> {
    if (state != null) {
      if (TreeNodeState.SELECTED.equals(state) || TreeNodeState.HAVE_SELECTED_ABOVE.equals(state)) {
        result.set(myState.putAndPass(stateWorker.getVf(), TreeNodeState.HAVE_SELECTED_ABOVE));
      }
      return false; // exit
    }
    return true;
  });

  if (! result.isNull()) return  result.get();

  for (VirtualFile selected : myState.getSelected()) {
    if (VfsUtilCore.isAncestor(stateWorker.getVf(), selected, true)) {
      return myState.putAndPass(stateWorker.getVf(), TreeNodeState.HAVE_SELECTED_BELOW);
    }
  }
  return TreeNodeState.CLEAR;
}
 
Example 4
Source File: AbstractFileTreeTable.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void select(@Nonnull VirtualFile toSelect, final TreeNode root) {
  for (int i = 0; i < root.getChildCount(); i++) {
    TreeNode child = root.getChildAt(i);
    VirtualFile file = ((FileNode)child).getObject();
    if (VfsUtilCore.isAncestor(file, toSelect, false)) {
      if (Comparing.equal(file, toSelect)) {
        TreeUtil.selectNode(getTree(), child);
        getSelectionModel().clearSelection();
        addSelectedPath(TreeUtil.getPathFromRoot(child));
        TableUtil.scrollSelectionToVisible(this);
      }
      else {
        select(toSelect, child);
      }
      return;
    }
  }
}
 
Example 5
Source File: GlobalSearchScopesCore.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public GlobalSearchScope uniteWith(@Nonnull GlobalSearchScope scope) {
  if (equals(scope)) return this;
  if (scope instanceof DirectoryScope) {
    DirectoryScope other = (DirectoryScope)scope;
    VirtualFile otherDirectory = other.myDirectory;
    if (myWithSubdirectories && VfsUtilCore.isAncestor(myDirectory, otherDirectory, false)) return this;
    if (other.myWithSubdirectories && VfsUtilCore.isAncestor(otherDirectory, myDirectory, false)) return other;
    BitSet newWithSubdirectories = new BitSet();
    newWithSubdirectories.set(0, myWithSubdirectories);
    newWithSubdirectories.set(1, other.myWithSubdirectories);
    return new DirectoriesScope(getProject(), new VirtualFile[]{myDirectory,otherDirectory}, newWithSubdirectories);
  }
  return super.uniteWith(scope);
}
 
Example 6
Source File: AbstractModuleNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean contains(@Nonnull VirtualFile file) {
  Module module = getValue();
  if (module == null || module.isDisposed()) return false;

  final VirtualFile testee;
  if (file.getFileSystem() instanceof ArchiveFileSystem) {
    testee = ((ArchiveFileSystem)file.getFileSystem()).getLocalVirtualFileFor(file);
    if (testee == null) return false;
  }
  else {
    testee = file;
  }
  for (VirtualFile root : ModuleRootManager.getInstance(module).getContentRoots()) {
    if (VfsUtilCore.isAncestor(root, testee, false)) return true;
  }
  return false;
}
 
Example 7
Source File: ModuleDefaultVcsRootPolicy.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@javax.annotation.Nullable
public VirtualFile getVcsRootFor(@Nonnull VirtualFile file) {
  FileIndexFacade indexFacade = ServiceManager.getService(myProject, FileIndexFacade.class);
  if (myBaseDir != null && indexFacade.isValidAncestor(myBaseDir, file)) {
    LOG.debug("File " + file + " is under project base dir " + myBaseDir);
    return myBaseDir;
  }
  VirtualFile contentRoot = ProjectRootManager.getInstance(myProject).getFileIndex().getContentRootForFile(file, Registry.is("ide.hide.excluded.files"));
  if (contentRoot != null) {
    LOG.debug("Content root for file " + file + " is " + contentRoot);
    if (contentRoot.isDirectory()) {
      return contentRoot;
    }
    VirtualFile parent = contentRoot.getParent();
    LOG.debug("Content root is not a directory, using its parent " + parent);
    return parent;
  }
  if (ProjectKt.isDirectoryBased(myProject)) {
    VirtualFile ideaDir = ProjectKt.getDirectoryStoreFile(myProject);
    if (ideaDir != null && VfsUtilCore.isAncestor(ideaDir, file, false)) {
      LOG.debug("File " + file + " is under .idea");
      return ideaDir;
    }
  }
  LOG.debug("Couldn't find proper root for " + file);
  return null;
}
 
Example 8
Source File: RecursiveFileHolder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsFile(final VirtualFile file) {
  if (myMap.containsKey(file)) return true;
  final VirtualFile floor = myDirMap.floorKey(file);
  if (floor == null) return false;
  final SortedMap<VirtualFile, T> floorMap = myDirMap.headMap(floor, true);
  for (VirtualFile parent : floorMap.keySet()) {
    if (VfsUtilCore.isAncestor(parent, file, false)) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: FileSystemTreeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isUnderRoots(@Nonnull VirtualFile file) {
  final List<VirtualFile> roots = myDescriptor.getRoots();
  if (roots.size() == 0) return true;

  for (VirtualFile root : roots) {
    if (root != null && VfsUtilCore.isAncestor(root, file, false)) {
      return true;
    }
  }

  return false;
}
 
Example 10
Source File: VcsIntegrationEnabler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isProjectBelowVcs(@Nonnull Collection<VirtualFile> vcsRoots) {
  //check if there are vcs roots strictly above the project dir
  VirtualFile baseDir = myProject.getBaseDir();
  for (VirtualFile root : vcsRoots) {
    if (VfsUtilCore.isAncestor(root, baseDir, true)) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: AdditionalIndexableFileSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInSet(@Nonnull VirtualFile file) {
  for (final VirtualFile root : getDirectories()) {
    if (VfsUtilCore.isAncestor(root, file, false)) {
      return true;
    }
  }
  return cachedFiles.contains(file);
}
 
Example 12
Source File: DvcsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static VirtualFile getVcsRootForLibraryFile(@Nonnull Project project, @Nonnull VirtualFile file) {
  ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
  // for a file inside .jar/.zip consider the .jar/.zip file itself
  VirtualFile root = vcsManager.getVcsRootFor(VfsUtilCore.getVirtualFileForJar(file));
  if (root != null) {
    LOGGER.debug("Found root for zip/jar file: " + root);
    return root;
  }

  // for other libs which don't have jars inside the project dir (such as JDK) take the owner module of the lib
  List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(file);
  Set<VirtualFile> libraryRoots = new HashSet<>();
  for (OrderEntry entry : entries) {
    if (entry instanceof LibraryOrderEntry || entry instanceof ModuleExtensionWithSdkOrderEntry) {
      VirtualFile moduleRoot = vcsManager.getVcsRootFor(entry.getOwnerModule().getModuleDir());
      if (moduleRoot != null) {
        libraryRoots.add(moduleRoot);
      }
    }
  }

  if (libraryRoots.size() == 0) {
    LOGGER.debug("No library roots");
    return null;
  }

  // if the lib is used in several modules, take the top module
  // (for modules of the same level we can't guess anything => take the first one)
  Iterator<VirtualFile> libIterator = libraryRoots.iterator();
  VirtualFile topLibraryRoot = libIterator.next();
  while (libIterator.hasNext()) {
    VirtualFile libRoot = libIterator.next();
    if (VfsUtilCore.isAncestor(libRoot, topLibraryRoot, true)) {
      topLibraryRoot = libRoot;
    }
  }
  LOGGER.debug("Several library roots, returning " + topLibraryRoot);
  return topLibraryRoot;
}
 
Example 13
Source File: AbstractFileTreeTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendChildrenTo(@Nonnull final Collection<ConvenientNode> children) {
  Project project = getObject();
  VirtualFile[] roots = ProjectRootManager.getInstance(project).getContentRoots();

  NextRoot:
  for (VirtualFile root : roots) {
    for (VirtualFile candidate : roots) {
      if (VfsUtilCore.isAncestor(candidate, root, true)) continue NextRoot;
    }
    if (myFilter.accept(root)) {
      children.add(new FileNode(root, project, myFilter));
    }
  }
}
 
Example 14
Source File: SwitchedFileHolder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
public String getBranchForFile(final VirtualFile file) {
  final VirtualFile floor = myMap.floorKey(file);
  if (floor == null) return null;
  final SortedMap<VirtualFile, Pair<Boolean, String>> floorMap = myMap.headMap(floor);
  for (VirtualFile parent : floorMap.keySet()) {
    if (VfsUtilCore.isAncestor(parent, file, false)) {
      return floorMap.get(parent).getSecond();
    }
  }
  return null;
}
 
Example 15
Source File: CoverageListNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean contains(VirtualFile file, PsiDirectory value) {
  if (myStateBean.myFlattenPackages) {
    return Comparing.equal(((PsiDirectory)value).getVirtualFile(), file.getParent());
  }

  if (VfsUtilCore.isAncestor(((PsiDirectory)value).getVirtualFile(), file, false)) {
    return true;
  }

  return false;
}
 
Example 16
Source File: NamedLibraryElementNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean containsFileInOrderType(final OrderEntry orderEntry, final OrderRootType orderType, final VirtualFile file) {
  if (!orderEntry.isValid()) return false;
  VirtualFile[] files = orderEntry.getFiles(orderType);
  for (VirtualFile virtualFile : files) {
    boolean ancestor = VfsUtilCore.isAncestor(virtualFile, file, false);
    if (ancestor) return true;
  }
  return false;
}
 
Example 17
Source File: ProjectLevelVcsManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isInDirectoryBasedRoot(final VirtualFile file) {
  if (file == null) return false;
  final VirtualFile baseDir = myProject.getBaseDir();
  if (baseDir == null) return false;
  final VirtualFile ideaDir = baseDir.findChild(Project.DIRECTORY_STORE_FOLDER);
  return ideaDir != null && ideaDir.isValid() && ideaDir.isDirectory() && VfsUtilCore.isAncestor(ideaDir, file, false);
}
 
Example 18
Source File: ModuleDefaultVcsRootPolicy.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matchesDefaultMapping(@Nonnull final VirtualFile file, final Object matchContext) {
  if (matchContext != null) {
    return true;
  }
  return myBaseDir != null && VfsUtilCore.isAncestor(myBaseDir, file, false);
}
 
Example 19
Source File: MindMapDocumentEditor.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void onClickOnExtra(@Nonnull final MindMapPanel source, final int modifiers, final int clicks, @Nonnull final Topic topic, @Nonnull final Extra<?> extra) {
  if (clicks > 1) {
    switch (extra.getType()) {
      case FILE: {
        final MMapURI fileURI = (MMapURI) extra.getValue();
        final boolean flagOpenFileLinkInSystemViewer = Boolean.parseBoolean(fileURI.getParameters().getProperty(FILELINK_ATTR_OPEN_IN_SYSTEM, "false"));
        final int lineNumber = FilePathWithLine.strToLine(fileURI.getParameters().getProperty(FILELINK_ATTR_LINE, null));

        final VirtualFile rootFolder = findRootFolderForEditedFile();
        final VirtualFile theFile = LocalFileSystem.getInstance().findFileByIoFile(fileURI.asFile(IdeaUtils.vfile2iofile(rootFolder)));

        if (theFile == null) {
          // file not found
          LOGGER.warn("Can't find FileObject for " + fileURI);
          getDialogProvider().msgError(null, String.format(BUNDLE.getString("MMDGraphEditor.onClickExtra.errorCanfFindFile"), fileURI.toString()));
        } else if (VfsUtilCore.isAncestor(rootFolder, theFile, false)) {
          // inside project
          if (flagOpenFileLinkInSystemViewer) {
            SelectIn.SYSTEM.open(this, theFile, -1);
          } else {
            SelectIn.IDE.open(this, theFile, lineNumber);
          }
        } else {
          // outside project
          if (flagOpenFileLinkInSystemViewer) {
            SelectIn.SYSTEM.open(this, theFile, -1);
          } else {
            SelectIn.IDE.open(this, theFile, lineNumber);
          }
        }
      }
      break;
      case LINK: {
        final MMapURI uri = ((ExtraLink) extra).getValue();
        if (!IdeaUtils.browseURI(uri.asURI(), isUseInsideBrowser())) { //NOI18N
          getDialogProvider().msgError(null, String.format(BUNDLE.getString("MMDGraphEditor.onClickOnExtra.msgCantBrowse"), uri.toString()));
        }
      }
      break;
      case NOTE: {
        editTextForTopic(topic);
      }
      break;
      case TOPIC: {
        final Topic theTopic = this.mindMapPanel.getModel().findTopicForLink((ExtraTopic) extra);
        if (theTopic == null) {
          // not presented
          getDialogProvider().msgWarn(null, BUNDLE.getString("MMDGraphEditor.onClickOnExtra.msgCantFindTopic"));
        } else {
          // detected
          this.mindMapPanel.focusTo(theTopic);
        }
      }
      break;
      default:
        throw new Error("Unexpected type " + extra);
    }
  }
}
 
Example 20
Source File: MockFileIndexFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValidAncestor(@Nonnull VirtualFile baseDir, @Nonnull VirtualFile child) {
  return VfsUtilCore.isAncestor(baseDir, child, false);
}