Removed unnecessary switches from stubs to AST

This commit is contained in:
Andrey Vlasovskikh
2013-01-14 20:49:21 +04:00
parent 6e9c98cef0
commit 154d1b1daa
5 changed files with 34 additions and 33 deletions
+14 -20
View File
@@ -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) {
@@ -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<PyClassStub> 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);
}
@@ -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<PyFunctionStub> 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);
}
@@ -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;
@@ -54,7 +54,7 @@ public class CompletionVariantsProcessor extends VariantsProcessor {
final String params = StringUtil.join(parameterList.getParameters(), new Function<PyParameter, String>() {
@Override
public String fun(PyParameter pyParameter) {
return pyParameter.getText();
return pyParameter.getName();
}
}, ", ");
item = item.withTailText("(" + params + ")");