PY-12344 Views in i18n_patterns are not resolved

This commit is contained in:
Ilya.Kazakevich
2014-03-28 23:41:20 +04:00
parent 6199ff61af
commit 2b867ce58d
7 changed files with 108 additions and 0 deletions
@@ -0,0 +1,15 @@
package com.jetbrains.python.nameResolver;
import org.jetbrains.annotations.NotNull;
/**
* Some enum value that represents one or more fully qualified names for some function
* @author Ilya.Kazakevich
*/
public interface FQNamesProvider {
/**
* @return one or more fully qualified names
*/
@NotNull
String[] getNames();
}
@@ -0,0 +1,40 @@
package com.jetbrains.python.nameResolver;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.util.ArrayUtil;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyQualifiedNameOwner;
import org.jetbrains.annotations.NotNull;
/**
* @author Ilya.Kazakevich
*/
public final class NameResolverTools {
private NameResolverTools() {
}
/**
* Checks if FQ element name is one of provided names
*
* @param element element to check
* @param namesProvider some enum that has one or more names
* @return true if element's fqn is one of names, provided by provider
*/
public static boolean isName(@NotNull final PyElement element, @NotNull final FQNamesProvider namesProvider) {
PyElement elementToCheck = element;
final PsiReference reference = element.getReference();
if (reference != null) {
final PsiElement resolvedElement = reference.resolve();
if (resolvedElement instanceof PyElement) {
elementToCheck = (PyElement)resolvedElement;
}
}
if (elementToCheck instanceof PyQualifiedNameOwner) {
final String qualifiedName = ((PyQualifiedNameOwner)elementToCheck).getQualifiedName();
return ArrayUtil.contains(qualifiedName, namesProvider.getNames());
}
return false;
}
}
@@ -0,0 +1,15 @@
/**
* Some code is bound to specific functions or classes or some other objects somewhere in namespace.
* Say, you want to add reference contributor to "my.package.some_function". You can not just check "callee text" here, because this
* function could be imported "from my.package import some_function as foo" and then used as "foo". So, you need to resolve it first.
*
* Additionally, there are some functions which should be processed identically. patterns() and i18n_patterns() from django is good example.
* So, you need to:
* <ol>
* <li>Create some enum that implements {@link com.jetbrains.python.nameResolver.FQNamesProvider} (see its javadoc for more info)</li>
* <li>Use {@link com.jetbrains.python.nameResolver.NameResolverTools#isName(com.jetbrains.python.psi.PyElement, FQNamesProvider)}
* to check that some element matches some names from that enum</li>
* </ol>
* @author Ilya.Kazakevich
*/
package com.jetbrains.python.nameResolver;
@@ -16,6 +16,7 @@
package com.jetbrains.python.psi;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.nameResolver.FQNamesProvider;
import com.jetbrains.python.psi.resolve.PyResolveContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -110,6 +111,15 @@ public interface PyCallExpression extends PyExpression {
*/
boolean isCalleeText(@NotNull String... nameCandidates);
/**
* Checks if the qualified name of the callee matches any of the specified names provided by provider.
* @see com.jetbrains.python.nameResolver
* @param name provider that provides one or more names to check
* @return true if matches, false otherwise
*/
boolean isCallee(@NotNull FQNamesProvider name);
/**
* Couples function with a flag describing the way it is called.
*/