From 88c5b7958ee08d8cba3ed8fd63efc96aa032bf9c Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Wed, 20 Aug 2014 17:21:30 +0400 Subject: [PATCH] Extracted getUnionType() and getParameterizedType() --- .../codeInsight/PyTypingTypeProvider.java | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java b/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java index ea573ebdd5e3..2e7c3f3bece3 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java @@ -72,38 +72,34 @@ public class PyTypingTypeProvider extends PyTypeProviderBase { @Nullable private static PyType getTypingType(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { + final PyType unionType = getUnionType(expression, context); + if (unionType != null) { + return unionType; + } + final PyType parameterizedType = getParameterizedType(expression, context); + if (parameterizedType != null) { + return parameterizedType; + } + final PyType builtinCollection = getBuiltinCollection(expression, context); + if (builtinCollection != null) { + return builtinCollection; + } + final PyType genericType = getGenericType(expression, context); + if (genericType != null) { + return genericType; + } + return null; + } + + @Nullable + private static PyType getUnionType(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { if (expression instanceof PySubscriptionExpression) { final PySubscriptionExpression subscriptionExpr = (PySubscriptionExpression)expression; final PyExpression operand = subscriptionExpr.getOperand(); - final PyExpression indexExpr = subscriptionExpr.getIndexExpression(); final String operandName = resolveToQualifiedName(operand, context); if ("typing.Union".equals(operandName)) { return PyUnionType.union(getIndexTypes(subscriptionExpr, context)); } - else { - final PyType operandType = getType(operand, context); - if (operandType instanceof PyClassType) { - final PyClass cls = ((PyClassType)operandType).getPyClass(); - if (PyNames.TUPLE.equals(cls.getQualifiedName())) { - final List indexTypes = getIndexTypes(subscriptionExpr, context); - return PyTupleType.create(expression, indexTypes.toArray(new PyType[indexTypes.size()])); - } - else if (indexExpr != null) { - final PyType indexType = context.getType(indexExpr); - return new PyCollectionTypeImpl(cls, false, indexType); - } - } - } - } - else { - final PyType builtinCollection = getBuiltinCollection(expression, context); - if (builtinCollection != null) { - return builtinCollection; - } - final PyType genericType = getGenericType(expression, context); - if (genericType != null) { - return genericType; - } } return null; } @@ -174,6 +170,28 @@ public class PyTypingTypeProvider extends PyTypeProviderBase { return types; } + @Nullable + private static PyType getParameterizedType(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { + if (expression instanceof PySubscriptionExpression) { + final PySubscriptionExpression subscriptionExpr = (PySubscriptionExpression)expression; + final PyExpression operand = subscriptionExpr.getOperand(); + final PyExpression indexExpr = subscriptionExpr.getIndexExpression(); + final PyType operandType = getType(operand, context); + if (operandType instanceof PyClassType) { + final PyClass cls = ((PyClassType)operandType).getPyClass(); + if (PyNames.TUPLE.equals(cls.getQualifiedName())) { + final List indexTypes = getIndexTypes(subscriptionExpr, context); + return PyTupleType.create(expression, indexTypes.toArray(new PyType[indexTypes.size()])); + } + else if (indexExpr != null) { + final PyType indexType = context.getType(indexExpr); + return new PyCollectionTypeImpl(cls, false, indexType); + } + } + } + return null; + } + @Nullable private static PyType getBuiltinCollection(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { final String collectionName = resolveToQualifiedName(expression, context);