Couple<PsiElement> (final PsiElement element1, final PsiElement element2) { final PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2); if (parent == null) { return null; } final PyElement statementList = PyPsiUtils.getStatementList(parent); if (statementList == null) { return null; } final PsiElement statement1 = PyPsiUtils.getParentRightBefore(element1, statementList); final PsiElement statement2 = PyPsiUtils.getParentRightBefore(element2, statementList); if (statement1 == null || statement2 == null){ return null; } // return elements if they are really first and last elements of statements if (element1 == PsiTreeUtil.getDeepestFirst(statement1) && element2 == PyPsiUtils.getPrevSignificantLeaf(PsiTreeUtil.getDeepestLast(statement2), !(element2 instanceof PsiComment))) { return Couple.of(statement1, statement2); } return null; }