HashSet<String> (@NotNull PsiCodeBlock body) { HashSet<String> variableNames = new HashSet<>(); for (PsiStatement statement : body.getStatements()) { if (statement instanceof PsiDeclarationStatement) { variableNames.addAll(processDeclarationStatement((PsiDeclarationStatement)statement)); continue; } if (statement instanceof PsiLoopStatement) { variableNames.addAll(processLoopStatement(statement)); continue; } if (statement instanceof PsiIfStatement) { Arrays.stream(statement.getChildren()) .filter(child -> child instanceof PsiBlockStatement) .forEach(blockStatement -> variableNames .addAll(processFunctionStatements(((PsiBlockStatement)blockStatement).getCodeBlock()))); } } return variableNames; }