diff --git a/idea/src/org/jetbrains/jet/lang/psi/JetNamedDeclaration.java b/idea/src/org/jetbrains/jet/lang/psi/JetNamedDeclaration.java index 9cf3319d8c3..50951009916 100644 --- a/idea/src/org/jetbrains/jet/lang/psi/JetNamedDeclaration.java +++ b/idea/src/org/jetbrains/jet/lang/psi/JetNamedDeclaration.java @@ -1,8 +1,11 @@ package org.jetbrains.jet.lang.psi; import com.intellij.lang.ASTNode; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiNameIdentifierOwner; +import com.intellij.psi.PsiReference; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -27,6 +30,58 @@ public abstract class JetNamedDeclaration extends JetDeclaration implements PsiN return findChildByType(JetTokens.IDENTIFIER); } + @Override + public PsiReference getReference() { + return new PsiReference() { + @Override + public PsiElement getElement() { + return getNameIdentifier(); + } + + @Override + public TextRange getRangeInElement() { + return getElement().getTextRange().shiftRight(getElement().getTextOffset()); + } + + @Override + public PsiElement resolve() { + return PsiTreeUtil.getParentOfType(JetNamedDeclaration.this, JetFile.class); + } + + @NotNull + @Override + public String getCanonicalText() { + return ""; + } + + @Override + public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { + throw new IncorrectOperationException(); + } + + @Override + public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { + throw new IncorrectOperationException(); + } + + @Override + public boolean isReferenceTo(PsiElement element) { + return resolve() == element; + } + + @NotNull + @Override + public Object[] getVariants() { + return EMPTY_ARRAY; + } + + @Override + public boolean isSoft() { + return false; + } + }; + } + @Override public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { return getNameIdentifier().replace(JetChangeUtil.createNameIdentifier(getProject(), name)); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java b/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java index 55285838a38..6fc53030677 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java @@ -8,6 +8,7 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.resolve.java.JavaPackageScope; import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices; +import org.jetbrains.jet.lang.types.ModuleDescriptor; /** * @author abreslav @@ -26,7 +27,7 @@ public class AnalyzingUtils { JavaSemanticServices javaSemanticServices = new JavaSemanticServices(project, semanticServices, bindingTraceContext); JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope(); - WritableScope scope = new WritableScope(libraryScope, libraryScope.getContainingDeclaration()); + WritableScope scope = new WritableScope(libraryScope, new ModuleDescriptor("")); // scope.importScope(javaSemanticServices.getDescriptorResolver().resolveNamespace("").getMemberScope()); // scope.importScope(javaSemanticServices.getDescriptorResolver().resolveNamespace("java.lang").getMemberScope()); scope.importScope(new JavaPackageScope("", null, javaSemanticServices)); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java b/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java index a4bcbbf1260..e9e95a0ace0 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/BindingContext.java @@ -14,6 +14,8 @@ public interface BindingContext { FunctionDescriptor getFunctionDescriptor(JetFunction declaration); PropertyDescriptor getPropertyDescriptor(JetProperty declaration); + DeclarationDescriptor getDeclarationDescriptor(JetDeclaration declaration); + Type getExpressionType(JetExpression expression); JetScope getTopLevelScope(); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java b/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java index f53c4b63d42..c65c6259511 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/BindingTraceContext.java @@ -48,6 +48,12 @@ public class BindingTraceContext extends BindingTrace implements BindingContext //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Override + public DeclarationDescriptor getDeclarationDescriptor(JetDeclaration declaration) { + return declarationsToDescriptors.get(declaration); + } + public NamespaceDescriptor getNamespaceDescriptor(JetNamespace declaration) { return (NamespaceDescriptor) declarationsToDescriptors.get(declaration); } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java b/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java index cc2c6f1dba7..debec6593d0 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/MutableClassDescriptor.java @@ -13,7 +13,8 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme private final WritableScope unsubstitutedMemberScope; private TypeConstructor typeConstructor; - public MutableClassDescriptor(@NotNull JetScope outerScope) { + public MutableClassDescriptor(@NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope) { + super(containingDeclaration); this.unsubstitutedMemberScope = new WritableScope(outerScope, this); } diff --git a/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java b/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java index b69c53dc9d7..9eb337badd8 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/MutableDeclarationDescriptor.java @@ -1,7 +1,6 @@ package org.jetbrains.jet.lang.resolve; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.types.Attribute; import org.jetbrains.jet.lang.types.DeclarationDescriptor; import org.jetbrains.jet.lang.types.DeclarationDescriptorVisitor; @@ -13,7 +12,11 @@ import java.util.List; */ public abstract class MutableDeclarationDescriptor implements DeclarationDescriptor { private String name; - private DeclarationDescriptor containingDeclaration; + private final DeclarationDescriptor containingDeclaration; + + public MutableDeclarationDescriptor(DeclarationDescriptor containingDeclaration) { + this.containingDeclaration = containingDeclaration; + } @Override public List getAttributes() { @@ -35,16 +38,11 @@ public abstract class MutableDeclarationDescriptor implements DeclarationDescrip return this; } - @NotNull @Override public DeclarationDescriptor getContainingDeclaration() { return containingDeclaration; } - public void setContainingDeclaration(@Nullable DeclarationDescriptor containingDeclaration) { - this.containingDeclaration = containingDeclaration; - } - @Override public void acceptVoid(DeclarationDescriptorVisitor visitor) { accept(visitor, null); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java index d01f258c4bf..1ae9765c428 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/TopDownAnalyzer.java @@ -58,7 +58,7 @@ public class TopDownAnalyzer { List importDirectives = namespace.getImportDirectives(); String name = namespace.getName(); - NamespaceDescriptor namespaceDescriptor = declaringScope.getNamespace(name); + NamespaceDescriptor namespaceDescriptor = declaringScope.getDeclaredNamespace(name); if (namespaceDescriptor == null) { namespaceDescriptor = new NamespaceDescriptor( declaringScope.getContainingDeclaration(), @@ -104,7 +104,7 @@ public class TopDownAnalyzer { } private WritableScope processClass(@NotNull WritableScope declaringScope, JetClass klass) { - MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(declaringScope); + MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor(declaringScope.getContainingDeclaration(), declaringScope); mutableClassDescriptor.setName(klass.getName()); declaringScope.addClassDescriptor(mutableClassDescriptor); diff --git a/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java b/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java index 548aba5e8ef..cac4153894c 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/WritableScope.java @@ -216,10 +216,16 @@ public class WritableScope extends JetScopeAdapter { } } - @Override - public NamespaceDescriptor getNamespace(@NotNull String name) { + public NamespaceDescriptor getDeclaredNamespace(@NotNull String name) { NamespaceDescriptor namespaceDescriptor = getNamespaceDescriptors().get(name); if (namespaceDescriptor != null) return namespaceDescriptor; + return null; + } + + @Override + public NamespaceDescriptor getNamespace(@NotNull String name) { + NamespaceDescriptor declaredNamespace = getDeclaredNamespace(name); + if (declaredNamespace != null) return declaredNamespace; NamespaceDescriptor namespace = super.getNamespace(name); if (namespace != null) return namespace; diff --git a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaTypeTransformer.java b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaTypeTransformer.java index 048a6156da1..99fab06005e 100644 --- a/idea/src/org/jetbrains/jet/lang/resolve/java/JavaTypeTransformer.java +++ b/idea/src/org/jetbrains/jet/lang/resolve/java/JavaTypeTransformer.java @@ -25,7 +25,6 @@ public class JavaTypeTransformer { @NotNull public Type transform(PsiType javaType) { - System.out.println("javaType = " + javaType.getInternalCanonicalText()); return javaType.accept(new PsiTypeVisitor() { @Override public Type visitClassType(PsiClassType classType) { diff --git a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java index 8a3c6a1a91e..6ac45aa8c92 100644 --- a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java +++ b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorImpl.java @@ -31,7 +31,6 @@ public abstract class DeclarationDescriptorImpl extends AnnotatedImpl implements return this; } - @NotNull @Override public DeclarationDescriptor getContainingDeclaration() { return containingDeclaration; diff --git a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorVisitor.java b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorVisitor.java index 374a2a01536..125c812ab15 100644 --- a/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorVisitor.java +++ b/idea/src/org/jetbrains/jet/lang/types/DeclarationDescriptorVisitor.java @@ -1,7 +1,5 @@ package org.jetbrains.jet.lang.types; -import org.jetbrains.jet.lang.resolve.LazySubstitutingClassDescriptor; - /** * @author abreslav */ @@ -25,4 +23,8 @@ public class DeclarationDescriptorVisitor { public R visitClassDescriptor(ClassDescriptor descriptor, D data) { return null; } + + public R visitModuleDeclaration(ModuleDescriptor moduleDescriptor, D data) { + return null; + } } diff --git a/idea/src/org/jetbrains/jet/lang/types/ModuleDescriptor.java b/idea/src/org/jetbrains/jet/lang/types/ModuleDescriptor.java new file mode 100644 index 00000000000..6318cb1f0c7 --- /dev/null +++ b/idea/src/org/jetbrains/jet/lang/types/ModuleDescriptor.java @@ -0,0 +1,17 @@ +package org.jetbrains.jet.lang.types; + +import java.util.Collections; + +/** + * @author abreslav + */ +public class ModuleDescriptor extends DeclarationDescriptorImpl { + public ModuleDescriptor(String name) { + super(null, Collections.emptyList(), name); + } + + @Override + public R accept(DeclarationDescriptorVisitor visitor, D data) { + return visitor.visitModuleDeclaration(this, data); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java b/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java index a87008b758f..4f44cc73f91 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java +++ b/idea/src/org/jetbrains/jet/plugin/JetQuickDocumentationProvider.java @@ -4,11 +4,13 @@ import com.intellij.lang.documentation.QuickDocumentationProvider; import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.jet.lang.ErrorHandler; +import org.jetbrains.jet.lang.psi.JetDeclaration; import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetReferenceExpression; import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.DeclarationDescriptor; +import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.resolve.DescriptorUtil; /** @@ -29,14 +31,27 @@ public class JetQuickDocumentationProvider extends QuickDocumentationProvider { BindingContext bindingContext = AnalyzingUtils.analyzeFile((JetFile) element.getContainingFile(), ErrorHandler.DO_NOTHING); DeclarationDescriptor declarationDescriptor = bindingContext.resolveReferenceExpression(ref); if (declarationDescriptor != null) { - String text = DescriptorUtil.renderPresentableText(declarationDescriptor); - text = text.replaceAll("<", "<"); - return text; + return render(declarationDescriptor); } return "Unresolved"; } + +// if (originalElement.getNode().getElementType() == JetTokens.IDENTIFIER) { +// JetDeclaration declaration = PsiTreeUtil.getParentOfType(originalElement, JetDeclaration.class); +// BindingContext bindingContext = AnalyzingUtils.analyzeFile((JetFile) element.getContainingFile(), ErrorHandler.DO_NOTHING); +// DeclarationDescriptor declarationDescriptor = bindingContext.getDeclarationDescriptor(declaration); +// if (declarationDescriptor != null) { +// return render(declarationDescriptor); +// } +// } return "Not a reference"; } + private String render(DeclarationDescriptor declarationDescriptor) { + String text = DescriptorUtil.renderPresentableText(declarationDescriptor); + text = text.replaceAll("<", "<"); + return text; + } + } diff --git a/idea/src/org/jetbrains/jet/resolve/DescriptorUtil.java b/idea/src/org/jetbrains/jet/resolve/DescriptorUtil.java index 8a3fdbfc0de..9a1f4d28b2e 100644 --- a/idea/src/org/jetbrains/jet/resolve/DescriptorUtil.java +++ b/idea/src/org/jetbrains/jet/resolve/DescriptorUtil.java @@ -16,13 +16,13 @@ public class DescriptorUtil { @Override public Void visitPropertyDescriptor(PropertyDescriptor descriptor, StringBuilder builder) { Type type = descriptor.getType(); - builder.append(descriptor.getName()).append(" : ").append(type); + builder.append(renderName(descriptor)).append(" : ").append(type); return super.visitPropertyDescriptor(descriptor, builder); } @Override public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) { - builder.append("fun ").append(descriptor.getName()); + builder.append("fun ").append(renderName(descriptor)); List typeParameters = descriptor.getTypeParameters(); renderTypeParameters(typeParameters, builder); builder.append("("); @@ -61,13 +61,13 @@ public class DescriptorUtil { @Override public Void visitNamespaceDescriptor(NamespaceDescriptor namespaceDescriptor, StringBuilder builder) { - builder.append("namespace ").append(namespaceDescriptor.getName()); + builder.append("namespace ").append(renderName(namespaceDescriptor)); return super.visitNamespaceDescriptor(namespaceDescriptor, builder); } @Override public Void visitClassDescriptor(ClassDescriptor descriptor, StringBuilder builder) { - builder.append("class ").append(descriptor.getName()); + builder.append("class ").append(renderName(descriptor)); renderTypeParameters(descriptor.getTypeConstructor().getParameters(), builder); return super.visitClassDescriptor(descriptor, builder); } @@ -76,8 +76,23 @@ public class DescriptorUtil { return stringBuilder.toString(); } + private static StringBuilder renderName(DeclarationDescriptor descriptor) { + StringBuilder stringBuilder = new StringBuilder(); + renderName(descriptor, stringBuilder); + return stringBuilder; + } + + private static void renderName(DeclarationDescriptor descriptor, StringBuilder stringBuilder) { + DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); + if (containingDeclaration != null) { + renderName(containingDeclaration, stringBuilder); + stringBuilder.append("::"); + } + stringBuilder.append(descriptor.getName()); + } + private static void renderTypeParameter(TypeParameterDescriptor descriptor, StringBuilder builder) { - builder.append(descriptor.getName()); + builder.append(renderName(descriptor)); if (!descriptor.getUpperBounds().isEmpty()) { Type bound = descriptor.getUpperBounds().iterator().next(); if (bound != JetStandardClasses.getAnyType()) {