aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/hierarchy/SdCallTreeStructure.java
blob: 626f5cf8fc5a30ad7d616c767740139c1547f233 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.hierarchy;

import ai.vespa.intellij.schema.model.Function;
import ai.vespa.intellij.schema.model.Schema;
import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
import ai.vespa.intellij.schema.SdUtil;
import ai.vespa.intellij.schema.psi.SdFile;
import ai.vespa.intellij.schema.psi.SdFunctionDefinition;
import ai.vespa.intellij.schema.psi.SdRankProfileDefinition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A general tree in the "Call Hierarchy" window.
 *
 * @author Shahar Ariel
 */
public abstract class SdCallTreeStructure extends HierarchyTreeStructure {

    protected final String myScopeType;
    protected final SdFile myFile;
    protected Map<String, List<Function>> functionsMap;
    protected Map<String, Set<SdRankProfileDefinition>> ranksHeritageMap;
    
    public SdCallTreeStructure(Project project, PsiElement element, String currentScopeType) {
        super(project, new SdCallHierarchyNodeDescriptor(null, element, true));
        myScopeType = currentScopeType;
        myFile = (SdFile) element.getContainingFile();
        functionsMap = new Schema(myFile).functions();
        ranksHeritageMap = new HashMap<>();
    }
    
    protected abstract Set<Function> getChildren(SdFunctionDefinition element);
    
    @Override
    protected Object[] buildChildren(HierarchyNodeDescriptor descriptor) {
        final List<SdCallHierarchyNodeDescriptor> descriptors = new ArrayList<>();
        
        if (descriptor instanceof SdCallHierarchyNodeDescriptor) {
            PsiElement element = descriptor.getPsiElement();
            if (element == null) {
                return ArrayUtilRt.EMPTY_OBJECT_ARRAY;
            }
            boolean isCallable = SdHierarchyUtil.isExecutable(element);
            HierarchyNodeDescriptor nodeDescriptor = getBaseDescriptor();
            if (!isCallable || nodeDescriptor == null) {
                return ArrayUtilRt.EMPTY_OBJECT_ARRAY;
            }
            
            Set<Function> children = getChildren((SdFunctionDefinition) element);
            
            Map<PsiElement, SdCallHierarchyNodeDescriptor> callerToDescriptorMap = new HashMap<>();
            PsiElement baseClass = PsiTreeUtil.getParentOfType(element, SdRankProfileDefinition.class);
            
            for (Function caller : children) {
                if (isInScope(baseClass, caller.definition(), myScopeType)) {
                    SdCallHierarchyNodeDescriptor callerDescriptor = callerToDescriptorMap.get(caller);
                    if (callerDescriptor == null) {
                        callerDescriptor = new SdCallHierarchyNodeDescriptor(descriptor, caller.definition(), false);
                        callerToDescriptorMap.put(caller.definition(), callerDescriptor);
                        descriptors.add(callerDescriptor);
                    }
                }
            }
        }
        return ArrayUtil.toObjectArray(descriptors);
    }

}