Skip to content

Commit a626347

Browse files
committed
feat(custom): enable for custom by varible
1 parent 71393df commit a626347

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

docs/autodev/custom-prompt-dsl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"title": "转译",
77
"autoInvoke": true,
88
"matchRegex": ".*",
9-
"template": "帮我重构下面的代码为 Kotlin 语言,方便于我理解: \n ${SPEC.XXX} ${SELECTION}"
9+
"template": "帮我重构下面的代码为 Kotlin 语言,方便于我理解: \n ${SPEC_XXX} ${SELECTION}"
1010
}
1111
]
1212
```

src/main/kotlin/cc/unitmesh/devti/custom/CustomIntention.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ class CustomIntention(private val intentionConfig: CustomIntentionConfig) : Abst
2929
val withRange = elementWithRange(editor, file, project) ?: return
3030
val selectedText = withRange.first
3131
val psiElement = withRange.second
32-
3332
val prompt: CustomIntentionPrompt = buildCustomPrompt(psiElement!!, selectedText, intentionConfig)
3433

35-
3634
sendToChatPanel(project, getActionType(), object : ContextPrompter() {
3735
override fun displayPrompt(): String {
3836
return prompt.displayPrompt
@@ -52,20 +50,19 @@ class CustomIntention(private val intentionConfig: CustomIntentionConfig) : Abst
5250
val stringBuilderWriter = StringWriter()
5351
val velocityContext = VelocityContext()
5452

55-
val resolverMap = LinkedHashMap<CustomIntentionVariableType, VariableResolver>(10)
56-
5753
val variableResolvers = arrayOf(
5854
MethodInputOutputVariableResolver(psiElement),
5955
SelectionVariableResolver(psiElement.language.displayName ?: "", selectedText),
6056
) + SpecResolverService.getInstance().createResolvers()
6157

58+
val resolverMap = LinkedHashMap<String, VariableResolver>(10)
6259
for (resolver in variableResolvers) {
63-
resolverMap[resolver.type] = resolver
60+
resolverMap[resolver.variableName()] = resolver
6461
}
6562

6663
resolverMap.forEach { (variableType, resolver) ->
6764
val value = resolver.resolve()
68-
velocityContext.put(variableType.toString(), value)
65+
velocityContext.put(variableType, value)
6966
}
7067

7168
Velocity.evaluate(velocityContext, stringBuilderWriter, "", intentionConfig.template)

src/main/kotlin/cc/unitmesh/devti/custom/variable/SpecResolverService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SpecResolverService {
1010

1111
fun createResolvers(): List<SpecVariableResolver> {
1212
return specs.map { (key, value) ->
13-
SpecVariableResolver(key, value)
13+
SpecVariableResolver("SPEC_$key", value)
1414
}
1515
}
1616

src/main/kotlin/cc/unitmesh/devti/custom/variable/SpecVariableResolver.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ class SpecVariableResolver(val key: String, val value: String) : VariableResolve
66
override fun resolve(): String {
77
return value
88
}
9+
10+
override fun variableName(): String {
11+
return key
12+
}
913
}

src/main/kotlin/cc/unitmesh/devti/custom/variable/VariableResolver.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ package cc.unitmesh.devti.custom.variable
33
interface VariableResolver {
44
val type: CustomIntentionVariableType
55
fun resolve(): String
6+
7+
fun variableName() = type.name
68
}

0 commit comments

Comments
 (0)