Skip to content

Commit

Permalink
feat(living-doc): add HarmonyOS living documentation support
Browse files Browse the repository at this point in the history
This commit adds support for HarmonyOS living documentation in the project. The `LivingDocumentation` interface is implemented in the `HarmonyOsLivingDocumentation` class, which provides functionality for updating documentation, finding nearest documentation targets, and finding documentation targets in the selected code. This feature allows users to document their code using HarmonyOS-specific rules and conventions.
  • Loading branch information
phodal committed Feb 23, 2024
1 parent 8a0ad02 commit d4612d6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
Expand Up @@ -165,8 +165,7 @@ class JavaScriptLivingDocumentation : LivingDocumentation {
)

val list = decls.filter {
containsElement(selectionModel, it as PsiElement)
&& isMeaningfulToDocumentInSelection(it as PsiElement)
containsElement(selectionModel, it as PsiElement) && isMeaningfulToDocumentInSelection(it as PsiElement)
}.toList()

return list.ifEmpty {
Expand Down
@@ -0,0 +1,65 @@
package cc.unitmesh.devti.provider

import cc.unitmesh.devti.custom.document.LivingDocumentationType
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.SelectionModel
import com.intellij.psi.NavigatablePsiElement
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.util.parentOfTypes

class HarmonyOsLivingDocumentation : LivingDocumentation {
override val forbiddenRules: List<String> = listOf(
"ArkTS is an extension of TypeScript, you can use TypeScript's rules",
"do not return example code",
"do not use @author and @version tags"
)

override fun startEndString(type: LivingDocumentationType): Pair<String, String> {
return when (type) {
LivingDocumentationType.COMMENT -> Pair("/**", "*/")
LivingDocumentationType.ANNOTATED -> Pair("", "")
LivingDocumentationType.CUSTOM -> Pair("", "")
}
}

override fun updateDoc(target: PsiElement, newDoc: String, type: LivingDocumentationType, editor: Editor) {
val project = target.project
val codeStyleManager = CodeStyleManager.getInstance(project)
WriteCommandAction.runWriteCommandAction(project, "Living Document", "cc.unitmesh.livingDoc", {
val startOffset = target.textRange.startOffset
val newEndOffset = startOffset + newDoc.length

editor.document.insertString(startOffset, newDoc)
codeStyleManager.reformatText(target.containingFile, startOffset, newEndOffset)
});
}

override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {
if (psiElement is PsiNameIdentifierOwner) {
return psiElement
}

var candidate: PsiElement? =
psiElement.parentOfTypes(PsiNameIdentifierOwner::class, NavigatablePsiElement::class)

while (candidate != null) {
if (candidate is PsiNameIdentifierOwner) {
return candidate
}

candidate = candidate.parentOfTypes(PsiNameIdentifierOwner::class, NavigatablePsiElement::class)
}

return null
}

override fun findDocTargetsInSelection(
psiElement: PsiElement,
selectionModel: SelectionModel
): List<PsiNameIdentifierOwner> {
return listOf()
}
}
15 changes: 13 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/provider/LivingDocumentation.kt
Expand Up @@ -3,6 +3,7 @@ package cc.unitmesh.devti.provider
import cc.unitmesh.devti.custom.document.LivingDocumentationType
import com.intellij.lang.Language
import com.intellij.lang.LanguageExtension
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.SelectionModel
import com.intellij.psi.PsiElement
Expand Down Expand Up @@ -51,8 +52,18 @@ interface LivingDocumentation {
private val languageExtension: LanguageExtension<LivingDocumentation> =
LanguageExtension("cc.unitmesh.livingDocumentation")

val logger = logger<LivingDocumentation>()
fun forLanguage(language: Language): LivingDocumentation? {
return languageExtension.forLanguage(language)
val documentation = languageExtension.forLanguage(language)
if (documentation != null) {
return documentation
}

if (language.displayName == "TypeScript" || language.displayName == "ArkTS") {
return HarmonyOsLivingDocumentation()
}

return null
}
}
}
}

0 comments on commit d4612d6

Please sign in to comment.