Repository: intellij-rust/intellij-rust
Issue: DisposalException when closing macro expansion popup

## Environment

* **IntelliJ Rust plugin version:** 0.4.169.SNAPSHOT-221-dev (9bff907f34c5af444fc70506b58996d9c64a365b)
* **Rust toolchain version:** 1.62.0-nightly (f4a7ce997 2022-04-08) x86_64-unknown-linux-gnu
* **IDE name and version:** IntelliJ IDEA 2022.1 RC Community Edition (IC-221.5080.192)
* **Operating system:** Linux 5.13.0-39-generic
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description
When closing a macro expansion popup (showed via `Rust->Show expanded macro`) on platform 221, a DisposalException happens.

## Steps to reproduce
Make sure you use platform 221
Open a project which uses a macro
Right click the macro usage and select `Rust->Show expanded macro`
-> The popup for the expansion opens
Click outside of the popup to close the popup again
-> an Exception happens

This problem seems to have been introduced with the platform update in #8545 in combination with #8684 (which switched to the 221 platform)
Since this change `MacroExpansionViewComponent.removeNotify()` is called twice instead of just once.
This now calls `EditorFactory.getInstance().releaseEditor(editor)` twice which is bad.

This is likely a Platform/JBRE issue?


Stack trace:
```
com.intellij.openapi.util.TraceableDisposable$DisposalException: Double release of editor:
	at com.intellij.openapi.util.TraceableDisposable.throwDisposalError(TraceableDisposable.java:44)
	at com.intellij.openapi.editor.impl.EditorImpl.throwDisposalError(EditorImpl.java:1006)
	at com.intellij.openapi.editor.impl.EditorImpl.release(EditorImpl.java:1013)
	at com.intellij.openapi.editor.impl.EditorFactoryImpl.lambda$releaseEditor$4(EditorFactoryImpl.java:230)
	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.openapi.editor.impl.EditorFactoryImpl.releaseEditor(EditorFactoryImpl.java:222)
	at org.rust.ide.actions.macroExpansion.MacroExpansionViewComponent.removeNotify(MacroExpansionViewUtils.kt:186)
	at java.desktop/java.awt.Container.removeNotify(Container.java:2831)
	at java.desktop/javax.swing.JComponent.removeNotify(JComponent.java:4809)
	at java.desktop/java.awt.Container.removeNotify(Container.java:2831)
	at java.desktop/javax.swing.JComponent.removeNotify(JComponent.java:4809)
	at java.desktop/java.awt.Container.removeNotify(Container.java:2831)
	at java.desktop/javax.swing.JComponent.removeNotify(JComponent.java:4809)
	at java.desktop/java.awt.Container.removeNotify(Container.java:2831)
	at java.desktop/javax.swing.JComponent.removeNotify(JComponent.java:4809)
	at java.desktop/javax.swing.JRootPane.removeNotify(JRootPane.java:741)
	at java.desktop/java.awt.Container.removeNotify(Container.java:2831)
	at java.desktop/java.awt.Window.removeNotify(Window.java:802)
	at java.desktop/java.awt.Window$1DisposeAction.run(Window.java:1221)
	at java.desktop/java.awt.Window.doDispose(Window.java:1237)
	at java.desktop/java.awt.Window.dispose(Window.java:1178)
	at com.intellij.ui.popup.HeavyWeightPopup.hide(HeavyWeightPopup.java:26)
	at com.intellij.ui.popup.PopupComponent$AwtPopupWrapper.hide(PopupComponent.java:179)
	at com.intellij.ui.popup.AbstractPopup.disposePopup(AbstractPopup.java:808)
	at com.intellij.ui.popup.AbstractPopup.cancel(AbstractPopup.java:793)
	at com.intellij.ui.popup.StackingPopupDispatcherImpl.dispatchMouseEvent(StackingPopupDispatcherImpl.java:128)
```

Changed files:
src/main/kotlin/org/rust/ide/actions/macroExpansion/MacroExpansionViewUtils.kt

diff --git a/src/main/kotlin/org/rust/ide/actions/macroExpansion/MacroExpansionViewUtils.kt b/src/main/kotlin/org/rust/ide/actions/macroExpansion/MacroExpansionViewUtils.kt
index 472405654..77783660c 100644
--- a/src/main/kotlin/org/rust/ide/actions/macroExpansion/MacroExpansionViewUtils.kt
+++ b/src/main/kotlin/org/rust/ide/actions/macroExpansion/MacroExpansionViewUtils.kt
@@ -17,6 +17,7 @@ import com.intellij.openapi.util.NlsContexts.PopupTitle
 import com.intellij.psi.PsiElement
 import com.intellij.psi.PsiFile
 import com.intellij.psi.codeStyle.CodeStyleManager
+import com.intellij.ui.ScreenUtil
 import com.intellij.ui.popup.PopupPositionManager
 import com.intellij.util.DocumentUtil
 import org.rust.lang.RsFileType
@@ -153,6 +154,7 @@ private fun reformatMacroExpansion(
 private class MacroExpansionViewComponent(expansion: MacroExpansion) : JPanel(BorderLayout()) {
 
     private val editor: EditorEx
+    private var isEditorReleased: Boolean = false
 
     init {
         require(expansion.elements.isNotEmpty()) { "Must be at least one expansion!" }
@@ -182,8 +184,10 @@ private class MacroExpansionViewComponent(expansion: MacroExpansion) : JPanel(Bo
      */
     override fun removeNotify() {
         super.removeNotify()
-
-        EditorFactory.getInstance().releaseEditor(editor)
+        if (ScreenUtil.isStandardAddRemoveNotify(this) && !isEditorReleased) {
+            isEditorReleased = true // remove notify can be called several times for popup windows
+            EditorFactory.getInstance().releaseEditor(editor)
+        }
     }
 }