Couple<PsiElement> (@NotNull PyStatement statement) { if (!(statement instanceof PyExpressionStatement)) return Couple.getEmpty(); // "match <caret>expr" case PsiElement prevSibling = PyPsiUtils.getPrevNonWhitespaceSiblingOnSameLine(statement); if (prevSibling instanceof PyExpressionStatement && isMatchIdentifier(prevSibling)) { return Couple.of(prevSibling, statement); } if (isMatchIdentifier(statement)) { // "<caret>match expr" PsiElement nextSibling = PyPsiUtils.getNextNonWhitespaceSiblingOnSameLine(statement); if (nextSibling instanceof PyExpressionStatement) { return Couple.of(statement, nextSibling); } // "match" case else if (nextSibling == null) { return Couple.of(statement, null); } } // "match + x" or "match [x]" case if (isMatchIdentifier(PsiTreeUtil.getDeepestFirst(statement))) { LanguageLevel languageLevel = LanguageLevel.forElement(statement); PyElementGenerator generator = PyElementGenerator.getInstance(statement.getProject()); String subjectSuspect = StringUtil.trimStart(statement.getText(), PyNames.MATCH); try { generator.createExpressionFromText(languageLevel, subjectSuspect.trim()); return Couple.of(statement, statement); } catch (IncorrectOperationException ignored) { } } return Couple.getEmpty(); }