fixed PY-7091 Specify type intentions are not available when cursor is at the very beginning of the reference with undefined type

This commit is contained in:
Ekaterina Tuzova
2012-08-31 15:39:36 +04:00
parent 0032210fe7
commit 9e42d4d30d
7 changed files with 30 additions and 23 deletions
@@ -43,11 +43,8 @@ public class SpecifyTypeInDocstringIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
PyExpression problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1),
PyNamedParameter.class);
if (problemElement == null)
problemElement = PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1),
PyQualifiedExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyNamedParameter.class, PyQualifiedExpression.class);
if (problemElement == null) return false;
if (problemElement instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression)problemElement).getQualifier();
@@ -85,9 +82,7 @@ public class SpecifyTypeInDocstringIntention implements IntentionAction {
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PyExpression problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1), PyNamedParameter.class);
if (problemElement == null)
problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()-1), PyExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyNamedParameter.class, PyQualifiedExpression.class);
if (problemElement != null) {
String name = problemElement.getName();
PsiReference reference = problemElement.getReference();
@@ -45,11 +45,8 @@ public class SpecifyTypeInPy3AnnotationsIntention implements IntentionAction {
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!LanguageLevel.forElement(file).isPy3K()) return false;
PyExpression problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1),
PyNamedParameter.class);
if (problemElement == null)
problemElement = PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1),
PyQualifiedExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyNamedParameter.class, PyQualifiedExpression.class);
if (problemElement == null) return false;
if (problemElement instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression)problemElement).getQualifier();
@@ -98,9 +95,7 @@ public class SpecifyTypeInPy3AnnotationsIntention implements IntentionAction {
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PyExpression problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1), PyNamedParameter.class);
if (problemElement == null)
problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()-1), PyExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyNamedParameter.class, PyQualifiedExpression.class);
if (problemElement != null) {
String name = problemElement.getName();
PsiReference reference = problemElement.getReference();
@@ -41,8 +41,7 @@ public class TypeAssertionIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
PyExpression problemElement =
PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset()-1), PyQualifiedExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyQualifiedExpression.class);
if (problemElement == null) return false;
if (problemElement instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression)problemElement).getQualifier();
@@ -63,8 +62,7 @@ public class TypeAssertionIntention implements IntentionAction {
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PyExpression problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()-1),
PyExpression.class);
PyExpression problemElement = PyUtil.findProblemElement(editor, file, PyQualifiedExpression.class);
if (problemElement != null) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
@@ -8,6 +8,7 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
@@ -1039,9 +1040,6 @@ public class PyUtil {
else if (expression instanceof PySequenceExpression) {
valuesLength = ((PySequenceExpression)expression).getElements().length;
}
else if (expression instanceof PyDictLiteralExpression) {
valuesLength = ((PyDictLiteralExpression)expression).getElements().length;
}
else if (expression instanceof PyStringLiteralExpression) {
valuesLength = ((PyStringLiteralExpression)expression).getStringValue().length();
}
@@ -1105,5 +1103,18 @@ public class PyUtil {
}
return null;
}
@Nullable
public static <T extends PyExpression> T findProblemElement(Editor editor, PsiFile file, @NotNull final Class<? extends T>... classes) {
for (Class claz : classes) {
PsiElement problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset() - 1), claz);
if (problemElement != null) return (T)problemElement;
problemElement = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), claz);
if (problemElement != null) return (T)problemElement;
}
return null;
}
}
@@ -0,0 +1,3 @@
def foo(a, b):
assert isinstance(a, object)
a.
@@ -0,0 +1 @@
def foo(a, b): <caret>a.
@@ -253,6 +253,10 @@ public class PyIntentionTest extends PyTestCase {
doTest(PyBundle.message("INTN.insert.assertion"));
}
public void testTypeAssertion2() {
doTest(PyBundle.message("INTN.insert.assertion"));
}
public void testDocStub() {
doDocStubTest();
}