From 45cb54cacf50fb72305b949c69896662a9a3d1d3 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 8 Apr 2014 22:06:57 +0400 Subject: [PATCH] PY-12565 Django: default template names for generic class-based views are not resolved --- .../nameResolver/NameResolverTools.java | 18 +++++++++++++++ .../src/com/jetbrains/python/psi/PyUtil.java | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/python/psi-api/src/com/jetbrains/python/nameResolver/NameResolverTools.java b/python/psi-api/src/com/jetbrains/python/nameResolver/NameResolverTools.java index a8929fd440fa..aaf32760898e 100644 --- a/python/psi-api/src/com/jetbrains/python/nameResolver/NameResolverTools.java +++ b/python/psi-api/src/com/jetbrains/python/nameResolver/NameResolverTools.java @@ -19,6 +19,23 @@ public final class NameResolverTools { } + /** + * For each provided element checks if FQ element name is one of provided names + * + * @param elements element to check + * @param namesProviders some enum that has one or more names + * @return true if element's fqn is one of names, provided by provider + */ + public static boolean isElementWithName(@NotNull final Collection elements, + @NotNull final FQNamesProvider... namesProviders) { + for (final PyElement element : elements) { + if (isName(element, namesProviders)) { + return true; + } + } + return false; + } + /** * Checks if FQ element name is one of provided names * @@ -44,6 +61,7 @@ public final class NameResolverTools { /** * Returns set of names all providers provide + * * @param providers providers to check * @return set of names */ diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index ec2ac31cb5ea..7ea85fbea186 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -804,6 +804,29 @@ public class PyUtil { return LanguageLevel.getDefault(); } + /** + * Clone of C# "as" operator. + * Checks if expression has correct type and casts it if it has. Returns null otherwise. + * It saves coder from "instanceof / cast" chains. + * + * @param expression expression to check + * @param clazz class to cast + * @param class to cast + * @return expression casted to appropriate type (if could be casted). Null otherwise. + */ + @Nullable + @SuppressWarnings("unchecked") + public static T as(@Nullable final Object expression, @NotNull final Class clazz) { + if (expression == null) { + return null; + } + if (clazz.isAssignableFrom(expression.getClass())) { + return (T)expression; + } + return null; + + } + public static class KnownDecoratorProviderHolder { public static PyKnownDecoratorProvider[] KNOWN_DECORATOR_PROVIDERS = Extensions.getExtensions(PyKnownDecoratorProvider.EP_NAME);