KotlinIconProvider: J2K and cleanup
This commit is contained in:
@@ -14,163 +14,106 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea;
|
package org.jetbrains.kotlin.idea
|
||||||
|
|
||||||
import com.intellij.ide.IconProvider;
|
import com.intellij.ide.IconProvider
|
||||||
import com.intellij.openapi.project.DumbAware;
|
import com.intellij.openapi.project.DumbAware
|
||||||
import com.intellij.openapi.util.Condition;
|
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.PsiElement
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.ui.RowIcon
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.util.PlatformIcons
|
||||||
import com.intellij.ui.RowIcon;
|
import org.jetbrains.kotlin.asJava.KtLightClassForExplicitDeclaration
|
||||||
import com.intellij.util.PlatformIcons;
|
import org.jetbrains.kotlin.asJava.KtLightClassForFacade
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import org.jetbrains.kotlin.idea.caches.resolve.KtLightClassForDecompiledDeclaration
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.asJava.KtLightClassForExplicitDeclaration;
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.asJava.KtLightClassForFacade;
|
import javax.swing.Icon
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.KtLightClassForDecompiledDeclaration;
|
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
|
||||||
import org.jetbrains.kotlin.psi.*;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
class KotlinIconProvider : IconProvider(), DumbAware {
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class KotlinIconProvider extends IconProvider implements DumbAware {
|
override fun getIcon(psiElement: PsiElement, flags: Int): Icon? {
|
||||||
|
if (psiElement is KtFile) {
|
||||||
public static KotlinIconProvider INSTANCE = new KotlinIconProvider();
|
val mainClass = getMainClass(psiElement)
|
||||||
|
return if (mainClass != null && psiElement.declarations.size == 1) getIcon(mainClass, flags) else KotlinIcons.FILE
|
||||||
@Nullable
|
|
||||||
public static KtClassOrObject getMainClass(@NotNull KtFile file) {
|
|
||||||
List<KtDeclaration> classes = ContainerUtil.filter(file.getDeclarations(), new Condition<KtDeclaration>() {
|
|
||||||
@Override
|
|
||||||
public boolean value(KtDeclaration jetDeclaration) {
|
|
||||||
return jetDeclaration instanceof KtClassOrObject;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (classes.size() == 1) {
|
|
||||||
if (StringUtil.getPackageName(file.getName()).equals(classes.get(0).getName())) {
|
|
||||||
return (KtClassOrObject) classes.get(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
val result = psiElement.getBaseIcon()
|
||||||
|
if (flags and Iconable.ICON_FLAG_VISIBILITY > 0 && result != null && psiElement is KtModifierListOwner) {
|
||||||
|
val list = psiElement.modifierList
|
||||||
|
return createRowIcon(result, getVisibilityIcon(list))
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
companion object {
|
||||||
public Icon getIcon(@NotNull PsiElement psiElement, int flags) {
|
|
||||||
if (psiElement instanceof KtFile) {
|
|
||||||
KtFile file = (KtFile) psiElement;
|
|
||||||
KtClassOrObject mainClass = getMainClass(file);
|
|
||||||
return mainClass != null && file.getDeclarations().size() == 1 ? getIcon(mainClass, flags) : KotlinIcons.FILE;
|
|
||||||
}
|
|
||||||
|
|
||||||
Icon result = getBaseIcon(psiElement);
|
var INSTANCE = KotlinIconProvider()
|
||||||
if ((flags & Iconable.ICON_FLAG_VISIBILITY) > 0 && psiElement instanceof KtModifierListOwner) {
|
|
||||||
KtModifierList list = ((KtModifierListOwner) psiElement).getModifierList();
|
|
||||||
result = createRowIcon(result, getVisibilityIcon(list));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static RowIcon createRowIcon(Icon baseIcon, Icon visibilityIcon) {
|
fun getMainClass(file: KtFile): KtClassOrObject? {
|
||||||
RowIcon rowIcon = new RowIcon(2);
|
val classes = file.declarations.filterIsInstance<KtClassOrObject>()
|
||||||
rowIcon.setIcon(baseIcon, 0);
|
if (classes.size == 1 && StringUtil.getPackageName(file.name) == classes[0].name) {
|
||||||
rowIcon.setIcon(visibilityIcon, 1);
|
return classes[0]
|
||||||
return rowIcon;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Icon getVisibilityIcon(@Nullable KtModifierList list) {
|
|
||||||
if (list != null) {
|
|
||||||
if (list.hasModifier(KtTokens.PRIVATE_KEYWORD)) {
|
|
||||||
return PlatformIcons.PRIVATE_ICON;
|
|
||||||
}
|
|
||||||
if (list.hasModifier(KtTokens.PROTECTED_KEYWORD)) {
|
|
||||||
return PlatformIcons.PROTECTED_ICON;
|
|
||||||
}
|
|
||||||
if (list.hasModifier(KtTokens.INTERNAL_KEYWORD)) {
|
|
||||||
return PlatformIcons.PACKAGE_LOCAL_ICON;
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
return PlatformIcons.PUBLIC_ICON;
|
private fun createRowIcon(baseIcon: Icon, visibilityIcon: Icon): RowIcon {
|
||||||
}
|
val rowIcon = RowIcon(2)
|
||||||
|
rowIcon.setIcon(baseIcon, 0)
|
||||||
public static Icon getBaseIcon(PsiElement psiElement) {
|
rowIcon.setIcon(visibilityIcon, 1)
|
||||||
if (psiElement instanceof KtPackageDirective) {
|
return rowIcon
|
||||||
return PlatformIcons.PACKAGE_ICON;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (psiElement instanceof KtLightClassForFacade) {
|
fun getVisibilityIcon(list: KtModifierList?): Icon {
|
||||||
return KotlinIcons.FILE;
|
if (list != null) {
|
||||||
}
|
if (list.hasModifier(KtTokens.PRIVATE_KEYWORD)) {
|
||||||
|
return PlatformIcons.PRIVATE_ICON
|
||||||
if (psiElement instanceof KtLightClassForDecompiledDeclaration) {
|
}
|
||||||
KtClassOrObject origin = ((KtLightClassForDecompiledDeclaration) psiElement).getOrigin();
|
if (list.hasModifier(KtTokens.PROTECTED_KEYWORD)) {
|
||||||
if (origin != null) {
|
return PlatformIcons.PROTECTED_ICON
|
||||||
psiElement = origin;
|
}
|
||||||
|
if (list.hasModifier(KtTokens.INTERNAL_KEYWORD)) {
|
||||||
|
return PlatformIcons.PACKAGE_LOCAL_ICON
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
return PlatformIcons.PUBLIC_ICON
|
||||||
|
}
|
||||||
|
|
||||||
|
fun PsiElement.getBaseIcon(): Icon? = when(this) {
|
||||||
|
is KtPackageDirective -> PlatformIcons.PACKAGE_ICON
|
||||||
|
is KtLightClassForFacade -> KotlinIcons.FILE
|
||||||
|
is KtLightClassForDecompiledDeclaration -> {
|
||||||
|
val origin = getOrigin()
|
||||||
//TODO (light classes for decompiled files): correct presentation
|
//TODO (light classes for decompiled files): correct presentation
|
||||||
return KotlinIcons.CLASS;
|
if (origin != null) origin.getBaseIcon() else KotlinIcons.CLASS
|
||||||
}
|
}
|
||||||
}
|
is KtLightClassForExplicitDeclaration -> navigationElement.getBaseIcon()
|
||||||
|
is KtNamedFunction -> when {
|
||||||
if (psiElement instanceof KtLightClassForExplicitDeclaration) {
|
receiverTypeReference != null -> KotlinIcons.EXTENSION_FUNCTION
|
||||||
psiElement = psiElement.getNavigationElement();
|
getStrictParentOfType<KtNamedDeclaration>() is KtClass ->
|
||||||
}
|
if (KtPsiUtil.isAbstract(this)) PlatformIcons.ABSTRACT_METHOD_ICON else PlatformIcons.METHOD_ICON
|
||||||
|
else -> KotlinIcons.FUNCTION
|
||||||
if (psiElement instanceof KtNamedFunction) {
|
|
||||||
if (((KtFunction) psiElement).getReceiverTypeReference() != null) {
|
|
||||||
return KotlinIcons.EXTENSION_FUNCTION;
|
|
||||||
}
|
}
|
||||||
|
is KtFunctionLiteral -> KotlinIcons.LAMBDA
|
||||||
if (PsiTreeUtil.getParentOfType(psiElement, KtNamedDeclaration.class) instanceof KtClass) {
|
is KtClass -> when {
|
||||||
if (KtPsiUtil.isAbstract((KtFunction) psiElement)) {
|
isInterface() -> KotlinIcons.TRAIT
|
||||||
return PlatformIcons.ABSTRACT_METHOD_ICON;
|
isEnum() -> KotlinIcons.ENUM
|
||||||
}
|
this is KtEnumEntry && getPrimaryConstructorParameterList() == null -> KotlinIcons.ENUM
|
||||||
else {
|
else -> KotlinIcons.CLASS
|
||||||
return PlatformIcons.METHOD_ICON;
|
}
|
||||||
|
is KtObjectDeclaration -> KotlinIcons.OBJECT
|
||||||
|
is KtParameter -> {
|
||||||
|
if (KtPsiUtil.getClassIfParameterIsProperty(this) != null) {
|
||||||
|
if (isMutable) KotlinIcons.FIELD_VAR else KotlinIcons.FIELD_VAL
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
KotlinIcons.PARAMETER
|
||||||
}
|
}
|
||||||
else {
|
is KtProperty -> if (isVar) KotlinIcons.FIELD_VAR else KotlinIcons.FIELD_VAL
|
||||||
return KotlinIcons.FUNCTION;
|
else -> null
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (psiElement instanceof KtFunctionLiteral) return KotlinIcons.LAMBDA;
|
|
||||||
|
|
||||||
if (psiElement instanceof KtClass) {
|
|
||||||
KtClass ktClass = (KtClass) psiElement;
|
|
||||||
if (ktClass.isInterface()) {
|
|
||||||
return KotlinIcons.TRAIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
Icon icon = ktClass.isEnum() ? KotlinIcons.ENUM : KotlinIcons.CLASS;
|
|
||||||
if (ktClass instanceof KtEnumEntry) {
|
|
||||||
KtEnumEntry enumEntry = (KtEnumEntry) ktClass;
|
|
||||||
if (enumEntry.getPrimaryConstructorParameterList() == null) {
|
|
||||||
icon = KotlinIcons.ENUM;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return icon;
|
|
||||||
}
|
|
||||||
if (psiElement instanceof KtObjectDeclaration) {
|
|
||||||
return KotlinIcons.OBJECT;
|
|
||||||
}
|
|
||||||
if (psiElement instanceof KtParameter) {
|
|
||||||
KtParameter parameter = (KtParameter) psiElement;
|
|
||||||
if (KtPsiUtil.getClassIfParameterIsProperty(parameter) != null) {
|
|
||||||
return parameter.isMutable() ? KotlinIcons.FIELD_VAR : KotlinIcons.FIELD_VAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return KotlinIcons.PARAMETER;
|
|
||||||
}
|
|
||||||
if (psiElement instanceof KtProperty) {
|
|
||||||
KtProperty property = (KtProperty) psiElement;
|
|
||||||
return property.isVar() ? KotlinIcons.FIELD_VAR : KotlinIcons.FIELD_VAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class KtClassOrObjectTreeNode extends AbstractPsiBasedNode<KtClassOrObjec
|
|||||||
data.setPresentableText(classOrObject.getName());
|
data.setPresentableText(classOrObject.getName());
|
||||||
|
|
||||||
AbstractTreeNode parent = getParent();
|
AbstractTreeNode parent = getParent();
|
||||||
if (KotlinIconProvider.getMainClass(classOrObject.getContainingKtFile()) != null) {
|
if (KotlinIconProvider.Companion.getMainClass(classOrObject.getContainingKtFile()) != null) {
|
||||||
if (parent instanceof KtFileTreeNode) {
|
if (parent instanceof KtFileTreeNode) {
|
||||||
update(parent.getParent());
|
update(parent.getParent());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user