cannot share data context between Swing events

Hi, 

I have a plugin that tracks when a user does copy or paste, and then logs that into a CSV file. But the action listener that I'm using threw an error, that I'm not understanding. (I've seen other posts that had this issue, but they have some sort of dialog or thing going on, yet to the best of my knowledge I'm not performing UI activity while the listener is listening for copy pastes).

 Source code: 
java.lang.Throwable: cannot share data context between Swing events; initial event count = 4899; current event count = 4903
	at com.intellij.openapi.diagnostic.Logger.error(Logger.java:123)
	at com.intellij.ide.impl.DataManagerImpl$MyDataContext.getData(DataManagerImpl.java:351)
	at com.intellij.openapi.actionSystem.DataContext.getData(DataContext.java:59)
	at com.intellij.openapi.actionSystem.AnActionEvent.getData(AnActionEvent.java:178)
	at CopyPasteListener.afterActionPerformed(CopyPasteListener.java:43)
	at com.intellij.openapi.actionSystem.impl.ActionManagerImpl.fireAfterActionPerformed(ActionManagerImpl.java:1210)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:670)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processActionOrWaitSecondStroke(IdeKeyEventDispatcher.java:520)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(IdeKeyEventDispatcher.java:475)
	at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:212)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:697)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
	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)``
 
 
 
 

 

 

0
4 comments
Official comment

Please see comment in com.intellij.openapi.actionSystem.ex.AnActionListener#afterActionPerformed:

  Note that using {@code dataContext} in implementing methods is unsafe - it could have been invalidated by the performed action.

 

https://github.com/nn3un/Show-Your-Work/blob/master/src/IdeInitializer.java#L207 is problematic since it keeps adding listeners to the application without ever removing them AFAIU.

Avatar
Permanently deleted user

Hi,

You can try using a message bus connection, this is how I send data between entities in my plugin.
http://www.jetbrains.org/intellij/sdk/docs/reference_guide/messaging_infrastructure.html?search=messag

Shachar

0
Avatar
Permanently deleted user
"Please see comment in com.intellij.openapi.actionSystem.ex.AnActionListener#afterActionPerformed:

  Note that using {@code dataContext} in implementing methods is unsafe - it could have been invalidated by the performed action."

Can you think of any other way I could figure out which editor is in focus, without using DataContext or AnActionEvent's getData(CommonDataKeys.Editor) method inside afterActionPerformed()? If that's not possible is there any other way of listening for copy pastes you can think of?

"https://github.com/nn3un/Show-Your-Work/blob/master/src/IdeInitializer.java#L207 is problematic since it keeps adding listeners to the application without ever removing them AFAIU."

I think I'm removing the DocumentListeners correctly. I'm not sure how I could remove the ones I'm hooking up through message bus though. Should I call connection.disconnect()? Should I implement Disposable for my listeners and dispose when I'm done?

Thank you very much. I'm pretty inexperienced in all this, so sorry if the questions don't make proper sense. 😅
0

Sorry for delay, please check out com.intellij.codeInsight.editorActions.CopyPastePostProcessor - it might cover the copy/paste use-case for editors.

0

Please sign in to leave a comment.