Skip to content

Commit

Permalink
feat(doc): init basic intention
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 8, 2023
1 parent bd174a5 commit 3c050e0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
@@ -0,0 +1,66 @@
package cc.unitmesh.devti.intentions.action

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.provider.LivingDocumentation
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.SelectionModel
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiUtilBase

class GenerateLivingDocIntention : IntentionAction {
override fun startInWriteAction(): Boolean = false

override fun getText(): String = AutoDevBundle.message("intentions.living.documentation.name")

override fun getFamilyName(): String = AutoDevBundle.message("intentions.living.documentation.family.name")

override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
if (editor == null || file == null) return false

return LivingDocumentation.forLanguage(file.language) != null
}

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null) return
if (!isAvailable(project, editor, file)) return

val selectionModel = editor.selectionModel
val selectedText = selectionModel.selectedText
if (selectedText != null) {
val owners: List<PsiNameIdentifierOwner> = findSelectedElementToDocument(editor, project, selectionModel)
for (identifierOwner in owners) {
val task: Task.Backgroundable =
object : Task.Backgroundable(project, "Generating Living Documentation") {
override fun run(indicator: ProgressIndicator) {


}
}
ProgressManager.getInstance()
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
}
}

}

private fun findSelectedElementToDocument(
editor: Editor,
project: Project,
selectionModel: SelectionModel,
): List<PsiNameIdentifierOwner> {
val rootFile = PsiUtilBase.getPsiFileInEditor(editor, project) ?: return emptyList()
val findFile: PsiFile = PsiManager.getInstance(project).findFile(rootFile.virtualFile) ?: return emptyList()
val support = LivingDocumentation.forLanguage(findFile.language) ?: return emptyList()

return support.findDocTargetsInSelection(findFile, selectionModel)
}

}
@@ -1,5 +1,7 @@
package cc.unitmesh.devti.provider

import com.intellij.lang.Language
import com.intellij.lang.LanguageExtension
import com.intellij.openapi.editor.SelectionModel
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
Expand All @@ -16,12 +18,24 @@ enum class LivingDocumentationProviderType {
* 2. annotated like Swagger
* 3. living documentation
*/
interface LivingDocumentationProvider {
interface LivingDocumentation {
fun updateDoc(psiElement: PsiElement, str: String)

fun findExampleDoc(psiNameIdentifierOwner: PsiNameIdentifierOwner): String

fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner?

fun findDocTargetsInSelection(psiElement: PsiElement, selectionModel: SelectionModel): List<PsiNameIdentifierOwner?>
/**
* Find the documentation targets in the selection, like the method, class
*/
fun findDocTargetsInSelection(psiElement: PsiElement, selectionModel: SelectionModel): List<PsiNameIdentifierOwner>

companion object {
private val languageExtension: LanguageExtension<LivingDocumentation> =
LanguageExtension("cc.unitmesh.livingDocumentation")

fun forLanguage(language: Language): LivingDocumentation? {
return languageExtension.forLanguage(language)
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/autodev-core.xml
Expand Up @@ -90,9 +90,9 @@
implements="cc.unitmesh.devti.context.builder.VariableContextBuilder"/>
</extensionPoint>

<extensionPoint name="livingDocumentation"
<extensionPoint qualifiedName="cc.unitmesh.livingDocumentation"
beanClass="com.intellij.lang.LanguageExtensionPoint" dynamic="true">
<with attribute="implementationClass" implements="cc.unitmesh.devti.provider.LivingDocumentationProvider"/>
<with attribute="implementationClass" implements="cc.unitmesh.devti.provider.LivingDocumentation"/>
</extensionPoint>

<extensionPoint qualifiedName="cc.unitmesh.devFlowProvider"
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/messages/AutoDevBundle.properties
Expand Up @@ -41,4 +41,6 @@ label.submit.issue=Want new feature?
intentions.explain.business.new.name=Explain business
intentions.explain.business.family.name=Explain business with this
intentions.companion.api.name=Talk with API
intentions.companion.api.family.name=AutoDev: Talk with API
intentions.companion.api.family.name=AutoDev: Talk with API
intentions.living.documentation.name=Generate Living Doc
intentions.living.documentation.family.name=Generate Living Doc

0 comments on commit 3c050e0

Please sign in to comment.