From 6917d15689a5504e2bf4db22144917b657ea5495 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 20 Nov 2018 11:19:37 +0100 Subject: [PATCH] extract RunLineMarkerContributor.withExecutorActions --- .../TestRunLineMarkerProvider.java | 22 ++------------- .../execution/lineMarker/ExecutorAction.kt | 26 +++++++++-------- .../lineMarker/RunLineMarkerContributor.java | 28 +++++++------------ .../testing/PyTestLineMarkerContributor.kt | 8 +----- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/java/java-impl/src/com/intellij/testIntegration/TestRunLineMarkerProvider.java b/java/java-impl/src/com/intellij/testIntegration/TestRunLineMarkerProvider.java index 7a4c5be9c4cd..da0d7d79b8c7 100644 --- a/java/java-impl/src/com/intellij/testIntegration/TestRunLineMarkerProvider.java +++ b/java/java-impl/src/com/intellij/testIntegration/TestRunLineMarkerProvider.java @@ -1,23 +1,8 @@ -/* - * 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.testIntegration; import com.intellij.codeInsight.TestFrameworks; import com.intellij.execution.TestStateStorage; -import com.intellij.execution.lineMarker.ExecutorAction; import com.intellij.execution.lineMarker.RunLineMarkerContributor; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -27,8 +12,6 @@ import com.intellij.psi.util.ClassUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import javax.swing.*; - /** * @author Dmitry Avdeev */ @@ -56,8 +39,7 @@ public class TestRunLineMarkerProvider extends RunLineMarkerContributor { @NotNull private static Info getInfo(TestStateStorage.Record state, boolean isClass) { - Icon icon = getTestStateIcon(state, isClass); - return new Info(icon, ExecutorAction.getActions(1), RunLineMarkerContributor.RUN_TEST_TOOLTIP_PROVIDER); + return RunLineMarkerContributor.withExecutorActions(getTestStateIcon(state, isClass)); } protected boolean isIdentifier(PsiElement e) { diff --git a/platform/lang-impl/src/com/intellij/execution/lineMarker/ExecutorAction.kt b/platform/lang-impl/src/com/intellij/execution/lineMarker/ExecutorAction.kt index 4932789b57ac..863349ab956d 100644 --- a/platform/lang-impl/src/com/intellij/execution/lineMarker/ExecutorAction.kt +++ b/platform/lang-impl/src/com/intellij/execution/lineMarker/ExecutorAction.kt @@ -31,13 +31,17 @@ class ExecutorAction private constructor(private val origin: AnAction, companion object { @JvmStatic @JvmOverloads - fun getActions(order: Int = 0): Array { + fun getActions(order: Int = 0) = getActionList(order).toTypedArray() + + @JvmStatic + @JvmOverloads + fun getActionList(order: Int = 0): List { val actionManager = ActionManager.getInstance() return ExecutorRegistry.getInstance().registeredExecutors - .map { executor -> executor to actionManager.getAction(executor.contextActionId) } - .filter { pair -> pair.second != null } - .map { pair -> ExecutorAction(pair.second, pair.first, order) } - .toTypedArray() + .mapNotNull { executor -> + val action = actionManager.getAction(executor.contextActionId) ?: return@mapNotNull null + ExecutorAction(action, executor, order) + } } private fun getConfigurations(dataContext: DataContext): List { @@ -80,17 +84,17 @@ class ExecutorAction private constructor(private val origin: AnAction, origin.actionPerformed(e) } - override fun canBePerformed(context: DataContext): Boolean = origin !is ActionGroup || origin.canBePerformed(context) + override fun canBePerformed(context: DataContext) = origin !is ActionGroup || origin.canBePerformed(context) - override fun getChildren(e: AnActionEvent?): Array = (origin as? ActionGroup)?.getChildren(e) ?: AnAction.EMPTY_ARRAY + override fun getChildren(e: AnActionEvent?) = (origin as? ActionGroup)?.getChildren(e) ?: AnAction.EMPTY_ARRAY - override fun isDumbAware(): Boolean = origin.isDumbAware + override fun isDumbAware() = origin.isDumbAware - override fun isPopup(): Boolean = origin !is ActionGroup || origin.isPopup + override fun isPopup() = origin !is ActionGroup || origin.isPopup - override fun hideIfNoVisibleChildren(): Boolean = origin is ActionGroup && origin.hideIfNoVisibleChildren() + override fun hideIfNoVisibleChildren() = origin is ActionGroup && origin.hideIfNoVisibleChildren() - override fun disableIfNoVisibleChildren(): Boolean = origin !is ActionGroup || origin.disableIfNoVisibleChildren() + override fun disableIfNoVisibleChildren() = origin !is ActionGroup || origin.disableIfNoVisibleChildren() fun getActionName(dataContext: DataContext): String? { val list = getConfigurations(dataContext) diff --git a/platform/lang-impl/src/com/intellij/execution/lineMarker/RunLineMarkerContributor.java b/platform/lang-impl/src/com/intellij/execution/lineMarker/RunLineMarkerContributor.java index ea05be026dee..568a11fe7c49 100644 --- a/platform/lang-impl/src/com/intellij/execution/lineMarker/RunLineMarkerContributor.java +++ b/platform/lang-impl/src/com/intellij/execution/lineMarker/RunLineMarkerContributor.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.execution.lineMarker; import com.intellij.execution.TestStateStorage; @@ -36,6 +22,11 @@ public abstract class RunLineMarkerContributor { static final LanguageExtension EXTENSION = new LanguageExtension<>("com.intellij.runLineMarkerContributor"); + @NotNull + public static Info withExecutorActions(@NotNull Icon icon) { + return new Info(icon, ExecutorAction.getActions(1), RUN_TEST_TOOLTIP_PROVIDER); + } + public static class Info { public final Icon icon; public final AnAction[] actions; @@ -76,11 +67,12 @@ public abstract class RunLineMarkerContributor { return action instanceof ExecutorAction ? ((ExecutorAction)action).getActionName(dataContext) : null; } - protected static Icon getTestStateIcon(String url, Project project, boolean isClass) { - TestStateStorage.Record state = TestStateStorage.getInstance(project).getState(url); - return getTestStateIcon(state, isClass); + @NotNull + protected static Icon getTestStateIcon(String url, @NotNull Project project, boolean isClass) { + return getTestStateIcon(TestStateStorage.getInstance(project).getState(url), isClass); } + @NotNull protected static Icon getTestStateIcon(@Nullable TestStateStorage.Record state, boolean isClass) { if (state != null) { TestStateInfo.Magnitude magnitude = TestIconMapper.getMagnitude(state.magnitude); diff --git a/python/src/com/jetbrains/python/testing/PyTestLineMarkerContributor.kt b/python/src/com/jetbrains/python/testing/PyTestLineMarkerContributor.kt index c7df68785a50..79dc5083fc70 100644 --- a/python/src/com/jetbrains/python/testing/PyTestLineMarkerContributor.kt +++ b/python/src/com/jetbrains/python/testing/PyTestLineMarkerContributor.kt @@ -1,7 +1,6 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.testing -import com.intellij.execution.lineMarker.ExecutorAction import com.intellij.execution.lineMarker.RunLineMarkerContributor import com.intellij.icons.AllIcons import com.intellij.psi.PsiElement @@ -15,7 +14,6 @@ import com.jetbrains.python.psi.types.TypeEvalContext object PyTestLineMarkerContributor : RunLineMarkerContributor() { override fun getInfo(element: PsiElement): Info? { - if ((element !is LeafPsiElement) || element.elementType != PyTokenTypes.IDENTIFIER) { return null } @@ -25,11 +23,7 @@ object PyTestLineMarkerContributor : RunLineMarkerContributor() { if ((testElement is PyClass || testElement is PyFunction) && (testElement is PsiNamedElement) && isTestElement(testElement, ThreeState.UNSURE, typeEvalContext)) { - - return RunLineMarkerContributor.Info( - AllIcons.RunConfigurations.TestState.Run, - ExecutorAction.getActions(1), - RunLineMarkerContributor.RUN_TEST_TOOLTIP_PROVIDER) + return RunLineMarkerContributor.withExecutorActions(AllIcons.RunConfigurations.TestState.Run) } return null }