[ide-console] Choose module popup dialog
This commit is contained in:
committed by
Pavel V. Talanov
parent
a17363459a
commit
5548647fd3
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.kotlin.console.actions
|
||||
|
||||
import com.intellij.openapi.actionSystem.*
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleManager
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory
|
||||
import org.jetbrains.kotlin.console.KotlinConsoleKeeper
|
||||
|
||||
public class KotlinConsoleModuleDialog(private val project: Project) {
|
||||
private val TITLE = "Choose context module..."
|
||||
|
||||
public fun showIfNeeded(dataContext: DataContext) {
|
||||
val module = getModule(dataContext)
|
||||
if (module != null) return runConsole(module)
|
||||
|
||||
val modules = ModuleManager.getInstance(project).modules
|
||||
|
||||
if (modules.isEmpty()) return errorNotification(project, "No modules were found")
|
||||
if (modules.size() == 1) return runConsole(modules.first())
|
||||
|
||||
val moduleActions = modules sortBy { it.name } map { runAction(it) }
|
||||
val moduleGroup = DefaultActionGroup(moduleActions)
|
||||
|
||||
val modulePopup = JBPopupFactory.getInstance().createActionGroupPopup(
|
||||
TITLE, moduleGroup, dataContext, JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, true, ActionPlaces.UNKNOWN
|
||||
)
|
||||
|
||||
modulePopup.showCenteredInCurrentWindow(project)
|
||||
}
|
||||
|
||||
private fun getModule(dataContext: DataContext): Module? {
|
||||
val file = CommonDataKeys.VIRTUAL_FILE.getData(dataContext)
|
||||
|
||||
if (file != null) {
|
||||
val moduleForFile = ModuleUtilCore.findModuleForFile(file, project)
|
||||
if (moduleForFile != null) return moduleForFile
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun runConsole(module: Module) = KotlinConsoleKeeper.getInstance(project).run(module) let { Unit }
|
||||
|
||||
private fun runAction(module: Module) = object : AnAction(module.name) {
|
||||
override fun actionPerformed(_: AnActionEvent) = runConsole(module)
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,7 @@ import com.intellij.notification.NotificationType
|
||||
import com.intellij.notification.Notifications
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleManager
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import org.jetbrains.kotlin.console.KotlinConsoleKeeper
|
||||
@@ -44,21 +40,8 @@ fun logError(cl: Class<*>, message: String) {
|
||||
public class RunKotlinConsoleAction : AnAction() {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val project = e.project ?: return errorNotification(null, "<p>Project not found</p>")
|
||||
val module = getModule(e) ?: return errorNotification(project, "<p>Module not found</p>")
|
||||
|
||||
KotlinConsoleKeeper.getInstance(project).run(module)
|
||||
}
|
||||
|
||||
private fun getModule(e: AnActionEvent): Module? {
|
||||
val project = e.project ?: return null
|
||||
val file = CommonDataKeys.VIRTUAL_FILE.getData(e.dataContext)
|
||||
|
||||
if (file != null) {
|
||||
val moduleForFile = ModuleUtilCore.findModuleForFile(file, project)
|
||||
if (moduleForFile != null) return moduleForFile
|
||||
}
|
||||
|
||||
return ModuleManager.getInstance(project).modules.firstOrNull()
|
||||
KotlinConsoleModuleDialog(project).showIfNeeded(e.dataContext)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user