KotlinStructureElementPresentation: J2K
This commit is contained in:
+68
-98
@@ -14,149 +14,119 @@
|
||||
* 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.LocationPresentation;
|
||||
import com.intellij.openapi.editor.colors.CodeInsightColors;
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey;
|
||||
import com.intellij.openapi.util.Iconable;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.NavigatablePsiElement;
|
||||
import com.intellij.util.PsiIconUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.kotlin.idea.KotlinDescriptorIconProvider;
|
||||
import org.jetbrains.kotlin.psi.KtAnonymousInitializer;
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner;
|
||||
import org.jetbrains.kotlin.psi.KtPsiUtil;
|
||||
import com.intellij.navigation.ColoredItemPresentation
|
||||
import com.intellij.navigation.LocationPresentation
|
||||
import com.intellij.openapi.editor.colors.CodeInsightColors
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey
|
||||
import com.intellij.openapi.util.Iconable
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.NavigatablePsiElement
|
||||
import com.intellij.util.PsiIconUtil
|
||||
import com.intellij.util.containers.ContainerUtil
|
||||
import com.intellij.util.ui.UIUtil
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.idea.KotlinDescriptorIconProvider
|
||||
import org.jetbrains.kotlin.psi.KtAnonymousInitializer
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer.Companion.ONLY_NAMES_WITH_SHORT_TYPES
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.getAllOverriddenDeclarations
|
||||
import org.jetbrains.kotlin.resolve.OverridingUtil.filterOutOverridden
|
||||
import javax.swing.Icon
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Set;
|
||||
internal class KotlinStructureElementPresentation(
|
||||
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;
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.getAllOverriddenDeclarations;
|
||||
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);
|
||||
override fun getTextAttributesKey(): TextAttributesKey? {
|
||||
return attributesKey
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextAttributesKey getTextAttributesKey() {
|
||||
return attributesKey;
|
||||
override fun getPresentableText(): String? {
|
||||
return elementText
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getPresentableText() {
|
||||
return elementText;
|
||||
override fun getLocationString(): String? {
|
||||
return locationString
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getLocationString() {
|
||||
return locationString;
|
||||
override fun getIcon(unused: Boolean): Icon? {
|
||||
return icon
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Icon getIcon(boolean unused) {
|
||||
return icon;
|
||||
override fun getLocationPrefix(): String {
|
||||
return if (isInherited) " " else LocationPresentation.DEFAULT_LOCATION_PREFIX
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocationPrefix() {
|
||||
return isInherited ? " " : LocationPresentation.DEFAULT_LOCATION_PREFIX;
|
||||
override fun getLocationSuffix(): String {
|
||||
return if (isInherited) "" else LocationPresentation.DEFAULT_LOCATION_SUFFIX
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocationSuffix() {
|
||||
return isInherited ? "" : LocationPresentation.DEFAULT_LOCATION_SUFFIX;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static TextAttributesKey getElementAttributesKey(boolean isInherited, @NotNull NavigatablePsiElement navigatablePsiElement) {
|
||||
private fun getElementAttributesKey(isInherited: Boolean, navigatablePsiElement: NavigatablePsiElement): TextAttributesKey? {
|
||||
if (isInherited) {
|
||||
return CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES;
|
||||
return CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES
|
||||
}
|
||||
|
||||
if (navigatablePsiElement instanceof KtModifierListOwner && KtPsiUtil.isDeprecated((KtModifierListOwner) navigatablePsiElement)) {
|
||||
return CodeInsightColors.DEPRECATED_ATTRIBUTES;
|
||||
if (navigatablePsiElement is KtModifierListOwner && KtPsiUtil.isDeprecated(navigatablePsiElement)) {
|
||||
return CodeInsightColors.DEPRECATED_ATTRIBUTES
|
||||
}
|
||||
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Icon getElementIcon(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
|
||||
private fun getElementIcon(navigatablePsiElement: NavigatablePsiElement, descriptor: DeclarationDescriptor?): Icon? {
|
||||
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 static String getElementText(@NotNull NavigatablePsiElement navigatablePsiElement, @Nullable DeclarationDescriptor descriptor) {
|
||||
private fun getElementText(navigatablePsiElement: NavigatablePsiElement, descriptor: DeclarationDescriptor?): String? {
|
||||
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)) {
|
||||
return text;
|
||||
return text
|
||||
}
|
||||
|
||||
if (navigatablePsiElement instanceof KtAnonymousInitializer) {
|
||||
return "<class initializer>";
|
||||
if (navigatablePsiElement is KtAnonymousInitializer) {
|
||||
return "<class initializer>"
|
||||
}
|
||||
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getElementLocationString(boolean isInherited, @Nullable DeclarationDescriptor descriptor) {
|
||||
if (!(isInherited && descriptor instanceof CallableMemberDescriptor)) return null;
|
||||
private fun getElementLocationString(isInherited: Boolean, descriptor: DeclarationDescriptor?): String? {
|
||||
if (!(isInherited && descriptor is CallableMemberDescriptor)) return null
|
||||
|
||||
CallableMemberDescriptor callableMemberDescriptor = (CallableMemberDescriptor) descriptor;
|
||||
|
||||
if (callableMemberDescriptor.getKind() == CallableMemberDescriptor.Kind.DECLARATION) {
|
||||
return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(callableMemberDescriptor.getContainingDeclaration()));
|
||||
if (descriptor.kind == CallableMemberDescriptor.Kind.DECLARATION) {
|
||||
return withRightArrow(ONLY_NAMES_WITH_SHORT_TYPES.render(descriptor.containingDeclaration))
|
||||
}
|
||||
|
||||
Set<CallableMemberDescriptor> overridingDescriptors = filterOutOverridden(getAllOverriddenDeclarations(callableMemberDescriptor));
|
||||
CallableMemberDescriptor firstOverriding = ContainerUtil.getFirstItem(overridingDescriptors);
|
||||
val overridingDescriptors = filterOutOverridden(getAllOverriddenDeclarations(descriptor))
|
||||
val firstOverriding = ContainerUtil.getFirstItem(overridingDescriptors)
|
||||
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
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
private static String withRightArrow(String str) {
|
||||
char rightArrow = '\u2192';
|
||||
return UIUtil.getLabelFont().canDisplay(rightArrow) ? rightArrow + str : "->" + str;
|
||||
private fun withRightArrow(str: String): String {
|
||||
val rightArrow = '\u2192'
|
||||
return if (UIUtil.getLabelFont().canDisplay(rightArrow)) rightArrow + str else "->" + str
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user