PY-48039 Add keyword completion for PEP-634 match statements

GitOrigin-RevId: 94c09f53819f022228d7ec9623a027fe598d62e6
This commit is contained in:
Mikhail Golubev
2021-06-23 13:40:18 +00:00
committed by intellij-monorepo-bot
parent 9a70b43f71
commit 06edb38202
20 changed files with 113 additions and 13 deletions
@@ -21,10 +21,10 @@ import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.PythonLanguage;
import com.jetbrains.python.codeInsight.PyUnindentingInsertHandler;
import com.jetbrains.python.codeInsight.mlcompletion.PyCompletionMlElementInfo;
import com.jetbrains.python.codeInsight.mlcompletion.PyCompletionMlElementKind;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.codeInsight.mlcompletion.PyCompletionMlElementInfo;
import com.jetbrains.python.codeInsight.mlcompletion.PyCompletionMlElementKind;
import com.jetbrains.python.documentation.doctest.PyDocstringFile;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NonNls;
@@ -371,7 +371,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor implem
private static final FilterPattern PY3K = new FilterPattern(new PyKeywordCompletionContributor.LanguageLevelAtLeastFilter(LanguageLevel.PYTHON30));
private static final FilterPattern PY35 = new FilterPattern(new LanguageLevelAtLeastFilter(LanguageLevel.PYTHON35));
private static final FilterPattern PY310 = new FilterPattern(new LanguageLevelAtLeastFilter(LanguageLevel.PYTHON310));
// ======
@@ -392,18 +392,19 @@ public class PyKeywordCompletionContributor extends CompletionContributor implem
}
private void addPreColonStatements() {
PsiElementPattern.Capture<PsiElement> startOfLine = psiElement()
.withLanguage(PythonLanguage.getInstance())
.and(FIRST_ON_LINE)
.andNot(IN_IMPORT_STMT)
.andNot(IN_PARAM_LIST)
.andNot(IN_ARG_LIST)
.andNot(BEFORE_COND)
.andNot(AFTER_QUALIFIER)
.andNot(IN_STRING_LITERAL);
extend(
CompletionType.BASIC,
psiElement()
.withLanguage(PythonLanguage.getInstance())
.and(FIRST_ON_LINE)
//.andNot(RIGHT_AFTER_COLON)
.andNot(IN_IMPORT_STMT)
.andNot(IN_PARAM_LIST)
.andNot(IN_ARG_LIST)
.andNot(BEFORE_COND)
.andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
,
startOfLine,
new CompletionProvider<>() {
@Override
protected void addCompletions(
@@ -416,6 +417,21 @@ public class PyKeywordCompletionContributor extends CompletionContributor implem
}
}
);
extend(
CompletionType.BASIC,
startOfLine.and(PY310),
new CompletionProvider<>() {
@Override
protected void addCompletions(
@NotNull final CompletionParameters parameters,
@NotNull final ProcessingContext context,
@NotNull final CompletionResultSet result
) {
putKeywords(result, TailType.NONE, PyNames.MATCH);
}
}
);
}
private void addStatements() {
@@ -477,6 +493,16 @@ public class PyKeywordCompletionContributor extends CompletionContributor implem
);
}
private void addCase() {
extend(
CompletionType.BASIC, psiElement()
.withLanguage(PythonLanguage.getInstance())
.and(PY310)
.and(IN_BEGIN_STMT)
.and(psiElement().withSuperParent(4, PyMatchStatement.class)),
new PyKeywordCompletionProvider(TailType.NONE, PyNames.CASE));
}
private void addWithinFuncs() {
extend(
CompletionType.BASIC, psiElement()
@@ -756,6 +782,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor implem
addElse();
addBreak();
addContinue();
addCase();
addWithinFuncs();
addWithinTry();
addInfixOperators();
@@ -0,0 +1,2 @@
match 42:
case
@@ -0,0 +1,2 @@
match 42:
cas<caret>
@@ -0,0 +1,2 @@
def func():
match
@@ -0,0 +1,2 @@
def func():
mat<caret>
@@ -0,0 +1 @@
match
@@ -0,0 +1 @@
mat<caret>
@@ -0,0 +1,2 @@
match 42:
cas<caret>
@@ -0,0 +1,2 @@
match 42:
cas<caret>
@@ -0,0 +1 @@
cas<caret>
@@ -0,0 +1 @@
cas<caret>
@@ -0,0 +1,2 @@
def func(p):
p.ma<caret>
@@ -0,0 +1,2 @@
def func(p):
p.ma<caret>
@@ -0,0 +1 @@
mat<caret>
@@ -0,0 +1 @@
mat<caret>
@@ -0,0 +1,2 @@
matches = True
if matches
@@ -0,0 +1,2 @@
matches = True
if mat<caret>
@@ -0,0 +1 @@
print(ma<caret>)
@@ -0,0 +1 @@
print(ma<caret>)
@@ -258,4 +258,49 @@ public class PythonKeywordCompletionTest extends PyTestCase {
public void testNoneInReturnAnnotation() {
doTest();
}
// PY-48039
public void testMatchInsideFunction() {
doTest();
}
// PY-48039
public void testMatchOnTopLevel() {
doTest();
}
// PY-48039
public void testNoMatchInsideArgumentList() {
doTest();
}
// PY-48039
public void testNoMatchInCondition() {
doTest();
}
// PY-48039
public void testNoMatchAfterQualifier() {
doTest();
}
// PY-48039
public void testNoMatchBefore310() {
runWithLanguageLevel(LanguageLevel.PYTHON39, this::doTest);
}
// PY-48039
public void testCaseInsideMatchStatement() {
doTest();
}
// PY-48039
public void testNoCaseBefore310() {
runWithLanguageLevel(LanguageLevel.PYTHON39, this::doTest);
}
// PY-48039
public void testNoCaseOutsideMatchStatement() {
doTest();
}
}