From 452f30bca59367d7741007c539ff2a2005a9a3e4 Mon Sep 17 00:00:00 2001 From: Anton Lobov Date: Thu, 19 Apr 2018 09:32:28 +0200 Subject: [PATCH] WEB-32237 Kubernetes+Json. QuickHelp. add key name, type, kubernetes version --- .../extension/JsonSchemaFileProvider.java | 5 ++ .../impl/JsonSchemaDocumentationProvider.java | 63 ++++++++++++++++++- .../jsonSchema/impl/JsonSchemaType.java | 5 ++ .../documentation/checkEscaping.html | 2 +- .../documentation/checkEscaping_short.html | 2 +- .../documentation/htmlDescription.html | 2 +- .../documentation/htmlDescription_short.html | 2 +- .../jsonSchema/documentation/secondLevel.html | 2 +- .../documentation/secondLevel_short.html | 2 +- .../jsonSchema/documentation/simple.html | 2 +- .../documentation/simple_short.html | 2 +- .../documentation/withDefinition.html | 2 +- .../documentation/withDefinition_short.html | 2 +- .../documentation/withTitleInDefinition.html | 2 +- .../withTitleInDefinition_short.html | 2 +- 15 files changed, 82 insertions(+), 15 deletions(-) diff --git a/json/src/com/jetbrains/jsonSchema/extension/JsonSchemaFileProvider.java b/json/src/com/jetbrains/jsonSchema/extension/JsonSchemaFileProvider.java index 9fa26ef3ea12..cabd89838e5a 100644 --- a/json/src/com/jetbrains/jsonSchema/extension/JsonSchemaFileProvider.java +++ b/json/src/com/jetbrains/jsonSchema/extension/JsonSchemaFileProvider.java @@ -21,4 +21,9 @@ public interface JsonSchemaFileProvider { default JsonSchemaVersion getSchemaVersion() { return JsonSchemaVersion.SCHEMA_4; } + + @Nullable + default String getThirdPartyApiInformation() { + return null; + } } diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java index deb24a1d69ff..bbd5d628056e 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java @@ -7,13 +7,16 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.intellij.util.ObjectUtils; +import com.intellij.util.containers.ContainerUtil; import com.jetbrains.jsonSchema.extension.JsonLikePsiWalker; +import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider; import com.jetbrains.jsonSchema.ide.JsonSchemaService; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; public class JsonSchemaDocumentationProvider implements DocumentationProvider { @@ -59,12 +62,66 @@ public class JsonSchemaDocumentationProvider implements DocumentationProvider { if (position == null) return null; final Collection schemas = new JsonSchemaResolver(rootSchema, true, position).resolve(); + String htmlDescription = null; + List possibleTypes = ContainerUtil.newArrayList(); for (JsonSchemaObject schema : schemas) { - final String htmlDescription = getBestDocumentation(preferShort, schema); - if (htmlDescription != null) return htmlDescription; + if (htmlDescription == null) { + htmlDescription = getBestDocumentation(preferShort, schema); + } + if (schema.getType() != null && schema.getType() != JsonSchemaType._any) { + possibleTypes.add(schema.getType()); + } + else if (schema.getTypeVariants() != null) { + possibleTypes.addAll(schema.getTypeVariants()); + } } - return null; + return htmlDescription == null + ? null + : appendNameTypeAndApi(position, getThirdPartyApiInfo(element, rootSchema), possibleTypes, htmlDescription); + } + + @Nullable + private static String concatTypeInfo(@NotNull List possibleTypes) { + if (possibleTypes.size() == 0) return null; + if (possibleTypes.size() == 1) return possibleTypes.get(0).getDescription(); + + return StringUtil.join(possibleTypes.stream().map(t -> t.getDescription()).distinct().sorted().collect(Collectors.toList()), " | "); + } + @NotNull + private static String appendNameTypeAndApi(@NotNull List position, + @NotNull String apiInfo, + @NotNull List possibleTypes, + @NotNull String htmlDescription) { + if (position.size() == 0) return htmlDescription; + + JsonSchemaVariantsTreeBuilder.Step lastStep = position.get(position.size() - 1); + String name = lastStep.getName(); + if (name == null) return htmlDescription; + + String type = ""; + String schemaType = concatTypeInfo(possibleTypes); + if (schemaType != null) { + type = ": " + schemaType; + } + + htmlDescription = "" + name + "" + type + apiInfo + "

" + htmlDescription; + return htmlDescription; + } + + @NotNull + private static String getThirdPartyApiInfo(@NotNull PsiElement element, + @NotNull JsonSchemaObject rootSchema) { + JsonSchemaService service = JsonSchemaService.Impl.get(element.getProject()); + String apiInfo = ""; + JsonSchemaFileProvider provider = service.getSchemaProvider(rootSchema.getSchemaFile()); + if (provider != null) { + String information = provider.getThirdPartyApiInformation(); + if (information != null) { + apiInfo = "  (" + information + ")"; + } + } + return apiInfo; } @Nullable diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java index 493360038a99..d9119b3b3204 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java @@ -37,4 +37,9 @@ public enum JsonSchemaType { return false; } } + + public String getDescription() { + if (this == _any) return "*"; + return getName(); + } } diff --git a/json/tests/testData/jsonSchema/documentation/checkEscaping.html b/json/tests/testData/jsonSchema/documentation/checkEscaping.html index eca626c4c961..70f2c3b78c6f 100644 --- a/json/tests/testData/jsonSchema/documentation/checkEscaping.html +++ b/json/tests/testData/jsonSchema/documentation/checkEscaping.html @@ -1 +1 @@ -Number of port to expose on the pod's IP address. "This" must be a valid port number, 0 < x < 65536. \ No newline at end of file +no-reference

Number of port to expose on the pod's IP address. "This" must be a valid port number, 0 < x < 65536. \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/checkEscaping_short.html b/json/tests/testData/jsonSchema/documentation/checkEscaping_short.html index c73fc4ac3bd3..000f062541ca 100644 --- a/json/tests/testData/jsonSchema/documentation/checkEscaping_short.html +++ b/json/tests/testData/jsonSchema/documentation/checkEscaping_short.html @@ -1 +1 @@ -Disallows /// <reference path=> imports (use ES6-style imports instead) \ No newline at end of file +no-reference

Disallows /// <reference path=> imports (use ES6-style imports instead) \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/htmlDescription.html b/json/tests/testData/jsonSchema/documentation/htmlDescription.html index 75282281f64a..440e208f414b 100644 --- a/json/tests/testData/jsonSchema/documentation/htmlDescription.html +++ b/json/tests/testData/jsonSchema/documentation/htmlDescription.html @@ -1 +1 @@ -Hello and enjoy new description! \ No newline at end of file +ccc

Hello and enjoy new description! \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/htmlDescription_short.html b/json/tests/testData/jsonSchema/documentation/htmlDescription_short.html index 75282281f64a..440e208f414b 100644 --- a/json/tests/testData/jsonSchema/documentation/htmlDescription_short.html +++ b/json/tests/testData/jsonSchema/documentation/htmlDescription_short.html @@ -1 +1 @@ -Hello and enjoy new description! \ No newline at end of file +ccc

Hello and enjoy new description! \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/secondLevel.html b/json/tests/testData/jsonSchema/documentation/secondLevel.html index 888aa3fb0ae8..07fdaf88a184 100644 --- a/json/tests/testData/jsonSchema/documentation/secondLevel.html +++ b/json/tests/testData/jsonSchema/documentation/secondLevel.html @@ -1 +1 @@ -my second level property description 1 \ No newline at end of file +my_second_level_property

my second level property description 1 \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/secondLevel_short.html b/json/tests/testData/jsonSchema/documentation/secondLevel_short.html index 888aa3fb0ae8..07fdaf88a184 100644 --- a/json/tests/testData/jsonSchema/documentation/secondLevel_short.html +++ b/json/tests/testData/jsonSchema/documentation/secondLevel_short.html @@ -1 +1 @@ -my second level property description 1 \ No newline at end of file +my_second_level_property

my second level property description 1 \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/simple.html b/json/tests/testData/jsonSchema/documentation/simple.html index c4941ce9e4db..22b66a96c70d 100644 --- a/json/tests/testData/jsonSchema/documentation/simple.html +++ b/json/tests/testData/jsonSchema/documentation/simple.html @@ -1 +1 @@ -my property description 1 \ No newline at end of file +my_property

my property description 1 \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/simple_short.html b/json/tests/testData/jsonSchema/documentation/simple_short.html index c4941ce9e4db..22b66a96c70d 100644 --- a/json/tests/testData/jsonSchema/documentation/simple_short.html +++ b/json/tests/testData/jsonSchema/documentation/simple_short.html @@ -1 +1 @@ -my property description 1 \ No newline at end of file +my_property

my property description 1 \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/withDefinition.html b/json/tests/testData/jsonSchema/documentation/withDefinition.html index d4bcb21909aa..a29d09cd6507 100644 --- a/json/tests/testData/jsonSchema/documentation/withDefinition.html +++ b/json/tests/testData/jsonSchema/documentation/withDefinition.html @@ -1 +1 @@ -Description found through definition \ No newline at end of file +source

Description found through definition \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/withDefinition_short.html b/json/tests/testData/jsonSchema/documentation/withDefinition_short.html index d4bcb21909aa..a29d09cd6507 100644 --- a/json/tests/testData/jsonSchema/documentation/withDefinition_short.html +++ b/json/tests/testData/jsonSchema/documentation/withDefinition_short.html @@ -1 +1 @@ -Description found through definition \ No newline at end of file +source

Description found through definition \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/withTitleInDefinition.html b/json/tests/testData/jsonSchema/documentation/withTitleInDefinition.html index d4bcb21909aa..a29d09cd6507 100644 --- a/json/tests/testData/jsonSchema/documentation/withTitleInDefinition.html +++ b/json/tests/testData/jsonSchema/documentation/withTitleInDefinition.html @@ -1 +1 @@ -Description found through definition \ No newline at end of file +source

Description found through definition \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/withTitleInDefinition_short.html b/json/tests/testData/jsonSchema/documentation/withTitleInDefinition_short.html index 3fc51fb08d52..f20987de0773 100644 --- a/json/tests/testData/jsonSchema/documentation/withTitleInDefinition_short.html +++ b/json/tests/testData/jsonSchema/documentation/withTitleInDefinition_short.html @@ -1 +1 @@ -Title found through definition \ No newline at end of file +source

Title found through definition \ No newline at end of file