How can I re-format the code after adding a new statement in quick fix?
Hello,
I'm making a plugin for Java. In my plugin, there is a quickfix that can add a statement before another statement. But after I call the "addBefore" API, the new statement will be added to the same line as the old statement. Can I programmatically reformat the code or manually add a newline between these two statements?
Thanks,
Tianshi
请先登录再写评论。
You can manually reformat using CodeStyleManager#reformat(PsiElement) or you can create PsiWhiteSpace element and use addBefore one more time.
Hi Yaroslav,
Could you help me see where was wrong with the following code snippet? My goal is to add a new declaration statement before current element I get from the problem descriptor in the quickfix. And I want to have a new line after the declaration statement and the old statement.
declarationStatement.getParent().addAfter(new PsiWhiteSpaceImpl("\n"), declarationStatement);element.getParent().addBefore(declarationStatement, element);
However, I got the following exception during runtime:com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl because: parent is null
invalidated at: no info
at com.intellij.psi.util.PsiUtilCore.ensureValid(PsiUtilCore.java:492)
at com.intellij.psi.impl.source.tree.ChangeUtil.generateTreeElement(ChangeUtil.java:149)
at com.intellij.psi.impl.source.tree.ChangeUtil.copyToElement(ChangeUtil.java:137)
at com.intellij.psi.impl.source.PsiFileImpl.addAfter(PsiFileImpl.java:874)
at org.intellij.privacyhelper.codeInspection.RuntimePermissionRequestInspection$getUpdatedPurposeArray.applyFix(RuntimePermissionRequestInspection.java:237)
at org.intellij.privacyhelper.codeInspection.RuntimePermissionRequestInspection$getUpdatedPurposeArray.applyFix(RuntimePermissionRequestInspection.java:205)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:86)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$2.run(ShowIntentionActionsHandler.java:189)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1009)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$3.run(ShowIntentionActionsHandler.java:205)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:124)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:99)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:85)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:210)
at com.intellij.codeInsight.intention.impl.IntentionListStep$2$1.run(IntentionListStep.java:275)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:345)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:329)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:857)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:658)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:386)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Thanks a lot!
Tianshi