diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyAnnotateVariableTypeIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyAnnotateVariableTypeIntention.java index 94f0c7d4f45a..f0bb668bf60c 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyAnnotateVariableTypeIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyAnnotateVariableTypeIntention.java @@ -1,6 +1,7 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.codeInsight.intentions; +import com.intellij.codeInsight.TargetElementUtil; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectFileIndex; @@ -60,7 +61,8 @@ public class PyAnnotateVariableTypeIntention extends PyBaseIntentionAction { @NotNull private static List findSuitableTargetsUnderCaret(@NotNull Project project, Editor editor, PsiFile file) { - final PyReferenceOwner elementAtCaret = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), + final int offset = TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()); + final PyReferenceOwner elementAtCaret = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PyReferenceExpression.class, PyTargetExpression.class); if (elementAtCaret == null) { return Collections.emptyList(); diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd.py new file mode 100644 index 000000000000..582a03a0fe76 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd.py @@ -0,0 +1 @@ +var = 42 \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd_after.py new file mode 100644 index 000000000000..9d46ae8fb48c --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtDefinitionNameEnd_after.py @@ -0,0 +1 @@ +var: [int] = 42 \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd.py new file mode 100644 index 000000000000..7459113dbfc4 --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd.py @@ -0,0 +1,2 @@ +var = 42 +var \ No newline at end of file diff --git a/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd_after.py b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd_after.py new file mode 100644 index 000000000000..1c954963aa0f --- /dev/null +++ b/python/testData/intentions/PyAnnotateVariableTypeIntentionTest/annotationCaretAtReferenceEnd_after.py @@ -0,0 +1,2 @@ +var: [int] = 42 +var \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java index 1e3cc65a4d6c..044a0b653b6d 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateVariableTypeIntentionTest.java @@ -191,6 +191,14 @@ public class PyAnnotateVariableTypeIntentionTest extends PyIntentionTestCase { doNegativeTest(); } + public void testAnnotationCaretAtReferenceEnd() { + doAnnotationTest(); + } + + public void testAnnotationCaretAtDefinitionNameEnd() { + doAnnotationTest(); + } + private void doAnnotationTest() { doTest(LanguageLevel.PYTHON36); }