Add shortcut and menu item for Run Scratch Action (KT-24180)
KT-24180 Fixed
This commit is contained in:
@@ -17,19 +17,18 @@
|
||||
package org.jetbrains.kotlin.idea.scratch.actions
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.scratch.ScratchFileLanguageProvider
|
||||
import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel
|
||||
|
||||
class ClearScratchAction(private val scratchPanel: ScratchTopPanel) : AnAction(
|
||||
KotlinBundle.message("scratch.clear.button"),
|
||||
class ClearScratchAction : ScratchAction(
|
||||
KotlinBundle.message("scratch.clear.button"),
|
||||
AllIcons.Actions.GC
|
||||
) {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val scratchFile = scratchPanel.scratchFile
|
||||
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
|
||||
val scratchFile = getScratchPanel(editor)?.scratchFile ?: return
|
||||
val psiFile = scratchFile.getPsiFile() ?: return
|
||||
|
||||
ScratchFileLanguageProvider.get(psiFile.language)?.getOutputHandler()?.clear(scratchFile)
|
||||
|
||||
@@ -17,25 +17,34 @@
|
||||
package org.jetbrains.kotlin.idea.scratch.actions
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys
|
||||
import com.intellij.openapi.compiler.CompilerManager
|
||||
import com.intellij.openapi.keymap.KeymapManager
|
||||
import com.intellij.openapi.keymap.KeymapUtil
|
||||
import com.intellij.openapi.project.DumbService
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.scratch.ScratchFile
|
||||
import org.jetbrains.kotlin.idea.scratch.ScratchFileLanguageProvider
|
||||
import org.jetbrains.kotlin.idea.scratch.output.ScratchOutputHandlerAdapter
|
||||
import org.jetbrains.kotlin.idea.scratch.printDebugMessage
|
||||
import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel
|
||||
import org.jetbrains.kotlin.idea.scratch.LOG as log
|
||||
|
||||
class RunScratchAction(private val scratchPanel: ScratchTopPanel) : AnAction(
|
||||
KotlinBundle.message("scratch.run.button"),
|
||||
class RunScratchAction : ScratchAction(
|
||||
KotlinBundle.message("scratch.run.button"),
|
||||
AllIcons.Actions.Execute
|
||||
) {
|
||||
|
||||
init {
|
||||
KeymapManager.getInstance().activeKeymap.getShortcuts("Kotlin.RunScratch").firstOrNull()?.let {
|
||||
templatePresentation.text += " (${KeymapUtil.getShortcutText(it)})"
|
||||
}
|
||||
}
|
||||
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val project = e.project ?: return
|
||||
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
|
||||
val scratchPanel = getScratchPanel(editor) ?: return
|
||||
|
||||
val scratchFile = scratchPanel.scratchFile
|
||||
val psiFile = scratchFile.getPsiFile() ?: return
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-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/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.scratch.actions
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
|
||||
import org.jetbrains.kotlin.idea.scratch.getScratchPanel
|
||||
import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel
|
||||
import javax.swing.Icon
|
||||
|
||||
abstract class ScratchAction(message: String, icon: Icon) : AnAction(message, message, icon) {
|
||||
override fun update(e: AnActionEvent) {
|
||||
val presentation = e.presentation
|
||||
|
||||
val project = e.project ?: return
|
||||
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
|
||||
val scratchPanel = getScratchPanel(editor)
|
||||
|
||||
presentation.isVisible = scratchPanel != null
|
||||
}
|
||||
|
||||
protected fun getScratchPanel(editor: Editor): ScratchTopPanel? {
|
||||
return TextEditorProvider.getInstance().getTextEditor(editor).getScratchPanel()
|
||||
}
|
||||
}
|
||||
@@ -121,9 +121,9 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
|
||||
|
||||
private fun createActionsToolbar(): JComponent {
|
||||
val toolbarGroup = DefaultActionGroup().apply {
|
||||
add(RunScratchAction(this@ScratchTopPanel))
|
||||
add(RunScratchAction())
|
||||
addSeparator()
|
||||
add(ClearScratchAction(this@ScratchTopPanel))
|
||||
add(ClearScratchAction())
|
||||
}
|
||||
|
||||
return ActionManager.getInstance().createActionToolbar(ActionPlaces.EDITOR_TOOLBAR, toolbarGroup, true).component
|
||||
|
||||
@@ -230,6 +230,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -228,6 +228,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -229,6 +229,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -230,6 +230,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -228,6 +228,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -228,6 +228,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -229,6 +229,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
|
||||
<add-to-group group-id="GenerateGroup" anchor="first"/>
|
||||
</group>
|
||||
|
||||
<action id="Kotlin.RunScratch" class="org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction"
|
||||
text="Run Kotlin Scratch" description="Run Kotlin Scratch">
|
||||
<keyboard-shortcut first-keystroke="control alt W" keymap="$default"/>
|
||||
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
|
||||
</action>
|
||||
<action id="Kotlin.ClearScratch" class="org.jetbrains.kotlin.idea.scratch.actions.ClearScratchAction"
|
||||
text="Clear Kotlin Scratch" description="Clear Kotlin Scratch">
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
@@ -114,7 +114,7 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() {
|
||||
val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile)?: error("Couldn't find scratch panel")
|
||||
scratchPanel.setReplMode(isRepl)
|
||||
|
||||
val action = RunScratchAction(scratchPanel)
|
||||
val action = RunScratchAction()
|
||||
val event = getActionEvent(scratchFile, action)
|
||||
launchAction(event, action)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user