Skip to content

Commit

Permalink
feat(typescript): simplify insert logic #2
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Apr 10, 2024
1 parent 5231b70 commit 8083b51
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
LivingDocumentationType.COMMENT -> {
val existingComment = JSDocumentationUtils.findOwnDocComment(target)
?: findDocFallback(target)
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDoc, "/**", "*/")

try {
didInsertComment(newDoc, target, existingComment)
} catch (e: Exception) {
// second attempt
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDoc, "/**", "*/")
try {
didInsertComment(fromSuggestion, target, existingComment)
} catch (e: Exception) {
editor.document.insertString(startOffset, newDoc)
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
val createJSDocComment: PsiElement =
JSPsiElementFactory.createJSDocComment(fromSuggestion, target)

if (existingComment != null) {
existingComment.replace(createJSDocComment)
} else {
val parent = target.parent
parent.addBefore(createJSDocComment, target)
JSChangeUtil.addWs(parent.node, target.node, "\n")
}
} catch (e: Exception) {
editor.document.insertString(startOffset, newDoc)
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
}
}

Expand All @@ -78,18 +82,6 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
})
}

private fun didInsertComment(newDoc: String, target: PsiElement, existingComment: JSDocComment?) {
val createJSDocComment: PsiElement = JSPsiElementFactory.createJSDocComment(newDoc, target)

if (existingComment != null) {
existingComment.replace(createJSDocComment)
} else {
val parent = target.parent
parent.addBefore(createJSDocComment, target)
JSChangeUtil.addWs(parent.node, target.node, "\n")
}
}

private fun findDocFallback(documentationTarget: PsiElement): JSDocComment? {
val parentOfDestructuring: PsiElement? by lazy {
var context = documentationTarget.context
Expand Down

0 comments on commit 8083b51

Please sign in to comment.