From 723180f142c0d5d4f82359fb84f7bd19d52a1c36 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Thu, 8 Nov 2012 16:59:34 +0400 Subject: [PATCH] fixed PY-7971 Insert type assertion: produces invalid assertion in case of caret at the second reference in expression --- .../codeInsight/intentions/TypeAssertionIntention.java | 6 ++++-- python/testData/intentions/afterTypeAssertion4.py | 4 ++++ python/testData/intentions/beforeTypeAssertion4.py | 3 +++ python/testSrc/com/jetbrains/python/PyIntentionTest.java | 7 +++++-- 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 python/testData/intentions/afterTypeAssertion4.py create mode 100644 python/testData/intentions/beforeTypeAssertion4.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/TypeAssertionIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/TypeAssertionIntention.java index c8781e57500f..d3c16fadbb11 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/TypeAssertionIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/TypeAssertionIntention.java @@ -41,7 +41,8 @@ public class TypeAssertionIntention implements IntentionAction { } public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyQualifiedExpression.class); + PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset()); + PyExpression problemElement = PsiTreeUtil.getParentOfType(elementAt, PyQualifiedExpression.class); if (problemElement == null) return false; if (problemElement.getParent() instanceof PyWithItem) return false; if (problemElement instanceof PyQualifiedExpression) { @@ -63,7 +64,8 @@ public class TypeAssertionIntention implements IntentionAction { } public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { - PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyQualifiedExpression.class); + PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset()); + PyExpression problemElement = PsiTreeUtil.getParentOfType(elementAt, PyQualifiedExpression.class); if (problemElement != null) { PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); diff --git a/python/testData/intentions/afterTypeAssertion4.py b/python/testData/intentions/afterTypeAssertion4.py new file mode 100644 index 000000000000..4bc4b1c2bfd9 --- /dev/null +++ b/python/testData/intentions/afterTypeAssertion4.py @@ -0,0 +1,4 @@ +def foo3(x, y): + assert isinstance(y, object) + i = x + y + return i diff --git a/python/testData/intentions/beforeTypeAssertion4.py b/python/testData/intentions/beforeTypeAssertion4.py new file mode 100644 index 000000000000..91d8d2e79927 --- /dev/null +++ b/python/testData/intentions/beforeTypeAssertion4.py @@ -0,0 +1,3 @@ +def foo3(x, y): + i = x + y + return i diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index b3d0a1e82275..7b92102469b9 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -249,8 +249,7 @@ public class PyIntentionTest extends PyTestCase { doDocReferenceTest(); } - - public void testTypeInDocstring6() { + public void testTypeInDocstring6() { //PY-7973 doNegativeTest(PyBundle.message("INTN.specify.return.type")); } @@ -300,6 +299,10 @@ public class PyIntentionTest extends PyTestCase { } } + public void testTypeAssertion4() { //PY-7971 + doTestTypeAssertion(); + } + public void testTypeAnnotation3() { //PY-7087 doTest(PyBundle.message("INTN.specify.type.in.annotation"), LanguageLevel.PYTHON32); }