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));
+ 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 =
}
}
- private static void putKeyword(
- @NotNull @NonNls String keyword,
- InsertHandler<PythonLookupElement> handler,
- TailType tail,
- CompletionResultSet result) {
- final PythonLookupElement lookup_elt = new PythonLookupElement(keyword, true, null);
- lookup_elt.setHandler(handler);
- result.addElement(TailTypeDecorator.withTail(lookup_elt, tail));
+ private static void putKeyword(@NotNull @NonNls String keyword, InsertHandler<PythonLookupElement> handler, TailType tail,
+ CompletionResultSet result) {
+ final PythonLookupElement lookupElement = new PythonLookupElement(keyword, true, null);
+ lookupElement.setHandler(handler);
+ result.addElement(TailTypeDecorator.withTail(lookupElement, tail));
}
private void addPreColonStatements() {
CompletionType.BASIC, psiElement()
.withLanguage(PythonLanguage.getInstance())
.and(FIRST_ON_LINE)
- .andOr(IN_IF_BODY, AFTER_IF) // NOTE: does allow 'elif' after 'else', may be useful for easier reordering of branches
- //.andNot(RIGHT_AFTER_COLON)
+ .andOr(IN_IF_BODY, AFTER_IF)
.andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL)
,
new PyKeywordCompletionProvider(TailType.NONE, UnindentingInsertHandler.INSTANCE, "elif"));
--- /dev/null
+if True:
+ a = 1
+else:
+ pass
+<caret>
\ No newline at end of file
doTest();
}
- public void testKeywordAfterComment() { // PY-697
- doTest();
- }
-
public void testClassPrivate() {
doTest();
}
doTest();
}
- public void testEmptyFile() { // PY-1845
- myFixture.configureByText(PythonFileType.INSTANCE, "");
- myFixture.completeBasic();
- final List<String> elements = myFixture.getLookupElementStrings();
- assertNotNull(elements);
- assertTrue(elements.contains("import"));
- }
-
public void testImportItself() { // PY-1895
myFixture.copyDirectoryToProject("completion/importItself/package1", "package1");
myFixture.configureFromTempProjectFile("package1/submodule1.py");
doTest();
}
- public void testNonlocal() { // PY-2289
- doTest3K();
- }
-
- public void testYield() {
- doTest();
- }
-
private void doTest3K() {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON30);
try {
doTest();
}
- public void testElse() {
- doTest();
- }
-
- public void testElseNotIndented() {
- doTest();
- }
-
- public void testElseInTryNotIndented() {
- doTest();
- }
-
- public void testElif() {
- doTest();
- }
-
- public void testElifNotIndented() {
- doTest();
- }
-
- public void testExcept() {
- doTest();
- }
-
- public void testExceptNotIndented() {
- doTest();
- }
-
- public void testFinallyInExcept() {
- doTest();
- }
-
- public void testContinue() {
- doTest();
- }
-
- public void testNoContinueInFinally() {
- final String testName = "completion/" + getTestName(true);
- myFixture.configureByFile(testName + ".py");
- myFixture.completeBasic();
- final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
- assertNotNull(lookupElementStrings);
- assertFalse(lookupElementStrings.contains("continue"));
- }
-
- public void testElseInCondExpr() { // PY-2397
- doTest();
- }
-
public void testLocalVarInDictKey() { // PY-2558
doTest();
}
doTest();
}
- public void testFromDotImport() { // PY-2772
- doTest3K();
- }
-
public void testNoIdentifiersInImport() {
doTest();
}
doMultiFileTest();
}
- public void testLambdaInExpression() { // PY-3150
- doTest();
- }
-
public void testVeryPrivate() { // PY-3246
doTest();
}
myFixture.checkResultByFile("completion/identifiersInPlainDocstring.after.py");
}
- public void testNoneInArgList() { // PY-3464
- doTest3K();
- }
-
public void testPep328Completion() { // PY-3409
myFixture.copyDirectoryToProject("completion/pep328", "pep328");
myFixture.configureByFile("pep328/package/subpackage1/moduleX.py");
doTest();
}
- // PY-5144
- public void testImportKeyword() {
- doTest();
- }
-
public void testAssignedNearby() {
doTest();
}
doTest();
}
- public void testAsInWith() { // PY-3701
- setLanguageLevel(LanguageLevel.PYTHON27);
- assertTrue(doTestByText("with open(foo) <caret>").contains("as"));
- }
-
- public void testAsInExcept() { // PY-1846
- setLanguageLevel(LanguageLevel.PYTHON27);
- assertTrue(doTestByText("try:\n" +
- " pass\n" +
- "except IOError <caret>").contains("as"));
- }
-
- public void testElseInFor() { // PY-6755
- assertTrue(doTestByText("for item in range(10):\n" +
- " pass\n" +
- "el<caret>").contains("else"));
- }
-
public void testArgs() { // PY-7208
doTestByText("def foo(*<caret>)");
myFixture.checkResult("def foo(*args)");
--- /dev/null
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python;
+
+import com.intellij.codeInsight.completion.impl.CamelHumpMatcher;
+import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.psi.LanguageLevel;
+import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
+
+import java.util.List;
+
+public class PythonKeywordCompletionTest extends PyTestCase {
+
+ private void doTest3K() {
+ PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON30);
+ try {
+ doTest();
+ }
+ finally {
+ PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), null);
+ }
+ }
+
+ private void doTest() {
+ CamelHumpMatcher.forceStartMatching(getTestRootDisposable());
+ final String testName = "keywordCompletion/" + getTestName(true);
+ myFixture.configureByFile(testName + ".py");
+ myFixture.completeBasic();
+ myFixture.checkResultByFile(testName + ".after.py");
+ }
+
+ private List<String> doTestByText(String text) {
+ myFixture.configureByText(PythonFileType.INSTANCE, text);
+ myFixture.completeBasic();
+ return myFixture.getLookupElementStrings();
+ }
+
+ public void testKeywordAfterComment() { // PY-697
+ doTest();
+ }
+
+ public void testEmptyFile() { // PY-1845
+ myFixture.configureByText(PythonFileType.INSTANCE, "");
+ myFixture.completeBasic();
+ final List<String> elements = myFixture.getLookupElementStrings();
+ assertNotNull(elements);
+ assertTrue(elements.contains("import"));
+ }
+
+ public void testNonlocal() { // PY-2289
+ doTest3K();
+ }
+
+ public void testYield() {
+ doTest();
+ }
+
+ public void testElse() {
+ doTest();
+ }
+
+ public void testElseNotIndented() {
+ doTest();
+ }
+
+ public void testElseInTryNotIndented() {
+ doTest();
+ }
+
+ public void testElif() {
+ doTest();
+ }
+
+ public void testElifNotIndented() {
+ doTest();
+ }
+
+ public void testExcept() {
+ doTest();
+ }
+
+ public void testExceptNotIndented() {
+ doTest();
+ }
+
+ public void testFinallyInExcept() {
+ doTest();
+ }
+
+ public void testContinue() {
+ doTest();
+ }
+
+ public void testNoContinueInFinally() {
+ final String testName = "keywordCompletion/" + getTestName(true);
+ myFixture.configureByFile(testName + ".py");
+ myFixture.completeBasic();
+ final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
+ assertNotNull(lookupElementStrings);
+ assertDoesntContain(lookupElementStrings, "continue");
+ }
+
+ public void testNoElifBeforeElse() {
+ final String testName = "keywordCompletion/" + getTestName(true);
+ myFixture.configureByFile(testName + ".py");
+ myFixture.completeBasic();
+ final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
+ assertNotNull(lookupElementStrings);
+ assertDoesntContain(lookupElementStrings, "elif");
+ }
+
+ public void testElseInCondExpr() { // PY-2397
+ doTest();
+ }
+
+ public void testFromDotImport() { // PY-2772
+ doTest3K();
+ }
+
+ public void testLambdaInExpression() { // PY-3150
+ doTest();
+ }
+
+ public void testNoneInArgList() { // PY-3464
+ doTest3K();
+ }
+
+ // PY-5144
+ public void testImportKeyword() {
+ doTest();
+ }
+
+ public void testAsInWith() { // PY-3701
+ setLanguageLevel(LanguageLevel.PYTHON27);
+ assertTrue(doTestByText("with open(foo) <caret>").contains("as"));
+ }
+
+ public void testAsInExcept() { // PY-1846
+ setLanguageLevel(LanguageLevel.PYTHON27);
+ assertTrue(doTestByText("try:\n" +
+ " pass\n" +
+ "except IOError <caret>").contains("as"));
+ }
+
+ public void testElseInFor() { // PY-6755
+ assertTrue(doTestByText("for item in range(10):\n" +
+ " pass\n" +
+ "el<caret>").contains("else"));
+ }
+}
/**
* @author yole
*/
-@TestDataPath("$CONTENT_ROOT/../../testData/")
+@TestDataPath("$CONTENT_ROOT/../testData/")
public abstract class PyTestCase extends UsefulTestCase {
public static String PYTHON_2_MOCK_SDK = "2.7";
public static String PYTHON_3_MOCK_SDK = "3.2";