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

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#getPresentableUrl() . 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: DiffRequestFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public TextMergeRequest createTextMergeRequest(@Nullable Project project,
                                               @Nonnull VirtualFile output,
                                               @Nonnull List<byte[]> byteContents,
                                               @Nullable String title,
                                               @Nonnull List<String> contentTitles,
                                               @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
  if (byteContents.size() != 3) throw new IllegalArgumentException();
  if (contentTitles.size() != 3) throw new IllegalArgumentException();

  final Document outputDocument = FileDocumentManager.getInstance().getDocument(output);
  if (outputDocument == null) throw new InvalidDiffRequestException("Can't get output document: " + output.getPresentableUrl());
  if (!DiffUtil.canMakeWritable(outputDocument)) throw new InvalidDiffRequestException("Output is read only: " + output.getPresentableUrl());

  DocumentContent outputContent = myContentFactory.create(project, outputDocument);
  CharSequence originalContent = outputDocument.getImmutableCharSequence();

  List<DocumentContent> contents = new ArrayList<>(3);
  for (byte[] bytes : byteContents) {
    contents.add(myContentFactory.createDocumentFromBytes(project, bytes, output));
  }

  return new TextMergeRequestImpl(project, outputContent, originalContent, contents, title, contentTitles, applyCallback);
}
 
Example 2
Source File: GotoFileCellRenderer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getRelativePath(final VirtualFile virtualFile, final Project project) {
  if (project == null) {
    return virtualFile.getPresentableUrl();
  }
  VirtualFile root = getAnyRoot(virtualFile, project);
  if (root != null) {
    return getRelativePathFromRoot(virtualFile, root);
  }

  String url = virtualFile.getPresentableUrl();
  final VirtualFile baseDir = project.getBaseDir();
  if (baseDir != null) {
    final String projectHomeUrl = baseDir.getPresentableUrl();
    if (url.startsWith(projectHomeUrl)) {
      final String cont = url.substring(projectHomeUrl.length());
      if (cont.isEmpty()) return null;
      url = "..." + cont;
    }
  }
  return url;
}
 
Example 3
Source File: PatchDiffRequestFactory.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static MergeRequest createBadMergeRequest(@javax.annotation.Nullable Project project,
                                                 @Nonnull Document document,
                                                 @Nullable VirtualFile file,
                                                 @Nonnull String localContent,
                                                 @Nonnull AppliedTextPatch textPatch,
                                                 @javax.annotation.Nullable String windowTitle,
                                                 @Nullable String localTitle,
                                                 @Nullable String resultTitle,
                                                 @javax.annotation.Nullable String patchTitle,
                                                 @Nullable Consumer<MergeResult> callback)
        throws InvalidDiffRequestException {
  if (!DiffUtil.canMakeWritable(document)) {
    throw new InvalidDiffRequestException("Output is read only" + (file != null ? " : '" + file.getPresentableUrl() +"'": ""));
  }

  if (windowTitle == null) windowTitle = getBadPatchTitle(file);
  if (localTitle == null) localTitle = VcsBundle.message("patch.apply.conflict.local.version");
  if (resultTitle == null) resultTitle = VcsBundle.message("patch.apply.conflict.patched.somehow.version");
  if (patchTitle == null) patchTitle = VcsBundle.message("patch.apply.conflict.patch");

  DocumentContent resultContent = DiffContentFactory.getInstance().create(project, document, file);
  return new ApplyPatchMergeRequest(project, resultContent, textPatch, localContent,
                                    windowTitle, localTitle, resultTitle, patchTitle, callback);
}
 
