LocalTask (@NotNull final Task origin, boolean clearContext, boolean newTask) { LocalTask activeTask = getActiveTask(); if (origin.equals(activeTask)) return activeTask; saveActiveTask(); final LocalTask task = doActivate(origin, true); Runnable restore = () -> { WorkingContextManager contextManager = WorkingContextManager.getInstance(myProject); if (clearContext) { contextManager.clearContext(); } contextManager.restoreContext(origin); }; boolean switched = false; if (isVcsEnabled()) { restoreVcsContext(task, newTask); if (!newTask) { switched = switchBranch(task, restore); } } if (!switched) restore.run(); return task; }