Skip to content

Commit ab568f0

Browse files
Merge pull request #226 from denofevil/v2
Updated documentation provider to match new style
2 parents 97d6847 + 5cf6afa commit ab568f0

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

src/main/com/intellij/lang/jsgraphql/ide/documentation/GraphQLDocumentationMarkdownRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static String getDescriptionAsPlainText(String description, boolean strip
4040
* @return an HTML representation of the specified markdown
4141
*/
4242
public static String getDescriptionAsHTML(String description) {
43-
return HTML_RENDERER.render(PARSER.parse(description.trim()));
43+
return description != null ? HTML_RENDERER.render(PARSER.parse(description.trim())) : null;
4444
}
4545

4646
}

src/main/com/intellij/lang/jsgraphql/ide/documentation/GraphQLDocumentationProvider.java

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,26 @@
1010
import com.intellij.lang.documentation.DocumentationProviderEx;
1111
import com.intellij.lang.jsgraphql.endpoint.psi.JSGraphQLEndpointDocumentationAware;
1212
import com.intellij.lang.jsgraphql.endpoint.psi.JSGraphQLEndpointFile;
13-
import com.intellij.lang.jsgraphql.psi.GraphQLDirectiveDefinition;
14-
import com.intellij.lang.jsgraphql.psi.GraphQLEnumValue;
1513
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;
2314
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.*;
2616
import com.intellij.lang.jsgraphql.schema.GraphQLTypeDefinitionRegistryServiceImpl;
2717
import com.intellij.psi.PsiElement;
2818
import com.intellij.psi.PsiManager;
2919
import com.intellij.psi.util.PsiTreeUtil;
3020
import graphql.schema.GraphQLArgument;
3121
import graphql.schema.GraphQLDirective;
32-
import graphql.schema.GraphQLEnumType;
3322
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.*;
3924
import org.apache.commons.lang.StringEscapeUtils;
25+
import org.jetbrains.annotations.NotNull;
4026
import org.jetbrains.annotations.Nullable;
4127

4228
import java.util.List;
4329
import java.util.Objects;
4430

31+
import static com.intellij.lang.documentation.DocumentationMarkup.*;
32+
4533
public class GraphQLDocumentationProvider extends DocumentationProviderEx {
4634

4735
private final static String GRAPHQL_DOC_PREFIX = "GraphQL";
@@ -58,10 +46,7 @@ public String getQuickNavigateInfo(PsiElement element, PsiElement originalElemen
5846

5947
@Override
6048
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);
6550
}
6651

