From 4a273f51d8f274600176fd13bb3a504d1563ddbf Mon Sep 17 00:00:00 2001 From: Nikita Katkov Date: Wed, 23 Oct 2024 14:20:58 +0200 Subject: [PATCH] [json + yaml] IJPL-163460 Implement fast exit for yaml deprecation inspection There is no point in traversing schema if there are no deprecated nodes specified in it. So we first index the in-memory schema instance and perform fast exit in case no deprecation keywords were detected (cherry picked from commit decd0a968fa3e04326b4fdeec3cdab01563439fb) IJ-CR-147983 GitOrigin-RevId: 2f816ec1c9867bf9b81f560524917d274705fc1a --- .../nodes/RootJsonSchemaObjectBackedByJackson.kt | 13 +++++++++++++ .../schema/YamlJsonSchemaDeprecationInspection.java | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/json/src/com/jetbrains/jsonSchema/impl/light/nodes/RootJsonSchemaObjectBackedByJackson.kt b/json/src/com/jetbrains/jsonSchema/impl/light/nodes/RootJsonSchemaObjectBackedByJackson.kt index f61e964e9771..78ea15905f5f 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/light/nodes/RootJsonSchemaObjectBackedByJackson.kt +++ b/json/src/com/jetbrains/jsonSchema/impl/light/nodes/RootJsonSchemaObjectBackedByJackson.kt @@ -16,6 +16,7 @@ import org.jetbrains.annotations.ApiStatus private val IDS_MAP_KEY = Key>("ids") private val DYNAMIC_ANCHORS_MAP_KEY = Key>("dynamicAnchors") private val INJECTIONS_MAP_KEY = Key("injections") +private val DEPRECATIONS_MAP_KEY = Key("deprecations") @ApiStatus.Internal class RootJsonSchemaObjectBackedByJackson(rootNode: JsonNode, val schemaFile: VirtualFile?) @@ -43,6 +44,18 @@ class RootJsonSchemaObjectBackedByJackson(rootNode: JsonNode, val schemaFile: Vi } } + fun checkHasDeprecations(): Boolean { + val deprecationMarker = schemaInterpretationStrategy.deprecationKeyword ?: return false + return getOrComputeValue(DEPRECATIONS_MAP_KEY) { + indexSchema(rawSchemaNode) { _, parentPointer -> + if (parentPointer.lastOrNull() == deprecationMarker) + true + else + null + }.any { it } + } + } + override fun getFileUrl(): String? { return schemaFile?.url } diff --git a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java index b482bcb72f61..246033b39d81 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java +++ b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java @@ -10,6 +10,7 @@ import com.jetbrains.jsonSchema.extension.JsonLikePsiWalker; import com.jetbrains.jsonSchema.impl.JsonSchemaObject; import com.jetbrains.jsonSchema.impl.JsonSchemaResolver; import com.jetbrains.jsonSchema.impl.MatchResult; +import com.jetbrains.jsonSchema.impl.light.nodes.RootJsonSchemaObjectBackedByJackson; import org.jetbrains.annotations.NotNull; import org.jetbrains.yaml.YAMLBundle; import org.jetbrains.yaml.psi.YAMLKeyValue; @@ -24,9 +25,13 @@ public class YamlJsonSchemaDeprecationInspection extends YamlJsonSchemaInspectio @NotNull LocalInspectionToolSession session, Collection roots, JsonSchemaObject schema) { + if (schema == null || (schema instanceof RootJsonSchemaObjectBackedByJackson rootSchema && !rootSchema.checkHasDeprecations())) { + return PsiElementVisitor.EMPTY_VISITOR; + } + PsiElement sampleElement = roots.iterator().next(); final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(sampleElement, schema); - if (walker == null || schema == null) { + if (walker == null) { return PsiElementVisitor.EMPTY_VISITOR; } Project project = sampleElement.getProject();