10
10
import com .intellij .lang .documentation .DocumentationProviderEx ;
11
11
import com .intellij .lang .jsgraphql .endpoint .psi .JSGraphQLEndpointDocumentationAware ;
12
12
import com .intellij .lang .jsgraphql .endpoint .psi .JSGraphQLEndpointFile ;
13
- import com .intellij .lang .jsgraphql .psi .GraphQLDirectiveDefinition ;
14
- import com .intellij .lang .jsgraphql .psi .GraphQLEnumValue ;
15
13
import com .intellij .lang .jsgraphql .psi .GraphQLFieldDefinition ;
16
- import com .intellij .lang .jsgraphql .psi .GraphQLFile ;
17
- import com .intellij .lang .jsgraphql .psi .GraphQLIdentifier ;
18
- import com .intellij .lang .jsgraphql .psi .GraphQLInputObjectTypeDefinition ;
19
- import com .intellij .lang .jsgraphql .psi .GraphQLInputObjectTypeExtensionDefinition ;
20
- import com .intellij .lang .jsgraphql .psi .GraphQLInputValueDefinition ;
21
- import com .intellij .lang .jsgraphql .psi .GraphQLNamedElement ;
22
- import com .intellij .lang .jsgraphql .psi .GraphQLPsiUtil ;
23
14
import com .intellij .lang .jsgraphql .psi .GraphQLType ;
24
- import com .intellij .lang .jsgraphql .psi .GraphQLTypeNameDefinition ;
25
- import com .intellij .lang .jsgraphql .psi .GraphQLTypeSystemDefinition ;
15
+ import com .intellij .lang .jsgraphql .psi .*;
26
16
import com .intellij .lang .jsgraphql .schema .GraphQLTypeDefinitionRegistryServiceImpl ;
27
17
import com .intellij .psi .PsiElement ;
28
18
import com .intellij .psi .PsiManager ;
29
19
import com .intellij .psi .util .PsiTreeUtil ;
30
20
import graphql .schema .GraphQLArgument ;
31
21
import graphql .schema .GraphQLDirective ;
32
- import graphql .schema .GraphQLEnumType ;
33
22
import graphql .schema .GraphQLEnumValueDefinition ;
34
- import graphql .schema .GraphQLInputObjectField ;
35
- import graphql .schema .GraphQLInputObjectType ;
36
- import graphql .schema .GraphQLInterfaceType ;
37
- import graphql .schema .GraphQLObjectType ;
38
- import graphql .schema .GraphQLSchema ;
23
+ import graphql .schema .*;
39
24
import org .apache .commons .lang .StringEscapeUtils ;
25
+ import org .jetbrains .annotations .NotNull ;
40
26
import org .jetbrains .annotations .Nullable ;
41
27
42
28
import java .util .List ;
43
29
import java .util .Objects ;
44
30
31
+ import static com .intellij .lang .documentation .DocumentationMarkup .*;
32
+
45
33
public class GraphQLDocumentationProvider extends DocumentationProviderEx {
46
34
47
35
private final static String GRAPHQL_DOC_PREFIX = "GraphQL" ;
@@ -58,10 +46,7 @@ public String getQuickNavigateInfo(PsiElement element, PsiElement originalElemen
58
46
59
47
@ Override
60
48
public String generateDoc (PsiElement element , @ Nullable PsiElement originalElement ) {
61
- if (isDocumentationSupported (element )) {
62
- return createQuickNavigateDocumentation (element , true );
63
- }
64
- return null ;
49
+ return createQuickNavigateDocumentation (element , true );
65
50
}
66
51
67
52
@ Override
@@ -78,7 +63,6 @@ private boolean isDocumentationSupported(PsiElement element) {
78
63
79
64
@ Nullable
80
65
private String createQuickNavigateDocumentation (PsiElement element , boolean fullDocumentation ) {
81
-
82
66
if (!isDocumentationSupported (element )) {
83
67
return null ;
84
68
}
@@ -100,7 +84,6 @@ private String createQuickNavigateDocumentation(PsiElement element, boolean full
100
84
101
85
if (parent instanceof GraphQLInputValueDefinition ) {
102
86
return getArgumentDocumentation (schema , (GraphQLInputValueDefinition ) parent );
103
-
104
87
}
105
88
106
89
if (parent instanceof GraphQLEnumValue ) {
@@ -114,14 +97,12 @@ private String createQuickNavigateDocumentation(PsiElement element, boolean full
114
97
return null ;
115
98
116
99
} else if (element instanceof JSGraphQLEndpointDocumentationAware ) {
117
-
118
100
final JSGraphQLEndpointDocumentationAware documentationAware = (JSGraphQLEndpointDocumentationAware ) element ;
119
101
final String documentation = documentationAware .getDocumentation (fullDocumentation );
120
- String doc = "" ;
102
+ String doc = DEFINITION_START + documentationAware . getDeclaration () + DEFINITION_END ;
121
103
if (documentation != null ) {
122
- doc += "<div style= \" margin-bottom: 4px \" >" + StringEscapeUtils .escapeHtml (documentation ) + "</div>" ;
104
+ doc += CONTENT_START + StringEscapeUtils .escapeHtml (documentation ) + CONTENT_END ;
123
105
}
124
- doc += "<code>" + documentationAware .getDeclaration () + "</code>" ;
125
106
return doc ;
126
107
}
127
108
@@ -134,10 +115,16 @@ private String getDirectiveDocumentation(GraphQLSchema schema, GraphQLDirectiveD
134
115
if (directiveName != null ) {
135
116
final GraphQLDirective schemaDirective = schema .getDirective (directiveName .getText ());
136
117
if (schemaDirective != null ) {
118
+ final StringBuilder result = new StringBuilder ().append (DEFINITION_START );
119
+ result .append ("@" ).append (schemaDirective .getName ());
120
+ result .append (DEFINITION_END );
137
121
final String description = schemaDirective .getDescription ();
138
122
if (description != null ) {
139
- return GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description );
123
+ result .append (CONTENT_START );
124
+ result .append (GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description ));
125
+ result .append (CONTENT_END );
140
126
}
127
+ return result .toString ();
141
128
}
142
129
}
143
130
return null ;
@@ -150,14 +137,21 @@ private String getEnumValueDocumentation(GraphQLSchema schema, GraphQLEnumValue
150
137
graphql .schema .GraphQLType schemaType = schema .getType (enumName );
151
138
if (schemaType instanceof GraphQLEnumType ) {
152
139
final String enumValueName = parent .getName ();
140
+ final StringBuilder result = new StringBuilder ().append (DEFINITION_START );
141
+ result .append (enumName ).append ("." ).append (enumValueName );
142
+ result .append (DEFINITION_END );
153
143
for (GraphQLEnumValueDefinition enumValueDefinition : ((GraphQLEnumType ) schemaType ).getValues ()) {
154
144
if (Objects .equals (enumValueDefinition .getName (), enumValueName )) {
155
145
final String description = enumValueDefinition .getDescription ();
156
146
if (description != null ) {
157
- return GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description );
147
+ result .append (CONTENT_START );
148
+ result .append (GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description ));
149
+ result .append (CONTENT_END );
150
+ return result .toString ();
158
151
}
159
152
}
160
153
}
154
+ return result .toString ();
161
155
}
162
156
}
163
157
return null ;
@@ -190,9 +184,7 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
190
184
if (Objects .equals (fieldDefinition .getName (), fieldName )) {
191
185
for (GraphQLArgument argument : fieldDefinition .getArguments ()) {
192
186
if (Objects .equals (argument .getName (), inputValueName )) {
193
- if (argument .getDescription () != null ) {
194
- return GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (argument .getDescription ());
195
- }
187
+ return getArgumentDocumentation (inputValueName , argument );
196
188
}
197
189
}
198
190
}
@@ -207,11 +199,7 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
207
199
if (schemaDirective != null ) {
208
200
for (GraphQLArgument argument : schemaDirective .getArguments ()) {
209
201
if (inputValueName .equals (argument .getName ())) {
210
- final String description = argument .getDescription ();
211
- if (description != null ) {
212
- return GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description );
213
- }
214
- break ;
202
+ return getArgumentDocumentation (inputValueName , argument );
215
203
}
216
204
}
217
205
}
@@ -223,11 +211,15 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
223
211
if (schemaType instanceof GraphQLInputObjectType ) {
224
212
for (GraphQLInputObjectField inputObjectField : ((GraphQLInputObjectType ) schemaType ).getFieldDefinitions ()) {
225
213
if (inputValueName .equals (inputObjectField .getName ())) {
214
+ GraphQLInputType type = inputObjectField .getType ();
215
+ final StringBuilder result = new StringBuilder ().append (DEFINITION_START );
216
+ result .append (schemaType .getName ()).append ("." );
217
+ result .append (inputValueName ).append (type != null ? ": " : "" ).append (type != null ? type .getName () : "" );
218
+ result .append (DEFINITION_END );
219
+
226
220
final String description = inputObjectField .getDescription ();
227
- if (description != null ) {
228
- return GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description );
229
- }
230
- break ;
221
+ appendDescription (result , description );
222
+ return result .toString ();
231
223
}
232
224
}
233
225
}
@@ -237,6 +229,21 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
237
229
return null ;
238
230
}
239
231
232
+ @ NotNull
233
+ private String getArgumentDocumentation (String inputValueName , GraphQLArgument argument ) {
234
+ final StringBuilder html = new StringBuilder ().append (DEFINITION_START );
235
+ GraphQLInputType argumentType = argument .getType ();
236
+ html .append (inputValueName ).append (argumentType != null ? ": " : " " ).append (argumentType != null ? argumentType .getName () : "" );
237
+ html .append (DEFINITION_END );
238
+ appendDescription (html , GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (argument .getDescription ()));
239
+ return html .toString ();
240
+ }
241
+
242
+ private void appendDescription (StringBuilder result , @ Nullable String descriptionAsHTML ) {
243
+ if (descriptionAsHTML == null ) return ;
244
+ result .append (CONTENT_START ).append (descriptionAsHTML ).append (CONTENT_END );
245
+ }
246
+
240
247
@ Nullable
241
248
private String getFieldDocumentation (PsiElement element , GraphQLSchema schema , GraphQLFieldDefinition parent ) {
242
249
final GraphQLType psiFieldType = parent .getType ();
@@ -246,10 +253,10 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
246
253
final graphql .schema .GraphQLType schemaType = schema .getType (psiTypeName .getText ());
247
254
if (schemaType != null ) {
248
255
final String fieldName = element .getText ();
249
- final StringBuilder html = new StringBuilder ().append ("<header><code>" );
250
- html .append (schemaType .getName ()).append (" " );
251
- html .append (fieldName ).append (": " ).append (psiFieldType != null ? psiFieldType .getText () : "" );
252
- html .append ("</code></header>" );
256
+ final StringBuilder html = new StringBuilder ().append (DEFINITION_START );
257
+ html .append (schemaType .getName ()).append (". " );
258
+ html .append (fieldName ).append (psiFieldType != null ? ": " : " " ).append (psiFieldType != null ? psiFieldType .getText () : "" );
259
+ html .append (DEFINITION_END );
253
260
List <graphql .schema .GraphQLFieldDefinition > fieldDefinitions = null ;
254
261
if (schemaType instanceof GraphQLObjectType ) {
255
262
fieldDefinitions = ((GraphQLObjectType ) schemaType ).getFieldDefinitions ();
@@ -259,11 +266,7 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
259
266
if (fieldDefinitions != null ) {
260
267
for (graphql .schema .GraphQLFieldDefinition fieldDefinition : fieldDefinitions ) {
261
268
if (fieldName .equals (fieldDefinition .getName ())) {
262
- if (fieldDefinition .getDescription () != null ) {
263
- html .append ("<section>" );
264
- html .append (GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (fieldDefinition .getDescription ()));
265
- html .append ("</section>" );
266
- }
269
+ appendDescription (html , GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (fieldDefinition .getDescription ()));
267
270
break ;
268
271
}
269
272
}
@@ -279,19 +282,20 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
279
282
private String getTypeDocumentation (PsiElement element , GraphQLTypeDefinitionRegistryServiceImpl typeRegistryService , GraphQLSchema schema , GraphQLTypeNameDefinition parent ) {
280
283
graphql .schema .GraphQLType schemaType = schema .getType (((GraphQLNamedElement ) element ).getName ());
281
284
if (schemaType != null ) {
285
+ final StringBuilder html = new StringBuilder ().append (DEFINITION_START );
286
+ PsiElement keyword = PsiTreeUtil .prevVisibleLeaf (parent );
287
+ if (keyword != null ) {
288
+ html .append (keyword .getText ()).append (" " );
289
+ }
290
+ html .append (element .getText ());
291
+ html .append (DEFINITION_END );
282
292
final String description = typeRegistryService .getTypeDescription (schemaType );
283
293
if (description != null ) {
284
- final StringBuilder html = new StringBuilder ().append ("<header><code>" );
285
- PsiElement keyword = PsiTreeUtil .prevVisibleLeaf (parent );
286
- if (keyword != null ) {
287
- html .append (keyword .getText ()).append (" " );
288
- }
289
- html .append (element .getText ());
290
- html .append ("</code></header><section>" );
294
+ html .append (CONTENT_START );
291
295
html .append (GraphQLDocumentationMarkdownRenderer .getDescriptionAsHTML (description ));
292
- html .append ("</section>" );
293
- return html .toString ();
296
+ html .append (CONTENT_END );
294
297
}
298
+ return html .toString ();
295
299
}
296
300
return null ;
297
301
}
0 commit comments