Skip to content

Commit c99b21d

Browse files
committed
feat(sql): add functionality to update living documentation
This commit adds the functionality to update living documentation in the `SqlLivingDocumentationProvider` class. It includes the ability to insert the new documentation at the correct position, reformat the code, and run the update as a write command action. Additionally, the commit includes changes to the `CustomLivingDocTask`, `CustomDocumentationIntention`, and `BasedDocumentationIntention` classes to support the new functionality.
1 parent a80bd50 commit c99b21d

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/provider/SqlLivingDocumentationProvider.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,38 @@ package cc.unitmesh.database.provider
22

33
import cc.unitmesh.devti.custom.document.LivingDocumentationType
44
import cc.unitmesh.devti.provider.LivingDocumentation
5+
import com.intellij.openapi.command.WriteCommandAction
56
import com.intellij.openapi.editor.Editor
67
import com.intellij.openapi.editor.SelectionModel
78
import com.intellij.psi.PsiElement
89
import com.intellij.psi.PsiNameIdentifierOwner
10+
import com.intellij.psi.codeStyle.CodeStyleManager
911

1012
class SqlLivingDocumentationProvider : LivingDocumentation {
1113
override val forbiddenRules: List<String>
12-
get() = TODO("Not yet implemented")
14+
get() = listOf()
1315

1416
override fun startEndString(type: LivingDocumentationType): Pair<String, String> {
1517
return Pair("--", "--")
1618
}
1719

1820
override fun updateDoc(target: PsiElement, newDoc: String, type: LivingDocumentationType, editor: Editor) {
19-
TODO("Not yet implemented")
21+
val project = target.project
22+
val codeStyleManager = CodeStyleManager.getInstance(project)
23+
val file = target.containingFile
24+
25+
val doc = newDoc + "\n"
26+
27+
WriteCommandAction.runWriteCommandAction(project, "Living Document", "cc.unitmesh.livingDoc", {
28+
val startOffset = target.textRange.startOffset
29+
val newEndOffset = startOffset + doc.length
30+
31+
editor.document.insertString(startOffset, doc)
32+
codeStyleManager.reformatText(file, startOffset, newEndOffset)
33+
})
2034
}
2135

2236
override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {
23-
// return when (psiElement) {
24-
// is SqlDdlStatement -> psiElement
25-
// is SqlDeclareStatement -> psiElement
26-
// else -> null
27-
// }
2837
return null
2938
}
3039

src/main/kotlin/cc/unitmesh/devti/custom/CustomDocumentationIntention.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.intellij.openapi.editor.Editor
88
import com.intellij.openapi.progress.ProgressManager
99
import com.intellij.openapi.progress.Task
1010
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
11-
import com.intellij.psi.PsiNameIdentifierOwner
11+
import com.intellij.psi.PsiElement
1212

1313
class CustomDocumentationIntention(override val config: CustomDocumentationConfig) : BasedDocumentationIntention() {
1414

@@ -17,14 +17,13 @@ class CustomDocumentationIntention(override val config: CustomDocumentationConfi
1717
override fun getFamilyName(): String = "AutoDev: Custom Documentation Intention"
1818
override fun priority(): Int = 99
1919

20-
override fun writingDocument(editor: Editor, element: PsiNameIdentifierOwner, documentation: LivingDocumentation) {
20+
override fun writingDocument(editor: Editor, element: PsiElement, documentation: LivingDocumentation) {
2121
val task: Task.Backgroundable = CustomLivingDocTask(editor, element, config)
2222
ProgressManager.getInstance()
2323
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
2424
}
2525

2626
companion object {
27-
fun create(config: CustomDocumentationConfig): CustomDocumentationIntention =
28-
CustomDocumentationIntention(config)
27+
fun create(config: CustomDocumentationConfig) = CustomDocumentationIntention(config)
2928
}
3029
}

src/main/kotlin/cc/unitmesh/devti/custom/document/CustomLivingDocTask.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import com.intellij.openapi.diagnostic.logger
66
import com.intellij.openapi.editor.Editor
77
import com.intellij.openapi.progress.ProgressIndicator
88
import com.intellij.openapi.progress.Task
9-
import com.intellij.psi.PsiNameIdentifierOwner
10-
import kotlinx.coroutines.flow.*
9+
import com.intellij.psi.PsiElement
10+
import kotlinx.coroutines.flow.cancellable
1111
import kotlinx.coroutines.runBlocking
1212

1313
class CustomLivingDocTask(
1414
val editor: Editor,
15-
val target: PsiNameIdentifierOwner,
15+
val target: PsiElement,
1616
val config: CustomDocumentationConfig,
1717
) :
1818
Task.Backgroundable(editor.project, config.title) {

src/main/kotlin/cc/unitmesh/devti/intentions/action/base/BasedDocumentationIntention.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import cc.unitmesh.devti.custom.document.CustomDocumentationConfig
44
import cc.unitmesh.devti.custom.document.LivingDocumentationType
55
import cc.unitmesh.devti.intentions.action.task.LivingDocumentationTask
66
import cc.unitmesh.devti.provider.LivingDocumentation
7+
import com.intellij.openapi.diagnostic.logger
78
import com.intellij.openapi.editor.Editor
89
import com.intellij.openapi.progress.ProgressManager
910
import com.intellij.openapi.progress.Task
1011
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
1112
import com.intellij.openapi.project.Project
13+
import com.intellij.psi.PsiElement
1214
import com.intellij.psi.PsiFile
1315
import com.intellij.psi.PsiManager
1416
import com.intellij.psi.PsiNameIdentifierOwner
@@ -21,6 +23,8 @@ abstract class BasedDocumentationIntention : AbstractChatIntention() {
2123

2224
override fun startInWriteAction(): Boolean = false
2325

26+
val logger = logger<BasedDocumentationIntention>()
27+
2428
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
2529
if (editor == null || file == null) return
2630
if (!isAvailable(project, editor, file)) return
@@ -35,22 +39,28 @@ abstract class BasedDocumentationIntention : AbstractChatIntention() {
3539
val findFile: PsiFile = PsiManager.getInstance(project).findFile(rootFile.virtualFile) ?: return
3640

3741
// find all targets in selection
38-
documentation.findDocTargetsInSelection(findFile, selectionModel).map {
39-
writingDocument(editor, it, documentation)
42+
val targetsInSelection = documentation.findDocTargetsInSelection(findFile, selectionModel)
43+
if (targetsInSelection.isNotEmpty()) {
44+
targetsInSelection.map {
45+
writingDocument(editor, it, documentation)
46+
}
47+
} else {
48+
writingDocument(editor, findFile, documentation)
4049
}
50+
4151
return
42-
} else {
43-
val element = PsiUtilBase.getElementAtCaret(editor) ?: return
44-
val nearestDocumentationTarget = documentation.findNearestDocumentationTarget(element) ?: return
52+
}
53+
val element = PsiUtilBase.getElementAtCaret(editor) ?: return
54+
val nearestDocumentationTarget = documentation.findNearestDocumentationTarget(element)
55+
if (nearestDocumentationTarget != null) {
4556
writingDocument(editor, nearestDocumentationTarget, documentation)
46-
4757
return
4858
}
4959

50-
60+
logger.warn("No selected text and no nearest documentation target found")
5161
}
5262

53-
open fun writingDocument(editor: Editor, element: PsiNameIdentifierOwner, documentation: LivingDocumentation) {
63+
open fun writingDocument(editor: Editor, element: PsiElement, documentation: LivingDocumentation) {
5464
val task = LivingDocumentationTask(editor, element, LivingDocumentationType.COMMENT, documentation)
5565
ProgressManager.getInstance()
5666
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import com.intellij.openapi.editor.Editor
1111
import com.intellij.openapi.progress.ProgressIndicator
1212
import com.intellij.openapi.progress.Task
1313
import com.intellij.psi.PsiElement
14-
import com.intellij.psi.PsiNameIdentifierOwner
1514
import kotlinx.coroutines.runBlocking
15+
import kotlinx.coroutines.flow.*
1616

1717
/**
1818
* The `LivingDocumentationTask` class represents a background task for generating living documentation.

0 commit comments

Comments
 (0)