001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.search.SearchScope;
022    import com.intellij.psi.util.PsiTreeUtil;
023    import com.intellij.util.IncorrectOperationException;
024    import org.jetbrains.annotations.NonNls;
025    import org.jetbrains.annotations.NotNull;
026    import org.jetbrains.annotations.Nullable;
027    import org.jetbrains.kotlin.lexer.JetTokens;
028    
029    import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
030    
031    public class JetObjectDeclarationName extends JetExpressionImpl {
032        public JetObjectDeclarationName(@NotNull ASTNode node) {
033            super(node);
034        }
035    
036        @Override
037        @Nullable
038        public String getName() {
039            PsiElement identifier = getNameIdentifier();
040            if (identifier != null) {
041                String text = identifier.getText();
042                return text != null ? JetPsiUtil.unquoteIdentifier(text) : null;
043            }
044            else {
045                return null;
046            }
047        }
048    
049        public PsiElement getNameIdentifier() {
050            return findChildByType(JetTokens.IDENTIFIER);
051        }
052    
053        public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
054            return getNameIdentifier().replace(JetPsiFactory(this).createNameIdentifier(name));
055        }
056    
057        @Override
058        public int getTextOffset() {
059            PsiElement identifier = getNameIdentifier();
060            return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
061        }
062    
063        @Override
064        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
065            return visitor.visitObjectDeclarationName(this, data);
066        }
067    
068        @NotNull
069        @Override
070        public SearchScope getUseScope() {
071            JetObjectDeclaration objectDeclaration = PsiTreeUtil.getParentOfType(this, JetObjectDeclaration.class);
072            return objectDeclaration != null ? objectDeclaration.getUseScope() : super.getUseScope();
073        }
074    }