Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New scenario #80

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig
import org.jetbrains.research.tasktracker.handler.scenario.ScenarioHandler

// Put into plugin.xml
class InitActivity : StartupActivity {
Expand All @@ -19,12 +18,5 @@ class InitActivity : StartupActivity {
// TODO: show an error message to the user if an error occurs
override fun runActivity(project: Project) {
TaskTrackerPlugin.initializationHandler.setupEnvironment(project) // TODO: move to the start point of the task
TaskTrackerPlugin.mainConfig.scenarioConfig?.let { scenarioConf ->
scenarioConf.scenario.getNextStep()?.let { scenarioStep ->
with(ScenarioHandler(TaskTrackerPlugin.mainConfig)) {
scenarioStep.run()
}
} ?: logger.warn("Try to init the plugin, but the scenario is empty or invalid")
} ?: logger.warn("Please, provide a scenario for the plugin behaviour")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.research.tasktracker.config.ide

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import kotlinx.serialization.Serializable
import org.jetbrains.research.tasktracker.config.BaseProjectConfig
import org.jetbrains.research.tasktracker.config.ide.inspection.InspectionConfig
import org.jetbrains.research.tasktracker.config.ide.settings.SettingsConfig
Expand All @@ -10,9 +11,11 @@ import org.jetbrains.research.tasktracker.handler.BaseProjectHandler
import org.jetbrains.research.tasktracker.handler.ide.IdeHandler
import java.io.File

class MainIdeConfig : BaseProjectConfig {
var inspectionConfig: InspectionConfig? = null
@Serializable
data class MainIdeConfig(
var inspectionConfig: InspectionConfig? = null,
var settingsConfig: SettingsConfig? = null
) : BaseProjectConfig {
override val configName: String
get() = "main_ide"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import java.io.File

@Serializable
data class SettingsConfig(
val enableCodeCompletion: SettingMode,
val enableZenMode: SettingMode,
val theme: Theme
val enableCodeCompletion: SettingMode = SettingMode.DEFAULT,
val enableZenMode: SettingMode = SettingMode.DEFAULT,
val theme: Theme = Theme.DEFAULT
) : BaseProjectConfig {

override val configName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.research.tasktracker.config.scenario
import kotlinx.serialization.Serializable
import org.jetbrains.research.tasktracker.config.BaseConfig
import org.jetbrains.research.tasktracker.config.YamlConfigLoadStrategy
import org.jetbrains.research.tasktracker.config.scenario.models.Scenario
import java.io.File

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
package org.jetbrains.research.tasktracker.config.scenario
package org.jetbrains.research.tasktracker.config.scenario.models

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.BaseConfig
import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage
import java.util.*

enum class ScenarioUnitType {
TASK_CONTENT,

// TODO: add other types of content
IDE_SETTINGS;

val config: BaseConfig?
get() = when (this) {
TASK_CONTENT -> TaskTrackerPlugin.mainConfig.taskContentConfig
IDE_SETTINGS -> TaskTrackerPlugin.mainConfig.mainIdeConfig
}
}

@Serializable
data class ScenarioStep(
// TODO: sort according to the plugin's implementation to make sure in the right order
val units: List<ScenarioUnitType>
) {
fun isValid() = units.any { it.config != null }
}

@Serializable
data class Scenario(
@Serializable(with = QueueSerializer::class) val steps: Queue<ScenarioStep>
) {
@Transient
private val logger: Logger = Logger.getInstance(javaClass)

fun getNextStep(): ScenarioStep? {
@Transient
private var currentStepIterator: Iterator<ScenarioUnit>? = null

private fun getNextStep(): ScenarioStep? {
var isValid: Boolean
var step: ScenarioStep
do {
Expand All @@ -58,6 +40,29 @@ data class Scenario(
return step
}

@Suppress("ReturnCount")
fun getNextUnit(project: Project): ScenarioUnit? {
if (!currentStepIterator.notNullAndHasNext()) {
return null
}
cleanStepSettings()
val currentStep = getNextStep() ?: return null
currentStep.prepareSettings(project)
currentStepIterator = currentStep.getUnits().iterator()
return if (currentStepIterator.notNullAndHasNext()) {
currentStepIterator?.next()
} else {
null
}
}

private fun Iterator<ScenarioUnit>?.notNullAndHasNext() = this?.hasNext() ?: false

private fun cleanStepSettings() =
MainPanelStorage.activeIdeHandlers.forEach {
it.destroy()
}

private class QueueSerializer<T>(private val dataSerializer: KSerializer<T>) :
KSerializer<Queue<T>> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jetbrains.research.tasktracker.config.scenario.models

import com.intellij.openapi.project.Project
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.jetbrains.research.tasktracker.config.ide.MainIdeConfig
import org.jetbrains.research.tasktracker.handler.BaseProjectHandler
import org.jetbrains.research.tasktracker.ui.main.panel.storage.MainPanelStorage

@Serializable
data class ScenarioStep(
private val units: List<ScenarioUnit>,
private val ideConfig: MainIdeConfig? = null,
val mode: ScenarioStepMode = ScenarioStepMode.ORDERED
) {

@Transient
private var mainIdeHandler: BaseProjectHandler? = null

fun prepareSettings(project: Project) {
mainIdeHandler = ideConfig?.buildHandler(project)
mainIdeHandler?.let {
MainPanelStorage.activeIdeHandlers.addFirst(it)
it.setup()
}
}

fun getUnits() = when (mode) {
ScenarioStepMode.ORDERED -> units
ScenarioStepMode.SHUFFLED -> units.shuffled()
}

// TODO check if there are any config of specified type in units.
@Suppress("FunctionOnlyReturningConstant")
fun isValid() = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jetbrains.research.tasktracker.config.scenario.models

enum class ScenarioStepMode {
ORDERED,
SHUFFLED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jetbrains.research.tasktracker.config.scenario.models

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.jetbrains.research.tasktracker.config.ide.MainIdeConfig

@Serializable
sealed interface ScenarioUnit

@Serializable
@SerialName("List")
class TaskListUnit(val taskIds: List<String>) : ScenarioUnit

@Serializable
@SerialName("SingleList")
class TaskListWithSingleChoiceUnit(val taskIds: List<String>) : ScenarioUnit

@Serializable
@SerialName("Task")
class TaskUnit(val id: String) : ScenarioUnit

@Serializable
@SerialName("Setting")
class IdeSettingUnit(val mainIdeConfig: MainIdeConfig) : ScenarioUnit

@Serializable
@SerialName("Survey")
class SurveyUnit(val id: String) : ScenarioUnit

@Serializable
@SerialName("External")
class ExternalSourceUnit(val url: String) : ScenarioUnit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.research.tasktracker.config.survey

import kotlinx.serialization.Serializable

@Serializable
data class Survey(val id: String, val htmlQuestions: List<HtmlQuestion>) {
fun toHtml() = htmlQuestions.joinToString(System.lineSeparator()) { it.toHtml() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import java.io.File

@Serializable
class SurveyConfig : BaseConfig {
val htmlQuestions: List<HtmlQuestion> = emptyList()
val surveys: List<Survey> = emptyList()
override val configName: String
get() = CONFIG_FILE_PREFIX

fun toHtml() = htmlQuestions.joinToString(System.lineSeparator()) { it.toHtml() }

companion object {
const val CONFIG_FILE_PREFIX: String = "survey"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class IdeHandler(override val config: MainIdeConfig, override val project: Proje
it?.setup()
}
}

override fun destroy() {
// TODO destroy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import org.jetbrains.research.tasktracker.handler.BaseProjectHandler
class InspectionHandler(override val config: InspectionConfig, override val project: Project) : BaseProjectHandler {
private var inspectionDisposable: Disposable? = null
private val logger: Logger = Logger.getInstance(InspectionHandler::class.java)
private var initialProfile: InspectionProfileImpl? = null

override fun setup() {
initialProfile = ProjectInspectionProfileManager.getInstance(project).currentProfile
// creating a new profile to make changes only in the current project
val profile = initTaskProfile(project)
applyConfig(profile)
Expand Down Expand Up @@ -58,6 +60,9 @@ class InspectionHandler(override val config: InspectionConfig, override val proj
}

override fun destroy() {
initialProfile?.let {
ProjectInspectionProfileManager.getInstance(project).setCurrentProfile(it)
}
inspectionDisposable?.let { Disposer.dispose(it) }
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import java.awt.event.ActionListener
* Note: This class requires JBCefApp to be supported for proper functioning.
*
*/
@Suppress("TooManyFunctions")
class MainPluginPanelFactory : ToolWindowFactory {
// TODO: init in other place, states can be saved between sessions
private val nextButton = createJButton("ui.button.next")
Expand Down Expand Up @@ -97,18 +98,20 @@ class MainPluginPanelFactory : ToolWindowFactory {

fun loadBasePage(
template: HtmlTemplate,
buttonTextKey: String,
isVisibleBackButton: Boolean = true,
buttonTextKey: String? = null,
isVisibleBackButton: Boolean = false,
backButtonText: String? = null,
isVisibleNextButton: Boolean = true
) {
mainWindow.loadHtmlTemplate(template)
nextButton.text = UIBundle.message(buttonTextKey)
backButton.isVisible = isVisibleBackButton
nextButton.isVisible = isVisibleNextButton
backButtonText?.let {
backButton.text = backButtonText
}
buttonTextKey?.let {
nextButton.text = UIBundle.message(buttonTextKey)
}
}

@Suppress("UnusedPrivateMember")
Expand Down Expand Up @@ -192,6 +195,8 @@ class MainPluginPanelFactory : ToolWindowFactory {
true
}

fun openExternalUrl(url: String) = mainWindow.openExternalUrl(url)

fun setNextAction(listener: ActionListener) = nextButton.setListener(listener)

fun setBackAction(listener: ActionListener) = backButton.setListener(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class MainPluginWindow(service: MainWindowService) {
}
}

fun openExternalUrl(url: String) = windowBrowser.loadURL(url)

companion object {
const val JS_QUERY_POOL_SIZE = 100
}
Expand Down
Loading
Loading