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.
Please sign in to leave a comment.
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.
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:
Unfortunately, this last item throws an exception complaining about the thread not being a "dispatch thread":
Any pointers on how to do this properly (without closing/opening forcefully the files in editors) would be appreciated ;)
Sorry for delay. You must run this code on EDT as detailed here https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html#modality-and-invokelater
Thanks Yann, that last piece of information was instrumental in solving my issue. It all works now (with some filtering on editor.isDisposable() ).