創建我的 IDEA 插件。如果給出路徑,我可以通過以下代碼打開對應的檔案:
VirtualFile classFile = LocalFileSystem.getInstance().findFileByPath(f.getAbsolutePath());
OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(project, classFile);
FileEditorManager.getInstance(project).openTextEditor(openFileDescriptor, true);
但我希望左側的導航視圖也能關注我給出的路徑所代表的檔案。

這類似于“始終選擇打開的檔案”的設定。

我沒有在官方 API中找到我需要的東西。
我真誠地希望得到幫助。
uj5u.com熱心網友回復:
一個可能更可靠的解決方案,需要更少的代碼:
private static void selectInProjectView(VirtualFile file, Project project) {
FileSelectInContext selectInContext = new FileSelectInContext(project, file);
SelectInTarget selectInTarget = SelectInManager.findSelectInTarget(ToolWindowId.PROJECT_VIEW, project);
if (selectInTarget != null) {
selectInTarget.selectIn(selectInContext, true /* request focus */);
}
}
uj5u.com熱心網友回復:
我在Selecting a File in Project Tree中找到了解決此問題的方法。
代碼部分貼在下面:
// file absulute path
String path="D:\Projects\Demo-Plugin\out\production\Demo-Plugin\TestAction.class"
// Files found by path, or obtain the `virtualfile` file in other ways
VirtualFile classFile = LocalFileSystem.getInstance().findFileByPath(path);
// SelectInContext
SelectInContext context = new SelectInContext() {
@Override
public @NotNull
Project getProject() {
// Project project = event.getData(PlatformDataKeys.PROJECT);
return project;
}
@Override
public @NotNull
VirtualFile getVirtualFile() {
// VirtualFile
return classFile;
}
@Override
public @Nullable
Object getSelectorInFile() {
return null;
}
@Override
public @Nullable
FileEditorProvider getFileEditorProvider() {
return null;
}
};
for (SelectInTarget target : SelectInManager.getInstance(project).getTargets()) {
if (target.canSelect(context)) {
target.selectIn(context, true);
return;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497202.html
