Skip to content

Commit 0e52bca

Browse files
committed
fix(devins-lang): refactor language injection check #101
Previously, the language injection check was prone to unnecessary re-evaluation due to the use of `||` and `&&`. This commit refactors the code to use `?:` and `?: return` to ensure that the check is performed only once and the function returns as soon as the condition is met, improving the efficiency and readability of the code.
1 parent 6a20095 commit 0e52bca

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/DevInLanguageInjector.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,16 @@ import com.intellij.psi.util.elementType
1313

1414
class DevInLanguageInjector : LanguageInjector {
1515
override fun getLanguagesToInject(host: PsiLanguageInjectionHost, registrar: InjectedLanguagePlaces) {
16-
if (host !is CodeBlockElement || !host.isValidHost()) {
17-
return
18-
}
16+
if (host !is CodeBlockElement || !host.isValidHost()) return
1917

20-
val hasContentsElement = host.children.any { it.elementType == DevInTypes.CODE_CONTENTS }
21-
if (!hasContentsElement) {
22-
return
23-
}
18+
val hasCodeContents = host.children.any { it.elementType == DevInTypes.CODE_CONTENTS }
19+
if (!hasCodeContents) return
2420

25-
val languageIdentifier = host.getLanguageId()
26-
val text = languageIdentifier?.text ?: return
21+
val text = host.getLanguageId()?.text ?: return
2722
val language = findLanguage(text)
2823

2924
val contentList = CodeBlockElement.obtainFenceContent(host) ?: return
30-
if (contentList.isEmpty()) {
31-
return
32-
}
25+
if (contentList.isEmpty()) return
3326

3427
injectAsOnePlace(host, language, registrar)
3528
}

0 commit comments

Comments
 (0)