Example 4
Source File: DirectoryChooser.java    From consulo with Apache License 2.0 6 votes vote down vote up
public String getPresentableUrl() {
  String directoryUrl;
  if (myDirectory != null) {
    directoryUrl = myDirectory.getVirtualFile().getPresentableUrl();
    final VirtualFile baseDir = myDirectory.getProject().getBaseDir();
    if (baseDir != null) {
      final String projectHomeUrl = baseDir.getPresentableUrl();
      if (directoryUrl.startsWith(projectHomeUrl)) {
        directoryUrl = "..." + directoryUrl.substring(projectHomeUrl.length());
      }
    }
  }
  else {
    directoryUrl = "";
  }
  return myPostfix != null ? directoryUrl + myPostfix : directoryUrl;
}
 
Example 5
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String doGenerateInfo(@Nonnull PsiElement element) {
  if (element instanceof PsiFile) {
    final VirtualFile virtualFile = ((PsiFile)element).getVirtualFile();
    if (virtualFile != null) {
      return virtualFile.getPresentableUrl();
    }
  }

  String info = getQuickNavigateInfo(element);
  if (info != null) {
    return info;
  }

  if (element instanceof NavigationItem) {
    final ItemPresentation presentation = ((NavigationItem)element).getPresentation();
    if (presentation != null) {
      return presentation.getPresentableText();
    }
  }

  return null;
}
 
Example 6
Source File: PlatformFrameTitleBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String getFileTitle(@Nonnull final Project project, @Nonnull final VirtualFile file) {
  if (SystemInfo.isMac) {
    return UniqueVFilePathBuilder.getInstance().getUniqueVirtualFilePath(project, file);
  }

  if (file instanceof VirtualFilePathWrapper) {
    return ((VirtualFilePathWrapper)file).getPresentablePath();
  }

  String url = FileUtil.getLocationRelativeToUserHome(file.getPresentableUrl());
  if (url == null) url = file.getPresentableUrl();

  VirtualFile baseDir = project.getBaseDir();

  if (baseDir != null) {
    final String projectHomeUrl = FileUtil.getLocationRelativeToUserHome(baseDir.getPresentableUrl());
    if (projectHomeUrl != null && url.startsWith(projectHomeUrl)) {
      url = "..." + url.substring(projectHomeUrl.length());
    }
  }

  return url;
}
 
Example 7
Source File: ReadOnlyAttributeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Sets specified read-only status for the spcified <code>file</code>.
 * This method can be performed only for files which are in local file system.
 *
 * @param file           file which read-only attribute to be changed.
 * @param readOnlyStatus new read-only status.
 * @throws java.lang.IllegalArgumentException
 *                     if passed <code>file</code> doesn't
 *                     belong to the local file system.
 * @throws IOException if some <code>IOException</code> occurred.
 */
public static void setReadOnlyAttribute(VirtualFile file, boolean readOnlyStatus) throws IOException {
  if (file.getFileSystem().isReadOnly()) {
    throw new IllegalArgumentException("Wrong file system: " + file.getFileSystem());
  }

  if (file.isWritable() == !readOnlyStatus) {
    return;
  }

  if (file instanceof NewVirtualFile) {
    ((NewVirtualFile)file).setWritable(!readOnlyStatus);
  }
  else {
    String path = file.getPresentableUrl();
    setReadOnlyAttribute(path, readOnlyStatus);
    file.refresh(false, false);
  }
}
 
Example 8
Source File: LightFilePointer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public String getPresentableUrl() {
  VirtualFile file = getFile();
  if (file != null) return file.getPresentableUrl();
  return toPresentableUrl(myUrl);
}
 
Example 9
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiDirectory createSubdirectory(@Nonnull String name) throws IncorrectOperationException {
  checkCreateSubdirectory(name);

  try {
    VirtualFile file = getVirtualFile().createChildDirectory(myManager, name);
    PsiDirectory directory = myManager.findDirectory(file);
    if (directory == null) throw new IncorrectOperationException("Cannot find directory in '" + file.getPresentableUrl() + "'");
    return directory;
  }
  catch (IOException e) {
    throw new IncorrectOperationException(e.toString());
  }
}
 
