Skip to content

Commit 3bcf5a6

Browse files
committed
fix(exts/devin-lang): enhance CodeBlockElement to correctly handle injection host validations #101
Previously, the `CodeBlockElement` class was using a hardcoded check to determine its ability to accept language injections, which was not ideal. This commit introduces a more robust method, `isAbleToAcceptInjections`, that takes into account the presence of start and end blocks, as well as the number of content elements within the code block. This ensures that the injection logic is more accurate and flexible, reducing the risk of incorrect language injections.
1 parent 8a80c12 commit 3bcf5a6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

exts/devin-lang/src/main/kotlin/cc/unitmesh/language/parser/CodeBlockElement.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ import com.intellij.psi.util.*
1717
class CodeBlockElement(node: ASTNode) : ASTWrapperPsiElement(node), PsiLanguageInjectionHost,
1818
InjectionBackgroundSuppressor {
1919
override fun isValidHost(): Boolean {
20-
// use MarkdownCodeFenceUtils.isAbleToAcceptInjections
21-
return true
20+
return isAbleToAcceptInjections(this)
21+
}
22+
23+
private fun isAbleToAcceptInjections(host: CodeBlockElement): Boolean {
24+
val hasStartBlock = host.firstChild?.elementType != DevInTypes.CODE_BLOCK_START
25+
val hasEndBlock = host.lastChild?.elementType != DevInTypes.CODE_BLOCK_END
26+
val hasContents = host.children.count { it.hasType(DevInTypes.CODE_CONTENTS) } < 2
27+
28+
return !(hasStartBlock && hasEndBlock && hasContents)
2229
}
2330

2431
override fun updateText(text: String): PsiLanguageInjectionHost {
@@ -33,10 +40,6 @@ class CodeBlockElement(node: ASTNode) : ASTWrapperPsiElement(node), PsiLanguageI
3340
return findChildByType(DevInTypes.LANGUAGE_ID)
3441
}
3542

36-
fun getContents(): PsiElement? {
37-
return findChildByType(DevInTypes.CODE_CONTENTS)
38-
}
39-
4043
companion object {
4144
fun obtainFenceContent(element: CodeBlockElement): List<PsiElement>? {
4245
return CachedValuesManager.getCachedValue(element) {

0 commit comments

Comments
 (0)