From 64edfe2fabe9a8d44bce1f24d9598d2df76e6619 Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Tue, 6 Mar 2018 17:06:51 +0300 Subject: [PATCH] IDEA-186349 add utility methods to simplify implementing Navigatable --- .../src/com/intellij/util/OpenSourceUtil.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/platform/platform-api/src/com/intellij/util/OpenSourceUtil.java b/platform/platform-api/src/com/intellij/util/OpenSourceUtil.java index 79610b8dd60d..16023d8084ed 100644 --- a/platform/platform-api/src/com/intellij/util/OpenSourceUtil.java +++ b/platform/platform-api/src/com/intellij/util/OpenSourceUtil.java @@ -23,6 +23,20 @@ public class OpenSourceUtil { navigate(requestFocus, CommonDataKeys.NAVIGATABLE_ARRAY.getData(context)); } + /** + * @return {@code true} if the specified {@code object} is {@link Navigatable} and supports navigation + */ + public static boolean canNavigate(@Nullable Object object) { + return object instanceof Navigatable && ((Navigatable)object).canNavigate(); + } + + /** + * @return {@code true} if the specified {@code object} is {@link Navigatable} and supports navigation to source + */ + public static boolean canNavigateToSource(@Nullable Object object) { + return object instanceof Navigatable && ((Navigatable)object).canNavigateToSource(); + } + /** * Invokes {@link #navigate(boolean, Navigatable...)} that always requests focus. */ @@ -57,13 +71,11 @@ public class OpenSourceUtil { Navigatable nonSourceNavigatable = null; boolean alreadyNavigatedToSource = false; for (Navigatable navigatable : navigatables) { - if (navigatable != null) { - if (navigateToSource(requestFocus, tryNotToScroll, navigatable)) { - alreadyNavigatedToSource = true; - } - else if (!alreadyNavigatedToSource && nonSourceNavigatable == null && navigatable.canNavigate()) { - nonSourceNavigatable = navigatable; - } + if (navigateToSource(requestFocus, tryNotToScroll, navigatable)) { + alreadyNavigatedToSource = true; + } + else if (!alreadyNavigatedToSource && nonSourceNavigatable == null && canNavigate(navigatable)) { + nonSourceNavigatable = navigatable; } } if (alreadyNavigatedToSource) return true; @@ -99,7 +111,7 @@ public class OpenSourceUtil { * @return {@code true} if navigation is done, {@code false} otherwise */ public static boolean navigateToSource(boolean requestFocus, boolean tryNotToScroll, @Nullable Navigatable navigatable) { - if (navigatable == null || !navigatable.canNavigateToSource()) return false; + if (!canNavigateToSource(navigatable)) return false; if (tryNotToScroll && navigatable instanceof StatePreservingNavigatable) { ((StatePreservingNavigatable)navigatable).navigate(requestFocus, true); }