diff: detect FileType in "Compare clipboard with selection" from diff viewer

This commit is contained in:
Aleksey Pivovarov
2015-04-10 19:01:52 +03:00
parent e4ee9f6d04
commit 737d7b8ecf
7 changed files with 57 additions and 12 deletions
@@ -52,6 +52,9 @@ public abstract class DiffContentFactory {
@NotNull
public abstract DocumentContent create(@Nullable Project project, @NotNull Document document);
@NotNull
public abstract DocumentContent create(@Nullable Project project, @NotNull Document document, @Nullable FileType fileType);
@NotNull
public abstract DocumentContent create(@Nullable Project project, @NotNull Document document, @Nullable VirtualFile file);
@@ -77,7 +77,14 @@ public class DiffContentFactoryImpl extends DiffContentFactory {
@Override
@NotNull
public DocumentContent create(@Nullable Project project, @NotNull Document document) {
return create(project, document, (FileType)null);
}
@Override
@NotNull
public DocumentContent create(@Nullable Project project, @NotNull Document document, @Nullable FileType fileType) {
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file == null) return new DocumentContentImpl(document, fileType, null, null, null);
return create(project, document, file);
}
@@ -17,9 +17,12 @@ package com.intellij.diff.actions;
import com.intellij.diff.DiffContentFactory;
import com.intellij.diff.DiffRequestFactory;
import com.intellij.diff.contents.DiffContent;
import com.intellij.diff.contents.DocumentContent;
import com.intellij.diff.requests.ContentDiffRequest;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.diff.tools.util.DiffDataKeys;
import com.intellij.diff.util.DiffUserDataKeys;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
@@ -28,6 +31,8 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
@@ -49,6 +54,22 @@ public class CompareClipboardWithSelectionAction extends BaseShowDiffAction {
return null;
}
@Nullable
private static FileType getEditorFileType(@NotNull AnActionEvent e) {
DiffContent content = e.getData(DiffDataKeys.CURRENT_CONTENT);
if (content != null && content.getContentType() != null) return content.getContentType();
DiffRequest request = e.getData(DiffDataKeys.DIFF_REQUEST);
if (request != null && request instanceof ContentDiffRequest) {
for (DiffContent diffContent : ((ContentDiffRequest)request).getContents()) {
FileType type = diffContent.getContentType();
if (type != null && type != UnknownFileType.INSTANCE) return type;
}
}
return null;
}
@Override
protected boolean isAvailable(@NotNull AnActionEvent e) {
Editor editor = getEditor(e);
@@ -60,9 +81,10 @@ public class CompareClipboardWithSelectionAction extends BaseShowDiffAction {
protected DiffRequest getDiffRequest(@NotNull AnActionEvent e) {
Project project = e.getRequiredData(CommonDataKeys.PROJECT);
Editor editor = getEditor(e);
FileType editorFileType = getEditorFileType(e);
assert editor != null;
DocumentContent content2 = createContent(project, editor);
DocumentContent content2 = createContent(project, editor, editorFileType);
DocumentContent content1 = DiffContentFactory.getInstance().createClipboardContent(content2);
String title1 = DiffBundle.message("diff.content.clipboard.content.title");
@@ -78,8 +100,8 @@ public class CompareClipboardWithSelectionAction extends BaseShowDiffAction {
}
@NotNull
private static DocumentContent createContent(@NotNull Project project, @NotNull Editor editor) {
DocumentContent content = DiffContentFactory.getInstance().create(project, editor.getDocument());
private static DocumentContent createContent(@NotNull Project project, @NotNull Editor editor, @Nullable FileType type) {
DocumentContent content = DiffContentFactory.getInstance().create(project, editor.getDocument(), type);
SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
@@ -17,6 +17,7 @@ package com.intellij.diff.tools.util;
import com.intellij.diff.DiffContext;
import com.intellij.diff.FrameDiffTool;
import com.intellij.diff.contents.DiffContent;
import com.intellij.diff.requests.DiffRequest;
import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.editor.Editor;
@@ -24,6 +25,7 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor;
public interface DiffDataKeys {
DataKey<Editor> CURRENT_EDITOR = DataKey.create("diff_current_editor");
DataKey<DiffContent> CURRENT_CONTENT = DataKey.create("diff_current_content");
DataKey<DiffRequest> DIFF_REQUEST = DataKey.create("diff_request");
DataKey<DiffContext> DIFF_CONTEXT = DataKey.create("diff_context");
@@ -26,7 +26,6 @@ import com.intellij.diff.util.DiffUtil;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.actions.EditorActionUtil;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.ex.EditorEx;
@@ -114,7 +113,7 @@ public abstract class TextDiffViewerBase extends ListenerDiffViewerBase {
@NotNull
protected List<AnAction> createEditorPopupActions() {
return ContainerUtil.list(
ActionManager.getInstance().getAction("CompareClipboardWithSelection") // TODO: pass FileType to DataContext for highlighting
ActionManager.getInstance().getAction("CompareClipboardWithSelection")
);
}
@@ -262,6 +262,11 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
return myCurrentSide.select(myEditors);
}
@NotNull
public DocumentContent getCurrentContent() {
return myCurrentSide.select(myActualContents);
}
@NotNull
@Override
protected List<? extends EditorEx> getEditors() {
@@ -306,10 +311,8 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
protected OpenFileDescriptor getOpenFileDescriptor() {
EditorEx editor = getCurrentEditor();
DocumentContent content = getCurrentSide().select(myActualContents);
int offset = editor.getCaretModel().getOffset();
return content.getOpenFileDescriptor(offset);
return getCurrentContent().getOpenFileDescriptor(offset);
}
public static boolean canShowRequest(@NotNull DiffContext context, @NotNull DiffRequest request) {
@@ -392,6 +395,9 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
if (DiffDataKeys.CURRENT_EDITOR.is(dataId)) {
return getCurrentEditor();
}
else if (DiffDataKeys.CURRENT_CONTENT.is(dataId)) {
return getCurrentContent();
}
return super.getData(dataId);
}
@@ -302,6 +302,12 @@ public abstract class TwosideTextDiffViewer extends TextDiffViewerBase {
return getCurrentSide().isLeft() ? myEditor1 : myEditor2;
}
@NotNull
public DocumentContent getCurrentContent() {
//noinspection ConstantConditions
return getCurrentSide().isLeft() ? myActualContent1 : myActualContent2;
}
@Nullable
protected EditorEx getEditor1() {
return myEditor1;
@@ -359,11 +365,8 @@ public abstract class TwosideTextDiffViewer extends TextDiffViewerBase {
protected OpenFileDescriptor getOpenFileDescriptor() {
EditorEx editor = getCurrentEditor();
DocumentContent content = getCurrentSide().select(myActualContent1, myActualContent2);
assert content != null;
int offset = editor.getCaretModel().getOffset();
return content.getOpenFileDescriptor(offset);
return getCurrentContent().getOpenFileDescriptor(offset);
}
public static boolean canShowRequest(@NotNull DiffContext context, @NotNull DiffRequest request) {
@@ -443,6 +446,9 @@ public abstract class TwosideTextDiffViewer extends TextDiffViewerBase {
if (DiffDataKeys.CURRENT_EDITOR.is(dataId)) {
return getCurrentEditor();
}
else if (DiffDataKeys.CURRENT_CONTENT.is(dataId)) {
return getCurrentContent();
}
return super.getData(dataId);
}