Skip to content

Commit

Permalink
feature: fully custom live templates from config + mutableStateOf tem…
Browse files Browse the repository at this point in the history
…plate
  • Loading branch information
popovanton0 committed May 22, 2024
1 parent 63be05f commit a7c546b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 40 deletions.
45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
Expand Down Expand Up @@ -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
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KelpConfig.LiveTemplate>,
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<String>): Element {
return Element("a").apply {
contextIds.forEach { name ->
addContent(
Element("option").apply {
setAttribute("name", name)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Variable> = emptyList(),
val context: List<String> = listOf("KOTLIN_EXPRESSION", "KOTLIN_STATEMENT"),
) {
@Serializable
class Variable(
val name: String,
val expression: String = "",
val defaultValue: String = "",
val alwaysStopAt: Boolean = true,
)
}
}

0 comments on commit a7c546b

Please sign in to comment.