if is None and if is not None postfix templates added
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 19 Jan 2016 13:13:05 +0000 (16:13 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 11 Feb 2016 20:36:59 +0000 (23:36 +0300)
(cherry picked from commit f46145d)

28 files changed:
python/resources/postfixTemplates/PyIsNonePostfixTemplate/after.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIsNonePostfixTemplate/before.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIsNonePostfixTemplate/description.html [new file with mode: 0644]
python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/after.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/before.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/description.html [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyBinaryConditionSurrounder.java [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyIsNonePostfixTemplate.java [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyIsNotNonePostfixTemplate.java [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
python/testData/postfix/isNone/complexExpression.py [new file with mode: 0644]
python/testData/postfix/isNone/complexExpression_after.py [new file with mode: 0644]
python/testData/postfix/isNone/function.py [new file with mode: 0644]
python/testData/postfix/isNone/function_after.py [new file with mode: 0644]
python/testData/postfix/isNone/nonApplicable.py [new file with mode: 0644]
python/testData/postfix/isNone/nonApplicable_after.py [new file with mode: 0644]
python/testData/postfix/isNone/topLevel.py [new file with mode: 0644]
python/testData/postfix/isNone/topLevel_after.py [new file with mode: 0644]
python/testData/postfix/isNotNone/complexExpression.py [new file with mode: 0644]
python/testData/postfix/isNotNone/complexExpression_after.py [new file with mode: 0644]
python/testData/postfix/isNotNone/function.py [new file with mode: 0644]
python/testData/postfix/isNotNone/function_after.py [new file with mode: 0644]
python/testData/postfix/isNotNone/nonApplicable.py [new file with mode: 0644]
python/testData/postfix/isNotNone/nonApplicable_after.py [new file with mode: 0644]
python/testData/postfix/isNotNone/topLevel.py [new file with mode: 0644]
python/testData/postfix/isNotNone/topLevel_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/postfix/PyIsNonePostfixTemplateTest.java [new file with mode: 0644]
python/testSrc/com/jetbrains/python/postfix/PyIsNotNonePostfixTemplateTest.java [new file with mode: 0644]

diff --git a/python/resources/postfixTemplates/PyIsNonePostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyIsNonePostfixTemplate/after.py.template
new file mode 100644 (file)
index 0000000..5c47a3a
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+  if a is None:
+    <spot></spot>
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIsNonePostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyIsNonePostfixTemplate/before.py.template
new file mode 100644 (file)
index 0000000..c0b3376
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+  <spot>a</spot>.ifn
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIsNonePostfixTemplate/description.html b/python/resources/postfixTemplates/PyIsNonePostfixTemplate/description.html
new file mode 100644 (file)
index 0000000..f971801
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<body>
+Checks if the expression is None.
+</body>
+</html>
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/after.py.template
new file mode 100644 (file)
index 0000000..d644ebf
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+  if a is not None:
+    <spot></spot>
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/before.py.template
new file mode 100644 (file)
index 0000000..229f7fc
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+  <spot>a</spot>.ifnn
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/description.html b/python/resources/postfixTemplates/PyIsNotNonePostfixTemplate/description.html
new file mode 100644 (file)
index 0000000..650a5b3
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<body>
+Checks if the expression if not None.
+</body>
+</html>
\ No newline at end of file
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyBinaryConditionSurrounder.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyBinaryConditionSurrounder.java
new file mode 100644 (file)
index 0000000..c118908
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2000-2016 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.codeInsight.postfix;
+
+import com.jetbrains.python.psi.*;
+import org.jetbrains.annotations.Nullable;
+
+public class PyBinaryConditionSurrounder extends PyExpressionAsConditionSurrounder {
+
+  private String myTextToGenerate;
+  private String myTemplateDescription;
+
+  public PyBinaryConditionSurrounder(String textToGenerate, String templateDescription) {
+    myTextToGenerate = textToGenerate;
+    myTemplateDescription = templateDescription;
+  }
+
+  @Override
+  protected String getTextToGenerate() {
+    return myTextToGenerate;
+  }
+
+  @Nullable
+  @Override
+  protected PyExpression getCondition(PyStatement statement) {
+    if (statement instanceof PyIfStatement) {
+      PyIfStatement ifStatement = (PyIfStatement)statement;
+      PyExpression condition = ifStatement.getIfPart().getCondition();
+      if (condition == null) {
+        return null;
+      }
+      return ((PyBinaryExpression)condition).getLeftExpression();
+    }
+    return null;
+  }
+
+  @Nullable
+  @Override
+  protected PyStatementListContainer getStatementListContainer(PyStatement statement) {
+    if (statement instanceof PyIfStatement) {
+      return ((PyIfStatement)statement).getIfPart();
+    }
+    return null;
+  }
+
+  @Override
+  public String getTemplateDescription() {
+    return myTemplateDescription;
+  }
+}
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyIsNonePostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyIsNonePostfixTemplate.java
new file mode 100644 (file)
index 0000000..11727ec
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2016 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.codeInsight.postfix;
+
+import com.intellij.codeInsight.template.postfix.templates.SurroundPostfixTemplateBase;
+import com.intellij.lang.surroundWith.Surrounder;
+import org.jetbrains.annotations.NotNull;
+
+public class PyIsNonePostfixTemplate extends SurroundPostfixTemplateBase {
+
+  public static final String TEMPLATE_DESCRIPTION = "if expr is None";
+
+  public PyIsNonePostfixTemplate() {
+    super("ifn", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
+  }
+
+  @NotNull
+  @Override
+  protected Surrounder getSurrounder() {
+    return new PyBinaryConditionSurrounder("if expr is None:\n pass", TEMPLATE_DESCRIPTION);
+  }
+}
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyIsNotNonePostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyIsNotNonePostfixTemplate.java
new file mode 100644 (file)
index 0000000..6a12ab6
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2016 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.codeInsight.postfix;
+
+import com.intellij.codeInsight.template.postfix.templates.SurroundPostfixTemplateBase;
+import com.intellij.lang.surroundWith.Surrounder;
+import org.jetbrains.annotations.NotNull;
+
+public class PyIsNotNonePostfixTemplate extends SurroundPostfixTemplateBase {
+
+  public static final String TEMPLATE_DESCRIPTION = "if expr is not None";
+
+  public PyIsNotNonePostfixTemplate() {
+    super("ifnn", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
+  }
+
+  @NotNull
+  @Override
+  protected Surrounder getSurrounder() {
+    return new PyBinaryConditionSurrounder("if a is not None: \n pass", TEMPLATE_DESCRIPTION);
+  }
+}
index 4531f6a1d75bf80faab851c25dc634ac9ea987b1..6cbceea1a1b7978e6afbf1afd4f47dc04191e2b1 100644 (file)
@@ -32,7 +32,9 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
                                                      new PyParenthesizedExpressionPostfixTemplate(),
                                                      new PyReturnPostfixTemplate(),
                                                      new PyIfPostfixTemplate(),
-                                                     new PyWhilePostfixTemplate());
+                                                     new PyWhilePostfixTemplate(),
+                                                     new PyIsNonePostfixTemplate(),
+                                                     new PyIsNotNonePostfixTemplate());
   }
 
   @Override
diff --git a/python/testData/postfix/isNone/complexExpression.py b/python/testData/postfix/isNone/complexExpression.py
new file mode 100644 (file)
index 0000000..5730e07
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a, b, c):
+    (a + b) * c.ifn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/complexExpression_after.py b/python/testData/postfix/isNone/complexExpression_after.py
new file mode 100644 (file)
index 0000000..b1016a3
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a, b, c):
+    if (a + b) * c is None:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/function.py b/python/testData/postfix/isNone/function.py
new file mode 100644 (file)
index 0000000..1126d12
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+    a.ifn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/function_after.py b/python/testData/postfix/isNone/function_after.py
new file mode 100644 (file)
index 0000000..a201899
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+    if a is None:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/nonApplicable.py b/python/testData/postfix/isNone/nonApplicable.py
new file mode 100644 (file)
index 0000000..adb9eb0
--- /dev/null
@@ -0,0 +1 @@
+a = 1.ifn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/nonApplicable_after.py b/python/testData/postfix/isNone/nonApplicable_after.py
new file mode 100644 (file)
index 0000000..644ea62
--- /dev/null
@@ -0,0 +1 @@
+a = 1.ifn   <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/topLevel.py b/python/testData/postfix/isNone/topLevel.py
new file mode 100644 (file)
index 0000000..99d64dc
--- /dev/null
@@ -0,0 +1 @@
+True.ifn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNone/topLevel_after.py b/python/testData/postfix/isNone/topLevel_after.py
new file mode 100644 (file)
index 0000000..32b2dcc
--- /dev/null
@@ -0,0 +1,2 @@
+if True is None:
+    <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/complexExpression.py b/python/testData/postfix/isNotNone/complexExpression.py
new file mode 100644 (file)
index 0000000..78fbcc6
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a, b, c):
+    (a + b) * c.ifnn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/complexExpression_after.py b/python/testData/postfix/isNotNone/complexExpression_after.py
new file mode 100644 (file)
index 0000000..3fdffb0
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a, b, c):
+    if (a + b) * c is not None:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/function.py b/python/testData/postfix/isNotNone/function.py
new file mode 100644 (file)
index 0000000..170f43d
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+    a.ifnn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/function_after.py b/python/testData/postfix/isNotNone/function_after.py
new file mode 100644 (file)
index 0000000..230ae83
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+    if a is not None:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/nonApplicable.py b/python/testData/postfix/isNotNone/nonApplicable.py
new file mode 100644 (file)
index 0000000..5cc97fe
--- /dev/null
@@ -0,0 +1 @@
+a = 1.ifnn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/nonApplicable_after.py b/python/testData/postfix/isNotNone/nonApplicable_after.py
new file mode 100644 (file)
index 0000000..ec30aff
--- /dev/null
@@ -0,0 +1 @@
+a = 1.ifnn  <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/topLevel.py b/python/testData/postfix/isNotNone/topLevel.py
new file mode 100644 (file)
index 0000000..005269a
--- /dev/null
@@ -0,0 +1 @@
+True.ifnn<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/isNotNone/topLevel_after.py b/python/testData/postfix/isNotNone/topLevel_after.py
new file mode 100644 (file)
index 0000000..3610039
--- /dev/null
@@ -0,0 +1,2 @@
+if True is not None:
+    <caret>
\ No newline at end of file
diff --git a/python/testSrc/com/jetbrains/python/postfix/PyIsNonePostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyIsNonePostfixTemplateTest.java
new file mode 100644 (file)
index 0000000..2c9d514
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2000-2016 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.postfix;
+
+public class PyIsNonePostfixTemplateTest extends PyPostfixTemplateTestCase {
+
+  public void testTopLevel() {
+    doTest();
+  }
+
+  public void testFunction() {
+    doTest();
+  }
+
+  public void testNonApplicable() {
+    doTest();
+  }
+
+  public void testComplexExpression() {
+    doTest();
+  }
+
+  @Override
+  protected String getTestDataDir() {
+    return "isNone/";
+  }
+}
diff --git a/python/testSrc/com/jetbrains/python/postfix/PyIsNotNonePostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyIsNotNonePostfixTemplateTest.java
new file mode 100644 (file)
index 0000000..6b2a580
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2000-2016 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.postfix;
+
+public class PyIsNotNonePostfixTemplateTest extends PyPostfixTemplateTestCase{
+
+  public void testTopLevel() {
+    doTest();
+  }
+
+  public void testFunction() {
+    doTest();
+  }
+
+  public void testNonApplicable() {
+    doTest();
+  }
+
+  public void testComplexExpression() {
+    doTest();
+  }
+
+  @Override
+  protected String getTestDataDir() {
+    return "isNotNone/";
+  }
+}