Skip to content

Commit 5231b70

Browse files
committed
feat(typescript): refactor JSDoc comment insertion logic to handle null values and exceptions more gracefully #2
1 parent 30f301e commit 5231b70

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/provider/JavaScriptLivingDocumentation.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
5252
?: findDocFallback(target)
5353

5454
try {
55-
val createJSDocComment: PsiElement = JSPsiElementFactory.createJSDocComment(newDoc, target)
56-
57-
if (existingComment != null) {
58-
existingComment.replace(createJSDocComment)
59-
} else {
60-
val parent = target.parent
61-
parent.addBefore(createJSDocComment, target)
62-
JSChangeUtil.addWs(parent.node, target.node, "\n")
63-
}
55+
didInsertComment(newDoc, target, existingComment)
6456
} catch (e: Exception) {
65-
editor.document.insertString(startOffset, newDoc)
66-
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
57+
// second attempt
58+
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDoc, "/**", "*/")
59+
try {
60+
didInsertComment(fromSuggestion, target, existingComment)
61+
} catch (e: Exception) {
62+
editor.document.insertString(startOffset, newDoc)
63+
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
64+
}
6765
}
6866
}
6967

@@ -80,6 +78,18 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
8078
})
8179
}
8280

81+
private fun didInsertComment(newDoc: String, target: PsiElement, existingComment: JSDocComment?) {
82+
val createJSDocComment: PsiElement = JSPsiElementFactory.createJSDocComment(newDoc, target)
83+
84+
if (existingComment != null) {
85+
existingComment.replace(createJSDocComment)
86+
} else {
87+
val parent = target.parent
88+
parent.addBefore(createJSDocComment, target)
89+
JSChangeUtil.addWs(parent.node, target.node, "\n")
90+
}
91+
}
92+
8393
private fun findDocFallback(documentationTarget: PsiElement): JSDocComment? {
8494
val parentOfDestructuring: PsiElement? by lazy {
8595
var context = documentationTarget.context

0 commit comments

Comments
 (0)