Skip to content

Commit

Permalink
feat(ts): init basic doc writing listener
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 27, 2023
1 parent cc7d14a commit 3786575
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
@@ -0,0 +1,59 @@
package cc.unitmesh.ide.webstorm.provider

import cc.unitmesh.devti.custom.document.LivingDocumentationType
import cc.unitmesh.devti.provider.LivingDocumentation
import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil
import com.intellij.lang.javascript.psi.JSFunction
import com.intellij.lang.javascript.psi.ecmal4.JSClass
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.SelectionModel
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiTreeUtil

class JavaScriptLivingDocumentation : LivingDocumentation {
override val forbiddenRules: List<String> = listOf(
"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) {
TODO("Not yet implemented")
}

override fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner? {
if (psiElement is JSFunction || psiElement is JSClass) return psiElement as PsiNameIdentifierOwner

val closestIdentifierOwner = PsiTreeUtil.getParentOfType(psiElement, PsiNameIdentifierOwner::class.java)
if (closestIdentifierOwner !is JSFunction) {
return PsiTreeUtil.getParentOfType(psiElement, JSFunction::class.java) ?: closestIdentifierOwner
}

return closestIdentifierOwner
}

override fun findDocTargetsInSelection(
root: PsiElement,
selectionModel: SelectionModel
): List<PsiNameIdentifierOwner> {
val findCommonParent = CollectHighlightsUtil.findCommonParent(
root,
selectionModel.selectionStart,
selectionModel.selectionEnd
) ?: return emptyList()

return emptyList()
}

private fun containsElement(selectionModel: SelectionModel, element: PsiElement): Boolean {
return selectionModel.selectionStart <= element.textRange.startOffset && element.textRange.endOffset <= selectionModel.selectionEnd
}
}
3 changes: 3 additions & 0 deletions webstorm/src/main/resources/cc.unitmesh.webstorm.xml
Expand Up @@ -34,6 +34,9 @@
<testContextProvider language="JavaScript" implementation="cc.unitmesh.ide.webstorm.provider.testing.JavaScriptWriteTestService"/>
<testContextProvider language="Typescript" implementation="cc.unitmesh.ide.webstorm.provider.testing.JavaScriptWriteTestService"/>

<livingDocumentation language="JavaScript"
implementationClass="cc.unitmesh.ide.webstorm.provider.JavaScriptLivingDocumentation"/>

<buildSystemProvider implementation="cc.unitmesh.ide.webstorm.provider.JavaScriptBuildSystemProvider" />
</extensions>
</idea-plugin>

0 comments on commit 3786575

Please sign in to comment.