How does Dart rename PsiElement?

Answered

Hello, I want to implement an automatic repair function

 

class DartPublicFunctionFix : LocalInspectionTool() {

override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return DartPublicFunctionApiFixVisitor(holder = holder)
}


}

class DartPublicFunctionApiFixVisitor(val holder: ProblemsHolder) : PsiElementVisitor() {

override fun visitElement(element: PsiElement) {
//如果节点是一个类
if(element is DartClassDefinitionImpl ) {

//判断是不是继承自StatefulWidget类
val superClassList = element.childrenOfType<DartSuperclassImpl>()
if(superClassList.isNotEmpty()){
val superClassName = superClassList.first().childrenOfType<DartTypeImpl>().first().text
if(checkSuperClassNames.contains(superClassName)){
///包含需要待检测的类
val methods = element.childrenOfType<DartClassBodyImpl>().first().childrenOfType<DartClassMembersImpl>()
.first().childrenOfType<DartMethodDeclarationImpl>()
val names = methods.filter { it.childrenOfType<DartComponentNameImpl>().first().text == "createState" }
if(names.isNotEmpty()){
val dmdElement = names.first() //DartMethodDeclarationImpl节点
val name = dmdElement.childrenOfType<DartComponentNameImpl>().first().text
if(name.equals("createState")){
val returnTypes = dmdElement.childrenOfType<DartReturnTypeImpl>()
if(returnTypes.isNotEmpty()){
val returnTypeEle = returnTypes.first()
if(returnTypeEle.text.indexOf("_") == 0){
holder.registerProblem(returnTypes.first(),"dart建议将返回类型修改为公有变量. QQ自学群:667186542欢迎加入",
ProblemHighlightType.WARNING,PublicApiRenameFix(returnTypeEle,returnTypeEle.text))
}

}
}
}
}
}
}
super.visitElement(element)
}
}

 

class PublicApiRenameFix(val element: PsiElement,var className: String): LocalQuickFixOnPsiElement(element) {
private val renameText = className.removePrefix("_")

override fun getFamilyName(): String {
return "梁典典: 将它重命名为$renameText"
}
override fun getText(): String {
return familyName
}

override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {

val factory = PsiElementFactory.getInstance(project)
val newNamePsi = factory.createDummyHolder(renameText, IElementType(
"RETURN_TYPE",DartLanguage.INSTANCE
),null)


startElement.replace(newNamePsi)
}

}

 

But an error occurred when testing,How can I solve this problem?

 

java.lang.AssertionError: Unknown element type: RETURN_TYPE
    at com.jetbrains.lang.dart.DartTokenTypes$Factory.createElement(DartTokenTypes.java:720)
    at com.jetbrains.lang.dart.DartParserDefinition.createElement(DartParserDefinition.java:71)
    at com.intellij.psi.impl.source.tree.CompositeElement.createPsiNoLock(CompositeElement.java:705)
    at com.intellij.psi.impl.source.tree.CompositeElement.getPsi(CompositeElement.java:692)
    at com.intellij.psi.impl.PsiElementFactoryImpl.createDummyHolder(PsiElementFactoryImpl.java:333)
    at shop.itbug.fluttercheckversionx.tools.PublicApiRenameFix.invoke(DartPublicFunctionFix.kt:99)
    at com.intellij.codeInspection.LocalQuickFixOnPsiElement.applyFix(LocalQuickFixOnPsiElement.java:100)
    at com.intellij.codeInspection.LocalQuickFixOnPsiElement.applyFix(LocalQuickFixOnPsiElement.java:90)
    at com.intellij.codeInspection.LocalQuickFixOnPsiElement.applyFix(LocalQuickFixOnPsiElement.java:22)
    at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:75)
    at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$invokeIntention$5(ShowIntentionActionsHandler.java:257)
    at com.intellij.openapi.application.WriteAction.lambda$run$1(WriteAction.java:86)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithClass(ApplicationImpl.java:1003)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1029)
    at com.intellij.openapi.application.WriteAction.run(WriteAction.java:85)
    at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.invokeIntention(ShowIntentionActionsHandler.java:257)
    at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.lambda$chooseActionAndInvoke$4(ShowIntentionActionsHandler.java:233)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:219)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:174)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:164)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:150)
    at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:232)
    at com.intellij.codeInsight.daemon.impl.DaemonTooltipAction.execute(DaemonTooltipActionProvider.kt:54)
    at com.intellij.codeInsight.daemon.impl.DaemonTooltipWithActionRenderer$addActionsRow$runFixAction$1.invoke(DaemonTooltipWithActionRenderer.kt:146)
    at com.intellij.codeInsight.daemon.impl.DaemonTooltipWithActionRenderer$addActionsRow$runFixAction$1.invoke(DaemonTooltipWithActionRenderer.kt:48)
    at com.intellij.codeInsight.daemon.impl.DaemonTooltipWithActionRendererKt$createActionLabel$1.hyperlinkActivated(DaemonTooltipWithActionRenderer.kt:370)
    at com.intellij.ui.HyperlinkAdapter.hyperlinkUpdate(HyperlinkAdapter.java:11)
    at com.intellij.ui.HyperlinkLabel.fireHyperlinkEvent(HyperlinkLabel.java:240)
    at com.intellij.ui.HyperlinkLabel.processMouseEvent(HyperlinkLabel.java:162)
    at java.desktop/java.awt.Component.processEvent(Component.java:6419)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5029)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4861)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2793)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4861)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:898)
    at com.intellij.ide.IdeEventQueue.dispatchMouseEvent(IdeEventQueue.java:820)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:743)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$6(IdeEventQueue.java:439)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:803)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$7(IdeEventQueue.java:438)
    at com.intellij.openapi.application.TransactionGuardImpl.performActivity(TransactionGuardImpl.java:119)
    at com.intellij.ide.IdeEventQueue.performActivity(IdeEventQueue.java:604)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:436)
    at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:873)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:484)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)
0
2 comments

Hi,

Your element type is not recognized by the Dart parser. Why do you create a new element type:

IElementType("RETURN_TYPE", DartLanguage.INSTANCE)


instead of using the existing DartTokenTypes.RETURN_TYPE?

0

Thanks for your reply, the issue is now resolved ☺️

0

Please sign in to leave a comment.