diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index a0bdbff59ac7..6cdce0a5a0e9 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -9,7 +9,6 @@ import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.lang.ASTFactory; 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; @@ -38,6 +37,7 @@ import com.intellij.util.containers.HashSet; import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyNames; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil; import com.jetbrains.python.codeInsight.stdlib.PyNamedTupleType; import com.jetbrains.python.documentation.EpydocUtil; @@ -437,29 +437,23 @@ public class PyUtil { if (!(target instanceof PyTargetExpression)) { return false; } - PyFunction method = PsiTreeUtil.getParentOfType(target, PyFunction.class); - if (method == null || method.getContainingClass() == null) { - return false; + final ScopeOwner owner = ScopeUtil.getScopeOwner(target); + if (owner instanceof PyFunction) { + final PyFunction method = (PyFunction)owner; + if (method.getContainingClass() != null) { + final PyParameter[] params = method.getParameterList().getParameters(); + if (params.length > 0) { + final PyTargetExpression targetExpr = (PyTargetExpression)target; + final PyExpression qualifier = targetExpr.getQualifier(); + return qualifier != null && qualifier.getText().equals(params[0].getName()); + } + } } - final PyParameter[] params = method.getParameterList().getParameters(); - if (params.length == 0) { - return false; - } - final PyTargetExpression targetExpr = (PyTargetExpression)target; - PyExpression qualifier = targetExpr.getQualifier(); - return qualifier != null && qualifier.getText().equals(params[0].getName()); + return false; } public static boolean isClassAttribute(PsiElement element) { - PyAssignmentStatement statement = PsiTreeUtil.getParentOfType(element, PyAssignmentStatement.class); - if (statement == null) { - return false; - } - PyStatementList stmtList = PsiTreeUtil.getParentOfType(statement, PyStatementList.class); - if (stmtList == null || !(stmtList.getParent() instanceof PyClass)) { - return false; - } - return PsiTreeUtil.isAncestor(statement.getLeftHandSideExpression(), element, false); + return element instanceof PyTargetExpression && ScopeUtil.getScopeOwner(element) instanceof PyClass; } public static boolean isDocString(PyExpression expression) { diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index 5856cb7a5a44..d7816af54147 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -17,6 +17,7 @@ import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.*; import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; +import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.resolve.PyResolveContext; import com.jetbrains.python.psi.resolve.PyResolveUtil; @@ -1078,7 +1079,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement @NotNull @Override public SearchScope getUseScope() { - final ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(this, ScopeOwner.class); + final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(this); if (scopeOwner instanceof PyFunction) { return new LocalSearchScope(scopeOwner); } diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java index 51a444988de4..89b57c320d51 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java @@ -21,6 +21,7 @@ import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonDocStringFinder; import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; +import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil; import com.jetbrains.python.documentation.StructuredDocString; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.resolve.QualifiedNameFinder; @@ -505,7 +506,7 @@ public class PyFunctionImpl extends PyPresentableElementImpl imp @NotNull @Override public SearchScope getUseScope() { - final ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(this, ScopeOwner.class); + final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(this); if (scopeOwner instanceof PyFunction) { return new LocalSearchScope(scopeOwner); } diff --git a/python/src/com/jetbrains/python/psi/impl/references/PyReferenceImpl.java b/python/src/com/jetbrains/python/psi/impl/references/PyReferenceImpl.java index 521f0e2e7f67..45ef257c07b3 100644 --- a/python/src/com/jetbrains/python/psi/impl/references/PyReferenceImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/references/PyReferenceImpl.java @@ -419,10 +419,14 @@ public class PyReferenceImpl implements PsiReferenceEx, PsiPolyVariantReference return element; } - private static boolean isGlobal(PsiElement anchor, String name) { - final ScopeOwner owner = ScopeUtil.getDeclarationScopeOwner(anchor, name); - if (owner != null) { - return ControlFlowCache.getScope(owner).isGlobal(name); + private static boolean isGlobal(PsiElement element, String name) { + final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(element); + if (scopeOwner instanceof PyFile) { + return true; + } + final ScopeOwner declarationScopeOwner = ScopeUtil.getDeclarationScopeOwner(element, name); + if (declarationScopeOwner != null) { + return ControlFlowCache.getScope(declarationScopeOwner).isGlobal(name); } return false; } @@ -535,15 +539,16 @@ public class PyReferenceImpl implements PsiReferenceEx, PsiPolyVariantReference private boolean resolvesToSameGlobal(PsiElement element, String elementName, ScopeOwner ourScopeOwner, ScopeOwner theirScopeOwner, PsiElement resolveResult) { // Handle situations when there is no top-level declaration for globals and transitive resolve doesn't help - final boolean ourIsGlobal = ControlFlowCache.getScope(ourScopeOwner).isGlobal(elementName); - final boolean theirIsGlobal = ControlFlowCache.getScope(theirScopeOwner).isGlobal(elementName); final PsiFile ourFile = getElement().getContainingFile(); final PsiFile theirFile = element.getContainingFile(); - - if (ourIsGlobal && theirIsGlobal && ourFile == theirFile) { - return true; + if (ourFile == theirFile) { + final boolean ourIsGlobal = ControlFlowCache.getScope(ourScopeOwner).isGlobal(elementName); + final boolean theirIsGlobal = ControlFlowCache.getScope(theirScopeOwner).isGlobal(elementName); + if (ourIsGlobal && theirIsGlobal) { + return true; + } } - if (theirIsGlobal && ScopeUtil.getScopeOwner(resolveResult) == ourFile) { + if (ScopeUtil.getScopeOwner(resolveResult) == ourFile && ControlFlowCache.getScope(theirScopeOwner).isGlobal(elementName)) { return true; } return false; diff --git a/python/src/com/jetbrains/python/psi/resolve/CompletionVariantsProcessor.java b/python/src/com/jetbrains/python/psi/resolve/CompletionVariantsProcessor.java index 894fe51f622e..8c3ca5edb3f5 100644 --- a/python/src/com/jetbrains/python/psi/resolve/CompletionVariantsProcessor.java +++ b/python/src/com/jetbrains/python/psi/resolve/CompletionVariantsProcessor.java @@ -54,7 +54,7 @@ public class CompletionVariantsProcessor extends VariantsProcessor { final String params = StringUtil.join(parameterList.getParameters(), new Function() { @Override public String fun(PyParameter pyParameter) { - return pyParameter.getText(); + return pyParameter.getName(); } }, ", "); item = item.withTailText("(" + params + ")");