Java Code Examples for com.intellij.openapi.vcs.FilePath#getParentPath()

The following examples show how to use com.intellij.openapi.vcs.FilePath#getParentPath() . 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: RemoteFileUtil.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
private static List<String> splitFilePaths(final FilePath filePath) {
    File[] roots = File.listRoots();
    List<String> revRet = new ArrayList<>();
    FilePath next = filePath;
    while (next != null) {
        String name = next.getName();
        if (name.length() == 0) {
            name = next.getIOFile().getAbsolutePath();
            while (name.endsWith(File.separator)) {
                name = name.substring(0, name.length() - File.separator.length());
            }
        }
        revRet.add(name);
        if (isRoot(next, roots)) {
            next = null;
        } else {
            next = next.getParentPath();
        }
    }
    Collections.reverse(revRet);
    return revRet;
}
 
Example 2
Source File: ChangesBrowserLocallyDeletedNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void render(@Nonnull ChangesBrowserNodeRenderer renderer, boolean selected, boolean expanded, boolean hasFocus) {
  // todo would be good to have render code in one place
  FilePath filePath = getUserObject().getPath();
  renderer.appendFileName(filePath.getVirtualFile(), filePath.getName(), FileStatus.NOT_CHANGED.getColor());

  if (renderer.isShowFlatten()) {
    FilePath parentPath = filePath.getParentPath();
    if (parentPath != null) {
      renderer.append(spaceAndThinSpace() + parentPath.getPresentableUrl(), SimpleTextAttributes.GRAYED_ATTRIBUTES);
    }
  }
  else if (getFileCount() != 1 || getDirectoryCount() != 0) {
    appendCount(renderer);
  }

  renderer.setIcon(getIcon());
}
 
Example 3
Source File: ChangesBrowserFilePathNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void render(final ChangesBrowserNodeRenderer renderer, final boolean selected, final boolean expanded, final boolean hasFocus) {
  final FilePath path = (FilePath)userObject;
  if (path.isDirectory() || !isLeaf()) {
    renderer.append(getRelativePath(path), SimpleTextAttributes.REGULAR_ATTRIBUTES);
    if (!isLeaf()) {
      appendCount(renderer);
    }
    renderer.setIcon(AllIcons.Nodes.TreeClosed);
  }
  else {
    if (renderer.isShowFlatten()) {
      renderer.append(path.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
      FilePath parentPath = path.getParentPath();
      renderer.append(spaceAndThinSpace() + FileUtil.getLocationRelativeToUserHome(parentPath.getPresentableUrl()), SimpleTextAttributes.GRAYED_ATTRIBUTES);
    }
    else {
      renderer.append(getRelativePath(path), SimpleTextAttributes.REGULAR_ATTRIBUTES);
    }
    renderer.setIcon(path.getFileType().getIcon());
  }
}
 
Example 4
Source File: StructureFilteringStrategy.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void append(final List<CommittedChangeList> changeLists) {
  final TreeState localState = myState != null && myFilePaths.isEmpty()
                               ? myState
                               : TreeState.createOn(myStructureTree, (DefaultMutableTreeNode)myStructureTree.getModel().getRoot());

  for (CommittedChangeList changeList : changeLists) {
    for (Change change : changeList.getChanges()) {
      final FilePath path = ChangesUtil.getFilePath(change);
      if (path.getParentPath() != null) {
        myFilePaths.add(path.getParentPath());
      }
    }
  }

  myStructureTree.setModel(TreeModelBuilder.buildFromFilePaths(myProject, false, myFilePaths));
  localState.applyTo(myStructureTree, (DefaultMutableTreeNode)myStructureTree.getModel().getRoot());
  myStructureTree.revalidate();
  myStructureTree.repaint();
  initRenderer();
}
 
Example 5
Source File: ModuleVcsPathPresenter.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String getPresentableRelativePath(@Nonnull final ContentRevision fromRevision, @Nonnull final ContentRevision toRevision) {
  // need to use parent path because the old file is already not there
  FilePath fromPath = fromRevision.getFile();
  FilePath toPath = toRevision.getFile();

  if ((fromPath.getParentPath() == null) || (toPath.getParentPath() == null)) {
    return null;
  }

  final VirtualFile oldFile = fromPath.getParentPath().getVirtualFile();
  final VirtualFile newFile = toPath.getParentPath().getVirtualFile();
  if (oldFile != null && newFile != null) {
    Module oldModule = ModuleUtilCore.findModuleForFile(oldFile, myProject);
    Module newModule = ModuleUtilCore.findModuleForFile(newFile, myProject);
    if (oldModule != newModule) {
      return getPresentableRelativePathFor(oldFile);
    }
  }
  final RelativePathCalculator calculator =
          new RelativePathCalculator(toPath.getIOFile().getAbsolutePath(), fromPath.getIOFile().getAbsolutePath());
  calculator.execute();
  final String result = calculator.getResult();
  return (result == null) ? null : result.replace("/", File.separator);
}
 
Example 6
Source File: ChangesUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
private static VirtualFile getValidParentUnderReadAction(@Nonnull FilePath filePath) {
  ThrowableComputable<VirtualFile,RuntimeException> action = () -> {
    VirtualFile result = null;
    FilePath parent = filePath;
    LocalFileSystem lfs = LocalFileSystem.getInstance();

    while (result == null && parent != null) {
      result = lfs.findFileByPath(parent.getPath());
      parent = parent.getParentPath();
    }

    return result;
  };
  return AccessRule.read(action);
}
 
Example 7
Source File: DiffRequestFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String getTitle(@Nonnull FilePath path1, @Nonnull FilePath path2, @Nonnull String separator) {
  if ((path1.isDirectory() || path2.isDirectory()) && path1.getPath().equals(path2.getPath())) {
    return path1.getPresentableUrl();
  }

  String name1 = path1.getName();
  String name2 = path2.getName();

  if (path1.isDirectory() ^ path2.isDirectory()) {
    if (path1.isDirectory()) name1 += File.separatorChar;
    if (path2.isDirectory()) name2 += File.separatorChar;
  }

  FilePath parent1 = path1.getParentPath();
  FilePath parent2 = path2.getParentPath();
  return getRequestTitle(name1, path1.getPresentableUrl(), parent1 != null ? parent1.getPresentableUrl() : null,
                         name2, path2.getPresentableUrl(), parent2 != null ? parent2.getPresentableUrl() : null,
                         separator);
}
 
Example 8
Source File: FileTreeUtil.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Break apart the path into parent directories, up to and including the {@literal parent}.
 * If the {@literal parent} is never reached, then this returns null.
 *
 * @param path   child path
 * @param parent parent path
 * @return the paths in the tree, or null if the path is not in the parent.  The returned
 *      paths will always contain at least the path itself, if it is under the parent.
 */
@Nullable
public static List<FilePath> getTreeTo(@NotNull FilePath path, @Nullable FilePath parent) {
    if (parent == null) {
        return null;
    }
    List<FilePath> ret = new ArrayList<>();
    FilePath prev;
    FilePath next = path;
    do {
        ret.add(next);
        prev = next;
        next = next.getParentPath();
    } while (next != null && !next.equals(prev) && !parent.equals(prev));
    if (parent.equals(prev) || parent.equals(next)) {
        return ret;
    }
    return null;
}
 
Example 9
Source File: FileTreeUtil.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Break apart the path so that it contains the complete directory chain.  The
 * First element returned is the path object passed in.
 *
 * @param path endpoint path
 * @return list of path directories.
 */
@NotNull
public static List<FilePath> getTree(@NotNull FilePath path) {
    List<FilePath> ret = new ArrayList<>();
    FilePath prev;
    FilePath next = path;
    do {
        ret.add(next);
        prev = next;
        next = next.getParentPath();
    } while (next != null && !next.equals(prev));
    return ret;
}
 
Example 10
Source File: MoveFilesToChangelistAction.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Intended to find a directory to use as the "working directory" for the path.  The returned path
 * is just a parent directory to one of the files in the move request.  The primary idea is finding
 * a base directory for the server to use as the root of the client workspace; with an AltRoot definition,
 * the server needs this, or it will generate error messages if the given files are not under the client
 * root used by the current working directory.
 *
 * @return a directory for the files.
 */
@NotNull
public File getCommonDir() {
    for (FilePath file : files) {
        FilePath parent = file.getParentPath();
        if (parent != null) {
            return parent.getIOFile();
        }
    }
    throw new RuntimeException("Unable to find a parent directory for " + files);
}
 
Example 11
Source File: VcsDirtyScopeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean belongsTo(final FilePath path, final Consumer<AbstractVcs> vcsConsumer) {
  if (myProject.isDisposed()) return false;
  final VcsRoot rootObject = myVcsManager.getVcsRootObjectFor(path);
  if (vcsConsumer != null && rootObject != null) {
    vcsConsumer.consume(rootObject.getVcs());
  }
  if (rootObject == null || rootObject.getVcs() != myVcs) {
    return false;
  }

  final VirtualFile vcsRoot = rootObject.getPath();
  if (vcsRoot != null) {
    for (VirtualFile contentRoot : myAffectedContentRoots) {
      // since we don't know exact dirty mechanics, maybe we have 3 nested mappings like:
      // /root -> vcs1, /root/child -> vcs2, /root/child/inner -> vcs1, and we have file /root/child/inner/file,
      // mapping is detected as vcs1 with root /root/child/inner, but we could possibly have in scope
      // "affected root" -> /root with scope = /root recursively
      if (VfsUtilCore.isAncestor(contentRoot, vcsRoot, false)) {
        THashSet<FilePath> dirsByRoot = myDirtyDirectoriesRecursively.get(contentRoot);
        if (dirsByRoot != null) {
          for (FilePath filePath : dirsByRoot) {
            if (path.isUnder(filePath, false)) return true;
          }
        }
      }
    }
  }

  if (!myDirtyFiles.isEmpty()) {
    FilePath parent = path.getParentPath();
    return isInDirtyFiles(path) || isInDirtyFiles(parent);
  }

  return false;
}
 
Example 12
Source File: ShelveFilesAction.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@NotNull
public File getCommonDir() {
    for (FilePath file : files) {
        FilePath parent = file.getParentPath();
        if (parent != null) {
            return parent.getIOFile();
        }
    }
    throw new RuntimeException("Unable to find a parent directory for " + files);
}
 
Example 13
Source File: ChangesBrowserChangeNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void render(@Nonnull ChangesBrowserNodeRenderer renderer, boolean selected, boolean expanded, boolean hasFocus) {
  Change change = getUserObject();
  FilePath filePath = ChangesUtil.getFilePath(change);
  VirtualFile file = filePath.getVirtualFile();

  if (myDecorator != null) {
    myDecorator.preDecorate(change, renderer, renderer.isShowFlatten());
  }

  renderer.appendFileName(file, filePath.getName(), change.getFileStatus().getColor());

  String originText = change.getOriginText(myProject);
  if (originText != null) {
    renderer.append(spaceAndThinSpace() + originText, SimpleTextAttributes.REGULAR_ATTRIBUTES);
  }

  if (renderer.isShowFlatten()) {
    FilePath parentPath = filePath.getParentPath();
    if (parentPath != null) {
      renderer.append(spaceAndThinSpace() + FileUtil.getLocationRelativeToUserHome(parentPath.getPath()),
                      SimpleTextAttributes.GRAYED_ATTRIBUTES);
    }
    appendSwitched(renderer, file);
  }
  else if (getFileCount() != 1 || getDirectoryCount() != 0) {
    appendSwitched(renderer, file);
    appendCount(renderer);
  }
  else {
    appendSwitched(renderer, file);
  }

  renderer.setIcon(getIcon(change, filePath));

  if (myDecorator != null) {
    myDecorator.decorate(change, renderer, renderer.isShowFlatten());
  }
}
 
Example 14
Source File: FetchFilesAction.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Intended to find a directory to use as the "working directory" for the path.  The returned path
 * is just a parent directory to one of the files in the sync request.  The primary idea is finding
 * a base directory for the server to use as the root of the client workspace; with an AltRoot definition,
 * the server needs this, or it will generate error messages if the given files are not under the client
 * root used by the current working directory.
 *
 * @return a directory for the files.
 */
@NotNull
public File getCommonDir() {
    for (FilePath file : syncPaths) {
        FilePath parent = file.getParentPath();
        if (parent != null) {
            return parent.getIOFile();
        }
    }
    throw new RuntimeException("Unable to find a parent directory for " + syncPaths);
}
 
Example 15
Source File: ConnectionTreeCellRenderer.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private void renderFilePath(FilePath value) {
    // TODO make this look and act like the display for the changelist files.
    // You should be able to navigate to the file from here.
    append(value.getName(), FILENAME_STYLE);
    FilePath parent = value.getParentPath();
    if (parent != null) {
        append("  ", PENDING_ACTION_SEPARATOR_STYLE);
        append(parent.getPath(), FILEPATH_STYLE);
    }
}
 
Example 16
Source File: DiffRequestFactoryImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static String getContentTitle(@Nonnull FilePath path) {
  if (path.isDirectory()) return path.getPresentableUrl();
  FilePath parent = path.getParentPath();
  return getContentTitle(path.getName(), path.getPresentableUrl(), parent != null ? parent.getPresentableUrl() : null);
}
 
Example 17
Source File: VcsDirtyScopeVfsListener.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ChangeApplier prepareChange(@Nonnull List<? extends VFileEvent> events) {
  if (myForbid || !myVcsManager.hasAnyMappings()) return null;
  final FilesAndDirs dirtyFilesAndDirs = new FilesAndDirs();
  // collect files and directories - sources of events
  for (VFileEvent event : events) {
    ProgressManager.checkCanceled();

    final boolean isDirectory;
    if (event instanceof VFileCreateEvent) {
      if (!((VFileCreateEvent)event).getParent().isInLocalFileSystem()) {
        continue;
      }
      isDirectory = ((VFileCreateEvent)event).isDirectory();
    }
    else {
      final VirtualFile file = Objects.requireNonNull(event.getFile(), "All events but VFileCreateEvent have @NotNull getFile()");
      if (!file.isInLocalFileSystem()) {
        continue;
      }
      isDirectory = file.isDirectory();
    }

    if (event instanceof VFileMoveEvent) {
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(((VFileMoveEvent)event).getOldPath(), isDirectory));
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(((VFileMoveEvent)event).getNewPath(), isDirectory));
    }
    else if (event instanceof VFilePropertyChangeEvent && ((VFilePropertyChangeEvent)event).isRename()) {
      // if a file was renamed, then the file is dirty and its parent directory is dirty too;
      // if a directory was renamed, all its children are recursively dirty, the parent dir is also dirty but not recursively.
      FilePath oldPath = VcsUtil.getFilePath(((VFilePropertyChangeEvent)event).getOldPath(), isDirectory);
      FilePath newPath = VcsUtil.getFilePath(((VFilePropertyChangeEvent)event).getNewPath(), isDirectory);
      // the file is dirty recursively
      add(myVcsManager, dirtyFilesAndDirs, oldPath);
      add(myVcsManager, dirtyFilesAndDirs, newPath);
      FilePath parentPath = oldPath.getParentPath();
      if (parentPath != null) {
        addAsFiles(myVcsManager, dirtyFilesAndDirs, parentPath); // directory is dirty alone
      }
    }
    else {
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(event.getPath(), isDirectory));
    }
  }

  return new ChangeApplier() {
    @Override
    public void afterVfsChange() {
      markDirtyOnPooled(dirtyFilesAndDirs);
    }
  };
}