mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
[github] IDEA-308234 Refactor YAML key-value sequence generation
We use a stream and `Collectors.joining()` for sorting and forming a string. The corresponding unit test has also been updated to test smaller code parts GitOrigin-RevId: c8c919ae1f186e850327cda9e9984d6baa3a05e6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
39f0697d66
commit
3e673702fe
@@ -17,6 +17,7 @@ import org.jetbrains.yaml.psi.impl.YAMLQuotedTextImpl;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class YAMLElementGenerator {
|
||||
private final Project myProject;
|
||||
@@ -62,16 +63,11 @@ public class YAMLElementGenerator {
|
||||
* @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()
|
||||
String yamlString = 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;
|
||||
.map(entry -> "%s: %s".formatted(entry.getKey(), entry.getValue()))
|
||||
.collect(Collectors.joining("\n"));
|
||||
return createYamlKeyValue(keyName, yamlString);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -120,15 +120,28 @@ public class YAMLMappingModificationTest extends BasePlatformTestCase {
|
||||
- bla""");
|
||||
}
|
||||
|
||||
public void testSetValueCompoundSequence() {
|
||||
public void testGenerateValueCompoundSequence() {
|
||||
YAMLElementGenerator yamlGenerator = YAMLElementGenerator.getInstance(getProject());
|
||||
YAMLKeyValue sequence = yamlGenerator.createYamlKeyValueWithSequence(
|
||||
"someKey",
|
||||
Map.of("abl", "1", "blah", "2", "acl", "3")
|
||||
);
|
||||
doValueTest(sequence.getText());
|
||||
assertEquals("""
|
||||
someKey:
|
||||
abl: 1
|
||||
acl: 3
|
||||
blah: 2""", sequence.getText());
|
||||
}
|
||||
|
||||
public void testSetValueCompoundSequence() {
|
||||
doValueTest("""
|
||||
someKey:
|
||||
abl: 1
|
||||
acl: 3
|
||||
blah: 2""");
|
||||
}
|
||||
|
||||
|
||||
private void doValueTest(final String valueText) {
|
||||
myFixture.configureByFile(getTestName(true) + ".yml");
|
||||
final int offset = myFixture.getCaretOffset();
|
||||
|
||||
Reference in New Issue
Block a user