aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/psi/impl/SdNamedElementImpl.java
blob: 79c5835b5a3fe4423f212c1efc4fed374071f6f0 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.psi.impl;

import ai.vespa.intellij.schema.psi.SdArgumentDeclaration;
import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.icons.AllIcons;
import com.intellij.lang.ASTNode;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import ai.vespa.intellij.schema.SdIcons;
import ai.vespa.intellij.schema.SdUtil;
import ai.vespa.intellij.schema.psi.SdAnnotationFieldDefinition;
import ai.vespa.intellij.schema.psi.SdArgumentDeclaration;
import ai.vespa.intellij.schema.psi.SdDeclarationType;
import ai.vespa.intellij.schema.psi.SdDocumentAnnotationDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentFieldDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentStructDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentStructFieldDefinition;
import ai.vespa.intellij.schema.psi.SdDocumentSummaryDefinition;
import ai.vespa.intellij.schema.psi.SdElementFactory;
import ai.vespa.intellij.schema.psi.SdFunctionDefinition;
import ai.vespa.intellij.schema.psi.SdIdentifier;
import ai.vespa.intellij.schema.psi.SdImportFieldDefinition;
import ai.vespa.intellij.schema.psi.SdNamedElement;
import ai.vespa.intellij.schema.psi.SdRankFeature;
import ai.vespa.intellij.schema.psi.SdRankProfileDefinition;
import ai.vespa.intellij.schema.psi.SdSchemaAnnotationDefinition;
import ai.vespa.intellij.schema.psi.SdSchemaFieldDefinition;
import ai.vespa.intellij.schema.psi.SdStructFieldDefinition;
import ai.vespa.intellij.schema.psi.SdTypes;

import javax.swing.Icon;

/**
 * This abstract class is used to wrap a Psi Element with SdNamedElement interface, which enables the element to be a
 * "name owner" (like an identifier). It allows the element to take a part in references, find usages and more.
 *
 * @author Shahar Ariel
 */
public abstract class SdNamedElementImpl extends ASTWrapperPsiElement implements SdNamedElement {
    
    public SdNamedElementImpl(ASTNode node) {
        super(node);
    }
    
