com.intellij.diff.contents.DiffContent Java Examples

The following examples show how to use com.intellij.diff.contents.DiffContent. 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: ErrorDiffTool.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private JComponent createComponent(@Nonnull DiffRequest request) {
  if (request instanceof MessageDiffRequest) {
    // TODO: explain some of ErrorDiffRequest exceptions ?
    String message = ((MessageDiffRequest)request).getMessage();
    return DiffUtil.createMessagePanel(message);
  }
  if (request instanceof ComponentDiffRequest) {
    return ((ComponentDiffRequest)request).getComponent(myContext);
  }
  if (request instanceof ContentDiffRequest) {
    List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
    for (final DiffContent content : contents) {
      if (content instanceof FileContent && UnknownFileType.INSTANCE == content.getContentType()) {
        final VirtualFile file = ((FileContent)content).getFile();

        UnknownFileTypeDiffRequest unknownFileTypeRequest = new UnknownFileTypeDiffRequest(file, myRequest.getTitle());
        return unknownFileTypeRequest.getComponent(myContext);
      }
    }
  }

  return DiffUtil.createMessagePanel("Can't show diff");
}
 
Example #2
Source File: HistoryDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected ContentDiffRequest createDifference(final FileDifferenceModel m) {
  final Ref<ContentDiffRequest> requestRef = new Ref<>();

  new Task.Modal(myProject, message("message.processing.revisions"), false) {
    public void run(@Nonnull final ProgressIndicator i) {
      ApplicationManager.getApplication().runReadAction(() -> {
        RevisionProcessingProgressAdapter p = new RevisionProcessingProgressAdapter(i);
        p.processingLeftRevision();
        DiffContent left = m.getLeftDiffContent(p);

        p.processingRightRevision();
        DiffContent right = m.getRightDiffContent(p);

        requestRef.set(new SimpleDiffRequest(m.getTitle(), left, right, m.getLeftTitle(p), m.getRightTitle(p)));
      });
    }
  }.queue();

  return requestRef.get();
}
 
Example #3
Source File: TestDiffRequestProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private DiffRequest loadRequest() {
  if (myIndex < 0 || myIndex >= myRequests.size()) return NoDiffRequest.INSTANCE;
  DiffHyperlink hyperlink = myRequests.get(myIndex);
  try {
    String title = hyperlink.getDiffTitle();

    Pair<String, DiffContent> content1 = createContentWithTitle("diff.content.expected.title",
                                                                hyperlink.getLeft(), hyperlink.getFilePath());
    Pair<String, DiffContent> content2 = createContentWithTitle("diff.content.actual.title",
                                                                hyperlink.getRight(), hyperlink.getActualFilePath());

    return new SimpleDiffRequest(title, content1.second, content2.second, content1.first, content2.first);
  }
  catch (Exception e) {
    return new ErrorDiffRequest(e);
  }
}
 
Example #4
Source File: DiffUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static List<JComponent> createTextTitles(@Nonnull ContentDiffRequest request, @Nonnull List<? extends Editor> editors) {
  List<DiffContent> contents = request.getContents();
  List<String> titles = request.getContentTitles();

  boolean equalCharsets = TextDiffViewerUtil.areEqualCharsets(contents);
  boolean equalSeparators = TextDiffViewerUtil.areEqualLineSeparators(contents);

  List<JComponent> result = new ArrayList<>(contents.size());

  if (equalCharsets && equalSeparators && !ContainerUtil.exists(titles, Condition.NOT_NULL)) {
    return Collections.nCopies(titles.size(), null);
  }

  for (int i = 0; i < contents.size(); i++) {
    JComponent title = createTitle(StringUtil.notNullize(titles.get(i)), contents.get(i), equalCharsets, equalSeparators, editors.get(i));
    title = createTitleWithNotifications(title, contents.get(i));
    result.add(title);
  }

  return result;
}
 
Example #5
Source File: TextDiffViewerUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void checkDifferentDocuments(@Nonnull ContentDiffRequest request) {
  // Actually, this should be a valid case. But it has little practical sense and will require explicit checks everywhere.
  // Some listeners will be processed once instead of 2 times, some listeners will cause illegal document modifications.
  List<DiffContent> contents = request.getContents();

  boolean sameDocuments = false;
  for (int i = 0; i < contents.size(); i++) {
    for (int j = i + 1; j < contents.size(); j++) {
      DiffContent content1 = contents.get(i);
      DiffContent content2 = contents.get(j);
      if (!(content1 instanceof DocumentContent)) continue;
      if (!(content2 instanceof DocumentContent)) continue;
      sameDocuments |= ((DocumentContent)content1).getDocument() == ((DocumentContent)content2).getDocument();
    }
  }

  if (sameDocuments) {
    StringBuilder message = new StringBuilder();
    message.append("DiffRequest with same documents detected\n");
    message.append(request.toString()).append("\n");
    for (DiffContent content : contents) {
      message.append(content.toString()).append("\n");
    }
    LOG.warn(new Throwable(message.toString()));
  }
}
 
