From f338c4fd53f9eb487209b2cdea6f8ee6aa6d21c2 Mon Sep 17 00:00:00 2001 From: Andrey Belyaev Date: Thu, 18 Jan 2024 11:42:02 +0100 Subject: [PATCH] [github] IDEA-308234 Added inspection and quick fix for the action parameters Modified YAMLGenerator for a complex manipulation GitOrigin-RevId: daa9b2ec247d5389898a2058d90f0b9aa75738c5 --- .../jetbrains/yaml/YAMLElementGenerator.java | 31 +++++++++++++++++++ .../YAMLMappingModificationTest.java | 13 +++++++- .../data/setValueCompoundSequence.txt | 7 +++++ .../data/setValueCompoundSequence.yml | 3 ++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.txt create mode 100644 plugins/yaml/testSrc/org/jetbrains/yaml/psiModification/data/setValueCompoundSequence.yml 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