fixed PY-4540 Completion: else keyword should be available in try block only after...
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Tue, 29 Oct 2013 14:03:40 +0000 (18:03 +0400)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Tue, 29 Oct 2013 14:03:40 +0000 (18:03 +0400)
python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java
python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java

index bb18d89d230272d93f7a01d25c7dd77f6ed32f63..1344f3daa02f920b4dde0536c5b9433a209947af 100644 (file)
@@ -310,7 +310,8 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
   private static final PsiElementPattern.Capture<PsiElement> IN_EXCEPT_BODY =
     psiElement().inside(psiElement(PyStatementList.class).inside(psiElement(PyExceptPart.class)));
 
-  private static final PsiElementPattern.Capture<PsiElement> AFTER_IF = afterStatement(psiElement(PyIfStatement.class).withLastChild(psiElement(PyIfPart.class)));
+  private static final PsiElementPattern.Capture<PsiElement> AFTER_IF = afterStatement(psiElement(PyIfStatement.class).withLastChild(
+    psiElement(PyIfPart.class)));
   private static final PsiElementPattern.Capture<PsiElement> AFTER_TRY = afterStatement(psiElement(PyTryExceptStatement.class));
 
   private static final PsiElementPattern.Capture<PsiElement> AFTER_LOOP_NO_ELSE =
@@ -325,8 +326,8 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
                                     .afterSiblingSkipping(psiElement().whitespaceCommentEmptyOrError(), statementPattern));
   }
 
-  private static final PsiElementPattern.Capture<PsiElement> AFTER_TRY_NO_ELSE = afterStatement(
-    psiElement().withChild(psiElement(PyTryPart.class)).withLastChild(StandardPatterns.not(psiElement(PyElsePart.class)))
+  private static final PsiElementPattern.Capture<PsiElement> AFTER_EXCEPT = afterStatement(
+    psiElement().withLastChild(psiElement(PyExceptPart.class))
   );
 
   private static final PsiElementPattern.Capture<PsiElement> IN_FINALLY_NO_LOOP =
@@ -500,8 +501,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor {
       CompletionType.BASIC, psiElement()
       .withLanguage(PythonLanguage.getInstance())
       .and(FIRST_ON_LINE)
-      .andOr(IN_COND_STMT, IN_TRY_BODY, IN_EXCEPT_BODY, AFTER_COND_STMT_NO_ELSE, AFTER_LOOP_NO_ELSE, AFTER_TRY_NO_ELSE)
-        //.andNot(RIGHT_AFTER_COLON)
+      .andOr(IN_COND_STMT, IN_EXCEPT_BODY, AFTER_COND_STMT_NO_ELSE, AFTER_LOOP_NO_ELSE, AFTER_EXCEPT)
       .andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
       ,
       new PyKeywordCompletionProvider(TailType.CASE_COLON, UnindentingInsertHandler.INSTANCE, "else"));
index 6e9f454592912ba9f7606acd5c7bde3af5e95610..241730f39f80e26b727ecef9c507a54d66568e5e 100644 (file)
@@ -122,6 +122,14 @@ public class PythonKeywordCompletionTest extends PyTestCase {
     assertDoesntContain(lookupElementStrings, "elif");
   }
 
+  public void testNoElseBeforeExcept() {
+    final List<String> lookupElementStrings = doTestByText("try:\n" +
+                                              "  a = 1\n" +
+                                              "<caret>");
+    assertNotNull(lookupElementStrings);
+    assertDoesntContain(lookupElementStrings, "else");
+  }
+
   public void testElseInCondExpr() {  // PY-2397
     doTest();
   }