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.PsiReference;
022 import com.intellij.psi.PsiReferenceService;
023 import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.annotations.Nullable;
026 import org.jetbrains.jet.JetNodeTypes;
027 import org.jetbrains.jet.lang.resolve.name.FqName;
028 import org.jetbrains.jet.lang.resolve.name.Name;
029
030 import java.util.List;
031
032 public class JetNamespaceHeader extends JetReferenceExpression {
033 private String qualifiedNameCache = null;
034
035 public JetNamespaceHeader(@NotNull ASTNode node) {
036 super(node);
037 }
038
039 @NotNull
040 public List<JetSimpleNameExpression> getParentNamespaceNames() {
041 List<JetSimpleNameExpression> parentParts = findChildrenByType(JetNodeTypes.REFERENCE_EXPRESSION);
042 JetSimpleNameExpression lastPart = (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
043 parentParts.remove(lastPart);
044 return parentParts;
045 }
046
047 @Nullable
048 public JetSimpleNameExpression getLastPartExpression() {
049 return (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
050 }
051
052 @Nullable
053 public PsiElement getNameIdentifier() {
054 JetSimpleNameExpression lastPart = (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
055 if (lastPart == null) {
056 return null;
057 }
058
059 return lastPart.getIdentifier();
060 }
061
062 @Override
063 @NotNull
064 public String getName() {
065 PsiElement nameIdentifier = getNameIdentifier();
066 return nameIdentifier == null ? "" : nameIdentifier.getText();
067 }
068
069 @NotNull
070 public Name getNameAsName() {
071 PsiElement nameIdentifier = getNameIdentifier();
072 return nameIdentifier == null ? JetPsiUtil.ROOT_NAMESPACE_NAME : Name.identifier(nameIdentifier.getText());
073 }
074
075 public boolean isRoot() {
076 return getName().length() == 0;
077 }
078
079 @NotNull
080 public FqName getFqName() {
081 String qualifiedName = getQualifiedName();
082 return qualifiedName.isEmpty() ? FqName.ROOT : new FqName(qualifiedName);
083 }
084
085 @NotNull
086 public FqName getParentFqName(JetReferenceExpression nameExpression) {
087 String parentQualifiedName = getQualifiedNameParentOf(nameExpression);
088 return parentQualifiedName.isEmpty() ? FqName.ROOT : new FqName(parentQualifiedName);
089 }
090
091 @NotNull
092 public String getQualifiedName() {
093 if (qualifiedNameCache == null) {
094 qualifiedNameCache = getQualifiedNameParentOf(null);
095 }
096
097 return qualifiedNameCache;
098 }
099
100 @NotNull
101 private String getQualifiedNameParentOf(@Nullable JetReferenceExpression nameExpression) {
102 StringBuilder builder = new StringBuilder();
103 for (JetSimpleNameExpression e : findChildrenByClass(JetSimpleNameExpression.class)) {
104 if (e == nameExpression) {
105 break;
106 }
107
108 if (builder.length() > 0) {
109 builder.append(".");
110 }
111 builder.append(e.getReferencedName());
112 }
113 return builder.toString();
114 }
115
116 @Override
117 public void subtreeChanged() {
118 qualifiedNameCache = null;
119 }
120 }
121