Skip to content

Commit

Permalink
Merge pull request #67 from AMPivovarov/master
Browse files Browse the repository at this point in the history
migrate to new diff api
  • Loading branch information
ktisha committed Feb 12, 2015
2 parents c0b8eee + c856c21 commit ce2fbfb
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 107 deletions.
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
serviceImplementation="com.jetbrains.crucible.configuration.CrucibleSettings"/>
<projectService serviceInterface="com.jetbrains.crucible.connection.CrucibleManager"
serviceImplementation="com.jetbrains.crucible.connection.CrucibleManager" />
<diff.DiffTool implementation="com.jetbrains.crucible.ui.toolWindow.diff.CommentsDiffTool"/>

<toolWindow id="Code Review" icon="/images/paw_orange.png" anchor="bottom" canCloseContents="true" factoryClass="com.jetbrains.crucible.ui.toolWindow.CrucibleToolWindowFactory"/>
</extensions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class CrucibleToolWindowFactory implements ToolWindowFactory, DumbAware {

@Override
public void createToolWindowContent(Project project, ToolWindow toolWindow) {
DiffManager.getInstance().registerDiffTool(new CommentsDiffTool());
final ContentManager contentManager = toolWindow.getContentManager();
final CruciblePanel cruciblePanel = new CruciblePanel(project);
final Content content = ContentFactory.SERVICE.getInstance().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.actions.OpenRepositoryVersionAction;
import com.intellij.openapi.vcs.changes.actions.ShowDiffWithLocalAction;
import com.intellij.openapi.vcs.changes.actions.diff.ShowDiffContext;
import com.intellij.openapi.vcs.changes.ui.ChangesBrowser;
import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
import com.intellij.ui.*;
Expand All @@ -22,7 +23,7 @@
import com.jetbrains.crucible.model.Review;
import com.jetbrains.crucible.model.User;
import com.jetbrains.crucible.utils.CrucibleBundle;
import com.jetbrains.crucible.utils.CrucibleDataKeys;
import com.jetbrains.crucible.utils.CrucibleUserDataKeys;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -268,15 +269,17 @@ protected void buildToolBar(final DefaultActionGroup toolBarGroup) {

@Override
public void calcData(DataKey key, DataSink sink) {
if (key == CrucibleDataKeys.REVIEW) {
sink.put(CrucibleDataKeys.REVIEW, myReview);
}
if (key == VcsDataKeys.SELECTED_CHANGES) {
final List<Change> list = myViewer.getSelectedChanges();
sink.put(VcsDataKeys.SELECTED_CHANGES, list.toArray(new Change[list.size()]));
}
super.calcData(key, sink);
}

@Override
protected void updateDiffContext(@NotNull ShowDiffContext context) {
context.putChainContext(CrucibleUserDataKeys.REVIEW, myReview);
}
}

private class ShowGeneralCommentsAction extends AnActionButton {
Expand Down
162 changes: 72 additions & 90 deletions src/com/jetbrains/crucible/ui/toolWindow/diff/CommentsDiffTool.java
Original file line number Diff line number Diff line change
@@ -1,131 +1,113 @@
package com.jetbrains.crucible.ui.toolWindow.diff;

import com.intellij.ide.DataManager;
import com.intellij.openapi.Disposable;
import com.intellij.diff.DiffContext;
import com.intellij.diff.DiffTool;
import com.intellij.diff.FrameDiffTool;
import com.intellij.diff.SuppressiveDiffTool;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.tools.simple.SimpleDiffTool;
import com.intellij.diff.tools.simple.SimpleDiffViewer;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.diff.DiffRequest;
import com.intellij.openapi.diff.impl.DiffPanelImpl;
import com.intellij.openapi.diff.impl.external.DiffManagerImpl;
import com.intellij.openapi.diff.impl.external.FrameDiffTool;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.MarkupModel;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.FilePathImpl;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeRequestChain;
import com.intellij.openapi.vcs.changes.ChangesUtil;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentable;
import com.intellij.openapi.vcs.changes.actions.diff.ChangeDiffRequestProducer;
import com.intellij.ui.PopupHandler;
import com.jetbrains.crucible.actions.AddCommentAction;
import com.jetbrains.crucible.model.Comment;
import com.jetbrains.crucible.model.Review;
import com.jetbrains.crucible.utils.CrucibleBundle;
import com.jetbrains.crucible.utils.CrucibleDataKeys;
import com.jetbrains.crucible.utils.CrucibleUserDataKeys;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.List;

/**
* User: ktisha
*/
public class CommentsDiffTool extends FrameDiffTool {

@Nullable private Review myReview;
@Nullable private Change[] myChanges;

public class CommentsDiffTool implements FrameDiffTool, SuppressiveDiffTool {
@NotNull
@Override
public boolean canShow(DiffRequest request) {
final boolean superCanShow = super.canShow(request);
final AsyncResult<DataContext> dataContextFromFocus = DataManager.getInstance().getDataContextFromFocus();
final DataContext context = dataContextFromFocus.getResult();
if (context == null) return false;
final Review review = CrucibleDataKeys.REVIEW.getData(context);
return superCanShow && review != null;
public String getName() {
return SimpleDiffTool.INSTANCE.getName();
}

@Nullable
@Override
protected DiffPanelImpl createDiffPanelImpl(@NotNull DiffRequest request, @Nullable Window window, @NotNull Disposable parentDisposable) {
final AsyncResult<DataContext> dataContextFromFocus = DataManager.getInstance().getDataContextFromFocus();
final DataContext context = dataContextFromFocus.getResult();
if (context == null) return null;
myReview = CrucibleDataKeys.REVIEW.getData(context);
myChanges = VcsDataKeys.SELECTED_CHANGES.getData(context);

DiffPanelImpl diffPanel = new CommentableDiffPanel(window, request);
diffPanel.setDiffRequest(request);
Disposer.register(parentDisposable, diffPanel);
return diffPanel;
public List<Class<? extends DiffTool>> getSuppressedTools() {
return Collections.<Class<? extends DiffTool>>singletonList(SimpleDiffTool.class);
}

private static void addCommentAction(@Nullable final Editor editor2,
@Nullable final FilePath filePath,
@Nullable final Review review) {
if (editor2 != null && review != null) {
DefaultActionGroup group = new DefaultActionGroup();
final AddCommentAction addCommentAction = new AddCommentAction(review, editor2, filePath, CrucibleBundle.message("crucible.add.comment"), false);
addCommentAction.setContextComponent(editor2.getComponent());
group.add(addCommentAction);
PopupHandler.installUnknownPopupHandler(editor2.getContentComponent(), group, ActionManager.getInstance());
}
@Override
public boolean canShow(@NotNull DiffContext context, @NotNull DiffRequest request) {
if (context.getUserData(CrucibleUserDataKeys.REVIEW) == null) return false;
if (request.getUserData(ChangeDiffRequestProducer.CHANGE_KEY) == null) return false;
return SimpleDiffViewer.canShowRequest(context, request);
}

private void addGutter(@NotNull final Review review,
@Nullable final ContentRevision revision,
Editor editor2, FilePath filePath) {
final List<Comment> comments = review.getComments();

for (Comment comment : comments) {
final String id = comment.getReviewItemId();
final String path = review.getPathById(id);
if (revision != null && path != null && filePath.getPath().endsWith(path) &&
(review.isInPatch(comment) || revision.getRevisionNumber().asString().equals(comment.getRevision()))) {
@NotNull
@Override
public DiffViewer createComponent(@NotNull DiffContext context, @NotNull DiffRequest request) {
return new MySimpleDiffViewer(context, request);
}

final MarkupModel markup = editor2.getMarkupModel();
private static class MySimpleDiffViewer extends SimpleDiffViewer {
public MySimpleDiffViewer(@NotNull DiffContext context, @NotNull DiffRequest request) {
super(context, request);
}

final RangeHighlighter highlighter = markup.addLineHighlighter(Integer.parseInt(comment.getLine()) - 1, HighlighterLayer.ERROR + 1, null);
final ReviewGutterIconRenderer gutterIconRenderer =
new ReviewGutterIconRenderer(review, filePath, comment);
highlighter.setGutterIconRenderer(gutterIconRenderer);
@Override
protected void onInit() {
super.onInit();
if (myEditor2 == null) return;

final Review review = myContext.getUserData(CrucibleUserDataKeys.REVIEW);
final Change change = myRequest.getUserData(ChangeDiffRequestProducer.CHANGE_KEY);
assert review != null && change != null;

final FilePath path = ChangesUtil.getFilePath(change);
ContentRevision revision = change.getAfterRevision();
if (revision == null) {
revision = change.getBeforeRevision();
}

addCommentAction(myEditor2, review, path);
addGutter(myEditor2, review, path, revision);
}
}

private class CommentableDiffPanel extends DiffPanelImpl {
public CommentableDiffPanel(Window window, DiffRequest request) {
super(window, request.getProject(), true, true, DiffManagerImpl.FULL_DIFF_DIVIDER_POLYGONS_OFFSET, CommentsDiffTool.this);
private static void addCommentAction(@NotNull final Editor editor2,
@NotNull final Review review,
@NotNull final FilePath filePath) {
final AddCommentAction addCommentAction =
new AddCommentAction(review, editor2, filePath, CrucibleBundle.message("crucible.add.comment"), false);
addCommentAction.setContextComponent(editor2.getComponent());

DefaultActionGroup group = new DefaultActionGroup(addCommentAction);
PopupHandler.installUnknownPopupHandler(editor2.getContentComponent(), group, ActionManager.getInstance());
}

@Override
public void setDiffRequest(DiffRequest request) {
super.setDiffRequest(request);

Object chain = request.getGenericData().get(VcsDataKeys.DIFF_REQUEST_CHAIN.getName());
if (chain instanceof ChangeRequestChain) {
DiffRequestPresentable currentRequest = ((ChangeRequestChain)chain).getCurrentRequest();
if (currentRequest != null) {
String path = currentRequest.getPathPresentation();
FilePath filePath = new FilePathImpl(new File(path), false);
Editor editor2 = getEditor2();
addCommentAction(editor2, filePath , myReview);

if (myChanges != null && myChanges.length == 1 && myReview != null) {
ContentRevision revision = myChanges[0].getAfterRevision();
if (revision == null) {
revision = myChanges[0].getBeforeRevision();
}
addGutter(myReview, revision, editor2, filePath);
}
private static void addGutter(@NotNull Editor editor2,
@NotNull final Review review,
@NotNull FilePath filePath,
@Nullable final ContentRevision revision) {
if (revision == null) return;

for (Comment comment : review.getComments()) {
final String id = comment.getReviewItemId();
final String path = review.getPathById(id);
if (path != null && filePath.getPath().endsWith(path) &&
(review.isInPatch(comment) || revision.getRevisionNumber().asString().equals(comment.getRevision()))) {

int line = Integer.parseInt(comment.getLine()) - 1;
final RangeHighlighter highlighter = editor2.getMarkupModel().addLineHighlighter(line, HighlighterLayer.ERROR + 1, null);
final ReviewGutterIconRenderer gutterIconRenderer = new ReviewGutterIconRenderer(review, filePath, comment);
highlighter.setGutterIconRenderer(gutterIconRenderer);
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/com/jetbrains/crucible/utils/CrucibleDataKeys.java

This file was deleted.

11 changes: 11 additions & 0 deletions src/com/jetbrains/crucible/utils/CrucibleUserDataKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.jetbrains.crucible.utils;

import com.intellij.openapi.util.Key;
import com.jetbrains.crucible.model.Review;

/**
* User: ktisha
*/
public interface CrucibleUserDataKeys {
Key<Review> REVIEW = Key.create("crucible.Review");
}

0 comments on commit ce2fbfb

Please sign in to comment.