Set up rendering for structure elements

This commit is contained in:
Nikolay Krasko
2014-05-23 22:03:15 +04:00
parent 2d6dc5db68
commit 58d9375a25
9 changed files with 278 additions and 106 deletions
@@ -46,8 +46,9 @@ public class JetStructureViewElement implements StructureViewTreeElement, Colore
private final NavigatablePsiElement element;
private String elementText;
private Icon icon;
public JetStructureViewElement(NavigatablePsiElement element) {
public JetStructureViewElement(@NotNull NavigatablePsiElement element) {
this.element = element;
}
@@ -55,10 +56,6 @@ public class JetStructureViewElement implements StructureViewTreeElement, Colore
element = fileElement;
}
private static DeclarationDescriptor getDescriptor(JetDeclaration declaration) {
return declaration.isValid() ? ResolvePackage.getLazyResolveSession(declaration).resolveToDescriptor(declaration) : null;
}
@NotNull
public NavigatablePsiElement getElement() {
return element;
@@ -102,6 +99,55 @@ public class JetStructureViewElement implements StructureViewTreeElement, Colore
}), TreeElement.class);
}
@Nullable
@Override
public TextAttributesKey getTextAttributesKey() {
if (element instanceof JetModifierListOwner && JetPsiUtil.isDeprecated((JetModifierListOwner) element)) {
return CodeInsightColors.DEPRECATED_ATTRIBUTES;
}
return null;
}
@Nullable
@Override
public String getPresentableText() {
if (elementText == null) {
elementText = getElementText(element, getDescriptor());
}
return elementText;
}
@Nullable
@Override
public String getLocationString() {
return null;
}
@Nullable
@Override
public Icon getIcon(boolean unused) {
if (icon == null) {
icon = getElementIcon(element, getDescriptor());
}
return icon;
}
@Nullable
private DeclarationDescriptor getDescriptor() {
if (!(element.isValid() && element instanceof JetDeclaration)) {
return null;
}
JetDeclaration declaration = (JetDeclaration) element;
if (declaration instanceof JetClassInitializer) {
return null;
}
return ResolvePackage.getLazyResolveSession(declaration).resolveToDescriptor(declaration);
}
private List<JetDeclaration> getChildrenDeclarations() {
if (element instanceof JetFile) {
JetFile jetFile = (JetFile) element;
@@ -129,74 +175,29 @@ public class JetStructureViewElement implements StructureViewTreeElement, Colore
return Collections.emptyList();
}
@Nullable
@Override
public TextAttributesKey getTextAttributesKey() {
if (element instanceof JetModifierListOwner && JetPsiUtil.isDeprecated((JetModifierListOwner) element)) {
return CodeInsightColors.DEPRECATED_ATTRIBUTES;
private static Icon getElementIcon(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
if (descriptor != null) {
return JetDescriptorIconProvider.getIcon(descriptor, navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY);
}
return PsiIconUtil.getProvidersIcon(navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY);
}
@Nullable
private static String getElementText(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
if (descriptor != null) {
return DescriptorRenderer.STARTS_FROM_NAME_WITH_SHORT_TYPES.render(descriptor);
}
String text = navigatablePsiElement.getName();
if (!StringUtil.isEmpty(text)) {
return text;
}
if (navigatablePsiElement instanceof JetClassInitializer) {
return "<class initializer>";
}
return null;
}
@Nullable
@Override
public String getPresentableText() {
if (elementText == null) {
elementText = getElementText();
}
return elementText;
}
@Nullable
@Override
public String getLocationString() {
return null;
}
@Nullable
@Override
public Icon getIcon(boolean unused) {
if (!element.isValid()) return null;
if (element instanceof JetDeclaration) {
DeclarationDescriptor descriptor = getDescriptor((JetDeclaration) element);
if (descriptor != null) {
JetDescriptorIconProvider.getIcon(descriptor, element, Iconable.ICON_FLAG_VISIBILITY);
}
}
return PsiIconUtil.getProvidersIcon(element, Iconable.ICON_FLAG_VISIBILITY);
}
private String getElementText() {
String text = "";
// Try to find text in correspondent descriptor
if (element instanceof JetDeclaration) {
JetDeclaration declaration = (JetDeclaration) element;
DeclarationDescriptor descriptor = getDescriptor(declaration);
if (descriptor != null) {
//text = getDescriptorTreeText(descriptor);
text = DescriptorRenderer.STARTS_FROM_NAME_WITH_SHORT_TYPES.render(descriptor);
}
}
if (StringUtil.isEmpty(text)) {
text = element.getName();
}
if (StringUtil.isEmpty(text)) {
if (element instanceof JetClassInitializer) {
return "<class initializer>";
}
if (element instanceof JetClassObject) {
return "<class object>";
}
}
return text;
}
}
@@ -0,0 +1,40 @@
-Render.kt
test1(): Unit
test2(String?): Unit
test3(T, U): Unit
test4(T): Unit
test5(): String
test6(): Comparable<String>
extension1() on String: Unit
extension2() on Comparable<T>: Unit
a: Int
a: String on Comparable<T>
b: Any
A1
-A2
a: Int
b: [ERROR : Annotation is absent]
-A3
a: Int
b: String
-A4
t: T?
-A5
Inner1
Inner2
-A6
-<class object>
test(): Unit
-A7
a: Int
<class initializer>
-Enum1 : Enum<Enum1>
FIRST : Enum1
SECOND : Enum1
Trait
Trait1 : Trait
TestWithWhere
testWithWhere(): Unit
-WithDefaultArgs
a: Int
withDefaulArgs(Int = ..., String = ...): Unit
@@ -0,0 +1,54 @@
//package test.render
fun test1() {}
fun test2(a: String?) {}
fun <T, U> test3(t: T, u: U) {}
fun <T: String> test4(t: T) {}
fun test5(): String = "some"
fun test6(): Comparable<String> = "some"
fun String.extension1() {}
fun <T> Comparable<T>.extension2() {}
val a: Int = 1
val <T> Comparable<T>.a = "String"
val b = object {}
class A1
class A2(val a: Int, var b = "some")
class A3(val a: Int) {
var b = "some"
}
class A4<T: Any>(val t: T?)
class A5 {
class Inner1
inner class Inner2
}
class A6 {
class object {
fun test() {}
}
}
class A7 {
val a: Int
{
a = 1
}
}
enum class Enum1 {
FIRST
SECOND
}
trait Trait
trait Trait1: Trait
class TestWithWhere<T> where T: Any?
fun <T> testWithWhere() where T: String {}
class WithDefaultArgs(val a: Int = 1, b: String = "str")
fun withDefaulArgs(a: Int = 1, b: String = "str") {}
@@ -1,5 +1,5 @@
-SeveralClasses.kt
A (<root>)
B (<root>)
Some (<root>)
Other (<root>)
A
B
Some : B, A
Other : A
@@ -1,6 +1,6 @@
-Simple.kt
-Test (<root>)
str:kotlin.String
some:kotlin.Int
foo():kotlin.Int
other():kotlin.Unit
-Test
str: String
some: Int
foo(): Int
other(): Unit
@@ -33,9 +33,7 @@ import org.jetbrains.jet.plugin.structureView.AbstractKotlinFileStructureTest;
@TestMetadata("idea/testData/structureView/fileStructure")
public class KotlinFileStructureTestGenerated extends AbstractKotlinFileStructureTest {
public void testAllFilesPresentInFileStructure() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage",
new File("idea/testData/structureView/fileStructure"), Pattern.compile("^(.+)\\.kt$"),
true);
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/structureView/fileStructure"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("EmptyFile.kt")
@@ -48,6 +46,11 @@ public class KotlinFileStructureTestGenerated extends AbstractKotlinFileStructur
doTest("idea/testData/structureView/fileStructure/InheritedMembers.kt");
}
@TestMetadata("Render.kt")
public void testRender() throws Exception {
doTest("idea/testData/structureView/fileStructure/Render.kt");
}
@TestMetadata("SeveralClasses.kt")
public void testSeveralClasses() throws Exception {
doTest("idea/testData/structureView/fileStructure/SeveralClasses.kt");