Skip to content

Commit 4d65801

Browse files
committed
feat(doc): make it works
1 parent 4fddd6d commit 4d65801

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.phodal.autodev
44
pluginName = AutoDev
55
pluginRepositoryUrl = https://github.com/unit-mesh/auto-dev
66
# SemVer format -> https://semver.org
7-
pluginVersion = 1.1.1
7+
pluginVersion = 1.1.2
88

99
# Supported IDEs: idea, pycharm
1010
baseIDE=idea

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaLivingDocumentation.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cc.unitmesh.idea.provider
33
import cc.unitmesh.devti.provider.LivingDocumentation
44
import cc.unitmesh.devti.provider.LivingDocumentationType
55
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
6+
import com.intellij.openapi.application.ReadAction
7+
import com.intellij.openapi.command.WriteCommandAction
68
import com.intellij.openapi.editor.SelectionModel
79
import com.intellij.psi.*
810
import com.intellij.psi.util.PsiTreeUtil
@@ -19,24 +21,21 @@ class JavaLivingDocumentation : LivingDocumentation {
1921

2022
override fun updateDoc(psiElement: PsiElement, str: String) {
2123
val project = psiElement.project
22-
val psiElementFactory = JavaPsiFacade.getElementFactory(project)
23-
val newDocComment = psiElementFactory.createDocCommentFromText(str)
24-
25-
if (psiElement is PsiDocCommentOwner) {
26-
try {
27-
psiElement.docComment?.replace(newDocComment)
28-
} catch (e: IncorrectOperationException) {
29-
val firstChild = psiElement.firstChild
30-
psiElement.addBefore(newDocComment, firstChild)
24+
WriteCommandAction.runWriteCommandAction(project, "Living Document", "cc.unitmesh.livingDoc", {
25+
val psiElementFactory = JavaPsiFacade.getElementFactory(project)
26+
val newDocComment = psiElementFactory.createDocCommentFromText(str)
27+
28+
if (psiElement is PsiDocCommentOwner) {
29+
val oldDocComment = psiElement.docComment
30+
if (oldDocComment != null) {
31+
oldDocComment.replace(newDocComment)
32+
} else {
33+
psiElement.addBefore(newDocComment, psiElement.firstChild)
34+
}
35+
} else {
36+
throw IncorrectOperationException("Unable to update documentation")
3137
}
32-
} else {
33-
throw IncorrectOperationException("Unable to update documentation")
34-
}
35-
}
36-
37-
38-
override fun findExampleDoc(psiNameIdentifierOwner: PsiNameIdentifierOwner): String {
39-
return ""
38+
})
4039
}
4140

4241
override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.intellij.openapi.editor.Editor
1111
import com.intellij.openapi.progress.ProgressIndicator
1212
import com.intellij.openapi.progress.Task
1313
import com.intellij.openapi.project.Project
14-
import com.intellij.psi.PsiElement
1514
import com.intellij.psi.PsiNameIdentifierOwner
1615
import kotlinx.coroutines.flow.collect
1716
import kotlinx.coroutines.runBlocking
@@ -38,6 +37,7 @@ class LivingDocumentationTask(
3837
}
3938
}
4039

40+
println(result)
4141
documentation.updateDoc(target, result)
4242
}
4343
}
@@ -73,25 +73,23 @@ class LivingDocumentationBuilder(
7373
}
7474

7575
fun buildPrompt(project: Project?, target: PsiNameIdentifierOwner, fallbackText: String): String {
76-
val instruction = StringBuilder(fallbackText)
77-
val element = this.contextProviders.firstNotNullOfOrNull { contextProvider ->
78-
val context = ReadAction.compute<LLMQueryContext?, Throwable> {
79-
contextProvider.from(target)
80-
}
81-
val contextInstruction = contextInstruction(context)
82-
contextInstruction
83-
} ?: "write documentation for given code"
76+
return ReadAction.compute<String, Throwable> {
77+
val instruction = StringBuilder(fallbackText)
78+
val element = this.contextProviders.firstNotNullOfOrNull { contextProvider ->
79+
contextInstruction(contextProvider.from(target))
80+
} ?: "write documentation for given code"
8481

85-
instruction.append(element)
86-
instruction.append(" , do not return example code, do not use @author and @version tags")
87-
instruction.append(target.text)
82+
instruction.append(element)
83+
instruction.append(" , do not return example code, do not use @author and @version tags")
84+
instruction.append(target.text)
8885

89-
val elementText = ReadAction.compute<String, Throwable> {
90-
"""${target.language.displayName}\n${target}\n```"""
91-
}
92-
instruction.append(elementText)
86+
instruction.append("""${target.language.displayName}\n${target}\n```""")
9387

94-
return instruction.toString()
88+
val startEndString = documentation.startEndString(type)
89+
instruction.append("\nstart your documentation here with ${startEndString.first} and ends with: ${startEndString.second} :\n```")
90+
91+
instruction.toString()
92+
}
9593
}
9694

9795
}

src/main/kotlin/cc/unitmesh/devti/provider/LivingDocumentation.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@ enum class LivingDocumentationType {
1919
* 3. living documentation
2020
*/
2121
interface LivingDocumentation {
22-
// val instruction: String
22+
// val instruction: String
2323
fun startEndString(type: LivingDocumentationType): Pair<String, String>
2424

2525
fun updateDoc(psiElement: PsiElement, str: String)
2626

27-
fun findExampleDoc(psiNameIdentifierOwner: PsiNameIdentifierOwner): String
28-
2927
fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner?
3028

31-
/**
32-
* Find the documentation targets in the selection, like the method, class
33-
*/
3429
fun findDocTargetsInSelection(psiElement: PsiElement, selectionModel: SelectionModel): List<PsiNameIdentifierOwner>
3530

3631
companion object {

0 commit comments

Comments
 (0)