summaryrefslogtreecommitdiffstats
path: root/sd-plugin/src/main/java/org/intellij/sdk/language/psi/impl/SdIdentifierMixin.java
blob: 536dfc4f2cc4a5e83691454f034285a66784fdc9 (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
package org.intellij.sdk.language.psi.impl;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import org.intellij.sdk.language.psi.SdElementFactory;
import org.intellij.sdk.language.psi.SdIdentifier;
import org.intellij.sdk.language.psi.SdIdentifierVal;
import org.jetbrains.annotations.NotNull;

/**
 * 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(@NotNull ASTNode node) {
        super(node);
    }
    
    @NotNull
    public String getName() {
        // IMPORTANT: Convert embedded escaped spaces to simple spaces
        return this.getText().replaceAll("\\\\ ", " ");
    }
    
    @NotNull
    public PsiElement setName(@NotNull 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;
    }
    

    
}