Listeners declared in plugin.xml not working

Answered

Hello,

I am trying to register some listeners using the <applicationListeners> tag in the plugin.xml as shown in https://jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_listeners.html 

They don't show up in any way, whether using println or breakpointing the listeners in the debugger.

I got this so far:

<applicationListeners>
<listener class="com.github.chicoferreira.projecttimecounter.listener.HeartbeatEditorMouseListener"
topic="com.intellij.openapi.editor.event.EditorMouseListener"/>
<listener class="com.github.chicoferreira.projecttimecounter.listener.HeartbeatVisibleAreaListener"
topic="com.intellij.openapi.editor.event.VisibleAreaListener"/>
</applicationListeners>

What I have in the plugin.xml inside <idea-plugin>

package com.github.chicoferreira.projecttimecounter.listener

import com.github.chicoferreira.projecttimecounter.HeartbeatService
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.event.EditorMouseEvent
import com.intellij.openapi.editor.event.EditorMouseListener
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project

class HeartbeatEditorMouseListener : EditorMouseListener {

override fun mousePressed(event: EditorMouseEvent) {
val instance = FileDocumentManager.getInstance()
val file = instance.getFile(event.editor.document)

if (file != null) {
HeartbeatService.registerHeartbeat(file)
}
}

}

package com.github.chicoferreira.projecttimecounter.listener

import com.github.chicoferreira.projecttimecounter.HeartbeatService
import com.intellij.openapi.editor.event.VisibleAreaEvent
import com.intellij.openapi.editor.event.VisibleAreaListener
import com.intellij.openapi.fileEditor.FileDocumentManager

class HeartbeatVisibleAreaListener : VisibleAreaListener {

override fun visibleAreaChanged(event: VisibleAreaEvent) {
val instance = FileDocumentManager.getInstance()
val file = instance.getFile(event.editor.document)

if (file != null) {
HeartbeatService.registerHeartbeat(file)
}
}
}

I am using kotlin and intellij 2020.2 for this project.

I already tested this classes written in java, changing <applicationListeners> to <projectListeners> (+adding Project to constructor) and setting activeInTestMode=true in both listeners. Nothing works.

Am I doing something wrong?

Thank you.

4 comments
Comment actions Permalink

EditorMouseListener is supposed to be registered not as a project/application listener, but editorFactoryMouseListener Extension Point:

<extensions defaultExtensionNs="com.intellij">
<editorFactoryMouseListener implementation="com.github.chicoferreira.projecttimecounter.listener.HeartbeatEditorMouseListener" />
</extensions>

VisibleAreaListener should be registered directly with the Editor's ScrollingModel:

editor.scrollingModel.addVisibleAreaListener(HeartbeatVisibleAreaListener());

 

0
Comment actions Permalink

Jakub Chrzanowski Thank you so much. Where can I find more information about registering those and other listeners?

0
Comment actions Permalink

I've already posted above the working examples, but if you want more - please go through the intellij-community repository.

0
Comment actions Permalink

Jakub Chrzanowski Absolutely, thank you for the help.

0

Please sign in to leave a comment.