From bf2f4468b2a652917c9a18bf598dee6969c75afb Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Thu, 25 Jul 2019 20:05:47 +0300 Subject: [PATCH] Complete `continue` in `finally` block in Python 3.8+ (PY-36003) GitOrigin-RevId: 7c378fb7dcee5e78beed1421aaaaed985c7cc765 --- .../PyKeywordCompletionContributor.java | 8 ++--- .../python/PythonCompletionTest.java | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java index 3084a7fb4ae4..40d25380b665 100644 --- a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java +++ b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java @@ -9,7 +9,6 @@ import com.intellij.openapi.editor.Document; import com.intellij.patterns.ElementPattern; import com.intellij.patterns.PatternCondition; import com.intellij.patterns.PsiElementPattern; -import com.intellij.patterns.StandardPatterns; import com.intellij.psi.*; import com.intellij.psi.filters.ElementFilter; import com.intellij.psi.filters.position.FilterPattern; @@ -31,6 +30,7 @@ import org.jetbrains.annotations.Nullable; import static com.intellij.patterns.PlatformPatterns.psiElement; import static com.intellij.patterns.PlatformPatterns.psiFile; +import static com.intellij.patterns.StandardPatterns.not; import static com.intellij.patterns.StandardPatterns.or; /** @@ -323,11 +323,11 @@ public class PyKeywordCompletionContributor extends CompletionContributor { private static final PsiElementPattern.Capture AFTER_TRY = afterStatement(psiElement(PyTryExceptStatement.class)); private static final PsiElementPattern.Capture AFTER_LOOP_NO_ELSE = - afterStatement(psiElement(PyLoopStatement.class).withLastChild(StandardPatterns.not(psiElement(PyElsePart.class)))); + afterStatement(psiElement(PyLoopStatement.class).withLastChild(not(psiElement(PyElsePart.class)))); private static final PsiElementPattern.Capture AFTER_COND_STMT_NO_ELSE = afterStatement(psiElement().withChild(psiElement(PyConditionalStatementPart.class)) - .withLastChild(StandardPatterns.not(psiElement(PyElsePart.class)))); + .withLastChild(not(psiElement(PyElsePart.class)))); private static PsiElementPattern.Capture afterStatement(final PsiElementPattern.Capture statementPattern) { return psiElement().atStartOf(psiElement(PyExpressionStatement.class) @@ -455,7 +455,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor { .andNot(AFTER_QUALIFIER) .andNot(IN_PARAM_LIST) .andNot(IN_ARG_LIST) - .andNot(IN_FINALLY_NO_LOOP) + .andOr(not(IN_FINALLY_NO_LOOP), new FilterPattern(new LanguageLevelAtLeastFilter(LanguageLevel.PYTHON38))) .and(IN_LOOP) , new PyKeywordCompletionProvider(TailType.NONE, PyNames.CONTINUE) diff --git a/python/testSrc/com/jetbrains/python/PythonCompletionTest.java b/python/testSrc/com/jetbrains/python/PythonCompletionTest.java index 8b47addb0e75..f58f50a63f96 100644 --- a/python/testSrc/com/jetbrains/python/PythonCompletionTest.java +++ b/python/testSrc/com/jetbrains/python/PythonCompletionTest.java @@ -1540,6 +1540,41 @@ public class PythonCompletionTest extends PyTestCase { doTest(); } + // PY-36003 + public void testContinueInFinallyBefore38() { + final String text = "for x in []:\n" + + " try:\n" + + " a = 1\n" + + " finally:\n" + + " cont"; + + runWithLanguageLevel( + LanguageLevel.PYTHON37, + () -> { + myFixture.configureByText(PythonFileType.INSTANCE, text + ""); + myFixture.completeBasic(); + myFixture.checkResult(text); + } + ); + } + + // PY-36003 + public void testContinueInFinallyAfter38() { + final String text = "for x in []:\n" + + " try:\n" + + " a = 1\n" + + " finally:\n" + + " cont"; + + runWithLanguageLevel( + LanguageLevel.PYTHON38, + () -> { + myFixture.configureByText(PythonFileType.INSTANCE, text + ""); + myFixture.completeBasic(); + myFixture.checkResult(text + "inue"); + } + ); + } private void assertNoVariantsInExtendedCompletion() { myFixture.copyDirectoryToProject(getTestName(true), "");