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 017package org.jetbrains.jet.lang.psi; 018 019import com.intellij.lang.ASTNode; 020import com.intellij.psi.PsiElement; 021import com.intellij.psi.PsiReference; 022import com.intellij.psi.PsiReferenceService; 023import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; 024import org.jetbrains.annotations.NotNull; 025import org.jetbrains.annotations.Nullable; 026import org.jetbrains.jet.JetNodeTypes; 027import org.jetbrains.jet.lang.resolve.name.FqName; 028import org.jetbrains.jet.lang.resolve.name.Name; 029 030import java.util.List; 031 032public 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 @NotNull 053 @Override 054 public PsiReference[] getReferences() { 055 return ReferenceProvidersRegistry.getReferencesFromProviders(this, PsiReferenceService.Hints.NO_HINTS); 056 } 057 058 @Nullable 059 @Override 060 public PsiReference getReference() { 061 PsiReference[] references = getReferences(); 062 return references.length == 1 ? references[0] : null; 063 } 064 065 @Nullable 066 public PsiElement getNameIdentifier() { 067 JetSimpleNameExpression lastPart = (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION); 068 if (lastPart == null) { 069 return null; 070 } 071 072 return lastPart.getIdentifier(); 073 } 074 075 @Override 076 @NotNull 077 public String getName() { 078 PsiElement nameIdentifier = getNameIdentifier(); 079 return nameIdentifier == null ? "" : nameIdentifier.getText(); 080 } 081 082 @NotNull 083 public Name getNameAsName() { 084 PsiElement nameIdentifier = getNameIdentifier(); 085 return nameIdentifier == null ? JetPsiUtil.ROOT_NAMESPACE_NAME : Name.identifier(nameIdentifier.getText()); 086 } 087 088 public boolean isRoot() { 089 return getName().length() == 0; 090 } 091 092 @NotNull 093 public FqName getFqName() { 094 String qualifiedName = getQualifiedName(); 095 return qualifiedName.isEmpty() ? FqName.ROOT : new FqName(qualifiedName); 096 } 097 098 @NotNull 099 public FqName getParentFqName(JetReferenceExpression nameExpression) { 100 String parentQualifiedName = getQualifiedNameParentOf(nameExpression); 101 return parentQualifiedName.isEmpty() ? FqName.ROOT : new FqName(parentQualifiedName); 102 } 103 104 @NotNull 105 public String getQualifiedName() { 106 if (qualifiedNameCache == null) { 107 qualifiedNameCache = getQualifiedNameParentOf(null); 108 } 109 110 return qualifiedNameCache; 111 } 112 113 @NotNull 114 private String getQualifiedNameParentOf(@Nullable JetReferenceExpression nameExpression) { 115 StringBuilder builder = new StringBuilder(); 116 for (JetSimpleNameExpression e : findChildrenByClass(JetSimpleNameExpression.class)) { 117 if (e == nameExpression) { 118 break; 119 } 120 121 if (builder.length() > 0) { 122 builder.append("."); 123 } 124 builder.append(e.getReferencedName()); 125 } 126 return builder.toString(); 127 } 128 129 @Override 130 public void subtreeChanged() { 131 qualifiedNameCache = null; 132 } 133} 134