Skip to content

Commit 90f446f

Browse files
committed
fix(go): improve documentation generation process
Refactored code to include parameter and return tag instructions in method documentation. Added runReadAction for project access in GoLivingDocumentationProvider.
1 parent 603a4c2 commit 90f446f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

goland/src/main/kotlin/cc/unitmesh/go/provider/GoLivingDocumentationProvider.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.goide.psi.*
66
import com.intellij.openapi.editor.Editor
77
import com.intellij.openapi.editor.SelectionModel
88
import com.goide.psi.impl.GoPsiUtil
9+
import com.intellij.openapi.application.runReadAction
910
import com.intellij.openapi.command.WriteCommandAction
1011
import com.intellij.psi.*
1112
import com.intellij.psi.codeStyle.CodeStyleManager
@@ -21,24 +22,26 @@ class GoLivingDocumentationProvider : LivingDocumentation {
2122
override fun startEndString(type: LivingDocumentationType): Pair<String, String> = "/*" to "*/"
2223

2324
override fun updateDoc(target: PsiElement, newDoc: String, type: LivingDocumentationType, editor: Editor) {
24-
val project = target.project
25+
val project = runReadAction { target.project }
2526
val codeStyleManager = CodeStyleManager.getInstance(project)
2627
WriteCommandAction.runWriteCommandAction(project, "Living Document", "cc.unitmesh.livingDoc", {
28+
val doc = newDoc + "\n"
2729
val startOffset = target.textRange.startOffset
28-
val newEndOffset = startOffset + newDoc.length
30+
val newEndOffset = startOffset + doc.length
2931

3032
when (type) {
3133
LivingDocumentationType.COMMENT -> {
32-
34+
editor.document.insertString(startOffset, doc)
35+
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
3336
}
3437

3538
LivingDocumentationType.ANNOTATED -> {
36-
editor.document.insertString(startOffset, newDoc)
39+
editor.document.insertString(startOffset, doc)
3740
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
3841
}
3942

4043
LivingDocumentationType.CUSTOM -> {
41-
editor.document.insertString(startOffset, newDoc)
44+
editor.document.insertString(startOffset, doc)
4245
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
4346
}
4447
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ open class LivingDocPromptBuilder(
7070
private fun methodInstruction(context: MethodContext): String? {
7171
if (context.name == null) return null
7272

73-
var instruction = "Write documentation for given ${context.root.language.displayName} language method " + context.name + "."
74-
if (context.paramNames.isNotEmpty()) {
73+
var instruction =
74+
"Write documentation for given ${context.root.language.displayName} language method " + context.name + "."
75+
if (context.paramNames.isNotEmpty() && documentation.parameterTagInstruction != null) {
7576
instruction = """
7677
$instruction
7778
${documentation.parameterTagInstruction ?: ""}
7879
""".trimIndent()
7980
}
8081

8182
val returnType = context.returnType
82-
if (!returnType.isNullOrEmpty()) {
83+
if (!returnType.isNullOrEmpty() && documentation.returnTagInstruction != null) {
8384
instruction = """
8485
$instruction
8586
${documentation.returnTagInstruction ?: ""}

0 commit comments

Comments
 (0)