diff: "Jump to Source" - do not use CommonDataKeys.NAVIGATABLE to avoid clash with...
authorAleksey Pivovarov <AMPivovarov@gmail.com>
Fri, 14 Oct 2016 11:32:22 +0000 (14:32 +0300)
committerAleksey Pivovarov <AMPivovarov@gmail.com>
Mon, 24 Oct 2016 15:50:39 +0000 (18:50 +0300)
platform/diff-impl/src/com/intellij/diff/actions/impl/DiffNavigatableArrayRule.java [new file with mode: 0644]
platform/diff-impl/src/com/intellij/diff/actions/impl/OpenInEditorAction.java
platform/diff-impl/src/com/intellij/diff/tools/util/DiffDataKeys.java
platform/diff-impl/src/com/intellij/diff/tools/util/base/DiffViewerBase.java
platform/diff-impl/src/com/intellij/openapi/diff/impl/dir/DirDiffPanel.java
platform/platform-resources/src/META-INF/PlatformExtensions.xml

diff --git a/platform/diff-impl/src/com/intellij/diff/actions/impl/DiffNavigatableArrayRule.java b/platform/diff-impl/src/com/intellij/diff/actions/impl/DiffNavigatableArrayRule.java
new file mode 100644 (file)
index 0000000..9855a6b
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.diff.actions.impl;
+
+import com.intellij.diff.tools.util.DiffDataKeys;
+import com.intellij.ide.impl.dataRules.GetDataRule;
+import com.intellij.openapi.actionSystem.DataProvider;
+import com.intellij.pom.Navigatable;
+
+public class DiffNavigatableArrayRule implements GetDataRule {
+  public Object getData(DataProvider dataProvider) {
+    final Navigatable element = DiffDataKeys.NAVIGATABLE.getData(dataProvider);
+    if (element == null) {
+      return null;
+    }
+
+    return new Navigatable[]{element};
+  }
+}
+
index 59f84900277b4537cb14d3c2ceebdc4de97bea11..e2eb94717b023e9445c5ce65acf900ad161c1681 100644 (file)
@@ -23,17 +23,17 @@ import com.intellij.diff.util.DiffUtil;
 import com.intellij.ide.actions.EditSourceAction;
 import com.intellij.openapi.actionSystem.ActionPlaces;
 import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.DataKey;
 import com.intellij.openapi.actionSystem.ex.ActionUtil;
 import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.Project;
 import com.intellij.pom.Navigatable;
