RIDER-77734 Compound run configs are duplicated if it's started out of the toolbar
[idea/community.git] / platform / execution-impl / src / com / intellij / execution / runToolbar / RunToolbarProcessMainAction.kt
1 // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.execution.runToolbar
3
4 import com.intellij.execution.Executor
5 import com.intellij.execution.ExecutorRegistryImpl
6 import com.intellij.execution.RunnerAndConfigurationSettings
7 import com.intellij.execution.runners.ExecutionUtil
8 import com.intellij.ide.macro.MacroManager
9 import com.intellij.openapi.actionSystem.AnActionEvent
10 import com.intellij.openapi.actionSystem.impl.ActionButtonWithText
11 import com.intellij.openapi.diagnostic.Logger
12
13 class RunToolbarProcessMainAction(process: RunToolbarProcess, executor: Executor) : RunToolbarProcessAction(process, executor) {
14   companion object {
15     private val LOG = Logger.getInstance(RunToolbarProcessMainAction::class.java)
16   }
17
18   init {
19     templatePresentation.putClientProperty(ActionButtonWithText.SHORTCUT_SHOULD_SHOWN, true)
20   }
21
22   override fun actionPerformed(e: AnActionEvent) {
23     e.project?.let { project ->
24       if (canRun(e)) {
25         getSelectedConfiguration(e)?.let {
26           val slotManager = RunToolbarSlotManager.getInstance(project)
27           val mainSlotData = slotManager.mainSlotData
28
29           mainSlotData.environment?.let { environment ->
30             MacroManager.getInstance().cacheMacrosPreview(e.getDataContext())
31             ExecutionUtil.restart(environment)
32           } ?: run {
33             ExecutorRegistryImpl.RunnerHelper.run(project, it.configuration, it, e.dataContext, executor)
34           }
35         }
36       }
37     }
38   }
39
40   override fun update(e: AnActionEvent) {
41     super.update(e)
42     traceLog(LOG, e)
43   }
44
45   override fun getSelectedConfiguration(e: AnActionEvent): RunnerAndConfigurationSettings? {
46     return e.project?.let {
47       val slotManager = RunToolbarSlotManager.getInstance(it)
48       slotManager.mainSlotData.configuration
49     }
50   }
51 }