問題描述
我目前正在編寫一個 IntelliJ 插件.我希望能夠存儲/恢復一組選項卡以在不同的選項卡會話之間切換(類似于 會話管理器 或 會話好友).
I am currently at the move to write an IntelliJ plugin. I want to be able to store/restore a set of tabs to switch between different tab sessions (comparable to browser plugins like Session Manager or Session Buddy).
因此我基本上需要三種類型的動作:
Therefore i need basically three types of actions:
- 讀取打開的選項卡(使用哪個文件和編輯器?)
- 將該信息永久存儲為選項卡會話
- 打開選定會話的標簽并關閉所有其他標簽
我查看了可用的操作:IdeActions.java - 似乎沒有我正在尋找的東西.但也許我看錯了地方.誰能告訴我我想要實現的目標是否可行,并給我一些正確方向的指點?
I looked at the available actions: IdeActions.java - it seems that there is not what i am searching for. But maybe i am looking at the wrong place. Can anyone tell me if what's i am trying to achieve is possible and give me some pointers in the right direction?
我成功創建了插件,它可以在 Github 上找到:http://alp82.github.com/idea-tabsession/
I succesfully created the plugin and it's available at Github: http://alp82.github.com/idea-tabsession/
它在官方插件庫中可用:Tab Session.
It is available in the official plugin repository: Tab Session.
這是關于拆分窗口的后續問題:檢索和設置拆分窗口設置
Here is a follow-up question regarding splitted windows: Retrieving and setting split window settings
推薦答案
2017年更新
我停止支持此插件,因為 IDEA 已經支持該功能.您可以輕松地保存和加載上下文,如下所示:https://github.com/alp82/idea-tabsession#discontinued
插件已準備就緒,可以在 IDEA -> 設置 -> 插件中下載.源代碼位于:https://github.com/alp82/idea-tabsession
The plugin is up and ready and can be downloaded in IDEA -> Settings -> Plugins. Source code is available at: https://github.com/alp82/idea-tabsession
要讀取當前打開的選項卡,請使用 EditorFactory
和 FileDocumentManager
單例:
To read which tabs are open right now, use the EditorFactory
and FileDocumentManager
Singletons:
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
是規范路徑的字符串數組):
To open tabs use the FileEditorManager
singleton (files
being a String Array of canonical paths):
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);
}
長答案
先決條件
- 激活插件開發、Groovy 和 UI Designer 插件
- 新建項目 -> IntelliJ IDEA 插件
簽出 IDEA Community Edition 源代碼到任何文件夾:
git clone git://git.jetbrains.org/idea/community.git idea
配置 IDEA SDK 和創建插件
插件結構
創建插件后,您需要編輯位于 META-INF 文件夾中的 plugin.xml.修改id
、name
和description
.
我們需要一個用于持久存儲的配置文件.在 src
文件夾中創建一個 mystorage.xml
文件.現在是時候創建所需的文件了:
We need a configuration file for persistant storage. Create a mystorage.xml
file in your src
folder. It's now time to create the needed files:
SessionComponent.java(使用Add Project Component
向導創建它以自動創建所需的xml設置):
SessionComponent.java (create it with the Add Project Component
wizard to automatically create the needed xml settings):
@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 中有一個像這樣的條目:
The component class should have an entry in your plugin.xml like this one:
<project-components>
<component>
<implementation-class>my.package.SessionComponent</implementation-class>
</component>
</project-components>
組件類提供了所有需要的功能,但從未使用過.因此,我們需要執行加載和保存操作:
The component class offers all needed functionality, but is never be used. Therefore, we need actions to perform loading and saving:
保存.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.
這篇關于IntelliJ IDEA 插件開發:保存選項卡組,持久保存并在用戶請求時重新加載一組選項卡的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!