Search in sources :

Example 1 with DiffContentFactory

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

the class ChangeDiffRequestProducer method createRequest.

@NotNull
private DiffRequest createRequest(@Nullable Project project, @NotNull Change change, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
    if (ChangesUtil.isTextConflictingChange(change)) {
        // three side diff
        // FIXME: This part is ugly as a VCS merge subsystem itself.
        FilePath path = ChangesUtil.getFilePath(change);
        VirtualFile file = path.getVirtualFile();
        if (file == null) {
            file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path.getPath());
        }
        if (file == null)
            throw new DiffRequestProducerException("Can't show merge conflict - file not found");
        if (project == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - project is unknown");
        }
        final AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
        if (vcs == null || vcs.getMergeProvider() == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - operation nos supported");
        }
        try {
            // FIXME: loadRevisions() can call runProcessWithProgressSynchronously() inside
            final Ref<Throwable> exceptionRef = new Ref<>();
            final Ref<MergeData> mergeDataRef = new Ref<>();
            final VirtualFile finalFile = file;
            ApplicationManager.getApplication().invokeAndWait(() -> {
                try {
                    mergeDataRef.set(vcs.getMergeProvider().loadRevisions(finalFile));
                } catch (VcsException e) {
                    exceptionRef.set(e);
                }
            });
            if (!exceptionRef.isNull()) {
                Throwable e = exceptionRef.get();
                if (e instanceof VcsException)
                    throw (VcsException) e;
                if (e instanceof Error)
                    throw (Error) e;
                if (e instanceof RuntimeException)
                    throw (RuntimeException) e;
                throw new RuntimeException(e);
            }
            MergeData mergeData = mergeDataRef.get();
            ContentRevision bRev = change.getBeforeRevision();
            ContentRevision aRev = change.getAfterRevision();
            String beforeRevisionTitle = getRevisionTitle(bRev, "Your version");
            String afterRevisionTitle = getRevisionTitle(aRev, "Server version");
            String title = DiffRequestFactory.getInstance().getTitle(file);
            List<String> titles = ContainerUtil.list(beforeRevisionTitle, "Base Version", afterRevisionTitle);
            DiffContentFactory contentFactory = DiffContentFactory.getInstance();
            List<DiffContent> contents = ContainerUtil.list(contentFactory.createFromBytes(project, mergeData.CURRENT, file), contentFactory.createFromBytes(project, mergeData.ORIGINAL, file), contentFactory.createFromBytes(project, mergeData.LAST, file));
            SimpleDiffRequest request = new SimpleDiffRequest(title, contents, titles);
            MergeUtil.putRevisionInfos(request, mergeData);
            return request;
        } catch (VcsException | IOException e) {
            LOG.info(e);
            throw new DiffRequestProducerException(e);
        }
    } else {
        ContentRevision bRev = change.getBeforeRevision();
        ContentRevision aRev = change.getAfterRevision();
        if (bRev == null && aRev == null) {
            LOG.warn("Both revision contents are empty");
            throw new DiffRequestProducerException("Bad revisions contents");
        }
        if (bRev != null)
            checkContentRevision(project, bRev, context, indicator);
        if (aRev != null)
            checkContentRevision(project, aRev, context, indicator);
        String title = getRequestTitle(change);
        indicator.setIndeterminate(true);
        DiffContent content1 = createContent(project, bRev, context, indicator);
        DiffContent content2 = createContent(project, aRev, context, indicator);
        final String userLeftRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_LEFT_CONTENT_TITLE);
        String beforeRevisionTitle = userLeftRevisionTitle != null ? userLeftRevisionTitle : getRevisionTitle(bRev, "Base version");
        final String userRightRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_RIGHT_CONTENT_TITLE);
        String afterRevisionTitle = userRightRevisionTitle != null ? userRightRevisionTitle : getRevisionTitle(aRev, "Your version");
        SimpleDiffRequest request = new SimpleDiffRequest(title, content1, content2, beforeRevisionTitle, afterRevisionTitle);
        boolean bRevCurrent = bRev instanceof CurrentContentRevision;
        boolean aRevCurrent = aRev instanceof CurrentContentRevision;
        if (bRevCurrent && !aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.LEFT);
        if (!bRevCurrent && aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
        return request;
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) DiffContentFactory(com.intellij.diff.DiffContentFactory) MergeData(com.intellij.openapi.vcs.merge.MergeData) IOException(java.io.IOException) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) Ref(com.intellij.openapi.util.Ref) VcsException(com.intellij.openapi.vcs.VcsException) DiffContent(com.intellij.diff.contents.DiffContent) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DiffContentFactory

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