Example #6
Source File: ThreesideDiffViewer.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static <T extends EditorHolder> boolean canShowRequest(@Nonnull DiffContext context,
                                                              @Nonnull DiffRequest request,
                                                              @Nonnull EditorHolderFactory<T> factory) {
  if (!(request instanceof ContentDiffRequest)) return false;

  List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
  if (contents.size() != 3) return false;

  boolean canShow = true;
  boolean wantShow = false;
  for (DiffContent content : contents) {
    canShow &= factory.canShowContent(content, context);
    wantShow |= factory.wantShowContent(content, context);
  }
  return canShow && wantShow;
}
 
Example #7
Source File: TwosideDiffViewer.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static <T extends EditorHolder> boolean canShowRequest(@Nonnull DiffContext context,
                                                              @Nonnull DiffRequest request,
                                                              @Nonnull EditorHolderFactory<T> factory) {
  if (!(request instanceof ContentDiffRequest)) return false;

  List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
  if (contents.size() != 2) return false;

  boolean canShow = true;
  boolean wantShow = false;
  for (DiffContent content : contents) {
    canShow &= factory.canShowContent(content, context);
    wantShow |= factory.wantShowContent(content, context);
  }
  return canShow && wantShow;
}
 
Example #8
Source File: OnesideDiffViewer.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static <T extends EditorHolder> boolean canShowRequest(@Nonnull DiffContext context,
                                                              @Nonnull DiffRequest request,
                                                              @Nonnull EditorHolderFactory<T> factory) {
  if (!(request instanceof ContentDiffRequest)) return false;

  List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
  if (contents.size() != 2) return false;

  DiffContent content1 = contents.get(0);
  DiffContent content2 = contents.get(1);

  if (content1 instanceof EmptyContent) {
    return factory.canShowContent(content2, context) && factory.wantShowContent(content2, context);
  }
  if (content2 instanceof EmptyContent) {
    return factory.canShowContent(content1, context) && factory.wantShowContent(content1, context);
  }
  return false;
}
 
Example #9
Source File: TextDiffViewerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean areEqualCharsets(@Nonnull List<? extends DiffContent> contents) {
  return areEqualDocumentContentProperties(contents, new Function<DocumentContent, Object>() {
    @Override
    public Object fun(DocumentContent documentContent) {
      return documentContent.getCharset();
    }
  });
}
 
Example #10
Source File: DirDiffViewer.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean canShowRequest(@Nonnull DiffContext context, @Nonnull DiffRequest request) {
  if (!(request instanceof ContentDiffRequest)) return false;
  List<DiffContent> contents = ((ContentDiffRequest)request).getContents();
  if (contents.size() != 2) return false;

  if (!canShowContent(contents.get(0))) return false;
  if (!canShowContent(contents.get(1))) return false;

  if (contents.get(0) instanceof EmptyContent && contents.get(1) instanceof EmptyContent) return false;

  return true;
}
 
Example #11
Source File: ThreesideDiffViewer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected SimpleDiffRequest createRequest() {
  List<DiffContent> contents = myRequest.getContents();
  List<String> titles = myRequest.getContentTitles();
  return new SimpleDiffRequest(myRequest.getTitle(),
                               mySide1.select(contents), mySide2.select(contents),
                               mySide1.select(titles), mySide2.select(titles));
}
 
Example #12
Source File: TextDiffViewerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static <T> boolean areEqualDocumentContentProperties(@Nonnull List<? extends DiffContent> contents,
                                                             @Nonnull final Function<DocumentContent, T> propertyGetter) {
  List<T> properties = ContainerUtil.mapNotNull(contents, new Function<DiffContent, T>() {
    @Override
    public T fun(DiffContent content) {
      if (content instanceof EmptyContent) return null;
      return propertyGetter.fun((DocumentContent)content);
    }
  });

  if (properties.size() < 2) return true;
  return ContainerUtil.newHashSet(properties).size() == 1;
}
 
