Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/8840509 to your computer and use it in GitHub Desktop.
Save dkandalov/8840509 to your computer and use it in GitHub Desktop.
Example of listening to VCS updates in IntelliJ (this is mini-plugin for https://github.com/dkandalov/live-plugin)
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.util.Pair
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.VcsKey
import com.intellij.openapi.vcs.update.UpdateEnvironment
import com.intellij.openapi.vcs.update.UpdatedFilesListener
import com.intellij.openapi.vcs.update.UpdatedFilesReverseSide
import com.intellij.util.Consumer
import com.intellij.util.messages.MessageBusConnection
import static liveplugin.PluginUtil.*
if (isIdeStartup) return
changeGlobalVar("BusVcsUpdateListener"){ MessageBusConnection connection ->
if (connection != null) {
connection.disconnect()
}
connection = project.messageBus.connect()
connection.subscribe(UpdatedFilesListener.UPDATED_FILES, new UpdatedFilesListener() {
@Override void consume(Set<String> files) {
show("On bus VCS update: ${files.join(",")}")
}
})
connection
}
def listenerManager = ProjectLevelVcsManager.getInstance(project).vcsEventsListenerManager
changeGlobalVar("BadVcsUpdateListener"){ key ->
if (key != null) listenerManager.removeUpdate(key)
key = listenerManager.addUpdate(new Consumer<Pair<VcsKey, Consumer<UpdateEnvironment>>>() {
@Override void consume(Pair<VcsKey, Consumer<UpdateEnvironment>> pair) {
// for some reason it's called twice on pressing "update project" shortcut
// and twice on actual update from VCS
show("On listener VCS update: " + pair.first + " " + pair.second)
}
})
key
}
show("Listening to VCS updates")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment