List<SwitchStatementBranch> (PsiCodeBlock body, Set<PsiSwitchLabelStatementBase> fallThroughTargets) { final List<SwitchStatementBranch> openBranches = new ArrayList<>(); final Set<PsiElement> declaredElements = new HashSet<>(); final List<SwitchStatementBranch> allBranches = new ArrayList<>(); SwitchStatementBranch currentBranch = null; final PsiElement[] children = body.getChildren(); List<PsiSwitchLabelStatementBase> labels = PsiTreeUtil.getChildrenOfTypeAsList(body, PsiSwitchLabelStatementBase.class); boolean defaultAlwaysExecuted = !labels.isEmpty() && SwitchUtils.isDefaultLabel(ContainerUtil.getLastItem(labels)) && fallThroughTargets.containsAll(labels.subList(1, labels.size())); for (int i = 1; i < children.length - 1; i++) { final PsiElement statement = children[i]; if (statement instanceof PsiSwitchLabelStatement label) { if (currentBranch == null || !fallThroughTargets.contains(statement)) { openBranches.clear(); currentBranch = new SwitchStatementBranch(); currentBranch.addPendingDeclarations(declaredElements); allBranches.add(currentBranch); openBranches.add(currentBranch); } else if (currentBranch.hasStatements()) { currentBranch = new SwitchStatementBranch(); allBranches.add(currentBranch); openBranches.add(currentBranch); } if (SwitchUtils.isDefaultLabel(label) && defaultAlwaysExecuted) { openBranches.retainAll(Collections.singleton(currentBranch)); } currentBranch.addCaseValues(label, defaultAlwaysExecuted); } else if (statement instanceof PsiSwitchLabeledRuleStatement rule) { openBranches.clear(); currentBranch = new SwitchStatementBranch(); PsiStatement ruleBody = rule.getBody(); if (ruleBody != null) { currentBranch.addStatement(ruleBody); } currentBranch.addCaseValues(rule, defaultAlwaysExecuted); openBranches.add(currentBranch); allBranches.add(currentBranch); } else { if (statement instanceof PsiStatement) { if (statement instanceof PsiDeclarationStatement declarationStatement) { Collections.addAll(declaredElements, declarationStatement.getDeclaredElements()); } for (SwitchStatementBranch branch : openBranches) { branch.addStatement((PsiStatement)statement); } } else { for (SwitchStatementBranch branch : openBranches) { if (statement instanceof PsiWhiteSpace) { branch.addWhiteSpace(statement); } else { branch.addComment(statement); } } } } } return allBranches; }