Example 10
Source File: TestDiffRequestProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private Pair<String, DiffContent> createContentWithTitle(String titleKey, String contentString, String contentFilePath) {
  String title;
  DiffContent content;
  VirtualFile vFile;
  if (contentFilePath != null && (vFile = LocalFileSystem.getInstance().findFileByPath(contentFilePath)) != null) {
    title = ExecutionBundle.message(titleKey) + " (" + vFile.getPresentableUrl() + ")";
    content = DiffContentFactory.getInstance().create(getProject(), vFile);
  }
  else {
    title = ExecutionBundle.message(titleKey);
    content = DiffContentFactory.getInstance().create(contentString);
  }
  return Pair.create(title, content);
}
 
Example 11
Source File: DiffRequestFactoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public MergeRequest createBinaryMergeRequestFromFiles(@Nullable Project project,
                                                      @Nonnull VirtualFile output,
                                                      @Nonnull List<VirtualFile> fileContents,
                                                      @Nullable String title,
                                                      @Nonnull List<String> contentTitles,
                                                      @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
  if (fileContents.size() != 3) throw new IllegalArgumentException();
  if (contentTitles.size() != 3) throw new IllegalArgumentException();


  try {
    FileContent outputContent = myContentFactory.createFile(project, output);
    if (outputContent == null) throw new InvalidDiffRequestException("Can't process file: " + output.getPresentableUrl());
    byte[] originalContent = output.contentsToByteArray();

    List<DiffContent> contents = new ArrayList<>(3);
    List<byte[]> byteContents = new ArrayList<>(3);
    for (VirtualFile file : fileContents) {
      FileContent content = myContentFactory.createFile(project, file);
      if (content == null) throw new InvalidDiffRequestException("Can't process file: " + file.getPresentableUrl());
      contents.add(content);
      byteContents.add(file.contentsToByteArray()); // TODO: we can read contents from file when needed
    }

    return new BinaryMergeRequestImpl(project, outputContent, originalContent, contents, byteContents, title, contentTitles, applyCallback);
  }
  catch (IOException e) {
    throw new InvalidDiffRequestException("Can't read from file", e);
  }
}
 
Example 12
Source File: DiffRequestFactoryImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public MergeRequest createMergeRequestFromFiles(@Nullable Project project,
                                                @Nonnull VirtualFile output,
                                                @Nonnull List<VirtualFile> fileContents,
                                                @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
  String title = "Merge " + output.getPresentableUrl();
  List<String> titles = ContainerUtil.list("Your Version", "Base Version", "Their Version");
  return createMergeRequestFromFiles(project, output, fileContents, title, titles, applyCallback);
}
 
Example 13
Source File: FavoritesTreeNodeDescriptor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String getLocation(final AbstractTreeNode element, final Project project) {
  Object nodeElement = element.getValue();
  if (nodeElement instanceof SmartPsiElementPointer) {
    nodeElement = ((SmartPsiElementPointer)nodeElement).getElement();
  }
  if (nodeElement instanceof PsiElement) {
    if (nodeElement instanceof PsiDirectory) {
      return ((PsiDirectory)nodeElement).getVirtualFile().getPresentableUrl();
    }
    if (nodeElement instanceof PsiFile) {
      final PsiFile containingFile = (PsiFile)nodeElement;
      final VirtualFile virtualFile = containingFile.getVirtualFile();
      return virtualFile != null ? virtualFile.getPresentableUrl() : "";
    }
  }

  if (nodeElement instanceof LibraryGroupElement) {
    return ((LibraryGroupElement)nodeElement).getModule().getName();
  }
  if (nodeElement instanceof NamedLibraryElement) {
    final NamedLibraryElement namedLibraryElement = ((NamedLibraryElement)nodeElement);
    final Module module = namedLibraryElement.getModule();
    return (module != null ? module.getName() : "") + ":" + namedLibraryElement.getOrderEntry().getPresentableName();
  }

  final FavoriteNodeProvider[] nodeProviders = Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, project);
  for (FavoriteNodeProvider provider : nodeProviders) {
    String location = provider.getElementLocation(nodeElement);
    if (location != null) return location;
  }
  return null;
}
 
