Search in sources :

Example 1 with FileContent

use of com.intellij.diff.contents.FileContent in project intellij-community by JetBrains.

the class TwosideBinaryDiffViewer method performRediff.

@Override
@NotNull
protected Runnable performRediff(@NotNull final ProgressIndicator indicator) {
    try {
        indicator.checkCanceled();
        List<DiffContent> contents = myRequest.getContents();
        if (!(contents.get(0) instanceof FileContent) || !(contents.get(1) instanceof FileContent)) {
            return applyNotification(null);
        }
        final VirtualFile file1 = ((FileContent) contents.get(0)).getFile();
        final VirtualFile file2 = ((FileContent) contents.get(1)).getFile();
        final JComponent notification = ReadAction.compute(() -> {
            if (!file1.isValid() || !file2.isValid()) {
                return DiffNotifications.createError();
            }
            try {
                // we can't use getInputStream() here because we can't restore BOM marker
                // (getBom() can return null for binary files, while getInputStream() strips BOM for all files).
                // It can be made for files from VFS that implements FileSystemInterface though.
                byte[] bytes1 = file1.contentsToByteArray();
                byte[] bytes2 = file2.contentsToByteArray();
                return Arrays.equals(bytes1, bytes2) ? DiffNotifications.createEqualContents() : null;
            } catch (IOException e) {
                LOG.warn(e);
                return null;
            }
        });
        return applyNotification(notification);
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (Throwable e) {
        LOG.error(e);
        return applyNotification(DiffNotifications.createError());
    }
}
Also used : FileContent(com.intellij.diff.contents.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) DiffContent(com.intellij.diff.contents.DiffContent) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FileContent

use of com.intellij.diff.contents.FileContent in project intellij-community by JetBrains.

the class DiffRequestFactoryImpl method createBinaryMergeRequest.

@NotNull
@Override
public MergeRequest createBinaryMergeRequest(@Nullable Project project, @NotNull VirtualFile output, @NotNull List<byte[]> byteContents, @Nullable String title, @NotNull List<String> contentTitles, @Nullable Consumer<MergeResult> applyCallback) throws InvalidDiffRequestException {
    if (byteContents.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);
        byte[] originalContent = output.contentsToByteArray();
        List<DiffContent> contents = new ArrayList<>(3);
        for (byte[] bytes : byteContents) {
            contents.add(myContentFactory.createFromBytes(project, bytes, output));
        }
        return new BinaryMergeRequestImpl(project, outputContent, originalContent, contents, byteContents, title, contentTitles, applyCallback);
    } catch (IOException e) {
        throw new InvalidDiffRequestException("Can't read from file", e);
    }
}
Also used : FileContent(com.intellij.diff.contents.FileContent) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BinaryMergeRequestImpl(com.intellij.diff.requests.BinaryMergeRequestImpl) DiffContent(com.intellij.diff.contents.DiffContent) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with FileContent

use of com.intellij.diff.contents.FileContent in project intellij-community by JetBrains.

the class DiffRequestFactoryImpl method createBinaryMergeRequestFromFiles.

@NotNull
public MergeRequest createBinaryMergeRequestFromFiles(@Nullable Project project, @NotNull VirtualFile output, @NotNull List<VirtualFile> fileContents, @Nullable String title, @NotNull 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);
            // TODO: we can read contents from file when needed
            byteContents.add(file.contentsToByteArray());
        }
        return new BinaryMergeRequestImpl(project, outputContent, originalContent, contents, byteContents, title, contentTitles, applyCallback);
    } catch (IOException e) {
        throw new InvalidDiffRequestException("Can't read from file", e);
    }
}
Also used : FileContent(com.intellij.diff.contents.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BinaryMergeRequestImpl(com.intellij.diff.requests.BinaryMergeRequestImpl) DiffContent(com.intellij.diff.contents.DiffContent) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with FileContent

use of com.intellij.diff.contents.FileContent in project intellij-community by JetBrains.

the class AnnotateDiffViewerAction method createAnnotationsLoader.

@Nullable
private static FileAnnotationLoader createAnnotationsLoader(@NotNull Project project, @NotNull DiffContent content) {
    if (content instanceof FileContent) {
        VirtualFile file = ((FileContent) content).getFile();
        AbstractVcs vcs = VcsUtil.getVcsFor(project, file);
        FileAnnotationLoader loader = doCreateAnnotationsLoader(project, vcs, file);
        if (loader != null)
            return loader;
    }
    Pair<FilePath, VcsRevisionNumber> info = content.getUserData(DiffUserDataKeysEx.REVISION_INFO);
    if (info != null) {
        FilePath filePath = info.first;
        AbstractVcs vcs = VcsUtil.getVcsFor(project, filePath);
        FileAnnotationLoader loader = doCreateAnnotationsLoader(vcs, filePath, info.second);
        if (loader != null)
            return loader;
    }
    return null;
}
Also used : FileContent(com.intellij.diff.contents.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileContent (com.intellij.diff.contents.FileContent)4 DiffContent (com.intellij.diff.contents.DiffContent)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 BinaryMergeRequestImpl (com.intellij.diff.requests.BinaryMergeRequestImpl)2 ArrayList (java.util.ArrayList)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 VcsRevisionNumber (com.intellij.openapi.vcs.history.VcsRevisionNumber)1 Nullable (org.jetbrains.annotations.Nullable)1