001    /*
002     * Copyright 2010-2013 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.jet.lang.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.jet.lexer.JetTokens;
028    
029    public class JetObjectDeclarationName extends JetExpressionImpl {
030        public JetObjectDeclarationName(@NotNull ASTNode node) {
031            super(node);
032        }
033    
034        @Override
035        @Nullable
036        public String getName() {
037            PsiElement identifier = getNameIdentifier();
038            if (identifier != null) {
039                String text = identifier.getText();
040                return text != null ? JetPsiUtil.unquoteIdentifier(text) : null;
041            }
042            else {
043                return null;
044            }
045        }
046    
047        public PsiElement getNameIdentifier() {
048            return findChildByType(JetTokens.IDENTIFIER);
049        }
050    
051        public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
052            return getNameIdentifier().replace(JetPsiFactory.createNameIdentifier(getProject(), name));
053        }
054    
055        @Override
056        public int getTextOffset() {
057            PsiElement identifier = getNameIdentifier();
058            return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
059        }
060    
061        @Override
062        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
063            return visitor.visitObjectDeclarationName(this, data);
064        }
065    
066        @NotNull
067        @Override
068        public SearchScope getUseScope() {
069            JetObjectDeclaration objectDeclaration = PsiTreeUtil.getParentOfType(this, JetObjectDeclaration.class);
070            return objectDeclaration != null ? objectDeclaration.getUseScope() : super.getUseScope();
071        }
072    }