20

我目前正在编写一个 IntelliJ 插件。我希望能够存储/恢复一组选项卡以在不同的选项卡会话之间切换(类似于Session ManagerSession Buddy等浏览器插件)。

因此,我基本上需要三种类型的动作:

  1. 阅读打开的标签(使用哪个文件和编辑器?)
  2. 将该信息永久存储为选项卡会话
  3. 打开选定会话的选项卡并关闭所有其他选项卡

我查看了可用的操作:IdeActions.java - 似乎没有我要搜索的内容。但也许我看错了地方。谁能告诉我我想要实现的目标是否可行,并给我一些正确方向的指示?

更新

我成功创建了插件,它在 Github 上可用:http: //alp82.github.com/idea-tabsession/

它在官方插件存储库中可用:Tab Session

更新 2

这是关于拆分窗口的后续问题:检索和设置拆分窗口设置

4

1 回答 1

24

2017 年更新

我停止支持这个插件,因为 IDEA 已经支持这个功能。您可以轻松地保存和加载上下文,如下所示:https ://github.com/alp82/idea-tabsession#discontinued

更新

该插件已准备就绪,可以在 IDEA -> 设置 -> 插件中下载。源代码位于:https ://github.com/alp82/idea-tabsession

简答

要读取当前打开的选项卡,请使用EditorFactoryFileDocumentManagerSingletons:

    Editor[] editors = EditorFactory.getInstance().getAllEditors();
    FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
    for(Editor editor : editors) {
        VirtualFile vf = fileDocManager.getFile(editor.getDocument());
        String path = vf.getCanonicalPath();
        System.out.println("path = " + path);
    }

要打开选项卡,请使用FileEditorManager单例(files作为规范路径的字符串数组):

    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    for(String path : files) {
        System.out.println("path = " + path);
        VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(path);
        fileEditorManager.openFile(vf, true, true);
    }

长答案

先决条件

  1. 激活插件开发、Groovy 和 UI Designer 插件
  2. 新项目 -> IntelliJ IDEA 插件
  3. 将IDEA 社区版源签出到任何文件夹:

    git clone git://git.jetbrains.org/idea/community.git idea
    
  4. 配置 IDEA SDK 并创建插件

插件结构

创建插件后,您需要编辑位于 META-INF 文件夹中的 plugin.xml。修改id,namedescription.

我们需要一个用于持久存储的配置文件。mystorage.xml在您的文件夹中创建一个文件src。现在是时候创建所需的文件了:

SessionComponent.java(使用Add Project Component向导创建它以自动创建所需的 xml 设置):

@State(
    name = "SessionComponent",
    storages = {
        @Storage(id = "default", file = StoragePathMacros.PROJECT_FILE),
        @Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/mystorage.xml", scheme = StorageScheme.DIRECTORY_BASED)
    }
)
public class SessionComponent implements ProjectComponent, PersistentStateComponent<SessionState> {

    Project project;
    SessionState sessionState;

    public SessionComponent(Project project) {
        this.project = project;
        sessionState = new SessionState();
    }

    public void initComponent() {
        // TODO: insert component initialization logic here
    }

    @Override
    public void loadState(SessionState sessionState) {
        System.out.println("load sessionState = " + sessionState);
        this.sessionState = sessionState;
    }

    public void projectOpened() {
        // called when project is opened
    }

    public void projectClosed() {
        // called when project is being closed
    }

    @Nullable
    @Override
    public SessionState getState() {
        System.out.println("save sessionState = " + sessionState);
        return sessionState;
    }

    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    @NotNull
    public String getComponentName() {
        return "SessionComponent";
    }

    public int saveCurrentTabs() {
        Editor[] editors = getOpenEditors();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        VirtualFile[] selectedFiles = fileEditorManager.getSelectedFiles();

        FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
        sessionState.files = new String[editors.length];
        int i = 0;
        for(Editor editor : editors) {
            VirtualFile vf = fileDocManager.getFile(editor.getDocument());
            String path = vf.getCanonicalPath();
            System.out.println("path = " + path);
            if(path.equals(selectedFiles[0].getCanonicalPath())) {
                sessionState.focusedFile = path;
            }
            sessionState.files[i] = path;
            i++;
        }

        return editors.length;
    }

    public int loadSession() {
        closeCurrentTabs();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        for(String path : sessionState.files) {
            System.out.println("path = " + path);
            VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(path);
            fileEditorManager.openFile(vf, true, true);
        }

        return sessionState.files.length;
    }

    public void closeCurrentTabs() {
        Editor[] editors = getOpenEditors();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
        FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
        for(Editor editor : editors) {
            System.out.println("editor = " + editor);
            VirtualFile vf = fileDocManager.getFile(editor.getDocument());
            fileEditorManager.closeFile(vf);
        }
    }

    public void showMessage(String htmlText) {
        StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
        JBPopupFactory.getInstance()
            .createHtmlTextBalloonBuilder(htmlText, MessageType.INFO, null)
            .setFadeoutTime(7500)
            .createBalloon()
            .show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.atRight);
    }

    private Editor[] getOpenEditors() {
        return EditorFactory.getInstance().getAllEditors();
    }

}

我们还需要存储类:

public class SessionState {
    public String[] files = new String[0];
    public String focusedFile = "";

    public String toString() {
        String result = "";
        for (String file : files) {
            result += file + ", ";
        }
        result += "selected: " + focusedFile;
        return result;
    }
}

组件类应该在您的 plugin.xml 中有一个条目,如下所示:

<project-components>
  <component>
    <implementation-class>my.package.SessionComponent</implementation-class>
  </component>
</project-components>

组件类提供了所有需要的功能,但从未使用过。因此,我们需要执行加载和保存操作:

保存.java:

public class Save extends AnAction {

    public Save() {
        super();
    }

    public void actionPerformed(AnActionEvent event) {
        Project project = event.getData(PlatformDataKeys.PROJECT);
        SessionComponent sessionComponent = project.getComponent(SessionComponent.class);

        int tabCount = sessionComponent.saveCurrentTabs();
        String htmlText = "Saved " + String.valueOf(tabCount) + " tabs";
        sessionComponent.showMessage(htmlText);
    }

}

加载.java:

public class Load extends AnAction {

    public Load() {
        super();
    }

    public void actionPerformed(AnActionEvent event) {
        Project project = event.getData(PlatformDataKeys.PROJECT);
        SessionComponent sessionComponent = project.getComponent(SessionComponent.class);

        int tabCount = sessionComponent.loadSession();
        String htmlText = "Loaded " + String.valueOf(tabCount) + " tabs";
        sessionComponent.showMessage(htmlText);
    }

}

Aaand...行动!

我们需要的最后一件事是选择这些操作的用户界面。简单地把它放在你的plugin.xml

  <actions>
    <!-- Add your actions here -->
      <group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu">
          <add-to-group group-id="MainMenu" anchor="last"  />
          <action id="MyPlugin.Save" class="my.package.Save" text="_Save" description="A test menu item" />
          <action id="MyPlugin.Load" class="my.package.Load" text="_Load" description="A test menu item" />
      </group>
  </actions>

Plugin Deployment

The basic functionality is ready. I will add support for multiple sessions and some other neat stuff before deploying this plugin and releasing it to the open-source community. Link will be posted here, when it's online.

于 2013-03-30T13:19:51.823 回答