aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/structure/SdStructureViewElement.java
blob: f2fe11c2841bb8745804f73bbedf917ef91cb480 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.structure;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.util.treeView.smartTree.SortableTreeElement;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.NavigatablePsiElement;
import com.intellij.psi.PsiElement;
import ai.vespa.intellij.schema.SdUtil;
import ai.vespa.intellij.schema.psi.SdDocumentAnnotationDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentStructDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentSummaryDefinition;
import ai.vespa.intellij.schema.psi.SdFile;
import ai.vespa.intellij.schema.psi.SdRankProfileDefinition;
import ai.vespa.intellij.schema.psi.SdSchemaAnnotationDefinition;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is used for the presentation of an element in the "Structure View" window.
 *
 * @author Shahar Ariel
 */
public class SdStructureViewElement implements StructureViewTreeElement, SortableTreeElement {
    
    private final NavigatablePsiElement myElement;
    
    public SdStructureViewElement(NavigatablePsiElement element) {
        this.myElement = element;
    }
    
    @Override
    public Object getValue() {
        return myElement;
    }
    
    @Override
    public void navigate(boolean requestFocus) {
        myElement.navigate(requestFocus);
    }
    
    @Override
    public boolean canNavigate() {
        return myElement.canNavigate();
    }
    
    @Override
    public boolean canNavigateToSource() {
        return myElement.canNavigateToSource();
    }
    
    @Override
    public String getAlphaSortKey() {
        String name = myElement.getName();
        return name != null ? name : "";
    }
    
    @Override
    public ItemPresentation getPresentation() {
        ItemPresentation presentation = myElement.getPresentation();
        return presentation != null ? presentation : new PresentationData();
    }
    
    @Override
    public TreeElement[] getChildren() {
        List<PsiElement> children = new ArrayList<>();
        if (myElement instanceof SdFile) {
            children = SdUtil.findSchemaChildren(myElement);
        } else if (myElement instanceof SdDocumentDefinition) {
            children = SdUtil.findDocumentChildren(myElement);
        } else if (myElement instanceof SdDocumentStructDefinition) {
            children = SdUtil.findDocumentStructChildren(myElement);
        } else if (myElement instanceof SdRankProfileDefinition) {
            children = SdUtil.findRankProfileChildren(myElement);
        } else if (myElement instanceof SdDocumentSummaryDefinition) {
            children = SdUtil.findDocumentSummaryChildren(myElement);
        } else if (myElement instanceof SdSchemaAnnotationDefinition ||
                   myElement instanceof SdDocumentAnnotationDefinition) {
            children = SdUtil.findAnnotationChildren(myElement);
        }
        
        List<TreeElement> treeElements = new ArrayList<>(children.size());
        for (PsiElement child : children) {
            treeElements.add(new SdStructureViewElement((NavigatablePsiElement) child));
        }
        return treeElements.toArray(new TreeElement[0]);
    }

}