Skip to content

Commit 9ce7734

Browse files
committed
feat(rust): fix for insert issue
1 parent f452428 commit 9ce7734

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

rust/src/main/kotlin/cc/unitmesh/rust/provider/RustLivingDocumentation.kt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ package cc.unitmesh.rust.provider
33
import cc.unitmesh.devti.custom.document.LivingDocumentationType
44
import cc.unitmesh.devti.provider.LivingDocumentation
55
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
6+
import com.intellij.openapi.command.WriteCommandAction
67
import com.intellij.openapi.editor.Editor
78
import com.intellij.openapi.editor.SelectionModel
89
import com.intellij.psi.PsiElement
910
import com.intellij.psi.PsiNameIdentifierOwner
11+
import com.intellij.psi.codeStyle.CodeStyleManager
1012
import com.intellij.psi.util.PsiTreeUtil
13+
import com.intellij.util.IncorrectOperationException
1114
import org.rust.lang.core.psi.RsFunction
1215
import org.rust.lang.core.psi.ext.RsNameIdentifierOwner
1316
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
1419

1520
class 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? {

src/main/kotlin/cc/unitmesh/devti/intentions/action/task/LivingDocPromptBuilder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ open class LivingDocPromptBuilder(
6363

6464
private fun classInstruction(context: ClassContext): String? {
6565
if (context.name == null) return null
66-
return "Write documentation for given class " + context.name + ". You should not add documentation for methods."
66+
return "Write documentation for given ${context.root.language} language class " + context.name + "."
6767
}
6868

6969
private fun methodInstruction(context: MethodContext): String? {
7070
if (context.name == null) return null
71-
var instruction = "Write documentation for given method " + context.name + "."
71+
var instruction = "Write documentation for given ${context.root.language} language method " + context.name + "."
7272
if (context.paramNames.isNotEmpty()) {
7373
instruction = """
7474
$instruction
@@ -101,7 +101,7 @@ open class LivingDocPromptBuilder(
101101
}
102102

103103
contextInstruction(llmQueryContext)
104-
} ?: "Write documentation for given code."
104+
} ?: "Write documentation for given ${target.language} language code."
105105

106106
instruction.append(basicInstruction)
107107

0 commit comments

Comments
 (0)