Example #13
Source File: VcsHistoryUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes {@link DiffManager#getDiffTool()} to show difference between the given revisions of the given file.
 * @param project   project under vcs control.
 * @param path  file which revisions are compared.
 * @param revision1 first revision - 'before', to the left.
 * @param revision2 second revision - 'after', to the right.
 * @throws VcsException
 * @throws IOException
 */
public static void showDiff(@Nonnull final Project project, @Nonnull FilePath path,
                            @Nonnull VcsFileRevision revision1, @Nonnull VcsFileRevision revision2,
                            @Nonnull String title1, @Nonnull String title2) throws VcsException, IOException {
  final byte[] content1 = loadRevisionContent(revision1);
  final byte[] content2 = loadRevisionContent(revision2);

  FilePath path1 = getRevisionPath(revision1);
  FilePath path2 = getRevisionPath(revision2);

  String title;
  if (path1 != null && path2 != null) {
    title = DiffRequestFactoryImpl.getTitle(path1, path2, " -> ");
  }
  else {
    title = DiffRequestFactoryImpl.getContentTitle(path);
  }

  DiffContent diffContent1 = createContent(project, content1, revision1, path);
  DiffContent diffContent2 = createContent(project, content2, revision2, path);

  final DiffRequest request = new SimpleDiffRequest(title, diffContent1, diffContent2, title1, title2);

  diffContent1.putUserData(DiffUserDataKeysEx.REVISION_INFO, getRevisionInfo(revision1));
  diffContent2.putUserData(DiffUserDataKeysEx.REVISION_INFO, getRevisionInfo(revision2));

  WaitForProgressToShow.runOrInvokeLaterAboveProgress(new Runnable() {
    public void run() {
      DiffManager.getInstance().showDiff(project, request);
    }
  }, null, project);
}
 
Example #14
Source File: TextDiffViewerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean areEqualLineSeparators(@Nonnull List<? extends DiffContent> contents) {
  return areEqualDocumentContentProperties(contents, new Function<DocumentContent, Object>() {
    @Override
    public Object fun(DocumentContent documentContent) {
      return documentContent.getLineSeparator();
    }
  });
}
 
Example #15
Source File: ListenerDiffViewerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Set<Document> getDocuments() {
  Set<Document> documents = new HashSet<Document>();
  for (DiffContent content : myRequest.getContents()) {
    if (content instanceof DocumentContent) {
      documents.add(((DocumentContent)content).getDocument());
    }
  }
  return documents;
}
 
Example #16
Source File: ExternalMergeTool.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean canShow(@Nonnull MergeRequest request) {
  if (request instanceof ThreesideMergeRequest) {
    DiffContent outputContent = ((ThreesideMergeRequest)request).getOutputContent();
    if (!canProcessOutputContent(outputContent)) return false;

    List<? extends DiffContent> contents = ((ThreesideMergeRequest)request).getContents();
    if (contents.size() != 3) return false;
    for (DiffContent content : contents) {
      if (!ExternalDiffToolUtil.canCreateFile(content)) return false;
    }
    return true;
  }
  return false;
}
 
Example #17
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 #18
Source File: FileHistoryDialogTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void assertDiffContents(String leftContent, String rightContent, FileHistoryDialogModel m) throws IOException {
  DiffContent left = getLeftDiffContent(m);
  DiffContent right = getRightDiffContent(m);

  assertContent(leftContent, left);
  assertContent(rightContent, right);
}
 
Example #19
Source File: SelectionDifferenceModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected DiffContent getEditableRightDiffContent(RevisionProcessingProgress p) {
  Document d = getDocument();

  int fromOffset = d.getLineStartOffset(myFrom);
  int toOffset = d.getLineEndOffset(myTo);

  DocumentContent documentContent = DiffContentFactory.getInstance().create(myProject, d);
  return new DocumentFragmentContent(myProject, documentContent, new TextRange(fromOffset, toOffset));
}
 
Example #20
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static JComponent createTitleWithNotifications(@Nullable JComponent title,
                                                       @Nonnull DiffContent content) {
  List<JComponent> notifications = getCustomNotifications(content);
  if (notifications.isEmpty()) return title;

  List<JComponent> components = new ArrayList<>();
  if (title != null) components.add(title);
  components.addAll(notifications);
  return createStackedComponents(components, TITLE_GAP);
}
 
Example #21
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static JComponent createTitle(@Nonnull String title,
                                      @Nonnull DiffContent content,
                                      boolean equalCharsets,
                                      boolean equalSeparators,
                                      @Nullable Editor editor) {
  if (content instanceof EmptyContent) return null;

  Charset charset = equalCharsets ? null : ((DocumentContent)content).getCharset();
  LineSeparator separator = equalSeparators ? null : ((DocumentContent)content).getLineSeparator();
  boolean isReadOnly = editor == null || editor.isViewer() || !canMakeWritable(editor.getDocument());

  return createTitle(title, charset, separator, isReadOnly);
}
 
Example #22
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile getVirtualFile(@Nonnull ContentDiffRequest request, @Nonnull Side currentSide) {
  List<DiffContent> contents = request.getContents();
  DiffContent content1 = currentSide.select(contents);
  DiffContent content2 = currentSide.other().select(contents);

  if (content1 instanceof FileContent) return ((FileContent)content1).getFile();
  if (content2 instanceof FileContent) return ((FileContent)content2).getFile();
  return null;
}
 
Example #23
Source File: DiffUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile getVirtualFile(@Nonnull ContentDiffRequest request, @Nonnull ThreeSide currentSide) {
  List<DiffContent> contents = request.getContents();
  DiffContent content1 = currentSide.select(contents);
  DiffContent content2 = ThreeSide.BASE.select(contents);

  if (content1 instanceof FileContent) return ((FileContent)content1).getFile();
  if (content2 instanceof FileContent) return ((FileContent)content2).getFile();
  return null;
}
 
Example #24
Source File: DiffContentFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Deprecated
public DiffContent createFromBytes(@javax.annotation.Nullable Project project,
                                   @Nonnull VirtualFile highlightFile,
                                   @Nonnull byte[] content) throws IOException {
  return createFromBytes(project, content, highlightFile);
}
 
Example #25
Source File: DiffContentFactory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Deprecated
public DiffContent createBinary(@javax.annotation.Nullable Project project,
                                @Nonnull String fileName,
                                @Nonnull FileType type,
                                @Nonnull byte[] content) throws IOException {
  return createBinary(project, content, type, fileName);
}
 
Example #26
Source File: SimpleDiffRequest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public SimpleDiffRequest(@javax.annotation.Nullable String title,
                         @Nonnull DiffContent content1,
                         @Nonnull DiffContent content2,
                         @javax.annotation.Nullable String title1,
                         @javax.annotation.Nullable String title2) {
  this(title, ContainerUtil.list(content1, content2), ContainerUtil.list(title1, title2));
}
 
Example #27
Source File: SimpleDiffRequest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public SimpleDiffRequest(@javax.annotation.Nullable String title,
                         @Nonnull List<DiffContent> contents,
                         @Nonnull List<String> titles) {
  assert contents.size() == titles.size();

  myTitle = title;
  myContents = contents;
  myContentTitles = titles;
}
 
Example #28
Source File: ShowUpdatedDiffAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public DiffRequest process(@Nonnull UserDataHolder context, @Nonnull ProgressIndicator indicator)
        throws DiffRequestProducerException, ProcessCanceledException {
  try {
    DiffContent content1;
    DiffContent content2;

    DiffContentFactoryEx contentFactory = DiffContentFactoryEx.getInstanceEx();

    if (FileStatus.ADDED.equals(myFileStatus)) {
      content1 = contentFactory.createEmpty();
    }
    else {
      byte[] bytes1 = loadContent(myFilePath, myBefore);
      content1 = contentFactory.createFromBytes(myProject, bytes1, myFilePath);
    }

    if (FileStatus.DELETED.equals(myFileStatus)) {
      content2 = contentFactory.createEmpty();
    }
    else {
      byte[] bytes2 = loadContent(myFilePath, myAfter);
      content2 = contentFactory.createFromBytes(myProject, bytes2, myFilePath);
    }

    String title = DiffRequestFactoryImpl.getContentTitle(myFilePath);
    return new SimpleDiffRequest(title, content1, content2, "Before update", "After update");
  }
  catch (IOException e) {
    throw new DiffRequestProducerException("Can't load content", e);
  }
}
 
Example #29
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 #30
Source File: VcsHistoryUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static DiffContent createContent(@Nonnull Project project, @Nonnull byte[] content, @Nonnull VcsFileRevision revision,
                                         @Nonnull FilePath filePath) throws IOException {
  DiffContentFactoryEx contentFactory = DiffContentFactoryEx.getInstanceEx();
  if (isCurrent(revision)) {
    VirtualFile file = filePath.getVirtualFile();
    if (file != null) return contentFactory.create(project, file);
  }
  if (isEmpty(revision)) {
    return contentFactory.createEmpty();
  }
  return contentFactory.createFromBytes(project, content, filePath);
}