diff: use editor from context in AbstractShowDiffAction

* do not just use some random editor for this file
  (ex: from preview in closed Find Usages toolwindow)
* do not access Editor from pooled thread
This commit is contained in:
Aleksey Pivovarov
2017-06-19 17:05:44 +03:00
parent c300119c07
commit fad716f355
3 changed files with 73 additions and 28 deletions
@@ -16,7 +16,13 @@
package com.intellij.openapi.vcs.actions;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.changes.ChangeListManager;
@@ -94,12 +100,16 @@ public abstract class AbstractShowDiffAction extends AbstractVcsAction{
VirtualFile file = vcsContext.getSelectedFiles()[0];
AbstractVcs vcs = assertNotNull(ChangesUtil.getVcsForFile(file, project));
DiffProvider provider = assertNotNull(vcs.getDiffProvider());
Editor editor = vcsContext.getEditor();
getExecutor(provider, file, project).showDiff();
getExecutor(provider, file, project, editor).showDiff();
}
}
protected DiffActionExecutor getExecutor(DiffProvider diffProvider, VirtualFile selectedFile, Project project) {
return new DiffActionExecutor.CompareToCurrentExecutor(diffProvider, selectedFile, project, getKey());
protected DiffActionExecutor getExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor) {
return new DiffActionExecutor.CompareToCurrentExecutor(diffProvider, selectedFile, project, editor, getKey());
}
}
@@ -15,10 +15,13 @@
*/
package com.intellij.openapi.vcs.actions;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.diff.DiffProvider;
import com.intellij.openapi.vcs.impl.VcsBackgroundableActions;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CompareWithLastVersion extends AbstractShowDiffAction{
@Override
@@ -27,7 +30,10 @@ public class CompareWithLastVersion extends AbstractShowDiffAction{
}
@Override
protected DiffActionExecutor getExecutor(final DiffProvider diffProvider, final VirtualFile selectedFile, final Project project) {
return new DiffActionExecutor.DeletionAwareExecutor(diffProvider, selectedFile, project, getKey());
protected DiffActionExecutor getExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor) {
return new DiffActionExecutor.DeletionAwareExecutor(diffProvider, selectedFile, project, editor, getKey());
}
}
@@ -20,19 +20,22 @@ import com.intellij.diff.DiffContentFactoryEx;
import com.intellij.diff.DiffManager;
import com.intellij.diff.DiffRequestFactory;
import com.intellij.diff.contents.DiffContent;
import com.intellij.diff.contents.DocumentContent;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.diff.util.DiffUserDataKeys;
import com.intellij.diff.util.DiffUserDataKeysEx;
import com.intellij.diff.util.Side;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vcs.*;
@@ -55,15 +58,38 @@ public abstract class DiffActionExecutor {
protected final DiffProvider myDiffProvider;
protected final VirtualFile mySelectedFile;
protected final Project myProject;
private final Integer mySelectedLine;
private final BackgroundableActionEnabledHandler myHandler;
protected DiffActionExecutor(final DiffProvider diffProvider, final VirtualFile selectedFile, final Project project,
final VcsBackgroundableActions actionKey) {
protected DiffActionExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor,
@NotNull VcsBackgroundableActions actionKey) {
final ProjectLevelVcsManagerImpl vcsManager = (ProjectLevelVcsManagerImpl) ProjectLevelVcsManager.getInstance(project);
myHandler = vcsManager.getBackgroundableActionHandler(actionKey);
myDiffProvider = diffProvider;
mySelectedFile = selectedFile;
myProject = project;
mySelectedLine = getSelectedLine(project, mySelectedFile, editor);
}
@Nullable
private Integer getSelectedLine(@NotNull Project project, @NotNull VirtualFile file, @Nullable Editor contextEditor) {
Editor editor = null;
if (contextEditor != null) {
VirtualFile contextFile = FileDocumentManager.getInstance().getFile(contextEditor.getDocument());
if (Comparing.equal(contextFile, mySelectedFile)) editor = contextEditor;
}
if (editor == null) {
FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(mySelectedFile);
if (fileEditor instanceof TextEditor) editor = ((TextEditor)fileEditor).getEditor();
}
if (editor == null) return null;
return editor.getCaretModel().getLogicalPosition().line;
}
@Nullable
@@ -126,21 +152,15 @@ public abstract class DiffActionExecutor {
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));
if (mySelectedLine != null) request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.LEFT, mySelectedLine));
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));
if (mySelectedLine != null) request.putUserData(DiffUserDataKeys.SCROLL_TO_LINE, Pair.create(Side.RIGHT, mySelectedLine));
request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
requestRef.set(request);
}
@@ -181,7 +201,7 @@ public abstract class DiffActionExecutor {
public static void showDiff(final DiffProvider diffProvider, final VcsRevisionNumber revisionNumber, final VirtualFile selectedFile,
final Project project, final VcsBackgroundableActions actionKey) {
final DiffActionExecutor executor = new CompareToFixedExecutor(diffProvider, selectedFile, project, revisionNumber, actionKey);
final DiffActionExecutor executor = new CompareToFixedExecutor(diffProvider, selectedFile, project, null, revisionNumber, actionKey);
executor.showDiff();
}
@@ -191,10 +211,13 @@ public abstract class DiffActionExecutor {
public static class CompareToFixedExecutor extends DiffActionExecutor {
private final VcsRevisionNumber myNumber;
public CompareToFixedExecutor(final DiffProvider diffProvider,
final VirtualFile selectedFile, final Project project, final VcsRevisionNumber number,
final VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, actionKey);
public CompareToFixedExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor,
@NotNull VcsRevisionNumber number,
@NotNull VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, editor, actionKey);
myNumber = number;
}
@@ -204,9 +227,12 @@ public abstract class DiffActionExecutor {
}
public static class CompareToCurrentExecutor extends DiffActionExecutor {
public CompareToCurrentExecutor(final DiffProvider diffProvider, final VirtualFile selectedFile, final Project project,
final VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, actionKey);
public CompareToCurrentExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor,
@NotNull VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, editor, actionKey);
}
@Nullable
@@ -218,9 +244,12 @@ public abstract class DiffActionExecutor {
public static class DeletionAwareExecutor extends DiffActionExecutor {
private boolean myFileStillExists;
public DeletionAwareExecutor(final DiffProvider diffProvider,
final VirtualFile selectedFile, final Project project, final VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, actionKey);
public DeletionAwareExecutor(@NotNull DiffProvider diffProvider,
@NotNull VirtualFile selectedFile,
@NotNull Project project,
@Nullable Editor editor,
@NotNull VcsBackgroundableActions actionKey) {
super(diffProvider, selectedFile, project, editor, actionKey);
}
protected VcsRevisionNumber getRevisionNumber() {