@@ -3,14 +3,19 @@ package cc.unitmesh.rust.provider
33import cc.unitmesh.devti.custom.document.LivingDocumentationType
44import cc.unitmesh.devti.provider.LivingDocumentation
55import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
6+ import com.intellij.openapi.command.WriteCommandAction
67import com.intellij.openapi.editor.Editor
78import com.intellij.openapi.editor.SelectionModel
89import com.intellij.psi.PsiElement
910import com.intellij.psi.PsiNameIdentifierOwner
11+ import com.intellij.psi.codeStyle.CodeStyleManager
1012import com.intellij.psi.util.PsiTreeUtil
13+ import com.intellij.util.IncorrectOperationException
1114import org.rust.lang.core.psi.RsFunction
1215import org.rust.lang.core.psi.ext.RsNameIdentifierOwner
1316import org.rust.lang.core.psi.ext.RsStructOrEnumItemElement
17+ import org.rust.lang.doc.psi.RsDocComment
18+ import org.rust.lang.doc.psi.ext.containingDoc
1419
1520class RustLivingDocumentation : LivingDocumentation {
1621 override val forbiddenRules: List <String >
@@ -22,13 +27,39 @@ class RustLivingDocumentation : LivingDocumentation {
2227
2328 override fun updateDoc (target : PsiElement , newDoc : String , type : LivingDocumentationType , editor : Editor ) {
2429 val project = target.project
25- val file = target.containingFile
26- val startOffset = target.textRange.startOffset
27- val newEndOffset = startOffset + newDoc.length
30+ val codeStyleManager = CodeStyleManager .getInstance(project)
31+ WriteCommandAction .runWriteCommandAction(project, " Living Document" , " cc.unitmesh.livingDoc" , {
32+ val startOffset = target.textRange.startOffset
33+ val newEndOffset = startOffset + newDoc.length
2834
29- editor.document.insertString(startOffset, newDoc)
30- val codeStyleManager = com.intellij.psi.codeStyle.CodeStyleManager .getInstance(project)
31- codeStyleManager.reformatText(file, startOffset, newEndOffset)
35+ when (type) {
36+ LivingDocumentationType .COMMENT -> {
37+ val psiElementFactory = org.rust.lang.core.psi.RsPsiFactory (project)
38+ val newDocComment = psiElementFactory.createBlockComment(newDoc)
39+
40+ if (target is RsDocComment ) {
41+ val oldDocComment = target.containingDoc
42+ if (oldDocComment != null ) {
43+ oldDocComment.replace(newDocComment)
44+ } else {
45+ target.addBefore(newDocComment, target.firstChild)
46+ }
47+ } else {
48+ target.addBefore(newDocComment, target.firstChild)
49+ }
50+ }
51+
52+ LivingDocumentationType .ANNOTATED -> {
53+ editor.document.insertString(startOffset, newDoc)
54+ codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
55+ }
56+
57+ LivingDocumentationType .CUSTOM -> {
58+ editor.document.insertString(startOffset, newDoc)
59+ codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
60+ }
61+ }
62+ })
3263 }
3364
3465 override fun findNearestDocumentationTarget (psiElement : PsiElement ): PsiNameIdentifierOwner ? {
0 commit comments