|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-present, Jim Kynde Meyer |
| 3 | + * All rights reserved. |
| 4 | + * <p> |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +package com.intellij.lang.jsgraphql; |
| 9 | + |
| 10 | +import com.intellij.ide.IconProvider; |
| 11 | +import com.intellij.lang.jsgraphql.icons.JSGraphQLIcons; |
| 12 | +import com.intellij.lang.jsgraphql.psi.GraphQLArgument; |
| 13 | +import com.intellij.lang.jsgraphql.psi.GraphQLElement; |
| 14 | +import com.intellij.lang.jsgraphql.psi.GraphQLEnumTypeDefinition; |
| 15 | +import com.intellij.lang.jsgraphql.psi.GraphQLEnumTypeExtensionDefinition; |
| 16 | +import com.intellij.lang.jsgraphql.psi.GraphQLEnumValue; |
| 17 | +import com.intellij.lang.jsgraphql.psi.GraphQLEnumValueDefinition; |
| 18 | +import com.intellij.lang.jsgraphql.psi.GraphQLField; |
| 19 | +import com.intellij.lang.jsgraphql.psi.GraphQLFieldDefinition; |
| 20 | +import com.intellij.lang.jsgraphql.psi.GraphQLFragmentDefinition; |
| 21 | +import com.intellij.lang.jsgraphql.psi.GraphQLFragmentSpread; |
| 22 | +import com.intellij.lang.jsgraphql.psi.GraphQLInlineFragment; |
| 23 | +import com.intellij.lang.jsgraphql.psi.GraphQLInputObjectTypeDefinition; |
| 24 | +import com.intellij.lang.jsgraphql.psi.GraphQLInputObjectTypeExtensionDefinition; |
| 25 | +import com.intellij.lang.jsgraphql.psi.GraphQLInputValueDefinition; |
| 26 | +import com.intellij.lang.jsgraphql.psi.GraphQLInterfaceTypeDefinition; |
| 27 | +import com.intellij.lang.jsgraphql.psi.GraphQLInterfaceTypeExtensionDefinition; |
| 28 | +import com.intellij.lang.jsgraphql.psi.GraphQLObjectField; |
| 29 | +import com.intellij.lang.jsgraphql.psi.GraphQLObjectTypeDefinition; |
| 30 | +import com.intellij.lang.jsgraphql.psi.GraphQLObjectTypeExtensionDefinition; |
| 31 | +import com.intellij.lang.jsgraphql.psi.GraphQLOperationType; |
| 32 | +import com.intellij.lang.jsgraphql.psi.GraphQLScalarTypeDefinition; |
| 33 | +import com.intellij.lang.jsgraphql.psi.GraphQLScalarTypeExtensionDefinition; |
| 34 | +import com.intellij.lang.jsgraphql.psi.GraphQLSelectionSetOperationDefinition; |
| 35 | +import com.intellij.lang.jsgraphql.psi.GraphQLTypeDefinition; |
| 36 | +import com.intellij.lang.jsgraphql.psi.GraphQLTypeExtension; |
| 37 | +import com.intellij.lang.jsgraphql.psi.GraphQLTypeName; |
| 38 | +import com.intellij.lang.jsgraphql.psi.GraphQLTypeNameDefinition; |
| 39 | +import com.intellij.lang.jsgraphql.psi.GraphQLTypedOperationDefinition; |
| 40 | +import com.intellij.lang.jsgraphql.psi.GraphQLUnionMembers; |
| 41 | +import com.intellij.lang.jsgraphql.psi.GraphQLUnionTypeDefinition; |
| 42 | +import com.intellij.lang.jsgraphql.psi.GraphQLUnionTypeExtensionDefinition; |
| 43 | +import com.intellij.psi.PsiElement; |
| 44 | +import com.intellij.psi.util.PsiTreeUtil; |
| 45 | +import org.jetbrains.annotations.NotNull; |
| 46 | +import org.jetbrains.annotations.Nullable; |
| 47 | + |
| 48 | +import javax.swing.*; |
| 49 | + |
| 50 | +public class GraphQLIconProvider extends IconProvider { |
| 51 | + @Nullable |
| 52 | + @Override |
| 53 | + public Icon getIcon(@NotNull PsiElement element, int flags) { |
| 54 | + |
| 55 | + if (element instanceof GraphQLElement) { |
| 56 | + |
| 57 | + if (element instanceof GraphQLSelectionSetOperationDefinition) { |
| 58 | + return JSGraphQLIcons.Schema.Query; |
| 59 | + } |
| 60 | + |
| 61 | + if (element instanceof GraphQLInlineFragment) { |
| 62 | + return JSGraphQLIcons.Schema.Fragment; |
| 63 | + } |
| 64 | + |
| 65 | + if (element instanceof GraphQLTypedOperationDefinition) { |
| 66 | + return getOperationIcon((GraphQLTypedOperationDefinition) element); |
| 67 | + } |
| 68 | + |
| 69 | + final PsiElement parent = element.getParent(); |
| 70 | + |
| 71 | + if (parent instanceof GraphQLTypedOperationDefinition) { |
| 72 | + return getOperationIcon((GraphQLTypedOperationDefinition) parent); |
| 73 | + } |
| 74 | + |
| 75 | + if (parent instanceof GraphQLEnumValue) { |
| 76 | + return JSGraphQLIcons.Schema.Enum; |
| 77 | + } |
| 78 | + |
| 79 | + if (parent instanceof GraphQLTypeNameDefinition) { |
| 80 | + final GraphQLTypeDefinition typeDefinition = PsiTreeUtil.getParentOfType(element, GraphQLTypeDefinition.class); |
| 81 | + if (typeDefinition instanceof GraphQLObjectTypeDefinition || typeDefinition instanceof GraphQLUnionTypeDefinition) { |
| 82 | + return JSGraphQLIcons.Schema.Type; |
| 83 | + } |
| 84 | + if (typeDefinition instanceof GraphQLInterfaceTypeDefinition || typeDefinition instanceof GraphQLInputObjectTypeDefinition) { |
| 85 | + return JSGraphQLIcons.Schema.Interface; |
| 86 | + } |
| 87 | + if (typeDefinition instanceof GraphQLScalarTypeDefinition) { |
| 88 | + return JSGraphQLIcons.Schema.Scalar; |
| 89 | + } |
| 90 | + if (typeDefinition instanceof GraphQLEnumTypeDefinition) { |
| 91 | + return JSGraphQLIcons.Schema.Enum; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (parent instanceof GraphQLTypeName) { |
| 96 | + |
| 97 | + final GraphQLTypeExtension typeDefinition = PsiTreeUtil.getParentOfType(element, GraphQLTypeExtension.class); |
| 98 | + if (typeDefinition instanceof GraphQLObjectTypeExtensionDefinition || typeDefinition instanceof GraphQLUnionTypeExtensionDefinition) { |
| 99 | + return JSGraphQLIcons.Schema.Type; |
| 100 | + } |
| 101 | + if (typeDefinition instanceof GraphQLInterfaceTypeExtensionDefinition || typeDefinition instanceof GraphQLInputObjectTypeExtensionDefinition) { |
| 102 | + return JSGraphQLIcons.Schema.Interface; |
| 103 | + } |
| 104 | + if (typeDefinition instanceof GraphQLScalarTypeExtensionDefinition) { |
| 105 | + return JSGraphQLIcons.Schema.Scalar; |
| 106 | + } |
| 107 | + if (typeDefinition instanceof GraphQLEnumTypeExtensionDefinition) { |
| 108 | + return JSGraphQLIcons.Schema.Enum; |
| 109 | + } |
| 110 | + |
| 111 | + if (PsiTreeUtil.getParentOfType(element, GraphQLUnionMembers.class) != null) { |
| 112 | + return JSGraphQLIcons.Schema.Type; |
| 113 | + } |
| 114 | + |
| 115 | + if (PsiTreeUtil.getParentOfType(element, GraphQLEnumValueDefinition.class) != null) { |
| 116 | + return JSGraphQLIcons.Schema.Enum; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + if (parent instanceof GraphQLFragmentDefinition || parent instanceof GraphQLInlineFragment || parent instanceof GraphQLFragmentSpread) { |
| 122 | + return JSGraphQLIcons.Schema.Fragment; |
| 123 | + } |
| 124 | + |
| 125 | + if (PsiTreeUtil.findFirstParent(element, false, p -> p instanceof GraphQLArgument || p instanceof GraphQLInputValueDefinition) != null) { |
| 126 | + return JSGraphQLIcons.Schema.Attribute; |
| 127 | + } |
| 128 | + if (PsiTreeUtil.findFirstParent(element, false, p -> p instanceof GraphQLField || p instanceof GraphQLFieldDefinition || p instanceof GraphQLObjectField) != null) { |
| 129 | + return JSGraphQLIcons.Schema.Field; |
| 130 | + } |
| 131 | + |
| 132 | + // fallback to just showing the GraphQL logo |
| 133 | + return JSGraphQLIcons.Logos.GraphQL; |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + private Icon getOperationIcon(GraphQLTypedOperationDefinition typedOperationDefinition) { |
| 141 | + final GraphQLOperationType operationType = typedOperationDefinition.getOperationType(); |
| 142 | + switch (operationType.getText()) { |
| 143 | + case "query": |
| 144 | + return JSGraphQLIcons.Schema.Query; |
| 145 | + case "mutation": |
| 146 | + return JSGraphQLIcons.Schema.Mutation; |
| 147 | + case "subscription": |
| 148 | + return JSGraphQLIcons.Schema.Subscription; |
| 149 | + default: |
| 150 | + return JSGraphQLIcons.Logos.GraphQL; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments