Skip to content

Commit 8083b51

Browse files
committed
feat(typescript): simplify insert logic #2
1 parent 5231b70 commit 8083b51

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
5050
LivingDocumentationType.COMMENT -> {
5151
val existingComment = JSDocumentationUtils.findOwnDocComment(target)
5252
?: findDocFallback(target)
53+
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDoc, "/**", "*/")
5354

5455
try {
55-
didInsertComment(newDoc, target, existingComment)
56-
} catch (e: Exception) {
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)
56+
val createJSDocComment: PsiElement =
57+
JSPsiElementFactory.createJSDocComment(fromSuggestion, target)
58+
59+
if (existingComment != null) {
60+
existingComment.replace(createJSDocComment)
61+
} else {
62+
val parent = target.parent
63+
parent.addBefore(createJSDocComment, target)
64+
JSChangeUtil.addWs(parent.node, target.node, "\n")
6465
}
66+
} catch (e: Exception) {
67+
editor.document.insertString(startOffset, newDoc)
68+
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
6569
}
6670
}
6771

@@ -78,18 +82,6 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
7882
})
7983
}
8084

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-
9385
private fun findDocFallback(documentationTarget: PsiElement): JSDocComment? {
9486
val parentOfDestructuring: PsiElement? by lazy {
9587
var context = documentationTarget.context

0 commit comments

Comments
 (0)