6752
@Override
@@ -78,7 +63,6 @@ private boolean isDocumentationSupported(PsiElement element) {
7863

7964
@Nullable
8065
private String createQuickNavigateDocumentation(PsiElement element, boolean fullDocumentation) {
81-
8266
if (!isDocumentationSupported(element)) {
8367
return null;
8468
}
@@ -100,7 +84,6 @@ private String createQuickNavigateDocumentation(PsiElement element, boolean full
10084

10185
if (parent instanceof GraphQLInputValueDefinition) {
10286
return getArgumentDocumentation(schema, (GraphQLInputValueDefinition) parent);
103-
10487
}
10588

10689
if (parent instanceof GraphQLEnumValue) {
@@ -114,14 +97,12 @@ private String createQuickNavigateDocumentation(PsiElement element, boolean full
11497
return null;
11598

11699
} else if (element instanceof JSGraphQLEndpointDocumentationAware) {
117-
118100
final JSGraphQLEndpointDocumentationAware documentationAware = (JSGraphQLEndpointDocumentationAware) element;
119101
final String documentation = documentationAware.getDocumentation(fullDocumentation);
120-
String doc = "";
102+
String doc = DEFINITION_START + documentationAware.getDeclaration() + DEFINITION_END;
121103
if (documentation != null) {
122-
doc += "<div style=\"margin-bottom: 4px\">" + StringEscapeUtils.escapeHtml(documentation) + "</div>";
104+
doc += CONTENT_START + StringEscapeUtils.escapeHtml(documentation) + CONTENT_END;
123105
}
124-
doc += "<code>" + documentationAware.getDeclaration() + "</code>";
125106
return doc;
126107
}
127108

@@ -134,10 +115,16 @@ private String getDirectiveDocumentation(GraphQLSchema schema, GraphQLDirectiveD
134115
if (directiveName != null) {
135116
final GraphQLDirective schemaDirective = schema.getDirective(directiveName.getText());
136117
if (schemaDirective != null) {
118+
final StringBuilder result = new StringBuilder().append(DEFINITION_START);
119+
result.append("@").append(schemaDirective.getName());
120+
result.append(DEFINITION_END);
137121
final String description = schemaDirective.getDescription();
138122
if (description != null) {
139-
return GraphQLDocumentationMarkdownRenderer.getDescriptionAsHTML(description);
123+
result.append(CONTENT_START);
124+
result.append(GraphQLDocumentationMarkdownRenderer.getDescriptionAsHTML(description));
125+
result.append(CONTENT_END);
140126
}
127+
return result.toString();
141128
}
142129
}
143130
return null;
@@ -150,14 +137,21 @@ private String getEnumValueDocumentation(GraphQLSchema schema, GraphQLEnumValue
150137
graphql.schema.GraphQLType schemaType = schema.getType(enumName);
151138
if (schemaType instanceof GraphQLEnumType) {
152139
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);
153143
for (GraphQLEnumValueDefinition enumValueDefinition : ((GraphQLEnumType) schemaType).getValues()) {
154144
if (Objects.equals(enumValueDefinition.getName(), enumValueName)) {
155145
final String description = enumValueDefinition.getDescription();
156146
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();
158151
}
159152
}
160153
}
154+
return result.toString();
161155
}
162156
}
163157
return null;
@@ -190,9 +184,7 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
190184
if (Objects.equals(fieldDefinition.getName(), fieldName)) {
191185
for (GraphQLArgument argument : fieldDefinition.getArguments()) {
192186
if (Objects.equals(argument.getName(), inputValueName)) {
193-
if (argument.getDescription() != null) {
194-
return GraphQLDocumentationMarkdownRenderer.getDescriptionAsHTML(argument.getDescription());
195-
}
187+
return getArgumentDocumentation(inputValueName, argument);
196188
}
197189
}
198190
}
@@ -207,11 +199,7 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
207199
if (schemaDirective != null) {
208200
for (GraphQLArgument argument : schemaDirective.getArguments()) {
209201
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);
215203
}
216204
}
217205
}
@@ -223,11 +211,15 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
223211
if (schemaType instanceof GraphQLInputObjectType) {
224212
for (GraphQLInputObjectField inputObjectField : ((GraphQLInputObjectType) schemaType).getFieldDefinitions()) {
225213
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+
226220
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();
231223
}
232224
}
233225
}
@@ -237,6 +229,21 @@ private String getArgumentDocumentation(GraphQLSchema schema, GraphQLInputValueD
237229
return null;
238230
}
239231

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+
240247
@Nullable
241248
private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, GraphQLFieldDefinition parent) {
242249
final GraphQLType psiFieldType = parent.getType();
@@ -246,10 +253,10 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
246253
final graphql.schema.GraphQLType schemaType = schema.getType(psiTypeName.getText());
247254
if (schemaType != null) {
248255
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);
253260
List<graphql.schema.GraphQLFieldDefinition> fieldDefinitions = null;
254261
if (schemaType instanceof GraphQLObjectType) {
255262
fieldDefinitions = ((GraphQLObjectType) schemaType).getFieldDefinitions();
@@ -259,11 +266,7 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
259266
if (fieldDefinitions != null) {
260267
for (graphql.schema.GraphQLFieldDefinition fieldDefinition : fieldDefinitions) {
261268
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()));
267270
break;
268271
}
269272
}
@@ -279,19 +282,20 @@ private String getFieldDocumentation(PsiElement element, GraphQLSchema schema, G
279282
private String getTypeDocumentation(PsiElement element, GraphQLTypeDefinitionRegistryServiceImpl typeRegistryService, GraphQLSchema schema, GraphQLTypeNameDefinition parent) {
280283
graphql.schema.GraphQLType schemaType = schema.getType(((GraphQLNamedElement) element).getName());
281284
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);
282292
final String description = typeRegistryService.getTypeDescription(schemaType);
283293
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);
291295
html.append(GraphQLDocumentationMarkdownRenderer.getDescriptionAsHTML(description));
292-
html.append("</section>");
293-
return html.toString();
296+
html.append(CONTENT_END);
294297
}
298+
return html.toString();
295299
}
296300
return null;
297301
}

0 commit comments

Comments
 (0)