有没有办法通过插件以编程方式刷新/同步 IntelliJ IDEA 项目?
我正在开发一个 IntelliJ IDEA 插件,它在活动项目中创建一个文件。我想通过插件自动刷新/同步项目,以便用户可以立即看到更改。这在 IntelliJ IDEA 中可能吗?
经过更多挖掘后,我找到了以下解决方案并且它起作用了。
public void actionPerformed(AnActionEvent e) {
// Create/Modify files
// Get the project from the ActionEvent
Project project = e.getData(PlatformDataKeys.PROJECT);
// Get the Base Dir and refresh with the following parameters
// 'asynchronous' set to false and 'recursive' set to true
project.getBaseDir().refresh(false,true);
}
您可以使用 SyncAction 来实现
SyncProjectAction().actionPerformed(actionEvent)
以上方法均不适用于我的情况。我用的是华为的DevEco。
我的解决方案是在我自己的操作中调用同步操作:
private fun getSyncActions() {
val actionManager = ActionManager.getInstance()
val actionIds = actionManager.getActionIds("")
for (id in actionIds) {
if (id.lowercase().contains("sync")) {
val action = actionManager.getAction(id)
val message =
"Action ID: " + id + " - " + action.getTemplatePresentation().text
println(message)
}
}
}
上面的
Action ID
就是我们所需要的。
ActionManager
fun invokeAnAction() {
// Execute asynchronously on EDT
ApplicationManager.getApplication().invokeLater {
try {
val am: ActionManager = ActionManager.getInstance()
val syncAndRefreshProjectAction = am.getAction("YOUR ACTION ID HERE")
if (syncAndRefreshProjectAction != null) {
am.tryToExecute(
syncAndRefreshProjectAction,
null,
null,
null,
true
)
} else {
// custome notify
Notify.error("Action not found.")
}
} catch (e: Exception) {
Notify.error("error in get Action: $e")
Logger.getInstance(this::class.java).error(e)
}
}
}
这对我有用,顺便说一句,华为 DevEco 中的 Sync Action ID 是
Ohos.SyncAndRefresh
。