Skip to content

Commit

Permalink
fix(exts/devin-lang): enhance CodeBlockElement to correctly handle in…
Browse files Browse the repository at this point in the history
…jection 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.
  • Loading branch information
phodal committed Mar 12, 2024
1 parent 8a80c12 commit 3bcf5a6
Showing 1 changed file with 9 additions and 6 deletions.
Expand Up @@ -17,8 +17,15 @@ import com.intellij.psi.util.*
class CodeBlockElement(node: ASTNode) : ASTWrapperPsiElement(node), PsiLanguageInjectionHost,
InjectionBackgroundSuppressor {
override fun isValidHost(): Boolean {
// use MarkdownCodeFenceUtils.isAbleToAcceptInjections
return true
return isAbleToAcceptInjections(this)
}

private fun isAbleToAcceptInjections(host: CodeBlockElement): Boolean {
val hasStartBlock = host.firstChild?.elementType != DevInTypes.CODE_BLOCK_START
val hasEndBlock = host.lastChild?.elementType != DevInTypes.CODE_BLOCK_END
val hasContents = host.children.count { it.hasType(DevInTypes.CODE_CONTENTS) } < 2

return !(hasStartBlock && hasEndBlock && hasContents)
}

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

fun getContents(): PsiElement? {
return findChildByType(DevInTypes.CODE_CONTENTS)
}

companion object {
fun obtainFenceContent(element: CodeBlockElement): List<PsiElement>? {
return CachedValuesManager.getCachedValue(element) {
Expand Down

0 comments on commit 3bcf5a6

Please sign in to comment.