Search in sources :

Example 1 with WalkingState

use of com.intellij.util.WalkingState in project intellij-community by JetBrains.

the class SliceLeafAnalyzer method calcLeafExpressions.

@NotNull
static Collection<PsiElement> calcLeafExpressions(@NotNull final SliceNode root, @NotNull AbstractTreeStructure treeStructure, @NotNull final Map<SliceNode, Collection<PsiElement>> map) {
    final SliceNodeGuide guide = new SliceNodeGuide(treeStructure);
    WalkingState<SliceNode> walkingState = new WalkingState<SliceNode>(guide) {

        @Override
        public void visit(@NotNull final SliceNode element) {
            element.calculateDupNode();
            node(element, map).clear();
            SliceNode duplicate = element.getDuplicate();
            if (duplicate != null) {
                node(element, map).addAll(node(duplicate, map));
            } else {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final SliceUsage sliceUsage = element.getValue();
                    Collection<SliceNode> children = element.getChildren();
                    if (children.isEmpty() && sliceUsage instanceof JavaSliceUsage) {
                        PsiElement value = ((JavaSliceUsage) sliceUsage).indexNesting == 0 ? sliceUsage.getElement() : null;
                        if (value != null) {
                            node(element, map).addAll(ContainerUtil.singleton(value, LEAF_ELEMENT_EQUALITY));
                        }
                    }
                });
                super.visit(element);
            }
        }

        @Override
        public void elementFinished(@NotNull SliceNode element) {
            SliceNode parent = guide.getParent(element);
            if (parent != null) {
                node(parent, map).addAll(node(element, map));
            }
        }
    };
    walkingState.visit(root);
    return node(root, map);
}
Also used : WalkingState(com.intellij.util.WalkingState) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with WalkingState

use of com.intellij.util.WalkingState in project intellij-community by JetBrains.

the class SliceNullnessAnalyzer method calcNullableLeaves.

@NotNull
static NullAnalysisResult calcNullableLeaves(@NotNull final SliceNode root, @NotNull AbstractTreeStructure treeStructure, @NotNull final Map<SliceNode, NullAnalysisResult> map) {
    final SliceLeafAnalyzer.SliceNodeGuide guide = new SliceLeafAnalyzer.SliceNodeGuide(treeStructure);
    WalkingState<SliceNode> walkingState = new WalkingState<SliceNode>(guide) {

        @Override
        public void visit(@NotNull final SliceNode element) {
            element.calculateDupNode();
            node(element, map).clear();
            SliceNode duplicate = element.getDuplicate();
            if (duplicate != null) {
                node(element, map).add(node(duplicate, map));
            } else {
                final PsiElement value = ApplicationManager.getApplication().runReadAction(new Computable<PsiElement>() {

                    @Override
                    public PsiElement compute() {
                        return element.getValue().getElement();
                    }
                });
                Nullness nullness = ApplicationManager.getApplication().runReadAction(new Computable<Nullness>() {

                    @Override
                    public Nullness compute() {
                        return checkNullness(value);
                    }
                });
                if (nullness == Nullness.NULLABLE) {
                    group(element, map, NullAnalysisResult.NULLS).add(value);
                } else if (nullness == Nullness.NOT_NULL) {
                    group(element, map, NullAnalysisResult.NOT_NULLS).add(value);
                } else {
                    Collection<? extends AbstractTreeNode> children = ApplicationManager.getApplication().runReadAction(new Computable<Collection<? extends AbstractTreeNode>>() {

                        @Override
                        public Collection<? extends AbstractTreeNode> compute() {
                            return element.getChildren();
                        }
                    });
                    if (children.isEmpty()) {
                        group(element, map, NullAnalysisResult.UNKNOWNS).add(value);
                    }
                    super.visit(element);
                }
            }
        }

        @Override
        public void elementFinished(@NotNull SliceNode element) {
            SliceNode parent = guide.getParent(element);
            if (parent != null) {
                node(parent, map).add(node(element, map));
            }
        }
    };
    walkingState.visit(root);
    return node(root, map);
}
Also used : WalkingState(com.intellij.util.WalkingState) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) NotNull(org.jetbrains.annotations.NotNull) Nullness(com.intellij.codeInspection.dataFlow.Nullness) Computable(com.intellij.openapi.util.Computable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

WalkingState (com.intellij.util.WalkingState)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullness (com.intellij.codeInspection.dataFlow.Nullness)1 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 Computable (com.intellij.openapi.util.Computable)1 PsiElement (com.intellij.psi.PsiElement)1