the class PsiDiffContentFactory method fromPsiElement.

@Nullable
private static DiffContent fromPsiElement(@NotNull PsiElement psiElement) {
    DiffContentFactory factory = DiffContentFactory.getInstance();
    if (psiElement instanceof PsiFile) {
        return factory.create(psiElement.getProject(), ((PsiFile) psiElement).getVirtualFile());
    } else if (psiElement instanceof PsiDirectory) {
        return factory.create(psiElement.getProject(), ((PsiDirectory) psiElement).getVirtualFile());
    }
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null) {
        String text = psiElement.getText();
        if (text == null)
            return null;
        return factory.create(psiElement.getProject(), text, psiElement.getLanguage().getAssociatedFileType(), false);
    }
    DocumentContent wholeFileContent = factory.createDocument(psiElement.getProject(), containingFile.getVirtualFile());
    if (wholeFileContent == null)
        return null;
    return factory.createFragment(psiElement.getProject(), wholeFileContent, psiElement.getTextRange());
}
Also used : DiffContentFactory(com.intellij.diff.DiffContentFactory) PsiDirectory(com.intellij.psi.PsiDirectory) DocumentContent(com.intellij.diff.contents.DocumentContent) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with DiffContentFactory

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

the class PatchDiffRequestFactory method createDiffRequest.

@NotNull
public static DiffRequest createDiffRequest(@Nullable Project project, @Nullable VirtualFile file, @NotNull List<String> contents, @Nullable String windowTitle, @NotNull List<String> titles) {
    assert contents.size() == 3;
    assert titles.size() == 3;
    if (windowTitle == null)
        windowTitle = getPatchTitle(file);
    String localTitle = StringUtil.notNullize(titles.get(0), VcsBundle.message("patch.apply.conflict.local.version"));
    String baseTitle = StringUtil.notNullize(titles.get(1), "Base Version");
    String patchedTitle = StringUtil.notNullize(titles.get(2), VcsBundle.message("patch.apply.conflict.patched.version"));
    FileType fileType = file != null ? file.getFileType() : null;
    DiffContentFactory contentFactory = DiffContentFactory.getInstance();
    DocumentContent localContent = file != null ? contentFactory.createDocument(project, file) : null;
    if (localContent == null)
        localContent = contentFactory.create(project, contents.get(0), fileType);
    DocumentContent baseContent = contentFactory.create(project, contents.get(1), fileType);
    DocumentContent patchedContent = contentFactory.create(project, contents.get(2), fileType);
    return new SimpleDiffRequest(windowTitle, localContent, baseContent, patchedContent, localTitle, baseTitle, patchedTitle);
}
Also used : SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffContentFactory(com.intellij.diff.DiffContentFactory) FileType(com.intellij.openapi.fileTypes.FileType) DocumentContent(com.intellij.diff.contents.DocumentContent) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DiffContentFactory (com.intellij.diff.DiffContentFactory)3 DocumentContent (com.intellij.diff.contents.DocumentContent)2 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)2 NotNull (org.jetbrains.annotations.NotNull)2 DiffRequestProducerException (com.intellij.diff.chains.DiffRequestProducerException)1 DiffContent (com.intellij.diff.contents.DiffContent)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Ref (com.intellij.openapi.util.Ref)1 AbstractVcs (com.intellij.openapi.vcs.AbstractVcs)1 FilePath (com.intellij.openapi.vcs.FilePath)1 VcsException (com.intellij.openapi.vcs.VcsException)1 MergeData (com.intellij.openapi.vcs.merge.MergeData)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFile (com.intellij.psi.PsiFile)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1