From 69d390a5e7dd8d36598528cd58fe2a5cf03ea3b8 Mon Sep 17 00:00:00 2001 From: Anton Lobov Date: Thu, 27 Jun 2019 18:23:23 +0200 Subject: [PATCH] IDEA-217138 Properly replace name element, add completion from property names schema, add highlighting in YAML GitOrigin-RevId: fa54e8c1c6b66cd840ade5102431e78e74d8a666 --- .../impl/JsonSchemaCompletionContributor.java | 14 ++++ .../impl/fixes/SuggestEnumValuesFix.java | 14 +++- .../yaml/schema/YamlPropertyAdapter.java | 5 +- .../yaml/schema/YamlPropertyKeyAdapter.java | 65 +++++++++++++++++++ .../YamlByJsonSchemaHighlightingTest.java | 14 ++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 plugins/yaml/src/org/jetbrains/yaml/schema/YamlPropertyKeyAdapter.java 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"); + } }