Search in sources :

Example 11 with DocumentContent

use of com.intellij.diff.contents.DocumentContent 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 12 with DocumentContent

use of com.intellij.diff.contents.DocumentContent 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)

Example 13 with DocumentContent

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

the class TextMergeViewer method getDiffContents.

@NotNull
private static List<DiffContent> getDiffContents(@NotNull TextMergeRequest mergeRequest) {
    List<DocumentContent> contents = mergeRequest.getContents();
    final DocumentContent left = ThreeSide.LEFT.select(contents);
    final DocumentContent right = ThreeSide.RIGHT.select(contents);
    final DocumentContent output = mergeRequest.getOutputContent();
    return ContainerUtil.list(left, output, right);
}
Also used : DocumentContent(com.intellij.diff.contents.DocumentContent)

Example 14 with DocumentContent

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

the class TextDiffViewerUtil method checkDifferentDocuments.

public static void checkDifferentDocuments(@NotNull 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()));
    }
}
Also used : DocumentContent(com.intellij.diff.contents.DocumentContent) DiffContent(com.intellij.diff.contents.DiffContent)

Example 15 with DocumentContent

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

the class DiffUtil method createTitle.

@Nullable
private static JComponent createTitle(@NotNull String title, @NotNull DiffContent content, boolean equalCharsets, boolean equalSeparators, @Nullable Editor editor) {
    if (content instanceof EmptyContent)
        return null;
    DocumentContent documentContent = (DocumentContent) content;
    Charset charset = equalCharsets ? null : documentContent.getCharset();
    Boolean bom = equalCharsets ? null : documentContent.hasBom();
    LineSeparator separator = equalSeparators ? null : documentContent.getLineSeparator();
    boolean isReadOnly = editor == null || editor.isViewer() || !canMakeWritable(editor.getDocument());
    return createTitle(title, separator, charset, bom, isReadOnly);
}
Also used : DocumentContent(com.intellij.diff.contents.DocumentContent) Charset(java.nio.charset.Charset) LineSeparator(com.intellij.util.LineSeparator) EmptyContent(com.intellij.diff.contents.EmptyContent)

Aggregations

DocumentContent (com.intellij.diff.contents.DocumentContent)18 NotNull (org.jetbrains.annotations.NotNull)6 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)5 FileType (com.intellij.openapi.fileTypes.FileType)5 Project (com.intellij.openapi.project.Project)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Nullable (org.jetbrains.annotations.Nullable)4 DiffContent (com.intellij.diff.contents.DiffContent)3 Editor (com.intellij.openapi.editor.Editor)3 DiffContentFactory (com.intellij.diff.DiffContentFactory)2 DiffRequest (com.intellij.diff.requests.DiffRequest)2 TextMergeRequestImpl (com.intellij.diff.requests.TextMergeRequestImpl)2 Document (com.intellij.openapi.editor.Document)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 ArrayList (java.util.ArrayList)2 DiffRequestPanel (com.intellij.diff.DiffRequestPanel)1 DiffTooBigException (com.intellij.diff.comparison.DiffTooBigException)1 EmptyContent (com.intellij.diff.contents.EmptyContent)1 LineFragment (com.intellij.diff.fragments.LineFragment)1 ContentDiffRequest (com.intellij.diff.requests.ContentDiffRequest)1