KotlinStructureElementPresentation: J2K

This commit is contained in:
Dmitry Jemerov
2016-11-18 11:56:06 +01:00
parent 6225867cae
commit 75a5b0ee0c
@@ -14,149 +14,119 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.structureView; package org.jetbrains.kotlin.idea.structureView
import com.intellij.navigation.ColoredItemPresentation; import com.intellij.navigation.ColoredItemPresentation
import com.intellij.navigation.LocationPresentation; import com.intellij.navigation.LocationPresentation
import com.intellij.openapi.editor.colors.CodeInsightColors; import com.intellij.openapi.editor.colors.CodeInsightColors
import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.util.Iconable; import com.intellij.openapi.util.Iconable
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.NavigatablePsiElement; import com.intellij.psi.NavigatablePsiElement
import com.intellij.util.PsiIconUtil; import com.intellij.util.PsiIconUtil
import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtil
import com.intellij.util.ui.UIUtil; import com.intellij.util.ui.UIUtil
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor; import org.jetbrains.kotlin.idea.KotlinDescriptorIconProvider
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.psi.KtAnonymousInitializer
import org.jetbrains.kotlin.idea.KotlinDescriptorIconProvider; import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.psi.KtAnonymousInitializer; import org.jetbrains.kotlin.psi.KtPsiUtil
import org.jetbrains.kotlin.psi.KtModifierListOwner; import org.jetbrains.kotlin.renderer.DescriptorRenderer.Companion.ONLY_NAMES_WITH_SHORT_TYPES
import org.jetbrains.kotlin.psi.KtPsiUtil; import org.jetbrains.kotlin.resolve.DescriptorUtils.getAllOverriddenDeclarations
import org.jetbrains.kotlin.resolve.OverridingUtil.filterOutOverridden
import javax.swing.Icon
import javax.swing.*; internal class KotlinStructureElementPresentation(
import java.util.Set; private val isInherited: Boolean,
navigatablePsiElement: NavigatablePsiElement,
descriptor: DeclarationDescriptor?
) : ColoredItemPresentation, LocationPresentation {
private val attributesKey = getElementAttributesKey(isInherited, navigatablePsiElement)
private val elementText = getElementText(navigatablePsiElement, descriptor)
private val locationString = getElementLocationString(isInherited, descriptor)
private val icon = getElementIcon(navigatablePsiElement, descriptor)
import static org.jetbrains.kotlin.renderer.DescriptorRenderer.ONLY_NAMES_WITH_SHORT_TYPES; override fun getTextAttributesKey(): TextAttributesKey? {
import static org.jetbrains.kotlin.resolve.DescriptorUtils.getAllOverriddenDeclarations; return attributesKey
import static org.jetbrains.kotlin.resolve.OverridingUtil.filterOutOverridden;
class KotlinStructureElementPresentation implements ColoredItemPresentation, LocationPresentation {
private final TextAttributesKey attributesKey;
private final String elementText;
private final String locationString;
private final Icon icon;
private final boolean isInherited;
public KotlinStructureElementPresentation(
boolean isInherited,
@NotNull NavigatablePsiElement navigatablePsiElement,
@Nullable DeclarationDescriptor descriptor
) {
this.isInherited = isInherited;
attributesKey = getElementAttributesKey(isInherited, navigatablePsiElement);
elementText = getElementText(navigatablePsiElement, descriptor);
locationString = getElementLocationString(isInherited, descriptor);
icon = getElementIcon(navigatablePsiElement, descriptor);
} }
@Nullable override fun getPresentableText(): String? {
@Override return elementText
public TextAttributesKey getTextAttributesKey() {
return attributesKey;
} }
@Nullable override fun getLocationString(): String? {
@Override return locationString
public String getPresentableText() {
return elementText;
} }
@Nullable override fun getIcon(unused: Boolean): Icon? {
@Override return icon
public String getLocationString() {
return locationString;
} }
@Nullable override fun getLocationPrefix(): String {
@Override return if (isInherited) " " else LocationPresentation.DEFAULT_LOCATION_PREFIX
public Icon getIcon(boolean unused) {
return icon;
} }
@Override override fun getLocationSuffix(): String {
public String getLocationPrefix() { return if (isInherited) "" else LocationPresentation.DEFAULT_LOCATION_SUFFIX
return isInherited ? " " : LocationPresentation.DEFAULT_LOCATION_PREFIX;
} }
@Override private fun getElementAttributesKey(isInherited: Boolean, navigatablePsiElement: NavigatablePsiElement): TextAttributesKey? {
public String getLocationSuffix() {
return isInherited ? "" : LocationPresentation.DEFAULT_LOCATION_SUFFIX;
}
@Nullable
private static TextAttributesKey getElementAttributesKey(boolean isInherited, @NotNull NavigatablePsiElement navigatablePsiElement) {
if (isInherited) { if (isInherited) {
return CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES; return CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES
} }
if (navigatablePsiElement instanceof KtModifierListOwner && KtPsiUtil.isDeprecated((KtModifierListOwner) navigatablePsiElement)) { if (navigatablePsiElement is KtModifierListOwner && KtPsiUtil.isDeprecated(navigatablePsiElement)) {
return CodeInsightColors.DEPRECATED_ATTRIBUTES; return CodeInsightColors.DEPRECATED_ATTRIBUTES
} }
return null; return null
} }
@Nullable private fun getElementIcon(navigatablePsiElement: NavigatablePsiElement, descriptor: DeclarationDescriptor?): Icon? {
private static Icon getElementIcon(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
if (descriptor != null) { if (descriptor != null) {
return KotlinDescriptorIconProvider.getIcon(descriptor, navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY); return KotlinDescriptorIconProvider.getIcon(descriptor, navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY)
} }
return PsiIconUtil.getProvidersIcon(navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY); return PsiIconUtil.getProvidersIcon(navigatablePsiElement, Iconable.ICON_FLAG_VISIBILITY)
} }
@Nullable private fun getElementText(navigatablePsiElement: NavigatablePsiElement, descriptor: DeclarationDescriptor?): String? {
private static String getElementText(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
if (descriptor != null) { if (descriptor != null) {
return ONLY_NAMES_WITH_SHORT_TYPES.render(descriptor); return ONLY_NAMES_WITH_SHORT_TYPES.render(descriptor)
} }
String text = navigatablePsiElement.getName(); val text = navigatablePsiElement.name
if (!StringUtil.isEmpty(text)) { if (!StringUtil.isEmpty(text)) {
return text; return text
} }
if (navigatablePsiElement instanceof KtAnonymousInitializer) { if (navigatablePsiElement is KtAnonymousInitializer) {
return "<class initializer>"; return "<class initializer>"
} }
return null; return null
} }
@Nullable private fun getElementLocationString(isInherited: Boolean, descriptor: DeclarationDescriptor?): String? {
private static String getElementLocationString(boolean isInherited, @Nullable DeclarationDescriptor descriptor) { if (!(isInherited && descriptor is CallableMemberDescriptor)) return null
if (!(isInherited && descriptor instanceof CallableMemberDescriptor)) return null;
CallableMemberDescriptor callableMemberDescriptor = (CallableMemberDescriptor) descriptor; if (descriptor.kind == CallableMemberDescriptor.Kind.DECLARATION) {
return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(descriptor.containingDeclaration))
if (callableMemberDescriptor.getKind() == CallableMemberDescriptor.Kind.DECLARATION) {
return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(callableMemberDescriptor.getContainingDeclaration()));
} }
Set<CallableMemberDescriptor> overridingDescriptors = filterOutOverridden(getAllOverriddenDeclarations(callableMemberDescriptor)); val overridingDescriptors = filterOutOverridden(getAllOverriddenDeclarations(descriptor))
CallableMemberDescriptor firstOverriding = ContainerUtil.getFirstItem(overridingDescriptors); val firstOverriding = ContainerUtil.getFirstItem(overridingDescriptors)
if (firstOverriding != null) { if (firstOverriding != null) {
return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(firstOverriding.getContainingDeclaration())); return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(firstOverriding.containingDeclaration))
} }
// Location can be missing when base in synthesized // Location can be missing when base in synthesized
return null; return null
} }
private static String withRightArrow(String str) { private fun withRightArrow(str: String): String {
char rightArrow = '\u2192'; val rightArrow = '\u2192'
return UIUtil.getLabelFont().canDisplay(rightArrow) ? rightArrow + str : "->" + str; return if (UIUtil.getLabelFont().canDisplay(rightArrow)) rightArrow + str else "->" + str
} }
} }