diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java index 18295a8b0da0..986cc6a20043 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java @@ -186,6 +186,7 @@ public class JsonSchemaCompletionContributor extends CompletionContributor { final Map schemaProperties = schema.getProperties(); addAllPropertyVariants(insertComma, hasValue, properties, adapter, schemaProperties, knownNames); addIfThenElsePropertyNameVariants(schema, insertComma, hasValue, properties, adapter, knownNames); + addPropertyNameSchemaVariants(schema); } if (isName != ThreeState.YES) { @@ -198,6 +199,19 @@ public class JsonSchemaCompletionContributor extends CompletionContributor { } } + private void addPropertyNameSchemaVariants(@NotNull JsonSchemaObject schema) { + JsonSchemaObject propertyNamesSchema = schema.getPropertyNamesSchema(); + if (propertyNamesSchema == null) return; + List anEnum = propertyNamesSchema.getEnum(); + if (anEnum == null) return; + for (Object o : anEnum) { + if (!(o instanceof String)) continue; + String key = ((String)o); + key = !shouldWrapInQuotes(key, false) ? key : StringUtil.wrapWithDoubleQuote(key); + myVariants.add(LookupElementBuilder.create(StringUtil.unquoteString(key))); + } + } + private void addIfThenElsePropertyNameVariants(@NotNull JsonSchemaObject schema, boolean insertComma, boolean hasValue, diff --git a/json/src/com/jetbrains/jsonSchema/impl/fixes/SuggestEnumValuesFix.java b/json/src/com/jetbrains/jsonSchema/impl/fixes/SuggestEnumValuesFix.java index 5f5a2b6e3837..535745194a86 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/fixes/SuggestEnumValuesFix.java +++ b/json/src/com/jetbrains/jsonSchema/impl/fixes/SuggestEnumValuesFix.java @@ -8,6 +8,8 @@ import com.intellij.codeInspection.BatchQuickFix; import com.intellij.codeInspection.CommonProblemDescriptor; import com.intellij.codeInspection.LocalQuickFix; import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.json.psi.JsonElementGenerator; +import com.intellij.json.psi.JsonProperty; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ex.EditorEx; @@ -61,7 +63,14 @@ public class SuggestEnumValuesFix implements LocalQuickFix, BatchQuickFix element.delete()); + PsiElement parent = element.getParent(); + boolean isJsonPropName = parent instanceof JsonProperty && ((JsonProperty)parent).getNameElement() == element; + if (isJsonPropName) { + WriteAction.run(() -> element.replace(new JsonElementGenerator(project).createStringLiteral(""))); + } + else { + WriteAction.run(() -> element.delete()); + } EditorEx editor = EditorUtil.getEditorEx(fileEditor); assert editor != null; // this is a workaround for buggy formatters such as in YAML - it removes the whitespace after ':' when deleting the value @@ -73,6 +82,9 @@ public class SuggestEnumValuesFix implements LocalQuickFix, BatchQuickFixmyPropertyXxx: a"); } + + public void testPropertyNameSchema() throws Exception { + doTest("{\n" + + " \"type\": \"object\",\n" + + " \"patternProperties\": {\n" + + " \".*\": {\n" + + " \"type\": \"boolean\"\n" + + " }\n" + + " },\n" + + " \"propertyNames\": {\n" + + " \"enum\": [\"a\", \"b\"]\n" + + " }\n" + + "}", "r: true"); + } }