+import com.intellij.util.containers.ContainerUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 public class OpenInEditorAction extends EditSourceAction implements DumbAware {
-  public static DataKey<OpenInEditorAction> KEY = DataKey.create("DiffOpenInEditorAction");
+  public static final DataKey<OpenInEditorAction> KEY = DataKey.create("DiffOpenInEditorAction");
 
   @Nullable private final Runnable myAfterRunnable;
 
@@ -63,8 +63,8 @@ public class OpenInEditorAction extends EditSourceAction implements DumbAware {
       return;
     }
 
-    Navigatable navigatable = e.getData(CommonDataKeys.NAVIGATABLE);
-    if (navigatable == null || !navigatable.canNavigate()) {
+    Navigatable[] navigatables = e.getData(DiffDataKeys.NAVIGATABLE_ARRAY);
+    if (navigatables == null || !ContainerUtil.exists(navigatables, (it) -> it.canNavigate())) {
       e.getPresentation().setVisible(true);
       e.getPresentation().setEnabled(false);
       return;
@@ -78,16 +78,24 @@ public class OpenInEditorAction extends EditSourceAction implements DumbAware {
     Project project = e.getProject();
     if (project == null) return;
 
-    Navigatable navigatable = e.getData(CommonDataKeys.NAVIGATABLE);
-    if (navigatable == null) return;
+    Navigatable[] navigatables = e.getData(DiffDataKeys.NAVIGATABLE_ARRAY);
+    if (navigatables == null) return;
 
-    openEditor(project, navigatable);
+    openEditor(project, navigatables);
   }
 
   public void openEditor(@NotNull Project project, @NotNull Navigatable navigatable) {
-    if (navigatable.canNavigate()) {
-      navigatable.navigate(true);
-      if (myAfterRunnable != null) myAfterRunnable.run();
+    openEditor(project, new Navigatable[]{navigatable});
+  }
+
+  public void openEditor(@NotNull Project project, @NotNull Navigatable[] navigatables) {
+    boolean success = false;
+    for (Navigatable navigatable : navigatables) {
+      if (navigatable.canNavigate()) {
+        navigatable.navigate(true);
+        success = true;
+      }
     }
+    if (success && myAfterRunnable != null) myAfterRunnable.run();
   }
 }
index 38fc598c19987ef9cbc03521339fd12bb7acd0cf..33de8b879e6520943bcdb7105e545acbd21dd451 100644 (file)
@@ -23,8 +23,12 @@ import com.intellij.diff.requests.DiffRequest;
 import com.intellij.diff.util.LineRange;
 import com.intellij.openapi.actionSystem.DataKey;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.pom.Navigatable;
 
 public interface DiffDataKeys {
+  DataKey<Navigatable> NAVIGATABLE = DataKey.create("diff_navigatable");
+  DataKey<Navigatable[]> NAVIGATABLE_ARRAY = DataKey.create("diff_navigatable_array");
+
   DataKey<Editor> CURRENT_EDITOR = DataKey.create("diff_current_editor");
   DataKey<DiffContent> CURRENT_CONTENT = DataKey.create("diff_current_content");
   DataKey<LineRange> CURRENT_CHANGE_RANGE = DataKey.create("diff_current_change_range");
index 045046cf1ea233b5a9234025ddfe067ec23d63dd..f2225a8b8457e61a3fe4393ed229c44df9ce6243 100644 (file)
@@ -19,6 +19,7 @@ import com.intellij.diff.DiffContext;
 import com.intellij.diff.FrameDiffTool;
 import com.intellij.diff.FrameDiffTool.DiffViewer;
 import com.intellij.diff.requests.ContentDiffRequest;
+import com.intellij.diff.tools.util.DiffDataKeys;
 import com.intellij.diff.util.DiffTaskQueue;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.application.ApplicationManager;
@@ -281,7 +282,7 @@ public abstract class DiffViewerBase implements DiffViewer, DataProvider {
   @Nullable
   @Override
   public Object getData(@NonNls String dataId) {
-    if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
+    if (DiffDataKeys.NAVIGATABLE.is(dataId)) {
       return getNavigatable();
     }
     else if (CommonDataKeys.PROJECT.is(dataId)) {
index 33b5651a28369d072b309fd608f34460a661ef51..b3442a08f63761b70d0778bc8ef9b1447bf3c383 100644 (file)
@@ -27,7 +27,6 @@ import com.intellij.diff.util.DiffPlaces;
 import com.intellij.diff.util.DiffUserDataKeysEx.ScrollToPolicy;
 import com.intellij.ide.DataManager;
 import com.intellij.ide.diff.DiffElement;
-import com.intellij.ide.diff.DirDiffElement;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.openapi.Disposable;
 import com.intellij.openapi.actionSystem.*;
@@ -507,31 +506,15 @@ public class DirDiffPanel implements Disposable, DataProvider {
     else if (DIR_DIFF_TABLE.is(dataId)) {
       return myTable;
     }
-    else if (CommonDataKeys.NAVIGATABLE_ARRAY.is(dataId)) {
+    else if (DiffDataKeys.NAVIGATABLE_ARRAY.is(dataId)) {
       return getNavigatableArray();
     }
-    else if (CommonDataKeys.NAVIGATABLE.is(dataId)) {
-      return getNavigatable();
-    }
     else if (DiffDataKeys.PREV_NEXT_DIFFERENCE_ITERABLE.is(dataId)) {
       return myPrevNextDifferenceIterable;
     }
     return null;
   }
 
-  @Nullable
-  private Navigatable getNavigatable() {
-    Project project = myModel.getProject();
-    List<DirDiffElementImpl> elements = myModel.getSelectedElements();
-    if (elements.isEmpty()) return null;
-    DirDiffElement element = elements.get(0);
-    DiffElement source = element.getSource();
-    DiffElement target = element.getTarget();
-    Navigatable navigatable1 = source != null ? source.getNavigatable(project) : null;
-    Navigatable navigatable2 = target != null ? target.getNavigatable(project) : null;
-    return navigatable2 != null ? navigatable2 : navigatable1;
-  }
-
   @Nullable
   private Navigatable[] getNavigatableArray() {
     Project project = myModel.getProject();
index 68bffee40e35dbbe2c9b40996fd85c57baf22e94..ebdea69a5879939e2d4a3fb04b10d6a8c5b8725e 100644 (file)
     <statistics.usagesCollector implementation="com.intellij.diff.statistics.DiffUsagesCollector"/>
     <colorAndFontPanelFactory implementation="com.intellij.openapi.diff.impl.settings.DiffColorsPageFactory"/>
     <colorAndFontDescriptorProvider implementation="com.intellij.openapi.diff.impl.settings.DiffColorsPageFactory"/>
+    <getDataRule key="diff_navigatable_array" implementationClass="com.intellij.diff.actions.impl.DiffNavigatableArrayRule"/>
 
     <daemon.highlightInfoFilter implementation="com.intellij.diff.contents.DiffPsiFileSupport$HighlightFilter"/>
     <daemon.intentionActionFilter implementation="com.intellij.diff.contents.DiffPsiFileSupport$IntentionFilter"/>