/** * Simplify getting of array psi elements in array or sequence context * * arguments: [@foo] * arguments: * - @foo * * TODO: can be handled nice know because on new yaml plugin */ @Nullable public static List<PsiElement> getYamlArrayOnSequenceOrArrayElements(@NotNull YAMLCompoundValue yamlCompoundValue) { if (yamlCompoundValue instanceof YAMLSequence) { return new ArrayList<>(((YAMLSequence) yamlCompoundValue).getItems()); } if (yamlCompoundValue instanceof YAMLMapping) { return new ArrayList<>(((YAMLMapping) yamlCompoundValue).getKeyValues()); } return null; }
@NotNull public static Collection<YAMLKeyValue> getTopLevelKeyValues(@NotNull YAMLFile yamlFile) { YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(yamlFile, YAMLDocument.class); if(yamlDocument == null) { return Collections.emptyList(); } YAMLValue topLevelValue = yamlDocument.getTopLevelValue(); if(!(topLevelValue instanceof YAMLMapping)) { return Collections.emptyList(); } return ((YAMLMapping) topLevelValue).getKeyValues(); }
/** * Visit all children key values of a parent key value */ private static void visitNextKeyValues(@NotNull YAMLKeyValue yamlKeyValue, @NotNull Consumer<YAMLKeyValue> consumer) { List<YAMLPsiElement> yamlElements = yamlKeyValue.getYAMLElements(); // @TODO: multiple? if(yamlElements.size() != 1) { return; } YAMLPsiElement next = yamlElements.iterator().next(); if(!(next instanceof YAMLMapping)) { return; } for (YAMLKeyValue keyValue : ((YAMLMapping) next).getKeyValues()) { consumer.consume(keyValue); } }
private Optional<PsiElement> addProperty(YAMLMapping yamlMapping, YAMLKeyValue yamlKeyValue) { final List<YAMLKeyValue> keyValues = Lists.newArrayList(yamlMapping.getKeyValues()); return Optional.ofNullable(ContainerUtil.getLastItem(keyValues)) .map( lastKeyValue -> { final PsiElement addedKeyValue = yamlMapping.addAfter(yamlKeyValue, lastKeyValue); yamlMapping.addBefore( new YAMLElementGenerator(yamlMapping.getProject()).createEol(), addedKeyValue); return addedKeyValue; }); }
for (YAMLKeyValue yamlKey : ((YAMLMapping) topLevelValue).getKeyValues()) { String keyText = yamlKey.getKeyText(); if(StringUtils.isBlank(keyText)) {
Collection<YAMLKeyValue> keyValues = ((YAMLMapping) topLevelValue).getKeyValues(); if(keyValues.size() > 0) { for(YAMLKeyValue yamlKeyValue: EntityHelper.getYamlModelFieldKeyValues(keyValues.iterator().next()).values()) {
PsiElement yamlKeyValueLastChild = yamlKeyValue.getLastChild(); if (yamlKeyValueLastChild instanceof YAMLMapping) { for (YAMLKeyValue keyValue : ((YAMLMapping) yamlKeyValueLastChild).getKeyValues()) { visitEnvironmentSquenceItems(consumer, keyValue);
/** * 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; }