Search in sources :

Example 11 with DiffContent

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

the class CompareFileWithEditorAction method getDiffRequest.

@Nullable
@Override
protected DiffRequest getDiffRequest(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    VirtualFile selectedFile = getSelectedFile(e);
    VirtualFile currentFile = getEditingFile(e);
    assert selectedFile != null && currentFile != null;
    ContentDiffRequest request = DiffRequestFactory.getInstance().createFromFiles(project, selectedFile, currentFile);
    DiffContent editorContent = request.getContents().get(1);
    if (editorContent instanceof DocumentContent) {
        Editor[] editors = EditorFactory.getInstance().getEditors(((DocumentContent) editorContent).getDocument());
        if (editors.length != 0) {
            request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.RIGHT, editors[0].getCaretModel().getLogicalPosition().line));
        }
    }
    return request;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DocumentContent(com.intellij.diff.contents.DocumentContent) ContentDiffRequest(com.intellij.diff.requests.ContentDiffRequest) Editor(com.intellij.openapi.editor.Editor) DiffContent(com.intellij.diff.contents.DiffContent) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with DiffContent

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

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

the class MigrateToNewDiffUtil method convertRequestFair.

@Nullable
private static DiffRequest convertRequestFair(@NotNull com.intellij.openapi.diff.DiffRequest oldRequest) {
    if (oldRequest.getOnOkRunnable() != null)
        return null;
    //if (oldRequest.getBottomComponent() != null) return null; // TODO: we need EDT to make this check. Let's ignore bottom component.
    // TODO: migrate layers
    com.intellij.openapi.diff.DiffContent[] contents = oldRequest.getContents();
    String[] titles = oldRequest.getContentTitles();
    List<DiffContent> newContents = new ArrayList<>(contents.length);
    for (int i = 0; i < contents.length; i++) {
        DiffContent convertedContent = convertContent(oldRequest.getProject(), contents[i]);
        if (convertedContent == null)
            return null;
        newContents.add(convertedContent);
    }
    SimpleDiffRequest newRequest = new SimpleDiffRequest(oldRequest.getWindowTitle(), newContents, Arrays.asList(titles));
    DiffNavigationContext navigationContext = (DiffNavigationContext) oldRequest.getGenericData().get(DiffTool.SCROLL_TO_LINE.getName());
    if (navigationContext != null) {
        newRequest.putUserData(DiffUserDataKeysEx.NAVIGATION_CONTEXT, navigationContext);
    }
    return newRequest;
}
Also used : SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) ArrayList(java.util.ArrayList) DiffNavigationContext(com.intellij.openapi.diff.DiffNavigationContext) DiffContent(com.intellij.diff.contents.DiffContent) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with DiffContent

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

the class DiffHttpService method execute.

@Override
@Nullable
public String execute(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException {
    final List<DiffContent> contents = new ArrayList<>();
    final List<String> titles = new ArrayList<>();
    boolean focused = true;
    String windowTitle = null;
    JsonReader reader = createJsonReader(request);
    if (reader.hasNext()) {
        String fileType = null;
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("fileType")) {
                fileType = reader.nextString();
            } else if (name.equals("focused")) {
                focused = reader.nextBoolean();
            } else if (name.equals("windowTitle")) {
                windowTitle = StringUtil.nullize(reader.nextString(), true);
            } else if (name.equals("contents")) {
                String error = readContent(reader, contents, titles, fileType);
                if (error != null) {
                    return error;
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    if (contents.isEmpty()) {
        return "Empty request";
    }
    Project project = getLastFocusedOrOpenedProject();
    if (project == null) {
        // Argument for @NotNull parameter 'project' of com/intellij/openapi/components/ServiceManager.getService must not be null
        project = ProjectManager.getInstance().getDefaultProject();
    }
    final boolean finalFocused = focused;
    final String finalWindowTitle = windowTitle;
    final Project finalProject = project;
    ApplicationManager.getApplication().invokeLater(() -> {
        if (finalFocused) {
            ProjectUtil.focusProjectWindow(finalProject, true);
        }
        DiffManager.getInstance().showDiff(finalProject, new SimpleDiffRequest(StringUtil.notNullize(finalWindowTitle, "Diff Service"), contents, titles));
    }, project.getDisposed());
    sendOk(request, context);
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) DiffContent(com.intellij.diff.contents.DiffContent) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with DiffContent

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

the class DiffUtil method createTextTitles.

@NotNull
public static List<JComponent> createTextTitles(@NotNull ContentDiffRequest request, @NotNull 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;
}
Also used : DiffContent(com.intellij.diff.contents.DiffContent) RelativePoint(com.intellij.ui.awt.RelativePoint)

Aggregations

DiffContent (com.intellij.diff.contents.DiffContent)37 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)14 NotNull (org.jetbrains.annotations.NotNull)12 Nullable (org.jetbrains.annotations.Nullable)10 ContentDiffRequest (com.intellij.diff.requests.ContentDiffRequest)7 ArrayList (java.util.ArrayList)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 IOException (java.io.IOException)6 DiffRequest (com.intellij.diff.requests.DiffRequest)5 DocumentContent (com.intellij.diff.contents.DocumentContent)3 FileContent (com.intellij.diff.contents.FileContent)3 Project (com.intellij.openapi.project.Project)3 FilePath (com.intellij.openapi.vcs.FilePath)3 DiffIgnoredRangeProvider (com.intellij.diff.lang.DiffIgnoredRangeProvider)2 BinaryMergeRequestImpl (com.intellij.diff.requests.BinaryMergeRequestImpl)2 NullRevisionsProgress (com.intellij.history.integration.ui.models.NullRevisionsProgress)2 Editor (com.intellij.openapi.editor.Editor)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Ref (com.intellij.openapi.util.Ref)2 VcsRevisionNumber (com.intellij.openapi.vcs.history.VcsRevisionNumber)2