use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyMakeMethodTopLevelProcessor method collectNewParameterNames.
@NotNull
@Override
protected List<String> collectNewParameterNames() {
final Set<String> attributeNames = new LinkedHashSet<>();
for (ScopeOwner owner : PsiTreeUtil.collectElementsOfType(myFunction, ScopeOwner.class)) {
final AnalysisResult result = analyseScope(owner);
if (!result.nonlocalWritesToEnclosingScope.isEmpty()) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.nonlocal.writes"));
}
if (!result.readsOfSelfParametersFromEnclosingScope.isEmpty()) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.self.reads"));
}
if (!result.readsFromEnclosingScope.isEmpty()) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.outer.scope.reads"));
}
if (!result.writesToSelfParameter.isEmpty()) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.special.usage.of.self"));
}
myReadsOfSelfParam.addAll(result.readsOfSelfParameter);
for (PsiElement usage : result.readsOfSelfParameter) {
if (usage.getParent() instanceof PyTargetExpression) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.attribute.writes"));
}
final PyReferenceExpression parentReference = as(usage.getParent(), PyReferenceExpression.class);
if (parentReference != null) {
final String attrName = parentReference.getName();
if (attrName != null && PyUtil.isClassPrivateName(attrName)) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.private.attributes"));
}
if (parentReference.getParent() instanceof PyCallExpression) {
if (!(Comparing.equal(myFunction.getName(), parentReference.getName()))) {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.method.calls"));
} else {
// do not add method itself to its parameters
continue;
}
}
attributeNames.add(attrName);
myAttributeReferences.putValue(attrName, parentReference);
} else {
throw new IncorrectOperationException(PyBundle.message("refactoring.make.function.top.level.error.special.usage.of.self"));
}
}
}
for (String name : attributeNames) {
final Collection<PyReferenceExpression> reads = myAttributeReferences.get(name);
final PsiElement anchor = ContainerUtil.getFirstItem(reads);
//noinspection ConstantConditions
if (!PyRefactoringUtil.isValidNewName(name, anchor)) {
final String indexedName = PyRefactoringUtil.appendNumberUntilValid(name, anchor);
myAttributeToParameterName.put(name, indexedName);
} else {
myAttributeToParameterName.put(name, name);
}
}
return Lists.newArrayList(myAttributeToParameterName.values());
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyIntroduceFieldHandler method inConstructor.
private static boolean inConstructor(@NotNull PsiElement expression) {
final PsiElement expr = expression instanceof PyClass ? expression : expression.getParent();
PyClass clazz = PyUtil.getContainingClassOrSelf(expr);
final ScopeOwner current = ScopeUtil.getScopeOwner(expression);
if (clazz != null && current != null && current instanceof PyFunction) {
PyFunction init = clazz.findMethodByName(PyNames.INIT, false, null);
if (current == init) {
return true;
}
}
return false;
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyIntroduceFieldHandler method dependsOnLocalScopeValues.
private static boolean dependsOnLocalScopeValues(PsiElement initializer) {
ScopeOwner scope = PsiTreeUtil.getParentOfType(initializer, ScopeOwner.class);
ResolvingVisitor visitor = new ResolvingVisitor(scope);
initializer.accept(visitor);
return visitor.hasLocalScopeDependencies;
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyIntroduceParameterHandler method isNotDeclared.
private static boolean isNotDeclared(PsiElement element) {
final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(element);
final boolean[] isValid = { true };
if (scopeOwner != null) {
final String name = element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : element.getText();
if (name != null && ControlFlowCache.getScope(scopeOwner).containsDeclaration(name)) {
return false;
}
new PyRecursiveElementVisitor() {
@Override
public void visitPyReferenceExpression(PyReferenceExpression node) {
super.visitPyReferenceExpression(node);
final String name = node.getName();
if (name != null && ControlFlowCache.getScope(scopeOwner).containsDeclaration(name)) {
isValid[0] = false;
}
}
}.visitElement(element);
}
return !isResolvedToParameter(element) && isValid[0];
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyUnreachableCodeInspection method hasAnyInterruptedControlFlowPaths.
public static boolean hasAnyInterruptedControlFlowPaths(@NotNull PsiElement element) {
final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
if (owner != null) {
final ControlFlow flow = ControlFlowCache.getControlFlow(owner);
final Instruction[] instructions = flow.getInstructions();
final int start = ControlFlowUtil.findInstructionNumberByElement(instructions, element);
if (start >= 0) {
final Ref<Boolean> resultRef = Ref.create(false);
ControlFlowUtil.iteratePrev(start, instructions, instruction -> {
if (instruction.allPred().isEmpty() && !isFirstInstruction(instruction)) {
resultRef.set(true);
return ControlFlowUtil.Operation.BREAK;
}
return ControlFlowUtil.Operation.NEXT;
});
return resultRef.get();
}
}
return false;
}
Aggregations