<commit_before>package com.github.shiraji.permissionsdispatcherplugin.actions

import com.github.shiraji.permissionsdispatcherplugin.config.GeneratePMCodeConfig
import com.github.shiraji.permissionsdispatcherplugin.data.PdVersion
import com.github.shiraji.permissionsdispatcherplugin.data.RebuildType
import com.github.shiraji.permissionsdispatcherplugin.extentions.generateVersionNumberFrom
import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerJava
import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerKt
import com.github.shiraji.permissionsdispatcherplugin.models.GeneratePMCodeModel
import com.github.shiraji.permissionsdispatcherplugin.views.GeneratePMCodeDialog
import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.actions.CodeInsightAction
import com.intellij.compiler.actions.CompileProjectAction
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression
import javax.swing.JOptionPane

class GeneratePMCodeAction : CodeInsightAction() {
    lateinit var model: GeneratePMCodeModel
    var isKotlin: Boolean = false
    override fun getHandler(): CodeInsightActionHandler {
        if (isKotlin) {
            return GeneratePMCodeHandlerKt(model)
        } else {
            return GeneratePMCodeHandlerJava(model)
        }
    }

    override fun update(e: AnActionEvent?) {
        e ?: return
        super.update(e)

        val project = e.getData(CommonDataKeys.PROJECT)
        val file = e.getData(CommonDataKeys.PSI_FILE)
        val clazz = when (file) {
            is PsiJavaFile -> file.classes[0]
            is KtFile -> file.classes[0]
            else -> null
        }

        e.presentation.isEnabledAndVisible = project != null && clazz != null && GeneratePMCodeModel(project).isActivityOrFragment(clazz)
    }

    override fun actionPerformed(e: AnActionEvent?) {
        val project = e?.getData(CommonDataKeys.PROJECT) ?: return
        isKotlin = e?.getData(CommonDataKeys.PSI_FILE) is KtFile

        var pdVersion: PdVersion = PdVersion.NOTFOUND

        fun updatePdVersion(dependenciesBlock: GrMethodCallExpression) {
            val pdLine = dependenciesBlock.findDescendantOfType<GrCommandArgumentList> {
                it.text.contains("com.github.hotchemi:permissionsdispatcher:")
            }

            pdLine?.text?.let {
                text ->
                // for now, forget about variables...
                val versionText = text.generateVersionNumberFrom()
                pdVersion = PdVersion.fromText(versionText)
            }
        }

        fun generatePMCode() {
            val dialog = GeneratePMCodeDialog(project, pdVersion)
            if (dialog.showAndGet()) {
                model = GeneratePMCodeModel(project)

                model.apply {
                    permissions = dialog.selectedPermissions
                    if (dialog.needsPermissionCheckBox.isSelected) needsPermissionMethodName = dialog.needsPermissionTextField.text
                    if (dialog.onShowRationaleCheckBox.isSelected) onShowRationaleMethodName = dialog.onShowRationaleTextField.text
                    if (dialog.onPermissionDeniedCheckBox.isSelected) onPermissionDeniedMethodName = dialog.onPermissionDeniedTextField.text
                    if (dialog.onNeverAskAgainCheckBox.isSelected) onNeverAskAgainMethodName = dialog.onNeverAskAgainTextField.text
                    val maxSdkVersionText = dialog.maxSdkVersionTextField.text
                    if (maxSdkVersionText != null && maxSdkVersionText.isNotBlank()) {
                        maxSdkVersion = maxSdkVersionText.toInt()
                    }
                }

                super.actionPerformed(e)
                rebuildAction(e)
            }
        }

        FilenameIndex.getAllFilesByExt(project, "gradle", GlobalSearchScope.projectScope(project)).forEach {
            val groovyFile = PsiManager.getInstance(project).findFile(it) as? GroovyFile ?: return@forEach
            val dependenciesBlock = groovyFile.findDescendantOfType<GrMethodCallExpression> {
                it.invokedExpression.text == "dependencies"
            } ?: return@forEach
            updatePdVersion(dependenciesBlock)
        }

        if (pdVersion == PdVersion.NOTFOUND) {
            // no dependencies found for PermissionsDispatcher!
            Notifications.Bus.notify(Notification(
                    "PermissionsManager Plugin",
                    "No PermissionsDispatcher dependency found",
                    "Please add PermissionsDispatcher dependency",
                    NotificationType.WARNING))
        }

        generatePMCode()
    }

    private fun rebuildAction(e: AnActionEvent?) {
        when (RebuildType.fromId(GeneratePMCodeConfig.rebuildTypeId)) {
            RebuildType.ALWAYS -> rebuild(e)
            RebuildType.PROMPT -> {
                val result = JOptionPane.showOptionDialog(null, "Do you want to rebuild this project?", "PermissionsManager Plugin", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, arrayOf("Rebuild", "Cancel"), null)
                if (result == JOptionPane.YES_OPTION) {
                    rebuild(e)
                }
            }
            RebuildType.NOT_ALWAYS -> println("Skip rebuild.")
        }
    }

    private fun rebuild(e: AnActionEvent?) {
        CompileProjectAction().actionPerformed(e)
    }
}<commit_msg>Make shorter popup title<commit_after>package com.github.shiraji.permissionsdispatcherplugin.actions

