diff --git a/plugins/yaml/editing/src/org/jetbrains/yaml/YAMLUtil.java b/plugins/yaml/editing/src/org/jetbrains/yaml/YAMLUtil.java index 2f6617457575..3ce8e12484c6 100644 --- a/plugins/yaml/editing/src/org/jetbrains/yaml/YAMLUtil.java +++ b/plugins/yaml/editing/src/org/jetbrains/yaml/YAMLUtil.java @@ -244,7 +244,7 @@ public final class YAMLUtil { throw e; } else { - Logger.getInstance(YAMLUtil.class).warn(YAMLBlockMappingImpl.EMPTY_MAP_MESSAGE); + Logger.getInstance(YAMLUtil.class).error(YAMLBlockMappingImpl.EMPTY_MAP_MESSAGE); } } } diff --git a/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java b/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java index 1a24dd813832..20964b1e8a78 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java +++ b/plugins/yaml/src/org/jetbrains/yaml/completion/YamlKeyCompletionInsertHandler.java @@ -6,6 +6,7 @@ import com.intellij.codeInsight.completion.InsertionContext; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.lang.ASTNode; import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtilEx; @@ -18,10 +19,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.yaml.YAMLBundle; -import org.jetbrains.yaml.YAMLElementGenerator; -import org.jetbrains.yaml.YAMLTokenTypes; -import org.jetbrains.yaml.YAMLUtil; +import org.jetbrains.yaml.*; import org.jetbrains.yaml.psi.*; import java.util.List; @@ -116,29 +114,44 @@ public abstract class YamlKeyCompletionInsertHandler im context.setTailOffset(keyValue.getTextRange().getEndOffset()); WriteCommandAction.runWriteCommandAction(context.getProject(), YAMLBundle.message("YamlKeyCompletionInsertHandler.remove.key"), - null, - () -> { - PsiElement parent = keyValue.getParent(); - PsiElement substitute = null; - boolean delete = parent.getNode().getChildren(null).length == 1; - if (parent instanceof YAMLMapping parentMapping) { - parentMapping.deleteKeyValue(keyValue); - ASTNode[] children = parent.getNode().getChildren(null); - if (children.length == 1 && children[0] instanceof LeafPsiElement) { - substitute = children[0].getPsi(); - } - } - else { - YAMLUtil.deleteSurroundingWhitespace(keyValue); - keyValue.delete(); - } - if (delete) { - parent.delete(); - } - else if (substitute != null) { - parent.replace(substitute); - } - }); + null, () -> { + PsiElement parent = keyValue.getParent(); + PsiElement substitute = null; + if (parent instanceof YAMLMapping parentMapping) { + parentMapping.deleteKeyValue(keyValue); + ASTNode[] children = parent.getNode().getChildren(null); + if (children.length == 1 && children[0] instanceof LeafPsiElement) { + substitute = children[0].getPsi(); + } + 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(); + } + } + if (substitute == null) { + Logger.getInstance(YamlKeyCompletionInsertHandler.class).error("Could not substitute: " + valueText); + } + } + } + else { + YAMLUtil.deleteSurroundingWhitespace(keyValue); + keyValue.delete(); + } + if (parent.getNode().getChildren(null).length == 0) { + parent.delete(); + } + else if (substitute != null) { + parent.replace(substitute); + } + }); return oldValue; }