<commit_before>package com.elpassion.intelijidea.action.select

import com.elpassion.intelijidea.action.configure.selector.MFSelectorResult
import com.elpassion.intelijidea.action.configure.selector.mfSelector
import com.elpassion.intelijidea.action.configure.selector.showSelectorDialog
import com.elpassion.intelijidea.task.mfBeforeRunTaskProvider
import com.elpassion.intelijidea.util.showInfo
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project

class MFSelectConfigurationsAction : AnAction(MF_SELECT_CONFIGURATIONS_ACTION) {

    override fun actionPerformed(event: AnActionEvent) {
        event.project?.let { selectConfigurations(it) }
    }

    private fun selectConfigurations(project: Project) {
        mfSelector(project) { configuration,templates ->
            showSelectorDialog(project, configuration, templates)
        }.map { result ->
            configureSelections(project, result)
        }.subscribe { (injected, restored) ->
            project.showConfigurationChangesCountInfo(injected, restored)
        }
    }

    private fun configureSelections(project: Project, selectorResult: MFSelectorResult) = with(selectorResult) {
        TaskManipulator(project).run {
            when (replaceAll) {
                true -> injectMFToConfigurationsWithReplacingMFTask(project.mfBeforeRunTaskProvider, toInject)
                false -> injectMFToConfigurations(project.mfBeforeRunTaskProvider, toInject)
            }
            restoreConfigurations(toRestore)
        }
        toInject.size to toRestore.size
    }

    private fun Project.showConfigurationChangesCountInfo(injected: Int, restored: Int) {
        showInfo(this, "Number of injected configurations $injected, restored to default $restored")
    }

    companion object {
        private val MF_SELECT_CONFIGURATIONS_ACTION = "Select configurations to inject mainframer or restore to default"
    }
}<commit_msg>#108 Add check settings before make injection on configuration select<commit_after>package com.elpassion.intelijidea.action.select

import com.elpassion.intelijidea.action.configure.configureMFToProject
import com.elpassion.intelijidea.action.configure.selector.MFSelectorResult
import com.elpassion.intelijidea.action.configure.selector.mfSelector
import com.elpassion.intelijidea.action.configure.selector.showSelectorDialog
import com.elpassion.intelijidea.task.MFBeforeTaskDefaultSettingsProvider
import com.elpassion.intelijidea.task.mfBeforeRunTaskProvider
import com.elpassion.intelijidea.util.showError
import com.elpassion.intelijidea.util.showInfo
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import io.reactivex.Maybe
import javax.swing.event.HyperlinkEvent

class MFSelectConfigurationsAction : AnAction(MF_SELECT_CONFIGURATIONS_ACTION) {

    override fun actionPerformed(event: AnActionEvent) {
        event.project?.let { selectConfigurations(it) }
    }

    private fun selectConfigurations(project: Project) {
        mfSelector(project) { configuration, templates ->
            showSelectorDialog(project, configuration, templates)
        }.flatMap {
            if (!it.toInject.isEmpty() && !isTaskSettingValid(project)) {
                Maybe.error(RuntimeException("Invalid inject configuration"))
            } else {
                Maybe.just(it)
            }
        }.map { result ->
            configureSelections(project, result)
        }.subscribe({ (injected, restored) ->
            project.showConfigurationChangesCountInfo(injected, restored)
        }, {
            showMFConfigurationError(project)
        })
    }

    private fun showMFConfigurationError(project: Project) {
        showError(project, "MF is not configured, \n" +
                "<a href=\"\">click here</a> to configure") {
            if (it.eventType == HyperlinkEvent.EventType.ACTIVATED) {
                configureMFToProject(project)
            }
        }
    }

    private fun configureSelections(project: Project, selectorResult: MFSelectorResult) = with(selectorResult) {
        TaskManipulator(project).run {
            when (replaceAll) {
                true -> injectMFToConfigurationsWithReplacingMFTask(project.mfBeforeRunTaskProvider, toInject)
                false -> injectMFToConfigurations(project.mfBeforeRunTaskProvider, toInject)
            }
            restoreConfigurations(toRestore)
        }
        toInject.size to toRestore.size
    }

    private fun isTaskSettingValid(project: Project) = MFBeforeTaskDefaultSettingsProvider.getInstance(project).taskData.isValid()

    private fun Project.showConfigurationChangesCountInfo(injected: Int, restored: Int) {
        showInfo(this, "Number of injected configurations $injected, restored to default $restored")
    }

    companion object {
        private val MF_SELECT_CONFIGURATIONS_ACTION = "Select configurations to inject mainframer or restore to default"
    }
}
{'fraction_non_alphanumeric': 0.08103855232100708, 'fraction_numerical': 0.0005900865460267506, 'mean_word_length': 3.572841726618705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2658136', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1319, 'n_words': 244}