    public String getName() {
        ASTNode node;
        if (this instanceof SdImportFieldDefinition) {
            ASTNode asNode = this.getNode().findChildByType(SdTypes.AS);
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_VAL, asNode);
        } else if (this instanceof SdRankProfileDefinition || this instanceof SdDocumentSummaryDefinition
                   || this instanceof SdRankFeature) {
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_WITH_DASH_VAL);
        } else {
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_VAL);
        }
        if (node != null) {
            return node.getText();
        } else {
            return "";
        }
    }
    
    public String getTypeName() {
        return getType().toString();
    }
    
    public SdDeclarationType getType() {
        if (this instanceof SdSchemaFieldDefinition) {
            return SdDeclarationType.SCHEMA_FIELD;
        } else if (this instanceof SdDocumentSummaryDefinition) {
            return SdDeclarationType.DOCUMENT_SUMMARY;
        } else if (this instanceof SdImportFieldDefinition) {
            return SdDeclarationType.IMPORTED_FIELD;
        } else if (this instanceof SdRankProfileDefinition) {
            return SdDeclarationType.RANK_PROFILE;
        } else if (this instanceof SdFunctionDefinition) {
            return SdDeclarationType.FUNCTION;
        } else if (this instanceof SdArgumentDeclaration) {
            return SdDeclarationType.FUNCTION_ARGUMENT;
        } else if (this instanceof SdDocumentDefinition) {
            return SdDeclarationType.DOCUMENT;
        } else if (this instanceof SdDocumentStructDefinition) {
            return SdDeclarationType.STRUCT;
        } else if (this instanceof SdSchemaAnnotationDefinition ||
                   this instanceof SdDocumentAnnotationDefinition) {
            return SdDeclarationType.ANNOTATION;
        } else if (this instanceof SdDocumentStructFieldDefinition) {
            return SdDeclarationType.DOCUMENT_STRUCT_FIELD;
        } else if (this instanceof SdDocumentFieldDefinition) {
            return SdDeclarationType.DOCUMENT_FIELD;
        } else if (this instanceof SdStructFieldDefinition) {
            return SdDeclarationType.STRUCT_FIELD;
        } else if (this instanceof SdAnnotationFieldDefinition) {
            return SdDeclarationType.ANNOTATION_FIELD;
        } else if (this instanceof SdRankFeature) {
            return SdDeclarationType.FEATURE;
        } else {
            return null;
        }
    }
    
    public PsiElement setName(String newName) {
        ASTNode node;
        if (this instanceof SdImportFieldDefinition) {
            ASTNode asNode = this.getNode().findChildByType(SdTypes.AS);
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_VAL, asNode);
        } else {
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_VAL);
        }
        SdIdentifier elementName = null;
        if (node != null) {
            elementName = SdElementFactory.createIdentifierVal(this.getProject(), newName);
        } else {
            node = this.getNode().findChildByType(SdTypes.IDENTIFIER_WITH_DASH_VAL);
        }
        if (node != null) {
            elementName = SdElementFactory.createIdentifierWithDashVal(this.getProject(), newName);
        }
        if (elementName != null) {
            ASTNode newNode = elementName.getFirstChild().getNode();
            this.getNode().replaceChild(node, newNode);
        }
        return this;
    }
    
    public PsiElement getNameIdentifier() {
        ASTNode keyNode = this.getNode().findChildByType(SdTypes.ID);
        if (keyNode != null) {
            return keyNode.getPsi();
        } else {
            return null;
        }
    }
    
    @Override
    public ItemPresentation getPresentation() {
        final SdNamedElement element = this;
        return new ItemPresentation() {
            @Override
            public String getPresentableText() {
                if (element instanceof SdFunctionDefinition) {
                    return SdUtil.createFunctionDescription((SdFunctionDefinition) element);
                }
                SdRankProfileDefinition rankProfileParent = PsiTreeUtil.getParentOfType(element, SdRankProfileDefinition.class);
                if (rankProfileParent != null) {
                    if (element instanceof SdRankFeature) {
                        return element.getName() + " in " + rankProfileParent.getName();
                    }
                    return rankProfileParent.getName() + "." + element.getName();
                }
                return element.getName();
            }
            
            @Override
            public String getLocationString() {
                return element.getContainingFile() != null ? element.getContainingFile().getName() : null;
            }
            
            @Override
            public Icon getIcon(boolean unused) {
                if (element instanceof SdSchemaFieldDefinition || element instanceof SdDocumentFieldDefinition || 
                    element instanceof SdAnnotationFieldDefinition || element instanceof SdRankFeature) {
                    return AllIcons.Nodes.Field;
                } else if (element instanceof SdStructFieldDefinition  ||
                           element instanceof SdDocumentStructFieldDefinition) {
                    return SdIcons.STRUCT_FIELD;
                } else if (element instanceof SdImportFieldDefinition) {
                    return SdIcons.IMPORTED_FIELD;
                } else if (element instanceof SdFunctionDefinition) {
                    return AllIcons.Nodes.Method;
                    // Didn't use isOverride() here because it causes the Structure View to load too slow 
                    // return ((SdFunctionDefinition) element).isOverride() ? SdIcons.OVERRIDE_MACRO : AllIcons.Nodes.Method;
                } else if (element instanceof SdDocumentStructDefinition) {
                    return SdIcons.STRUCT;
                } else if (element instanceof SdRankProfileDefinition) {
                    return AllIcons.Nodes.Record;
                } else if (element instanceof SdDocumentSummaryDefinition) {
                    return SdIcons.DOCUMENT_SUMMARY;
                } else if (element instanceof SdDocumentDefinition) {
                    return SdIcons.DOCUMENT;
                } else if (element instanceof SdSchemaAnnotationDefinition ||
                           element instanceof SdDocumentAnnotationDefinition) {
                    return AllIcons.Nodes.ObjectTypeAttribute;
                }
                else {
                    return null;
                }
            }
        };
    }

}