diff --git a/README.md b/README.md index 89456fb..dff3c89 100644 --- a/README.md +++ b/README.md @@ -213,9 +213,33 @@ You can read more about it [here](https://www.jetbrains.com/help/idea/managing-p "description": "Writes \"DsTheme.typography\"" }, { - "abbreviation": "dtt", + "abbreviation": "dti", "text": "com.your.designsystem.DsTheme.icons.$CODE_COMPLETION$", "description": "Writes \"DsTheme.icons\"" + }, + { + "abbreviation": "mso", + "text": "$VAL_TYPE$ $NAME$ by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf($VALUE$) }", + "description": "Creates mutableStateOf", + "reformat": true, + "variables": [ + { + "name": "VAL_TYPE", + "expression": "enum(\"var\", \"val\")" + }, + { + "name": "NAME" + }, + { + "name": "VALUE", + "expression": "enum(\"false\", \"null\", \"0\", \"0f\")" + } + ], + "context": [ + "KOTLIN_CLASS", + "KOTLIN_STATEMENT", + "KOTLIN_TOPLEVEL" + ] } ] } @@ -305,28 +329,15 @@ You can read more about it [here](https://www.jetbrains.com/help/idea/managing-p // Installs live templates into the IDE. // Useful for writing frequent code, like "MaterialTheme.colors." in just 3 keystrokes. - // After completion, opens code completion in place of $CODE_COMPLETION$, saving even more effort. + // If you'd like add your own custom templates, export them and manually convert xml to json. + // More: https://www.jetbrains.com/help/idea/2024.1/sharing-live-templates.html#the-quick-way-copy-and-paste "liveTemplates": [ - { - "abbreviation": "dt", - "text": "com.your.designsystem.DsTheme.$CODE_COMPLETION$", - "description": "Writes \"DsTheme.\"" - }, { "abbreviation": "dtc", "text": "com.your.designsystem.DsTheme.colors.$CODE_COMPLETION$", "description": "Writes \"DsTheme.colors\"" - }, - { - "abbreviation": "dtt", - "text": "com.your.designsystem.DsTheme.typography.$CODE_COMPLETION$", - "description": "Writes \"DsTheme.typography\"" - }, - { - "abbreviation": "dtt", - "text": "com.your.designsystem.DsTheme.icons.$CODE_COMPLETION$", - "description": "Writes \"DsTheme.icons\"" } + // see more in the comment-less json ] } ``` diff --git a/gradle.properties b/gradle.properties index 6f1689f..513d182 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = ru.ozon.ideplugin.kelp pluginName = Kelp pluginRepositoryUrl = https://github.com/ozontech/kelp # SemVer format -> https://semver.org -pluginVersion = 0.0.7 +pluginVersion = 0.0.8 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # https://plugins.jetbrains.com/docs/intellij/android-studio-releases-list.html diff --git a/src/main/kotlin/ru/ozon/ideplugin/kelp/liveTemplates/AddLiveTemplates.kt b/src/main/kotlin/ru/ozon/ideplugin/kelp/liveTemplates/AddLiveTemplates.kt index 151f4b1..203d66d 100644 --- a/src/main/kotlin/ru/ozon/ideplugin/kelp/liveTemplates/AddLiveTemplates.kt +++ b/src/main/kotlin/ru/ozon/ideplugin/kelp/liveTemplates/AddLiveTemplates.kt @@ -6,9 +6,44 @@ import org.jdom.Element import ru.ozon.ideplugin.kelp.pluginConfig.KelpConfig internal object AddLiveTemplates { - private val kotlinTemplateContext by lazy { - Element("a").apply { - listOf("KOTLIN_EXPRESSION", "KOTLIN_STATEMENT").forEach { name -> + fun execute(config: KelpConfig, projectName: String) { + val templateSettings = TemplateSettings.getInstance() + removeAllTemplates(templateSettings, projectName) + + val templates = config.liveTemplates.orEmpty() + templatesFromKelpConfig(templates, projectName) + .forEach(templateSettings::addTemplate) + } + + private fun templatesFromKelpConfig( + templates: List, + projectName: String + ) = templates.map { template -> + TemplateImpl( + /* key = */ template.abbreviation, + /* string = */ template.text, + /* group = */ "Kelp ($projectName)" + ).apply { + description = template.description + isToReformat = template.reformat + isToShortenLongNames = template.shortenFQNames + template.variables.forEach { variable -> + addVariable( + /* name = */ variable.name, + /* expression = */ variable.expression, + /* defaultValue = */ variable.defaultValue, + /* isAlwaysStopAt = */ variable.alwaysStopAt, + ) + } + templateContext.readTemplateContext( + buildTemplateContext(template.context) + ) + } + } + + private fun buildTemplateContext(contextIds: List): Element { + return Element("a").apply { + contextIds.forEach { name -> addContent( Element("option").apply { setAttribute("name", name) @@ -19,24 +54,6 @@ internal object AddLiveTemplates { } } - fun execute(config: KelpConfig, projectName: String) { - val templates = config.liveTemplates.orEmpty() - val templateSettings = TemplateSettings.getInstance() - removeAllTemplates(templateSettings, projectName) - templates.forEach { template -> - TemplateImpl( - /* key = */ template.abbreviation, - /* string = */ template.text, - /* group = */ "Kelp ($projectName)" - ).apply { - template.description?.let { description = it } - addVariable("CODE_COMPLETION", "complete()", "", true) - templateContext.readTemplateContext(kotlinTemplateContext) - templateSettings.addTemplate(this) - } - } - } - private fun removeAllTemplates( templateSettings: TemplateSettings, projectName: String diff --git a/src/main/kotlin/ru/ozon/ideplugin/kelp/pluginConfig/KelpConfig.kt b/src/main/kotlin/ru/ozon/ideplugin/kelp/pluginConfig/KelpConfig.kt index 4c8ee95..921af44 100644 --- a/src/main/kotlin/ru/ozon/ideplugin/kelp/pluginConfig/KelpConfig.kt +++ b/src/main/kotlin/ru/ozon/ideplugin/kelp/pluginConfig/KelpConfig.kt @@ -78,5 +78,17 @@ class KelpConfig( val abbreviation: String, val text: String, val description: String? = null, - ) + val reformat: Boolean = false, + val shortenFQNames: Boolean = true, + val variables: List = emptyList(), + val context: List = listOf("KOTLIN_EXPRESSION", "KOTLIN_STATEMENT"), + ) { + @Serializable + class Variable( + val name: String, + val expression: String = "", + val defaultValue: String = "", + val alwaysStopAt: Boolean = true, + ) + } }