Search in sources :

Example 11 with DiffRequest

use of com.intellij.diff.requests.DiffRequest in project intellij-community by JetBrains.

the class CompareFilesAction method getDiffRequest.

@Nullable
@Override
protected DiffRequest getDiffRequest(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    DiffRequest diffRequest = e.getData(DIFF_REQUEST);
    if (diffRequest != null) {
        return diffRequest;
    }
    VirtualFile[] data = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
    if (data.length == 1) {
        VirtualFile otherFile = getOtherFile(project, data[0]);
        if (otherFile == null)
            return null;
        if (!hasContent(data[0]))
            return null;
        return DiffRequestFactory.getInstance().createFromFiles(project, data[0], otherFile);
    } else {
        return DiffRequestFactory.getInstance().createFromFiles(project, data[0], data[1]);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DiffRequest(com.intellij.diff.requests.DiffRequest) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with DiffRequest

use of com.intellij.diff.requests.DiffRequest in project intellij-community by JetBrains.

the class DiffShelvedChangesAction method processTextChanges.

private static void processTextChanges(@NotNull final Project project, @NotNull List<ShelvedChange> changesFromFirstList, @NotNull List<MyDiffRequestProducer> diffRequestProducers) {
    final String base = project.getBasePath();
    final ApplyPatchContext patchContext = new ApplyPatchContext(project.getBaseDir(), 0, false, false);
    final PatchesPreloader preloader = new PatchesPreloader(project);
    for (final ShelvedChange shelvedChange : changesFromFirstList) {
        final String beforePath = shelvedChange.getBeforePath();
        final String afterPath = shelvedChange.getAfterPath();
        final FilePath filePath = VcsUtil.getFilePath(new File(base, afterPath == null ? beforePath : afterPath));
        final boolean isNewFile = FileStatus.ADDED.equals(shelvedChange.getFileStatus());
        // isNewFile -> parent directory, !isNewFile -> file
        final VirtualFile file;
        try {
            file = ApplyFilePatchBase.findPatchTarget(patchContext, beforePath, afterPath, isNewFile);
            if (!isNewFile && (file == null || !file.exists()))
                throw new FileNotFoundException(beforePath);
        } catch (IOException e) {
            diffRequestProducers.add(new MyDiffRequestProducer(shelvedChange, filePath) {

                @NotNull
                @Override
                public DiffRequest process(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
                    throw new DiffRequestProducerException("Cannot find base for '" + (beforePath != null ? beforePath : afterPath) + "'");
                }
            });
            continue;
        }
        diffRequestProducers.add(new MyDiffRequestProducer(shelvedChange, filePath) {

            @NotNull
            @Override
            public DiffRequest process(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
                if (!isNewFile && file.getFileType() == UnknownFileType.INSTANCE) {
                    return new UnknownFileTypeDiffRequest(file, getName());
                }
                if (shelvedChange.isConflictingChange(project)) {
                    try {
                        final CommitContext commitContext = new CommitContext();
                        final TextFilePatch patch = preloader.getPatch(shelvedChange, commitContext);
                        final FilePath pathBeforeRename = patchContext.getPathBeforeRename(file);
                        final String relativePath = patch.getAfterName() == null ? patch.getBeforeName() : patch.getAfterName();
                        final Getter<CharSequence> baseContentGetter = new Getter<CharSequence>() {

                            @Override
                            public CharSequence get() {
                                BaseRevisionTextPatchEP baseRevisionTextPatchEP = Extensions.findExtension(PatchEP.EP_NAME, project, BaseRevisionTextPatchEP.class);
                                return baseRevisionTextPatchEP.provideContent(relativePath, commitContext);
                            }
                        };
                        Getter<ApplyPatchForBaseRevisionTexts> getter = new Getter<ApplyPatchForBaseRevisionTexts>() {

                            @Override
                            public ApplyPatchForBaseRevisionTexts get() {
                                return ApplyPatchForBaseRevisionTexts.create(project, file, pathBeforeRename, patch, baseContentGetter);
                            }
                        };
                        return PatchDiffRequestFactory.createConflictDiffRequest(project, file, patch, "Shelved Version", getter, getName(), context, indicator);
                    } catch (VcsException e) {
                        throw new DiffRequestProducerException("Can't show diff for '" + getName() + "'", e);
                    }
                } else {
                    final Change change = shelvedChange.getChange(project);
                    return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator);
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Getter(com.intellij.openapi.util.Getter) FileNotFoundException(java.io.FileNotFoundException) DiffRequest(com.intellij.diff.requests.DiffRequest) UnknownFileTypeDiffRequest(com.intellij.diff.requests.UnknownFileTypeDiffRequest) UnknownFileTypeDiffRequest(com.intellij.diff.requests.UnknownFileTypeDiffRequest) NotNull(org.jetbrains.annotations.NotNull) UserDataHolder(com.intellij.openapi.util.UserDataHolder) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CommitContext(com.intellij.openapi.vcs.changes.CommitContext) ApplyPatchForBaseRevisionTexts(com.intellij.openapi.vcs.changes.patch.ApplyPatchForBaseRevisionTexts) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) FilePath(com.intellij.openapi.vcs.FilePath) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) IOException(java.io.IOException) Change(com.intellij.openapi.vcs.changes.Change) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 13 with DiffRequest

use of com.intellij.diff.requests.DiffRequest in project intellij-community by JetBrains.

the class DiffShelvedChangesAction method processBinaryFiles.

private static void processBinaryFiles(@NotNull final Project project, @NotNull List<ShelvedBinaryFile> files, @NotNull List<MyDiffRequestProducer> diffRequestProducers) {
    final String base = project.getBaseDir().getPath();
    for (final ShelvedBinaryFile shelvedChange : files) {
        final File file = new File(base, shelvedChange.AFTER_PATH == null ? shelvedChange.BEFORE_PATH : shelvedChange.AFTER_PATH);
        final FilePath filePath = VcsUtil.getFilePath(file);
        diffRequestProducers.add(new MyDiffRequestProducer(shelvedChange, filePath) {

            @NotNull
            @Override
            public DiffRequest process(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
                Change change = shelvedChange.createChange(project);
                return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator);
            }
        });
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) DiffRequest(com.intellij.diff.requests.DiffRequest) UnknownFileTypeDiffRequest(com.intellij.diff.requests.UnknownFileTypeDiffRequest) Change(com.intellij.openapi.vcs.changes.Change) NotNull(org.jetbrains.annotations.NotNull) UserDataHolder(com.intellij.openapi.util.UserDataHolder) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 14 with DiffRequest

use of com.intellij.diff.requests.DiffRequest in project intellij-community by JetBrains.

the class DiffActionExecutor method showDiff.

public void showDiff() {
    final Ref<VcsException> exceptionRef = new Ref<>();
    final Ref<DiffRequest> requestRef = new Ref<>();
    final Task.Backgroundable task = new Task.Backgroundable(myProject, VcsBundle.message("show.diff.progress.title.detailed", mySelectedFile.getPresentableUrl()), true) {

        public void run(@NotNull ProgressIndicator indicator) {
            final VcsRevisionNumber revisionNumber = getRevisionNumber();
            try {
                if (revisionNumber == null) {
                    return;
                }
                DiffContent content1 = createRemote(revisionNumber);
                if (content1 == null)
                    return;
                DiffContent content2 = DiffContentFactory.getInstance().create(myProject, mySelectedFile);
                String title = DiffRequestFactory.getInstance().getTitle(mySelectedFile);
                boolean inverted = false;
                String title1;
                String title2;
                final FileStatus status = FileStatusManager.getInstance(myProject).getStatus(mySelectedFile);
                if (status == null || FileStatus.NOT_CHANGED.equals(status) || FileStatus.UNKNOWN.equals(status) || FileStatus.IGNORED.equals(status)) {
                    final VcsRevisionNumber currentRevision = myDiffProvider.getCurrentRevision(mySelectedFile);
                    inverted = revisionNumber.compareTo(currentRevision) > 0;
                    title1 = revisionNumber.asString();
                    title2 = VcsBundle.message("diff.title.local.with.number", currentRevision.asString());
                } else {
                    title1 = revisionNumber.asString();
                    title2 = VcsBundle.message("diff.title.local");
                }
                Integer line = null;
                if (content2 instanceof DocumentContent) {
                    Editor[] editors = EditorFactory.getInstance().getEditors(((DocumentContent) content2).getDocument(), myProject);
                    if (editors.length != 0)
                        line = editors[0].getCaretModel().getLogicalPosition().line;
                }
                if (inverted) {
                    SimpleDiffRequest request = new SimpleDiffRequest(title, content2, content1, title2, title1);
                    if (line != null)
                        request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.LEFT, line));
                    request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.LEFT);
                    requestRef.set(request);
                } else {
                    SimpleDiffRequest request = new SimpleDiffRequest(title, content1, content2, title1, title2);
                    if (line != null)
                        request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.RIGHT, line));
                    request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
                    requestRef.set(request);
                }
            } catch (ProcessCanceledException e) {
            //ignore
            } catch (VcsException e) {
                exceptionRef.set(e);
            } catch (IOException e) {
                exceptionRef.set(new VcsException(e));
            }
        }

        @Override
        public void onCancel() {
            onSuccess();
        }

        @Override
        public void onSuccess() {
            myHandler.completed(VcsBackgroundableActions.keyFrom(mySelectedFile));
            if (!exceptionRef.isNull()) {
                AbstractVcsHelper.getInstance(myProject).showError(exceptionRef.get(), VcsBundle.message("message.title.diff"));
                return;
            }
            if (!requestRef.isNull()) {
                DiffManager.getInstance().showDiff(myProject, requestRef.get());
            }
        }
    };
    myHandler.register(VcsBackgroundableActions.keyFrom(mySelectedFile));
    ProgressManager.getInstance().run(task);
}
Also used : SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) Task(com.intellij.openapi.progress.Task) DiffRequest(com.intellij.diff.requests.DiffRequest) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DocumentContent(com.intellij.diff.contents.DocumentContent) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) Editor(com.intellij.openapi.editor.Editor) DiffContent(com.intellij.diff.contents.DiffContent) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 15 with DiffRequest

