Converting VSCode extension to Intellij plugin questions
I've just written my first extension for Visual Studio Code (https://marketplace.visualstudio.com/items?itemName=kshetline.ligatures-limited). That's working well, and now I'm trying to create a similar plug-in for Intellij IDEA.
NOTHING is going smoothly, or working much like any of the examples I've found.
Perhaps I'm trying to learn too many new things at once: I don't know much about Gradle, IDEA's extension API, or Kotlin (I do at least know Java well).
What I'm stuck on now is the basics of getting events or notifications from IDEA which at least roughly correspond to these VSCode extension callbacks:
workspace.onDidOpenTextDocument
workspace.onDidCloseTextDocument
window.onDidChangeVisibleTextEditors
window.onDidChangeTextEditorSelection
workspace.onDidChangeTextDocument
workspace.onDidChangeConfiguration
Basically I need to know:
- Which documents/editors are currently open at the start of opening a project
- When documents/editors are opened and closed after that
- When the current cursor position or text selection changes in any open editors
- When text is modified in any of the open editors
- When the configuration of the plugin is modified
Here's what little I have of a plugin.xml file now:
<idea-plugin>
<id>com.shetline.ligatures-limited</id>
<name>Ligatures Limited</name>
<vendor email="kerry@shetline.com" url="http://github.com/kshetline/ligatures-limited-idea">Kerry Shetline</vendor>
<description><![CDATA[
Code ligatures <i>only where you want them</i>, not where you don't
]]></description>
<depends>com.intellij.modules.platform</depends>
<applicationListeners>
<listener class="com.shetline.lligatures.LigaturesLimited" topic="com.intellij.ide.AppLifecycleListener"/>
</applicationListeners>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.shetline.lligatures.LigaturesLimited"/>
</extensions>
<actions>
</actions>
</idea-plugin>
The ONE event I've managed to capture is when the application starts up:
package com.shetline.lligatures
import com.intellij.ide.AppLifecycleListener
class LigaturesLimited : AppLifecycleListener {
override fun appFrameCreated(commandLineArgs: MutableList<String>) {
println("*** appFrameCreated")
}
}
I can understand from sample code I've seen how I might, for example, attach an action to a document, and then as soon as a user triggered that action, learn about the associated document and editor... but I don't want to have to wait for something like that. I need to be immediately aware of the fact that an editor is open and looking at a document as soon as that happens.
I've tried looking at the source for Rainbow Brackets, because I'd have thought there'd be a fair amount of overlap between what that extension does and what I'm trying to do -- which is basically apply contextual formatting to the code the user is editing. But I'm afraid I follow very little about how Rainbow Brackets manages to do what it does.
Please sign in to leave a comment.
Hi, sorry for delay in answer.
I hope I can give you some pointers:
com.intellij.openapi.fileEditor.FileEditorManager and com.intellij.openapi.fileEditor.FileEditorManagerListener#FILE_EDITOR_MANAGER will allow to query currently open files/editors and listen to changes
Listening to Document changes https://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/documents.html#how-do-i-get-notified-when-documents-change
Please note that the IntelliJ Platform architecture is _very_ different from VSCode and thus many flows cannot be mapped 1:1 conceptually.
Thanks for the info. I've already seen how different these APIs are, but I've managed to make some progress. Using highlightingPassFactory and highlightVisitor does most of what I need.
What would help now is to find out a way to trigger highlightVisitor callbacks. They seem to be triggered when documents are loaded or edited, which covers most situations, but I'd like to be able to trigger this same callbacks when, say, changing the settings of my plugin.
When I tried to see how some other plugins handled this situation... they simply didn't handle it. It turns out that if you change the color settings in Rainbow Brackets, for instance, that you won't see those changes until you edit your documents, or close and re-open them.
You can force re-highlighting of open editors via com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart() from your plugin's settings "apply".
Thanks! What I specifically needed was this: