void (@NotNull Project project, @NotNull Runnable task) { AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(project, myExternalSystemId); Object systemStateToRestore = null; if (systemSettings instanceof PersistentStateComponent) { systemStateToRestore = ((PersistentStateComponent<?>)systemSettings).getState(); } systemSettings.copyFrom(getControl().getSystemSettings()); Collection projectSettingsToRestore = systemSettings.getLinkedProjectsSettings(); Set<ExternalProjectSettings> projects = new HashSet<>(systemSettings.getLinkedProjectsSettings()); projects.add(getCurrentExternalProjectSettings()); systemSettings.setLinkedProjectsSettings(projects); try { task.run(); } finally { if (systemStateToRestore != null) { ((PersistentStateComponent)systemSettings).loadState(systemStateToRestore); } else { systemSettings.setLinkedProjectsSettings(projectSettingsToRestore); } } }