use of com.intellij.diff.requests.DiffRequest in project intellij-community by JetBrains.

the class VcsHistoryUtil method showDiff.

/**
   * Invokes {@link com.intellij.openapi.diff.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(@NotNull final Project project, @NotNull FilePath path, @NotNull VcsFileRevision revision1, @NotNull VcsFileRevision revision2, @NotNull String title1, @NotNull 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);
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffRequest(com.intellij.diff.requests.DiffRequest) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffContent(com.intellij.diff.contents.DiffContent)

Aggregations

DiffRequest (com.intellij.diff.requests.DiffRequest)17 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)7 DiffContent (com.intellij.diff.contents.DiffContent)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 NotNull (org.jetbrains.annotations.NotNull)5 DiffRequestProducerException (com.intellij.diff.chains.DiffRequestProducerException)4 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)4 ContentDiffRequest (com.intellij.diff.requests.ContentDiffRequest)3 Project (com.intellij.openapi.project.Project)3 FilePath (com.intellij.openapi.vcs.FilePath)3 IOException (java.io.IOException)3 DocumentContent (com.intellij.diff.contents.DocumentContent)2 UnknownFileTypeDiffRequest (com.intellij.diff.requests.UnknownFileTypeDiffRequest)2 FileType (com.intellij.openapi.fileTypes.FileType)2 Task (com.intellij.openapi.progress.Task)2 Ref (com.intellij.openapi.util.Ref)2 UserDataHolder (com.intellij.openapi.util.UserDataHolder)2 Change (com.intellij.openapi.vcs.changes.Change)2 File (java.io.File)2