List<PsiLocalVariable> (@NotNull PsiElement start, @NotNull PsiElement end, @NotNull Set<PsiLocalVariable> ignoreVariables) { // Only the variables declared at the current code block's level can be reused after the end of the fragment. List<PsiLocalVariable> result = new SmartList<>(); for (PsiElement element = start; element != null; element = element != end ? element.getNextSibling() : null) { if (element instanceof PsiDeclarationStatement) { PsiElement[] declaredElements = ((PsiDeclarationStatement)element).getDeclaredElements(); for (PsiElement declaredElement : declaredElements) { if (declaredElement instanceof PsiLocalVariable && !ignoreVariables.contains(declaredElement)) { result.add((PsiLocalVariable)declaredElement); } } } } return result; }