/** * 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[]{}); }
YAMLValue topLevelValue = ((YAMLFile) psiFile).getDocuments().get(0).getTopLevelValue(); if(topLevelValue instanceof YAMLMapping) { Collection<YAMLKeyValue> keyValues = ((YAMLMapping) topLevelValue).getKeyValues();
/** * 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; }
YAMLValue topLevelValue = yamlFile.getDocuments().get(0).getTopLevelValue(); if(topLevelValue instanceof YAMLMapping) { childOfType = (YAMLMapping) topLevelValue;