diff --git a/plugins/yaml/src/org/jetbrains/yaml/YAMLElementGenerator.java b/plugins/yaml/src/org/jetbrains/yaml/YAMLElementGenerator.java
index 3393a272354a..8795f50d091e 100644
--- a/plugins/yaml/src/org/jetbrains/yaml/YAMLElementGenerator.java
+++ b/plugins/yaml/src/org/jetbrains/yaml/YAMLElementGenerator.java
@@ -16,6 +16,7 @@ import org.jetbrains.yaml.psi.impl.YAMLQuotedTextImpl;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
public class YAMLElementGenerator {
private final Project myProject;
@@ -43,6 +44,36 @@ public class YAMLElementGenerator {
return sb.toString();
}
+ /**
+ * Create a YAML key-value pair with a sequence as the value.
+ * Example:
+ *
{@code
+ * Given:
+ * keyName = "fruits";
+ * sequence = Map.of("apple","red", "banana","yellow");
+ *
+ * would generate YAML:
+ * fruits:
+ * apple: red
+ * banana: yellow
+ * }
+ * @param keyName The name of the key.
+ * @param sequence The sequence to be used as the value.
+ * @return The created YAML key-value pair.
+ */
+ public YAMLKeyValue createYamlKeyValueWithSequence(@NotNull String keyName, @NotNull Map sequence) {
+ if (sequence.isEmpty()) return createYamlKeyValue(keyName, "");
+
+ StringBuilder sb = new StringBuilder(keyName + ":\n");
+ sequence.entrySet().stream()
+ .sorted(Map.Entry.comparingByKey())
+ .forEach(entry -> sb.append(" %s: %s\n".formatted(entry.getKey(), entry.getValue())));
+
+ YAMLKeyValue yamlKeyValueWithSequence = YAMLUtil.getTopLevelKeys(createDummyYamlWithText(sb.toString()))
+ .stream().findFirst().orElseThrow(() -> new RuntimeException("Cannot create dummy YAML file using string: "+sb));
+ return yamlKeyValueWithSequence;
+ }
+
@NotNull
public YAMLKeyValue createYamlKeyValue(@NotNull String keyName, @NotNull String valueText) {
final PsiFile tempValueFile = createDummyYamlWithText(valueText);
diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/YAMLMappingModificationTest.java b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/YAMLMappingModificationTest.java
index 0c6720450618..caa693d190f0 100644
--- a/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/YAMLMappingModificationTest.java
+++ b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/YAMLMappingModificationTest.java
@@ -12,6 +12,8 @@ import org.jetbrains.yaml.psi.YAMLFile;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YAMLMapping;
+import java.util.Map;
+
public class YAMLMappingModificationTest extends BasePlatformTestCase {
@Override
protected String getTestDataPath() {
@@ -113,7 +115,16 @@ public class YAMLMappingModificationTest extends BasePlatformTestCase {
- bla
- bla""");
}
-
+
+ public void testSetValueCompoundSequence() {
+ YAMLElementGenerator yamlGenerator = YAMLElementGenerator.getInstance(getProject());
+ YAMLKeyValue sequence = yamlGenerator.createYamlKeyValueWithSequence(
+ "someKey",
+ Map.of("abl", "1", "blah", "2", "acl", "3")
+ );
+ doValueTest(sequence.getText());
+ }
+
private void doValueTest(final String valueText) {
myFixture.configureByFile(getTestName(true) + ".yml");
final int offset = myFixture.getCaretOffset();
diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.txt b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.txt
new file mode 100644
index 000000000000..587e5550747a
--- /dev/null
+++ b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.txt
@@ -0,0 +1,7 @@
+foo:
+ bar:
+ someKey:
+ abl: 1
+ acl: 3
+ blah: 2
+ baz: afoo
\ No newline at end of file
diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.yml b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.yml
new file mode 100644
index 000000000000..62e9724b0ec8
--- /dev/null
+++ b/plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.yml
@@ -0,0 +1,3 @@
+foo:
+ bar:
+ baz: afoo
\ No newline at end of file