@@ -3,14 +3,19 @@ package cc.unitmesh.rust.provider
3
3
import cc.unitmesh.devti.custom.document.LivingDocumentationType
4
4
import cc.unitmesh.devti.provider.LivingDocumentation
5
5
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
6
+ import com.intellij.openapi.command.WriteCommandAction
6
7
import com.intellij.openapi.editor.Editor
7
8
import com.intellij.openapi.editor.SelectionModel
8
9
import com.intellij.psi.PsiElement
9
10
import com.intellij.psi.PsiNameIdentifierOwner
11
+ import com.intellij.psi.codeStyle.CodeStyleManager
10
12
import com.intellij.psi.util.PsiTreeUtil
13
+ import com.intellij.util.IncorrectOperationException
11
14
import org.rust.lang.core.psi.RsFunction
12
15
import org.rust.lang.core.psi.ext.RsNameIdentifierOwner
13
16
import org.rust.lang.core.psi.ext.RsStructOrEnumItemElement
17
+ import org.rust.lang.doc.psi.RsDocComment
18
+ import org.rust.lang.doc.psi.ext.containingDoc
14
19
15
20
class RustLivingDocumentation : LivingDocumentation {
16
21
override val forbiddenRules: List <String >
@@ -22,13 +27,39 @@ class RustLivingDocumentation : LivingDocumentation {
22
27
23
28
override fun updateDoc (target : PsiElement , newDoc : String , type : LivingDocumentationType , editor : Editor ) {
24
29
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
28
34
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
+ })
32
63
}
33
64
34
65
override fun findNearestDocumentationTarget (psiElement : PsiElement ): PsiNameIdentifierOwner ? {
0 commit comments