[amper] provide a custom insert handler for enum item for a json schema

this is needed to handle cases when the value is "expandable" - there are two different options to write the same (product = lib or product {type = lib}), but in some cases one of them requires expanding by default (lib requires a set of platforms)

instead of introducing the notion of "expandable" into the schema, we just provide an ability to provide a custom completion insert handler to handle tricky cases like that

GitOrigin-RevId: 5e54f19326891d7d02061b338efb392fc0ae2634
This commit is contained in:
Anton Lobov
2024-04-11 11:17:25 +02:00
committed by intellij-monorepo-bot
parent 873e54b7eb
commit d2d2a80c11
3 changed files with 28 additions and 7 deletions

View File

@@ -190,6 +190,8 @@
interface="com.jetbrains.jsonSchema.extension.JsonSchemaNestedCompletionsTreeProvider" dynamic="true"/>
<extensionPoint qualifiedName="com.intellij.json.jsonSchemaEnabler" interface="com.jetbrains.jsonSchema.extension.JsonSchemaEnabler"
dynamic="true"/>
<extensionPoint qualifiedName="com.intellij.json.jsonSchemaCompletionHandlerProvider" interface="com.jetbrains.jsonSchema.extension.JsonSchemaCompletionHandlerProvider"
dynamic="true"/>
<extensionPoint qualifiedName="com.intellij.json.jsonWidgetSuppressor"
interface="com.jetbrains.jsonSchema.extension.JsonWidgetSuppressor" dynamic="true"/>
<extensionPoint qualifiedName="com.intellij.json.jsonLiteralChecker" interface="com.intellij.json.codeinsight.JsonLiteralChecker"

View File

@@ -0,0 +1,16 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.jsonSchema.extension;
import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.jetbrains.jsonSchema.impl.JsonSchemaObject;
import org.jetbrains.annotations.Nullable;
public interface JsonSchemaCompletionHandlerProvider {
ExtensionPointName<JsonSchemaCompletionHandlerProvider> EXTENSION_POINT_NAME = ExtensionPointName.create("com.intellij.json.jsonSchemaCompletionHandlerProvider");
default @Nullable InsertHandler<LookupElement> createHandlerForEnumValue(JsonSchemaObject schema, String value) {
return null;
}
}

View File

@@ -37,10 +37,7 @@ import com.intellij.util.ObjectUtils;
import com.intellij.util.ThreeState;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.JBIterable;
import com.jetbrains.jsonSchema.extension.JsonLikePsiWalker;
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider;
import com.jetbrains.jsonSchema.extension.JsonSchemaNestedCompletionsTreeProvider;
import com.jetbrains.jsonSchema.extension.SchemaType;
import com.jetbrains.jsonSchema.extension.*;
import com.jetbrains.jsonSchema.extension.adapters.JsonObjectValueAdapter;
import com.jetbrains.jsonSchema.extension.adapters.JsonPropertyAdapter;
import com.jetbrains.jsonSchema.ide.JsonSchemaService;
@@ -284,7 +281,7 @@ public final class JsonSchemaCompletionContributor extends CompletionContributor
});
}
// some schemas provide empty array / empty object in enum values...
// some schemas provide an empty array or an empty object in enum values...
private static final Set<String> filtered = Set.of("[]", "{}", "[ ]", "{ }");
private void suggestValues(JsonSchemaObject schema, boolean isSurelyValue, SchemaPath completionPath) {
@@ -292,7 +289,9 @@ public final class JsonSchemaCompletionContributor extends CompletionContributor
suggestValuesForSchemaVariants(schema.getOneOf(), isSurelyValue, completionPath);
suggestValuesForSchemaVariants(schema.getAllOf(), isSurelyValue, completionPath);
if (schema.getEnum() != null && completionPath == null) {
if (schema.getEnum() != null && completionPath == null) {
// custom insert handlers are currently applicable only to enum values but can be extended later to cover more cases
List<JsonSchemaCompletionHandlerProvider> customHandlers = JsonSchemaCompletionHandlerProvider.EXTENSION_POINT_NAME.getExtensionList();
Map<String, Map<String, String>> metadata = schema.getEnumMetadata();
boolean isEnumOrderSensitive = Boolean.parseBoolean(schema.readChildNodeValue(X_INTELLIJ_ENUM_ORDER_SENSITIVE));
List<Object> anEnum = schema.getEnum();
@@ -305,7 +304,11 @@ public final class JsonSchemaCompletionContributor extends CompletionContributor
String description = valueMetadata == null ? null : valueMetadata.get("description");
String deprecated = valueMetadata == null ? null : valueMetadata.get("deprecationMessage");
Integer order = isEnumOrderSensitive ? i : null;
addValueVariant(variant, description, deprecated != null ? (variant + " (" + deprecated + ")") : null, null, order);
List<InsertHandler<LookupElement>> handlers = customHandlers.stream()
.map(p -> p.createHandlerForEnumValue(schema, variant))
.filter(h -> h != null).toList();
addValueVariant(variant, description, deprecated != null ? (variant + " (" + deprecated + ")") : null,
handlers.size() == 1 ? handlers.get(0) : null, order);
}
}
}