PY-48014 Implement Complete Current Statement for match statement and its clauses

GitOrigin-RevId: dba29d39fc90397a2f4af8a347cfcbf208ce5c92
This commit is contained in:
Mikhail Golubev
2021-06-24 15:04:01 +00:00
committed by intellij-monorepo-bot
parent 3a3f672cd5
commit 353d7f24d1
46 changed files with 373 additions and 0 deletions
@@ -49,6 +49,8 @@ public class PySmartEnterProcessor extends SmartEnterProcessor {
.add(new PyClassFixer())
.add(new PyWithFixer())
.add(new PyCollectionLiteralFixer())
.add(new PyMatchStatementFixer())
.add(new PyCaseClauseFixer())
.build();
private static final List<EnterProcessor> ourProcessors = ImmutableList.of(new PyCommentBreakerEnterProcessor(),
new PyPlainEnterProcessor());
@@ -0,0 +1,78 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyCaseClause;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyPattern;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.NotNull;
import static com.jetbrains.python.psi.PyUtil.as;
public final class PyCaseClauseFixer extends PyFixer<PyCaseClause> {
public PyCaseClauseFixer() {
super(PyCaseClause.class);
}
@Override
protected void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyCaseClause element) {
PyPattern pattern = element.getPattern();
PsiElement ifKeyword = PyPsiUtils.getFirstChildOfType(element, PyTokenTypes.IF_KEYWORD);
PyExpression condition = element.getGuardCondition();
PsiElement colon = PyPsiUtils.getFirstChildOfType(element, PyTokenTypes.COLON);
Document document = editor.getDocument();
int colonOffset;
if (colon == null) {
String colonSuffix;
if (condition != null) {
colonSuffix = ":";
colonOffset = condition.getTextRange().getEndOffset();
}
else if (ifKeyword != null) {
colonSuffix = " :";
colonOffset = ifKeyword.getTextRange().getEndOffset() + 1;
}
else if (pattern != null) {
colonSuffix = ":";
colonOffset = pattern.getTextRange().getEndOffset();
}
else {
colonSuffix = " :";
colonOffset = element.getFirstChild().getTextRange().getEndOffset() + 1;
}
document.insertString(colonOffset - (colonSuffix.length() - 1), colonSuffix);
}
else {
colonOffset = colon.getTextOffset();
}
if (pattern == null) {
if (ifKeyword != null) {
int ifOffset = ifKeyword.getTextOffset();
PsiWhiteSpace prevWhitespace = as(ifKeyword.getPrevSibling(), PsiWhiteSpace.class);
if (prevWhitespace != null && prevWhitespace.getTextLength() < 2) {
document.insertString(ifOffset, " ");
}
processor.registerUnresolvedError(ifOffset);
}
else {
processor.registerUnresolvedError(colonOffset);
}
}
else if (ifKeyword != null && condition == null) {
if (ifKeyword.getTextRange().getEndOffset() == colonOffset) {
document.insertString(colonOffset, " ");
processor.registerUnresolvedError(colonOffset + 1);
}
else {
processor.registerUnresolvedError(colonOffset);
}
}
}
}
@@ -0,0 +1,107 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.NotNull;
import static com.jetbrains.python.psi.PyUtil.as;
public final class PyMatchStatementFixer extends PyFixer<PyStatement> {
public PyMatchStatementFixer() {
super(PyStatement.class);
}
@Override
protected void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyStatement element) {
Document document = editor.getDocument();
PyMatchStatement matchStatement = as(element, PyMatchStatement.class);
if (matchStatement != null) {
PsiElement colon = PyPsiUtils.getFirstChildOfType(element, PyTokenTypes.COLON);
assert colon != null;
if (matchStatement.getSubject() == null) {
processor.registerUnresolvedError(colon.getTextOffset());
return;
}
int colonEndOffset = colon.getTextRange().getEndOffset();
// It's not enough to check matchStatement.getCaseClauses().isEmpty()
boolean hasEmptyBody = colonEndOffset == matchStatement.getTextRange().getEndOffset();
if (hasEmptyBody) {
String matchIndent = PyIndentUtil.getElementIndent(element);
String indent = PyIndentUtil.getIndentFromSettings(element.getContainingFile());
String caseClausePlaceholder = "\n" + matchIndent + indent + PyNames.CASE + " :";
document.insertString(colonEndOffset, caseClausePlaceholder);
processor.registerUnresolvedError(colonEndOffset + caseClausePlaceholder.length() - 1);
}
return;
}
Couple<PsiElement> pair = findMatchKeywordAndSubject(element);
PsiElement matchKeyword = pair.getFirst();
if (matchKeyword == null) {
return;
}
PsiElement subject = pair.getSecond();
if (subject != null) {
int endOffset = subject.getTextRange().getEndOffset();
String matchIndent = PyIndentUtil.getElementIndent(matchKeyword);
String indent = PyIndentUtil.getIndentFromSettings(element.getContainingFile());
String caseClausePlaceholder = ":\n" + matchIndent + indent + PyNames.CASE + " :";
document.insertString(endOffset, caseClausePlaceholder);
processor.registerUnresolvedError(endOffset + caseClausePlaceholder.length() - 1);
}
else {
int endOffset = element.getTextRange().getEndOffset();
document.insertString(endOffset, " :");
processor.registerUnresolvedError(endOffset + 1);
}
}
@NotNull
private static Couple<PsiElement> findMatchKeywordAndSubject(@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();
}
private static boolean isMatchIdentifier(@NotNull PsiElement element) {
return element.getText().equals(PyNames.MATCH);
}
}
@@ -0,0 +1,2 @@
match x:
case int(n) if<caret>
@@ -0,0 +1,2 @@
match x:
case int(n) if <caret>:
@@ -0,0 +1,2 @@
match x:
case<caret>
@@ -0,0 +1,2 @@
match x:
case if n > 0<caret>
@@ -0,0 +1,2 @@
match x:
case <caret> if n > 0:
@@ -0,0 +1,2 @@
match x:
case i<caret>f
@@ -0,0 +1,2 @@
match x:
case <caret> if :
@@ -0,0 +1,2 @@
match x:
case <caret>:
@@ -0,0 +1,2 @@
match x:
case 42<caret>
@@ -0,0 +1,2 @@
match x:
case int(n) if n > 0<caret>
@@ -0,0 +1,3 @@
match x:
case int(n) if n > 0:
<caret>
@@ -0,0 +1,3 @@
match x:
case 42:
<caret>
@@ -0,0 +1,2 @@
match x:
case 42<caret>:
@@ -0,0 +1,3 @@
match x:
case 42:
<caret>
@@ -0,0 +1,3 @@
mat<caret>ch x:
case 42:
pass
@@ -0,0 +1,2 @@
match x:
case<caret> :
@@ -0,0 +1,2 @@
match x:
case if n > 0<caret>:
@@ -0,0 +1,2 @@
match x:
case <caret> if n > 0:
@@ -0,0 +1,2 @@
match x:
case int(n) if<caret>:
@@ -358,4 +358,110 @@ public class PySmartEnterTest extends PyTestCase {
public void testColonAfterReturnTypeAnnotation() {
runWithLanguageLevel(LanguageLevel.PYTHON34, this::doTest);
}
// PY-48014
public void testColonAndFirstClauseAfterEmptyMatchStatementWithSubjectCaretOnSubject() {
doTest();
}
// PY-48014
public void testColonAndFirstClauseAfterEmptyMatchStatementWithSubjectCaretOnMatch() {
doTest();
}
// PY-48014
public void testColonAndFirstClauseAfterEmptyMatchStatementWithSubjectLookingLikeBinaryExpression() {
doTest();
}
// PY-48014
public void testColonAndFirstClauseAfterEmptyMatchStatementWithSubjectLookingLikeCallExpression() {
doTest();
}
// PY-48014
public void testNothingAfterUnambiguousExpressionStartingWithMatch() {
doTest();
}
// PY-48014
public void testColonAndFirstClauseAfterEmptyMatchStatementWithSubjectCustomIndent() {
getIndentOptions().INDENT_SIZE = 2;
doTest();
}
// PY-48014
public void testColonAfterEmptyMatchStatementWithoutSubject() {
doTest();
}
// PY-48014
public void testOnlyCaretMoveAfterMatchStatementWithColonWithoutSubject() {
doTest();
}
// PY-48014
public void testFirstClauseAfterEmptyMatchStatementWithSubjectAndColon() {
doTest();
}
// PY-48014
public void testLineBreakAndIndentAfterNonEmptyMatchStatementWithSubjectAndColon() {
doTest();
}
// PY-48014
public void testNoFirstClauseInMatchStatementWithIncompleteStatementInside() {
doTest();
}
// PY-48014
public void testColonAndIndentAfterCaseClauseWithPattern() {
doTest();
}
// PY-48014
public void testColonAfterCaseClauseWithoutPattern() {
doTest();
}
// PY-48014
public void testOnlyCaretMoveAfterCaseClauseWithColonWithoutPattern() {
doTest();
}
// PY-48014
public void testIndentAfterCaseClauseWithPatternAndColon() {
doTest();
}
// PY-48014
public void testColonAndIndentAfterCaseClauseWithPatternAndGuardCondition() {
doTest();
}
// PY-48014
public void testColonAfterCaseClauseWithPatternWithoutGuardCondition() {
doTest();
}
// PY-48014
public void testOnlyCaretMoveAfterCaseClauseWithGuardWithoutPattern() {
doTest();
}
// PY-48014
public void testOnlyCaretMoveAfterCaseClauseWithPatternAndColonWithoutGuardCondition() {
doTest();
}
// PY-48014
public void testColonAfterCaseClauseWithoutPatternWithoutGuardCondition() {
doTest();
}
// PY-48014
public void testColonAfterCaseClauseWithoutPatternWithGuardCondition() {
doTest();
}
}