aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/psi/impl/SdIdentifierMixin.java
blob: e7ea4bbcb593e89c1f56086ff1bb41336b3dedb5 (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
// 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 com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import ai.vespa.intellij.schema.psi.SdElementFactory;
import ai.vespa.intellij.schema.psi.SdIdentifier;
import ai.vespa.intellij.schema.psi.SdIdentifierVal;

/**
 * This abstract class is used for methods' implementations for SdIdentifier. Connected with "mixin" to IdentifierVal and 
 * IdentifierWithDashVal rules in sd.bnf
 *
 * @author Shahar Ariel
 */
public abstract class SdIdentifierMixin extends SdIdentifierMixinImpl implements PsiNamedElement {
    
    public SdIdentifierMixin(ASTNode node) {
        super(node);
    }
    
    public String getName() {
        // IMPORTANT: Convert embedded escaped spaces to simple spaces
        return this.getText().replaceAll("\\\\ ", " ");
    }
    
    public PsiElement setName(String newName) {
        ASTNode node =  this.getNode().getFirstChildNode();
        if (node != null) {
            SdIdentifier elementName;
            if (this instanceof SdIdentifierVal) {
                elementName = SdElementFactory.createIdentifierVal(this.getProject(), newName);
            } else { // this instanceof SdIdentifierWithDashVal
                elementName = SdElementFactory.createIdentifierWithDashVal(this.getProject(), newName);
            }
            ASTNode newNode = elementName.getFirstChild().getNode();
            this.getNode().replaceChild(node, newNode);
        }
        return this;
    }
    
}