YAMLValue topLevelValue = yamlFile.getDocuments().get(0).getTopLevelValue(); if(topLevelValue instanceof YAMLMapping) { childOfType = (YAMLMapping) topLevelValue; YAMLFile dummyFile = YAMLElementGenerator.getInstance(yamlFile.getProject()).createDummyYamlWithText(chainedKey); new WriteCommandAction(yamlFile.getProject()) { @Override protected void run(@NotNull Result result) throws Throwable {
String name = yamlFile.getName().toLowerCase(); boolean iAmMetadataFile = name.contains(".odm.") || name.contains(".orm.") || name.contains(".mongodb.") || name.contains(".couchdb.");
/** * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#insertKeyIntoFile * TODO empty file */ public void skipTestInsertKeyIntoEmptyFile() { YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, ""); YamlHelper.insertKeyIntoFile(yamlFile, "value", "car", "bar", "apple"); assertEquals("" + "foo:\n" + " bar:\n" + " car: test\n" + "car:\n" + " bar:\n" + " apple: value", yamlFile.getText() ); }
/** * Find last known KeyValue of key path, so that we can merge new incoming keys */ @NotNull private static Pair<YAMLKeyValue, String[]> findLastKnownKeyInFile(@NotNull YAMLFile file, @NotNull String... keys) { YAMLKeyValue last = null; YAMLMapping mapping = ObjectUtils.tryCast(file.getDocuments().get(0).getTopLevelValue(), YAMLMapping.class); for (int i = 0; i < keys.length; i++) { String s = keys[i]; if (mapping == null) { return Pair.create(last, Arrays.copyOfRange(keys, i, keys.length)); } YAMLKeyValue keyValue = mapping.getKeyValueByKey(s); if (keyValue == null) { return Pair.create(last, Arrays.copyOfRange(keys, i, keys.length)); } last = keyValue; mapping = ObjectUtils.tryCast(keyValue.getValue(), YAMLMapping.class); } return Pair.create(last, new String[]{}); }
@NotNull public static Collection<PhpClass> getPhpClassesInYamlFile(@NotNull YAMLFile yamlFile, @NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) { Collection<PhpClass> phpClasses = new HashSet<>(); for (YAMLKeyValue keyValue : YamlHelper.getQualifiedKeyValuesInFile(yamlFile, "services")) { YAMLValue value = keyValue.getValue(); if (value instanceof YAMLMapping) { // foo.bar: // classes: ... String serviceId = ServiceContainerUtil.getServiceClassFromServiceMapping((YAMLMapping) value); if (StringUtils.isNotBlank(serviceId)) { PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(yamlFile.getProject(), serviceId, lazyServiceCollector); if (serviceClass != null) { phpClasses.add(serviceClass); } } } else if(value instanceof YAMLPlainTextImpl) { // Foo\Bar: ~ String text = keyValue.getKeyText(); if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text)) { phpClasses.addAll(PhpElementsUtil.getClassesInterface(yamlFile.getProject(), text)); } } } return phpClasses; } }
/** * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#insertKeyIntoFile */ public void testInsertKeyIntoFileOnRoot() { YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, "" + "foo:\n" + " bar:\n" + " car: test" ); YamlHelper.insertKeyIntoFile(yamlFile, "value", "car", "bar", "apple"); assertEquals("" + "foo:\n" + " bar:\n" + " car: test\n" + "car:\n" + " bar:\n" + " apple: value", yamlFile.getText() ); }
YAMLValue topLevelValue = ((YAMLFile) psiFile).getDocuments().get(0).getTopLevelValue(); if(topLevelValue instanceof YAMLMapping) { Collection<YAMLKeyValue> keyValues = ((YAMLMapping) topLevelValue).getKeyValues();
VirtualFile baseDir = yamlFile.getProject().getBaseDir(); for (VirtualFile appDir : FilesystemUtil.getAppDirectories(yamlFile.getProject())) { String path = StringUtils.stripStart(second.substring("%kernel.root_dir%".length()), "/"); VirtualFile relativeFile = VfsUtil.findRelativeFile(yamlFile.getProject().getBaseDir(), path.split("/")); if(relativeFile != null) { String relativePath = VfsUtil.getRelativePath(relativeFile, baseDir, '/');
/** * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#insertKeyIntoFile */ public void testInsertKeyIntoFile() { YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, "" + "foo:\n" + " bar:\n" + " car: test" ); YamlHelper.insertKeyIntoFile(yamlFile, "value", "foo", "bar", "apple"); assertEquals("" + "foo:\n" + " bar:\n" + " car: test\n" + " apple: value", yamlFile.getText() ); }
/** * Try to find a valid indent value, which are spaces which we need to fill */ public static int getIndentSpaceForFile(@NotNull YAMLFile yamlFile) { List<YAMLDocument> documents = yamlFile.getDocuments(); YAMLMapping mapping = ObjectUtils.tryCast(documents.get(0).getTopLevelValue(), YAMLMapping.class); if(mapping != null) { // first first INDENT element in mapping PsiElementPattern.Capture<PsiElement> pattern = PlatformPatterns .psiElement(YAMLTokenTypes.INDENT) .with(new PsiElementPatternCondition()); for (YAMLPsiElement yamlPsiElement : mapping.getKeyValues()) { // get first value PsiElement firstChild = yamlPsiElement.getFirstChild(); if(firstChild == null) { continue; } // first valid INDENT PsiElement nextSiblingOfType = PsiElementUtils.getNextSiblingOfType(firstChild, pattern); if(nextSiblingOfType != null && nextSiblingOfType.getTextLength() > 0) { return nextSiblingOfType.getTextLength(); } } } // default value return 4; }
/** * @see YamlUpdateArgumentServicesCallback#insert(java.util.List) */ public void testArgumentInsertOfArrayArguments() { YAMLFile yamlFile = YAMLElementGenerator.getInstance(getProject()).createDummyYamlWithText("" + "services:\n" + " foo:\n" + " class: Foo\\Foo\n" + " arguments: [ @service_container ]" ); invokeInsert(yamlFile); assertEquals("" + "services:\n" + " foo:\n" + " class: Foo\\Foo\n" + " arguments: [ @service_container, '@foo', '@bar' ]", yamlFile.getText() ); }
/** * @see YamlUpdateArgumentServicesCallback#insert(java.util.List) */ public void testArgumentInsertOfSequenceArrayArguments() { YAMLFile yamlFile = YAMLElementGenerator.getInstance(getProject()).createDummyYamlWithText("" + "services:\n" + " foo:\n" + " class: Foo\\Foo\n" + " arguments:\n" + " - @service_container" ); invokeInsert(yamlFile); assertEquals("" + "services:\n" + " foo:\n" + " class: Foo\\Foo\n" + " arguments:\n" + " - @service_container\n" + " - '@foo'\n" + " - '@bar'", yamlFile.getText() ); }
/** * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#insertKeyIntoFile */ public void testInsertKeyValueWithMissingMainKeyInRoot() { YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, "foo: foo"); YAMLKeyValue yamlKeyValue = YamlPsiElementFactory.createFromText(getProject(), YAMLKeyValue.class, "" + "my_service:\n" + " class: foo\n" + " tag: foo" ); assertNotNull(yamlKeyValue); YamlHelper.insertKeyIntoFile(yamlFile, yamlKeyValue, "services"); assertEquals("" + "foo: foo\n" + "services:\n" + " my_service:\n" + " class: foo\n" + " tag: foo", yamlFile.getText() ); }
/** * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#insertKeyIntoFile */ public void testInsertKeyWithArrayValue() { YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, "" + "services:\n" + " foo:\n" + " car: test" ); YAMLKeyValue yamlKeyValue = YamlPsiElementFactory.createFromText(getProject(), YAMLKeyValue.class, "" + "my_service:\n" + " class: foo\n" + " tag:\n" + " - foo\n" ); assertNotNull(yamlKeyValue); YamlHelper.insertKeyIntoFile(yamlFile, yamlKeyValue, "services"); assertEquals("" + "services:\n" + " foo:\n" + " car: test\n" + " my_service:\n" + " class: foo\n" + " tag:\n" + " - foo", yamlFile.getText() ); }
" arguments:\n" + " ['@foo', '@?', '@?']\n", yamlFile.getText() );