Advanced way for getting package fqn from psi

This commit is contained in:
Nikolay Krasko
2012-08-06 00:04:32 +04:00
parent 8c78a36ad0
commit 157bd147f1
2 changed files with 24 additions and 31 deletions
@@ -24,6 +24,7 @@ import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.List;
@@ -89,9 +90,30 @@ public class JetNamespaceHeader extends JetReferenceExpression {
return getName().length() == 0;
}
@NotNull
public FqName getFqName() {
String qualifiedName = getQualifiedName();
return qualifiedName.isEmpty() ? FqName.ROOT : new FqName(qualifiedName);
}
@NotNull
public FqName getParentFqName(JetReferenceExpression nameExpression) {
String parentQualifiedName = getQualifiedNameParentOf(nameExpression);
return parentQualifiedName.isEmpty() ? FqName.ROOT : new FqName(parentQualifiedName);
}
@NotNull
public String getQualifiedName() {
return getQualifiedNameParentOf(null);
}
private String getQualifiedNameParentOf(@Nullable JetReferenceExpression nameExpression) {
StringBuilder builder = new StringBuilder();
for (JetSimpleNameExpression e : findChildrenByClass(JetSimpleNameExpression.class)) {
if (e == nameExpression) {
break;
}
if (builder.length() > 0) {
builder.append(".");
}
@@ -97,16 +97,6 @@ public class JetPsiUtil {
return rootElements;
}
@Nullable
public static JetNamedFunction getSurroundingFunction(@Nullable PsiElement element) {
while (element != null) {
if (element instanceof JetNamedFunction) return (JetNamedFunction) element;
if (element instanceof JetClassOrObject || element instanceof JetFile) return null;
element = element.getParent();
}
return null;
}
@NotNull
public static String unquoteIdentifier(@NotNull String quoted) {
if (quoted.indexOf('`') < 0) {
@@ -135,23 +125,9 @@ public class JetPsiUtil {
}
}
private static FqName getFQName(JetNamespaceHeader header) {
StringBuilder builder = new StringBuilder();
for (JetSimpleNameExpression nameExpression : header.getParentNamespaceNames()) {
builder.append(nameExpression.getReferencedName());
builder.append(".");
}
builder.append(header.getName());
return new FqName(builder.toString());
}
public static FqName getFQName(JetFile file) {
if (file.isScript()) {
return FqName.ROOT;
}
else {
return getFQName(file.getNamespaceHeader());
}
JetNamespaceHeader header = file.getNamespaceHeader();
return header != null ? header.getFqName() : FqName.ROOT;
}
@Nullable
@@ -202,11 +178,6 @@ public class JetPsiUtil {
return new ImportPath(text.replaceAll(" ", "") + (importDirective.isAllUnder() ? ".*" : ""));
}
@NotNull
private static FqName makeFQName(@NotNull FqName prefix, @NotNull JetClassOrObject jetClass) {
return prefix.child(Name.identifier(jetClass.getName()));
}
public static boolean isIrrefutable(JetWhenEntry entry) {
if (entry.isElse()) return true;
for (JetWhenCondition condition : entry.getConditions()) {