Freewind @ Thoughtworks scala java javascript dart 工具 编程实践 月结 math python english [comments admin] [feed]

(2014-09-27) 如何在编写Idea Plugin的时候,知道项目中哪些文件被打开、关闭等

广告: 云梯:翻墙vpn (省10元) 土行孙:科研用户翻墙http proxy (有优惠)

plugin.xml

<project-components>
    <component>
        <implementation-class>com.thoughtworks.pli.intellij.remotepair.RemotePairProjectComponent</implementation-class>
    </component>
</project-components>

RemotePairProjectComponent.scala

package com.thoughtworks.pli.intellij.remotepair

import com.intellij.openapi.fileEditor.{FileEditorManagerEvent, FileEditorManager, FileEditorManagerAdapter, FileEditorManagerListener}
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.annotations.NotNull
import com.intellij.util.messages.MessageBusConnection
import com.intellij.openapi.components.ProjectComponent
import com.intellij.openapi.project.Project

class RemotePairProjectComponent(project: Project) extends ProjectComponent {

  override def initComponent() {}

  override def disposeComponent() {}

  override def getComponentName: String = "RemotePairProjectComponent"

  override def projectOpened() {
    val connection: MessageBusConnection = project.getMessageBus.connect(project)
    connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter {
      override def fileOpened(@NotNull source: FileEditorManager, @NotNull file: VirtualFile) {
        System.out.println("########## file opened: " + file)
      }

      override def fileClosed(source: FileEditorManager, file: VirtualFile) {
        System.out.println("########## file closed: " + file)
      }

      override def selectionChanged(event: FileEditorManagerEvent) {
        val oldFile = event.getOldFile
        val newFile = event.getNewFile
        System.out.println(s"########## file selection changed: $oldFile -> $newFile")
      }
    })
  }

  override def projectClosed() {}

}

运行

运行该插件,在新打开的IDEA中打开一个项目,进行一些打开、关闭、切换文件的操作,可以看到原IDEA的console中会输出:

########## file selection changed: null -> file:///.../test/src/Aaa.java
########## file opened: file:///.../test/src/Aaa.java
########## file selection changed: file:///.../test/src/Aaa.java -> file:///.../test/src/pubspec.yaml
########## file opened: file:///.../test/src/pubspec.yaml
########## file selection changed: file:///.../test/src/pubspec.yaml -> file:///.../test/src/Aaa.java
########## file selection changed: file:///.../test/src/Aaa.java -> file:///.../test/src/pubspec.yaml
comments powered by Disqus