Example 14
Source File: DocumentationManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String generateFileDoc(@Nonnull PsiFile psiFile, boolean withUrl) {
  VirtualFile file = PsiUtilCore.getVirtualFile(psiFile);
  File ioFile = file == null || !file.isInLocalFileSystem() ? null : VfsUtilCore.virtualToIoFile(file);
  BasicFileAttributes attr = null;
  try {
    attr = ioFile == null ? null : Files.readAttributes(Paths.get(ioFile.toURI()), BasicFileAttributes.class);
  }
  catch (Exception ignored) {
  }
  if (attr == null) return null;
  FileType type = file.getFileType();
  String typeName = type == UnknownFileType.INSTANCE ? "Unknown" : type == PlainTextFileType.INSTANCE ? "Text" : type instanceof ArchiveFileType ? "Archive" : type.getId();
  String languageName = type.isBinary() ? "" : psiFile.getLanguage().getDisplayName();
  return (withUrl ? DocumentationMarkup.DEFINITION_START + file.getPresentableUrl() + DocumentationMarkup.DEFINITION_END + DocumentationMarkup.CONTENT_START : "") +
         getVcsStatus(psiFile.getProject(), file) +
         getScope(psiFile.getProject(), file) +
         "<p><span class='grayed'>Size:</span> " +
         StringUtil.formatFileSize(attr.size()) +
         "<p><span class='grayed'>Type:</span> " +
         typeName +
         (type.isBinary() || typeName.equals(languageName) ? "" : " (" + languageName + ")") +
         "<p><span class='grayed'>Modified:</span> " +
         DateFormatUtil.formatDateTime(attr.lastModifiedTime().toMillis()) +
         "<p><span class='grayed'>Created:</span> " +
         DateFormatUtil.formatDateTime(attr.creationTime().toMillis()) +
         (withUrl ? DocumentationMarkup.CONTENT_END : "");
}
 
Example 15
Source File: DiffContentUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static String getTitle(VirtualFile virtualFile) {
  VirtualFile parent = virtualFile.getParent();
  if (parent == null) return virtualFile.getPresentableUrl();
  return virtualFile.getName() + " (" + parent.getPresentableUrl() + ")";
}
 
Example 16
Source File: ComponentWithBrowseButton.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
protected String chosenFileToResultingText(@Nonnull VirtualFile chosenFile) {
  return chosenFile.getPresentableUrl();
}
 
Example 17
Source File: StructureFilterPopupComponent.java    From consulo with Apache License 2.0 4 votes vote down vote up
private SelectVisibleRootAction(@Nonnull VirtualFile root) {
  super(root.getName(), root.getPresentableUrl(), null);
  myRoot = root;
  myIcon = new CheckboxColorIcon(CHECKBOX_ICON_SIZE, VcsLogGraphTable.getRootBackgroundColor(myRoot, myColorManager));
  getTemplatePresentation().setIcon(EmptyIcon.create(CHECKBOX_ICON_SIZE)); // see PopupFactoryImpl.calcMaxIconSize
}
 
Example 18
Source File: ExternalSystemNotificationManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void addMessage(@Nonnull final Notification notification,
                        @Nonnull final ProjectSystemId externalSystemId,
                        @Nonnull final NotificationData notificationData) {
  final VirtualFile virtualFile =
          notificationData.getFilePath() != null ? ExternalSystemUtil.waitForTheFile(notificationData.getFilePath()) : null;
  final String groupName = virtualFile != null ? virtualFile.getPresentableUrl() : notificationData.getTitle();

  myMessageCounter
          .increment(groupName, notificationData.getNotificationSource(), notificationData.getNotificationCategory(), externalSystemId);

  int line = notificationData.getLine() - 1;
  int column = notificationData.getColumn() - 1;
  if (virtualFile == null) line = column = -1;
  final int guiLine = line < 0 ? -1 : line + 1;
  final int guiColumn = column < 0 ? 0 : column + 1;

  final Navigatable navigatable = notificationData.getNavigatable() != null
                                  ? notificationData.getNavigatable()
                                  : virtualFile != null ? new OpenFileDescriptor(myProject, virtualFile, line, column) : null;

  final ErrorTreeElementKind kind =
          ErrorTreeElementKind.convertMessageFromCompilerErrorType(notificationData.getNotificationCategory().getMessageCategory());
  final String[] message = notificationData.getMessage().split("\n");
  final String exportPrefix = NewErrorTreeViewPanel.createExportPrefix(guiLine);
  final String rendererPrefix = NewErrorTreeViewPanel.createRendererPrefix(guiLine, guiColumn);

  UIUtil.invokeLaterIfNeeded(new Runnable() {
    @Override
    public void run() {
      boolean activate =
              notificationData.getNotificationCategory() == NotificationCategory.ERROR ||
              notificationData.getNotificationCategory() == NotificationCategory.WARNING;
      final NewErrorTreeViewPanel errorTreeView =
              prepareMessagesView(externalSystemId, notificationData.getNotificationSource(), activate);
      final GroupingElement groupingElement = errorTreeView.getErrorViewStructure().getGroupingElement(groupName, null, virtualFile);
      final NavigatableMessageElement navigatableMessageElement;
      if (notificationData.hasLinks()) {
        navigatableMessageElement = new EditableNotificationMessageElement(
                notification,
                kind,
                groupingElement,
                message,
                navigatable,
                exportPrefix,
                rendererPrefix);
      }
      else {
        navigatableMessageElement = new NotificationMessageElement(
                kind,
                groupingElement,
                message,
                navigatable,
                exportPrefix,
                rendererPrefix);
      }

      errorTreeView.getErrorViewStructure().addNavigatableMessage(groupName, navigatableMessageElement);
      errorTreeView.updateTree();
    }
  });
}
 
Example 19
Source File: NewErrorTreeViewPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static String getQualifiedName(final VirtualFile file) {
  return file.getPresentableUrl();
}
 
Example 20
Source File: GitLabOpenInBrowserAction.java    From IDEA-GitLab-Integration with MIT License 4 votes vote down vote up
@Override
public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getData(PlatformDataKeys.PROJECT);
    final VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
    final Editor editor = e.getData(PlatformDataKeys.EDITOR);
    if (virtualFile == null || project == null || project.isDisposed()) {
        return;
    }

    GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
    final GitRepository repository = manager.getRepositoryForFile(virtualFile);
    if (repository == null) {
        StringBuilder details = new StringBuilder("file: " + virtualFile.getPresentableUrl() + "; Git repositories: ");
        for (GitRepository repo : manager.getRepositories()) {
            details.append(repo.getPresentableUrl()).append("; ");
        }
        showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find git repository", details.toString());
        return;
    }

    final String rootPath = repository.getRoot().getPath();
    final String path = virtualFile.getPath();

    List<AnAction> remoteSelectedActions = new ArrayList<AnAction>();

    for (GitRemote remote : repository.getRemotes()) {
        remoteSelectedActions.add(new RemoteSelectedAction(project, repository, editor, remote, rootPath, path));
    }

    if (remoteSelectedActions.size() > 1) {
        DefaultActionGroup remotesActionGroup = new DefaultActionGroup();
        remotesActionGroup.addAll(remoteSelectedActions);
        DataContext dataContext = e.getDataContext();
        final ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(
                        "Select remote",
                        remotesActionGroup,
                        dataContext,
                        JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
                        true);

        popup.showInBestPositionFor(dataContext);
    } else if (remoteSelectedActions.size() == 1) {
        remoteSelectedActions.get(0).actionPerformed(null);
    } else {
        showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find gitlab remote");
    }
}