[github] IDEA-308234 Added inspection and quick fix for the action parameters

Modified YAMLGenerator for a complex manipulation

GitOrigin-RevId: daa9b2ec247d5389898a2058d90f0b9aa75738c5
This commit is contained in:
Andrey Belyaev
2024-01-18 13:59:36 +00:00
committed by intellij-monorepo-bot
parent 1c03fffa1e
commit f338c4fd53
4 changed files with 53 additions and 1 deletions
@@ -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:
* <pre>{@code
* Given:
* keyName = "fruits";
* sequence = Map.of("apple","red", "banana","yellow");
*
* would generate YAML:
* fruits:
* apple: red
* banana: yellow
* }</pre>
* @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<String, String> 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);
@@ -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();
@@ -0,0 +1,7 @@
foo:
bar:
someKey:
abl: 1
acl: 3
blah: 2
baz: afoo
@@ -0,0 +1,3 @@
foo:
bar<caret>:
baz: afoo