How to create Dart PsiCommentImpl through text?
已回答
I want to complete the function of automatically generating a Dart document comment, but I don't know how to generate a PsiCommentImpl from a text. Is there an API that can create a PsiElement?
/// TODO Automatically generate document comments
private fun createDartDocPsiElement(project: Project, element: PsiElement, text: String) {
}
This is my code
class GenerateFunctionDocument : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val data = e.getData(CommonDataKeys.PSI_ELEMENT)
if (data is DartComponentNameImpl) {
val nextSibling = data.nextSibling
if (nextSibling is DartFormalParameterListImpl) {
val psis = PsiTreeUtil.collectElements(
nextSibling
) { element -> element is DartNormalFormalParameterImpl }
val sb = StringBuilder()
sb.append("///\n")
psis.forEach {
val name = it.firstChild.lastChild.text
sb.append("/// [$name] - \n")
}.takeIf { psis.isNotEmpty() }
e.project?.let {
createDartDocPsiElement(it, data, sb.toString())
}
}
}
}
/// TODO Automatically generate document comments
private fun createDartDocPsiElement(project: Project, element: PsiElement, text: String) {
}
}
请先登录再写评论。
Hi,
It is usually done by creating a dummy file from text and retrieving the element you need and inserting it into the existing PSI tree. Please check the docs:
https://plugins.jetbrains.com/docs/intellij/modifying-psi.html
In your case, you should create a minimal dummy file with the comment you want to create, find the comment element in the created file and insert it in the file where you want to generate it.