From 36a929c19dd7441ded9a2611fff04c1ba9baad11 Mon Sep 17 00:00:00 2001 From: Konstantin Aleev Date: Fri, 2 Aug 2024 17:21:27 +0200 Subject: [PATCH] [yaml] IDEA-210113 key completion insert handler: fix removing lookup if it is the only key value with colon GitOrigin-RevId: 48c750e214e352a09b52921d2dbdafc695a608a9 --- .../YamlKeyCompletionInsertHandler.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java b/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java index 20964b1e8a78..fabd69bd73c7 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java +++ b/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java @@ -125,19 +125,26 @@ public abstract class YamlKeyCompletionInsertHandler im } else if (children.length != 0 && ContainerUtil.find(children, node -> node.getElementType() == YAMLElementTypes.KEY_VALUE_PAIR) == null) { - String rootKey = "root"; - String valueText = parent.getText(); - YAMLFile file = YAMLElementGenerator.getInstance(parent.getProject()) - .createDummyYamlWithText(rootKey + ":\n " + valueText); - YAMLValue value = file.getDocuments().get(0).getTopLevelValue(); - if (value instanceof YAMLMapping mapping) { - YAMLKeyValue root = mapping.getKeyValueByKey(rootKey); - if (root != null) { - substitute = root.getValue(); - } + List notSpaces = + ContainerUtil.filter(children, child -> !YAMLElementTypes.SPACE_ELEMENTS.contains(child.getElementType())); + if (notSpaces.size() == 1) { + substitute = notSpaces.get(0).getPsi(); } - if (substitute == null) { - Logger.getInstance(YamlKeyCompletionInsertHandler.class).error("Could not substitute: " + valueText); + else { + String rootKey = "root"; + String valueText = parent.getText(); + YAMLFile file = YAMLElementGenerator.getInstance(parent.getProject()) + .createDummyYamlWithText(rootKey + ":\n " + valueText); + YAMLValue value = file.getDocuments().get(0).getTopLevelValue(); + if (value instanceof YAMLMapping mapping) { + YAMLKeyValue root = mapping.getKeyValueByKey(rootKey); + if (root != null) { + substitute = root.getValue(); + } + } + if (substitute == null) { + Logger.getInstance(YamlKeyCompletionInsertHandler.class).error("Could not substitute: " + valueText); + } } } }