我目前在转会写的IntelliJ插件。 我希望能够保存/恢复一组选项卡的不同标签的会话(与浏览器插件,像之间切换会话管理器或会话的好友 )。
因此我需要三种基本类型的动作:
- 阅读打开的选项卡(其中文件和编辑用的?)
- 永久地存储信息的标签会话
- 选择会话并关闭所有其他打开的标签页
我看着可用的操作: IdeActions.java -似乎没有什么我寻找。 但是,也许我期待在错误的地方。 谁能告诉我,如果有什么我想实现是可能的,给我一些指点方向是正确的?
更新
我成功地创建了插件,它可以在Github上: http://alp82.github.com/idea-tabsession/
这是官方的插件库可供选择: 选项卡会话 。
更新2
下面是关于分裂的窗户后续问题: 读取和设置分割窗口设置
2017年更新
我停止对这个插件的支持,因为IDEA已经支持该功能。 您可以保存并如下所示轻松地将背景: https://github.com/alp82/idea-tabsession#discontinued
更新
这个插件是,准备,并且可以在IDEA下载 - >设置 - >插件。 源代码可在: https://github.com/alp82/idea-tabsession
简答
要阅读其标签是打开的,现在,使用EditorFactory
和FileDocumentManager
单身:
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);
}
长的答案
先决条件
- 激活插件开发,Groovy和UI设计师插件
- 新建项目 - > IntelliJ IDEA的插件
结帐IDEA社区版源到任何文件夹:
git clone git://git.jetbrains.org/idea/community.git idea
配置IDEA SDK,并创建插件
插件结构
创建了插件后,你需要编辑的plugin.xml坐落在META-INF文件夹中。 修改id
, name
和description
。
我们需要持续性的存储配置文件。 创建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>
该组件类提供了所有需要的功能,但永远不会被使用。 因此,我们需要行动来执行加载和保存:
Save.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);
}
}
Load.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>
插件部署
基本功能已准备就绪。 我会部署这个插件,并发布到开源社区之前添加多个会话和其他一些奇妙的东西支持。 链接将被张贴在这里,当它的在线。
文章来源: IntelliJ IDEA Plugin Development: Save groups of tabs, save them persistently and reload a set of tabs if requested by the user