import com.github.shiraji.permissionsdispatcherplugin.config.GeneratePMCodeConfig
import com.github.shiraji.permissionsdispatcherplugin.data.PdVersion
import com.github.shiraji.permissionsdispatcherplugin.data.RebuildType
import com.github.shiraji.permissionsdispatcherplugin.extentions.generateVersionNumberFrom
import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerJava
import com.github.shiraji.permissionsdispatcherplugin.handlers.GeneratePMCodeHandlerKt
import com.github.shiraji.permissionsdispatcherplugin.models.GeneratePMCodeModel
import com.github.shiraji.permissionsdispatcherplugin.views.GeneratePMCodeDialog
import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.actions.CodeInsightAction
import com.intellij.compiler.actions.CompileProjectAction
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression
import javax.swing.JOptionPane

class GeneratePMCodeAction : CodeInsightAction() {
    lateinit var model: GeneratePMCodeModel
    var isKotlin: Boolean = false
    override fun getHandler(): CodeInsightActionHandler {
        if (isKotlin) {
            return GeneratePMCodeHandlerKt(model)
        } else {
            return GeneratePMCodeHandlerJava(model)
        }
    }

    override fun update(e: AnActionEvent?) {
        e ?: return
        super.update(e)

        val project = e.getData(CommonDataKeys.PROJECT)
        val file = e.getData(CommonDataKeys.PSI_FILE)
        val clazz = when (file) {
            is PsiJavaFile -> file.classes[0]
            is KtFile -> file.classes[0]
            else -> null
        }

        e.presentation.isEnabledAndVisible = project != null && clazz != null && GeneratePMCodeModel(project).isActivityOrFragment(clazz)
    }

    override fun actionPerformed(e: AnActionEvent?) {
        val project = e?.getData(CommonDataKeys.PROJECT) ?: return
        isKotlin = e?.getData(CommonDataKeys.PSI_FILE) is KtFile

        var pdVersion: PdVersion = PdVersion.NOTFOUND

        fun updatePdVersion(dependenciesBlock: GrMethodCallExpression) {
            val pdLine = dependenciesBlock.findDescendantOfType<GrCommandArgumentList> {
                it.text.contains("com.github.hotchemi:permissionsdispatcher:")
            }

            pdLine?.text?.let {
                text ->
                // for now, forget about variables...
                val versionText = text.generateVersionNumberFrom()
                pdVersion = PdVersion.fromText(versionText)
            }
        }

        fun generatePMCode() {
            val dialog = GeneratePMCodeDialog(project, pdVersion)
            if (dialog.showAndGet()) {
                model = GeneratePMCodeModel(project)

                model.apply {
                    permissions = dialog.selectedPermissions
                    if (dialog.needsPermissionCheckBox.isSelected) needsPermissionMethodName = dialog.needsPermissionTextField.text
                    if (dialog.onShowRationaleCheckBox.isSelected) onShowRationaleMethodName = dialog.onShowRationaleTextField.text
                    if (dialog.onPermissionDeniedCheckBox.isSelected) onPermissionDeniedMethodName = dialog.onPermissionDeniedTextField.text
                    if (dialog.onNeverAskAgainCheckBox.isSelected) onNeverAskAgainMethodName = dialog.onNeverAskAgainTextField.text
                    val maxSdkVersionText = dialog.maxSdkVersionTextField.text
                    if (maxSdkVersionText != null && maxSdkVersionText.isNotBlank()) {
                        maxSdkVersion = maxSdkVersionText.toInt()
                    }
                }

                super.actionPerformed(e)
                rebuildAction(e)
            }
        }

        FilenameIndex.getAllFilesByExt(project, "gradle", GlobalSearchScope.projectScope(project)).forEach {
            val groovyFile = PsiManager.getInstance(project).findFile(it) as? GroovyFile ?: return@forEach
            val dependenciesBlock = groovyFile.findDescendantOfType<GrMethodCallExpression> {
                it.invokedExpression.text == "dependencies"
            } ?: return@forEach
            updatePdVersion(dependenciesBlock)
        }

        if (pdVersion == PdVersion.NOTFOUND) {
            // no dependencies found for PermissionsDispatcher!
            Notifications.Bus.notify(Notification(
                    "PermissionsManager Plugin",
                    "No PermissionsDispatcher dependency",
                    "Please add PermissionsDispatcher dependency",
                    NotificationType.WARNING))
        }

        generatePMCode()
    }

    private fun rebuildAction(e: AnActionEvent?) {
        when (RebuildType.fromId(GeneratePMCodeConfig.rebuildTypeId)) {
            RebuildType.ALWAYS -> rebuild(e)
            RebuildType.PROMPT -> {
                val result = JOptionPane.showOptionDialog(null, "Do you want to rebuild this project?", "PermissionsManager Plugin", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, arrayOf("Rebuild", "Cancel"), null)
                if (result == JOptionPane.YES_OPTION) {
                    rebuild(e)
                }
            }
            RebuildType.NOT_ALWAYS -> println("Skip rebuild.")
        }
    }

    private fun rebuild(e: AnActionEvent?) {
        CompileProjectAction().actionPerformed(e)
    }
}
{'fraction_non_alphanumeric': 0.0793818625457503, 'fraction_numerical': 0.00032533550223668155, 'mean_word_length': 2.860596546310832, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '2149133', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3213, 'n_words': 518}