Search in sources :

Example 36 with ScopeOwner

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());
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with ScopeOwner

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;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiElement(com.intellij.psi.PsiElement)

Example 38 with ScopeOwner

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;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner)

Example 39 with ScopeOwner

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];
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 40 with ScopeOwner

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;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Instruction(com.intellij.codeInsight.controlflow.Instruction) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow)

Aggregations

ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)51 PsiElement (com.intellij.psi.PsiElement)25 Nullable (org.jetbrains.annotations.Nullable)18 NotNull (org.jetbrains.annotations.NotNull)14 Pair (com.intellij.openapi.util.Pair)6 QualifiedName (com.intellij.psi.util.QualifiedName)6 ArrayList (java.util.ArrayList)6 Instruction (com.intellij.codeInsight.controlflow.Instruction)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)4 List (java.util.List)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 Ref (com.intellij.openapi.util.Ref)3 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)2 ASTNode (com.intellij.lang.ASTNode)2