how to use the message infrastructure to highlight text in editor

Answered

Hi,

(newbie here) I am trying to create a plugin for code review. The general idea is for several IDEs to connect a central server, in turn, this server pushes comment notifications to every client. These comments materialize mostly as highlighted regions of text in the editor. I have reached two issues I do not know how to address:

  • Which event to subscribe to when the editor opens a file (used to "draw" the highlights for comments).
  • Using the Messaging Infrastructure, where (in which context) to put my custom topic subscriber to have access to the open editor.

Everything is scoped to a Project. Open files are assumed read-only, so no insert/click events can be used to draw comments highlights. I can't share the source code but I can give details ;)

Thanks for your help.

0
4 comments

What about updates from server with new highlights to be rendered while file is already open? You can use com.intellij.openapi.fileEditor.FileEditorManagerListener to listen to file open/close events.

0
Avatar
Permanently deleted user

Yann, thanks for your answer it solves the first bullet point.

I'm not clear on what the first sentence of your answer would actually look like in terms of code (and of course, I don't try to highlight code in files that are not opened). Not only do I need to highlight code when opening the file, but I also need to do it whilst the file is opened (as a new server message instructing to highlight code in the currently open file can and will happen). This is this async way of redrawing highlights I'm not sure how to implement.

Here's what I attempted:

  • Create a service to keep track of editors for my project (it is a glorified Hashmap<filename, editor>)
  • Listen in on com.intellij.openapi.fileEditor.FileEditorManagerListener and add/remove the editor to the service
  • Upon reception of a message from the server, use the service to get the editor matching the filename and try to highlight code

Unfortunately, this last item throws an exception complaining about the thread not being a "dispatch thread":

com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@1dee1197
Current thread: Thread[EventThread,6,Idea Thread Group] 595994497
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,Idea Thread Group] 578668489
at com.intellij.openapi.application.impl.ApplicationImpl.throwThreadAccessException(ApplicationImpl.java:1088)
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1063)
at com.intellij.openapi.editor.impl.view.EditorView.assertIsDispatchThread(EditorView.java:690)
at com.intellij.openapi.editor.impl.view.EditorView.visualToLogicalPosition(EditorView.java:179)
at com.intellij.openapi.editor.impl.EditorImpl.visualToLogicalPosition(EditorImpl.java:2268)
at com.intellij.openapi.editor.Editor.visualPositionToOffset(Editor.java:212)
at com.xxx.yyy.logic.messages.JsonAnnotation.coordinatesToOffsets(JsonAnnotation.java:60)
[...]

Any pointers on how to do this properly (without closing/opening forcefully the files in editors) would be appreciated ;)

0
Avatar
Permanently deleted user

Thanks Yann, that last piece of information was instrumental in solving my issue. It all works now (with some filtering on editor.isDisposable() ).

0

Please sign in to leave a comment.