Java Code Examples for org.jetbrains.yaml.psi.YAMLMapping

The following examples show how to use org.jetbrains.yaml.psi.YAMLMapping. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: intellij-spring-assistant   Source File: DuplicateKeyAnnotator.java    License: MIT License 6 votes vote down vote up
@Override
public void annotate(@NotNull final PsiElement element,
    @NotNull final AnnotationHolder annotationHolder) {
  if (element instanceof YAMLMapping) {
    // TODO: Fix
    //      final YAMLMapping mapping = (YAMLMapping) element;
    //      final Collection<YAMLKeyValue> keyValues = mapping.getKeyValues();
    //      final Set<String> existingKeys = new THashSet<>(keyValues.size());
    //      for (final YAMLKeyValue keyValue : keyValues) {
    //        if (keyValue.getKey() != null && !existingKeys.add(keyValue.getKeyText().trim())) {
    //          annotationHolder.createErrorAnnotation(keyValue.getKey(),
    //              "Duplicated PROPERTY '" + keyValue.getKeyText() + "'")
    //              .registerFix(new DeletePropertyIntentionAction());
    //        }
    //      }
  }
}
 
Example 2
Source Project: intellij-neos   Source File: EelHelperUtil.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static HashMap<String, String> getHelpersInFile(@NotNull PsiFile psiFile) {
    YAMLKeyValue defaultContext = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, "Neos", "Fusion", "defaultContext");

    if (defaultContext == null) {
        defaultContext = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, "TYPO3", "TypoScript", "defaultContext");
    }

    HashMap<String, String> result = new HashMap<>();
    if (defaultContext != null) {
        PsiElement mapping = defaultContext.getLastChild();
        if (mapping instanceof YAMLMapping) {
            for (PsiElement mappingElement : mapping.getChildren()) {
                if (mappingElement instanceof YAMLKeyValue) {
                    YAMLKeyValue keyValue = (YAMLKeyValue) mappingElement;
                    result.put(keyValue.getKeyText(), keyValue.getValueText());
                    NeosProjectService.getLogger().debug(keyValue.getKeyText() + ": " + keyValue.getValueText());
                }
            }
        }
    }

    return result;
}
 
Example 3
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder annotationHolder) {
    if (!KubernetesYamlPsiUtil.isKubernetesFile(element)) {
        return;
    }
    final ModelProvider modelProvider = ModelProvider.INSTANCE;
    final ResourceTypeKey resourceKey = KubernetesYamlPsiUtil.findResourceKey(element);
    if (resourceKey != null && element instanceof YAMLKeyValue) {
        final YAMLKeyValue keyValue = (YAMLKeyValue) element;
        final Model model = KubernetesYamlPsiUtil.modelForKey(modelProvider, resourceKey, keyValue);
        if (keyValue.getValue() instanceof YAMLMapping && model != null) {
            final YAMLMapping mapping = (YAMLMapping) keyValue.getValue();
            final Set<String> expectedProperties = model.getProperties().keySet();
            //noinspection ConstantConditions
            mapping.getKeyValues()
                   .stream()
                   .filter(k -> !expectedProperties.contains(k.getKeyText().trim()))
                   .forEach(k -> annotationHolder.createWarningAnnotation(k.getKey(), "Property '" + k.getKeyText() + "' is not expected here.").registerFix(new DeletePropertyIntentionAction()));
        }
    }
}
 
Example 4
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder annotationHolder) {
    if (!KubernetesYamlPsiUtil.isKubernetesFile(element)) {
        return;
    }
    final ModelProvider modelProvider = ModelProvider.INSTANCE;
    final ResourceTypeKey resourceKey = KubernetesYamlPsiUtil.findResourceKey(element);
    if (resourceKey != null && element instanceof YAMLKeyValue) {
        final YAMLKeyValue keyValue = (YAMLKeyValue) element;
        final Model model = KubernetesYamlPsiUtil.modelForKey(modelProvider, resourceKey, keyValue);
        if (model != null && keyValue.getKey() != null) {
            if (keyValue.getValue() instanceof YAMLMapping) {
                final YAMLMapping mapping = (YAMLMapping) keyValue.getValue();
                addErrors(annotationHolder, model, keyValue.getKey(), mapping);
            } else if (keyValue.getValue() instanceof YAMLSequence) {
                final YAMLSequence sequence = (YAMLSequence) keyValue.getValue();
                for (final YAMLSequenceItem item : sequence.getItems()) {
                    if (item.getValue() instanceof YAMLMapping) {
                        addErrors(annotationHolder, model, item.getFirstChild(), (YAMLMapping) item.getValue());
                    }
                }
            }
        }
    }
}
 
Example 5
Source Project: intellij-kubernetes   Source File: DuplicateKeyAnnotator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder annotationHolder) {
    if (!KubernetesYamlPsiUtil.isKubernetesFile(element)) {
        return;
    }
    if (element instanceof YAMLMapping) {
        final YAMLMapping mapping = (YAMLMapping) element;
        final Collection<YAMLKeyValue> keyValues = mapping.getKeyValues();
        final Set<String> existingKeys = new HashSet<>(keyValues.size());
        for (final YAMLKeyValue keyValue : keyValues) {
            if (keyValue.getKey() != null && !existingKeys.add(keyValue.getKeyText().trim())) {
                annotationHolder.createErrorAnnotation(keyValue.getKey(), "Duplicated property '" + keyValue.getKeyText() + "'").registerFix(new DeletePropertyIntentionAction());
            }
        }
    }
}
 
Example 6
@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    for (PsiElement el : elements) {
        if (!NeosProjectService.isEnabled(el)) {
            return;
        }

        if (!el.getContainingFile().getVirtualFile().getName().startsWith("NodeTypes.")) {
            continue;
        }

        if (!(el instanceof YAMLKeyValue)) {
            continue;
        }

        YAMLMapping parentMapping = ((YAMLKeyValue) el).getParentMapping();
        if (parentMapping != null && parentMapping.getParent() instanceof YAMLDocument) {
            String nodeType = ((YAMLKeyValue) el).getKeyText();
            String[] nodeTypeSplit = nodeType.split(":");

            if (nodeTypeSplit.length < 2) {
                continue;
            }

            List<PsiElement> targets = ResolveEngine.getPrototypeDefinitions(el.getProject(), nodeTypeSplit[1], nodeTypeSplit[0]);
            if (!targets.isEmpty()) {
                RelatedItemLineMarkerInfo info = NavigationGutterIconBuilder
                        .create(FusionIcons.PROTOTYPE)
                        .setTargets(targets)
                        .setTooltipText("Go to Fusion prototype")
                        .createLineMarkerInfo(el);
                result.add(info);
            }
        }
    }
}
 
Example 7
Source Project: intellij-swagger   Source File: PathFinder.java    License: MIT License 5 votes vote down vote up
private PsiElement getNextObjectParent(final PsiElement psiElement) {
  if (psiElement == null) {
    return null;
  }

  if (psiElement instanceof JsonObject
      || (psiElement instanceof YAMLKeyValue || psiElement instanceof YAMLMapping)
          && !(psiElement instanceof JsonStringLiteral)) {
    return psiElement;
  }

  return getNextObjectParent(psiElement.getParent());
}
 
Example 8
private void addErrors(final @NotNull AnnotationHolder annotationHolder, final Model model, final PsiElement errorTarget, final YAMLMapping mapping) {
    final Set<String> existingKeys = mapping.getKeyValues().stream().map(YAMLKeyValue::getKeyText).collect(Collectors.toSet());

    // Find out the keys that are needed, and remove any which have been defined
    // The resulting set are the properties required but not added
    final Set<String> requiredProperties = new HashSet<>(model.getRequiredProperties());
    requiredProperties.removeAll(existingKeys);

    if (!requiredProperties.isEmpty()) {
        annotationHolder.createWarningAnnotation(errorTarget, "Missing required properties on " + model.getId() + ": " + String.join(", ", requiredProperties))
                        .registerFix(new CreateMissingPropertiesIntentionAction(requiredProperties, mapping));
    }
}
 
Example 9
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder annotationHolder) {
    if (!KubernetesYamlPsiUtil.isKubernetesFile(element)) {
        return;
    }
    final ModelProvider modelProvider = ModelProvider.INSTANCE;
    final ResourceTypeKey resourceKey = KubernetesYamlPsiUtil.findResourceKey(element);
    if (resourceKey != null && element instanceof YAMLKeyValue) {
        final YAMLKeyValue keyValue = (YAMLKeyValue) element;
        final Property property = KubernetesYamlPsiUtil.propertyForKey(modelProvider, resourceKey, keyValue);
        final YAMLValue value = keyValue.getValue();
        if (property != null && property.getType() != null && value != null) {
            switch (property.getType()) {
                case ARRAY:
                    if (!(value instanceof YAMLSequence)) {
                        annotationHolder.createErrorAnnotation(value, "The content of " + keyValue.getKeyText() + " should be an array.");
                    }
                    break;
                case OBJECT:
                    if (!(value instanceof YAMLMapping)) {
                        annotationHolder.createErrorAnnotation(value, "The content of " + keyValue.getKeyText() + " should be an object.");
                    }
                    break;
            }
        }
    }
}
 
Example 10
Source Project: intellij-kubernetes   Source File: KubernetesYamlPsiUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Find the {@link ResourceTypeKey} for the current document.
 *
 * @param element an element within the document to check.
 * @return the resource type key, or {@code null} if the "apiVersion" and "kind" fields are not present.
 */
@Nullable
public static ResourceTypeKey findResourceKey(final PsiElement element) {
    // Get the top-level mapping
    final YAMLMapping topLevelMapping = getTopLevelMapping(element);
    final String apiVersion = getValueText(topLevelMapping, "apiVersion");
    final String kind = getValueText(topLevelMapping, "kind");
    if (apiVersion != null && kind != null) {
        return new ResourceTypeKey(apiVersion, kind);
    } else {
        return null;
    }
}
 
Example 11
Source Project: intellij-kubernetes   Source File: KubernetesYamlPsiUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the top-level mapping in the document, if present.
 *
 * @param element an element within the document.
 * @return the top-level mapping, or {@code null} if one is not defined (e.g. in an empty document).
 */
@Nullable
public static YAMLMapping getTopLevelMapping(final PsiElement element) {
    final YAMLDocument document = PsiTreeUtil.getParentOfType(element, YAMLDocument.class);
    if (document != null) {
        final YAMLValue topLevelValue = document.getTopLevelValue();
        if (topLevelValue instanceof YAMLMapping) {
            return (YAMLMapping) topLevelValue;
        }
    }
    return null;
}
 
Example 12
Source Project: consulo-unity3d   Source File: Unity3dMetaIndexExtension.java    License: Apache License 2.0 5 votes vote down vote up
public static String findGUIDFromFile(@Nonnull YAMLFile psiFile)
{
	String guid = null;
	List<YAMLDocument> documents = psiFile.getDocuments();
	for(YAMLDocument document : documents)
	{
		YAMLValue topLevelValue = document.getTopLevelValue();
		if(topLevelValue instanceof YAMLMapping)
		{
			YAMLKeyValue guidValue = ((YAMLMapping) topLevelValue).getKeyValueByKey(Unity3dMetaManager.GUID_KEY);
			guid = guidValue == null ? null : guidValue.getValueText();
		}
	}
	return guid;
}
 
Example 13
@Nullable
@Override
public String findClassForElement(@NotNull PsiElement psiElement) {
    YAMLMapping parentOfType = PsiTreeUtil.getParentOfType(psiElement, YAMLMapping.class);
    if(parentOfType == null) {
        return null;
    }

    return YamlHelper.getYamlKeyValueAsString(parentOfType, "class");
}
 
Example 14
@Nullable
@Override
public String findIdForElement(@NotNull PsiElement psiElement) {
    YAMLMapping parentOfType = PsiTreeUtil.getParentOfType(psiElement, YAMLMapping.class);
    if(parentOfType == null) {
        return null;
    }

    return YamlHelper.getYamlKeyValueAsString(parentOfType, "id");
}
 
Example 15
protected void visitRoot(PsiFile psiFile, String rootName, @NotNull ProblemsHolder holder) {
    if(!(psiFile instanceof YAMLFile)) {
        return;
    }

    YAMLKeyValue yamlKeyValue = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, rootName);
    if(yamlKeyValue == null) {
        return;
    }

    YAMLCompoundValue yaml = PsiTreeUtil.findChildOfType(yamlKeyValue, YAMLMapping.class);
    if(yaml != null) {
        YamlHelper.attachDuplicateKeyInspection(yaml, holder);
    }
}
 
Example 16
private boolean isValidService(ServiceActionUtil.ServiceYamlContainer serviceYamlContainer) {
    YAMLKeyValue serviceKey = serviceYamlContainer.getServiceKey();

    Set<String> keySet = YamlHelper.getKeySet(serviceKey);
    if(keySet == null) {
        return true;
    }

    for(String s: INVALID_KEYS) {
        if(keySet.contains(s)) {
            return false;
        }
    }

    // check autowire scope
    Boolean serviceAutowire = YamlHelper.getYamlKeyValueAsBoolean(serviceKey, "autowire");
    if(serviceAutowire != null) {
        // use service scope for autowire
        if(serviceAutowire) {
            return false;
        }
    } else {
        // find file scope defaults
        // defaults: [autowire: true]
        YAMLMapping key = serviceKey.getParentMapping();
        if(key != null) {
            YAMLKeyValue defaults = YamlHelper.getYamlKeyValue(key, "_defaults");
            if(defaults != null) {
                Boolean autowire = YamlHelper.getYamlKeyValueAsBoolean(defaults, "autowire");
                if(autowire != null && autowire) {
                    return false;
                }
            }
        }
    }

    return true;
}
 
Example 17
Source Project: idea-php-symfony2-plugin   Source File: YamlServiceTagTest.java    License: MIT License 5 votes vote down vote up
public void testYmlTagAttributeExtraction() {
    YAMLHashImpl fromText = YamlPsiElementFactory.createFromText(getProject(), YAMLHashImpl.class, "{ name: routing.loader, method: foo }");
    YamlServiceTag tag = new YamlServiceTag("foo", "routing.loader", (YAMLMapping) fromText);

    assertEquals("foo", tag.getAttribute("method"));
    assertEquals("routing.loader", tag.getAttribute("name"));
}
 
Example 18
private void collectRouteInlineClasses(@NotNull Collection<LineMarkerInfo> results, @NotNull Project project, @NotNull PsiElement psiElement) {

        if(!(YamlElementPatternHelper.getSingleLineScalarKey("_form").accepts(psiElement) ||
            YamlElementPatternHelper.getSingleLineScalarKey("_entity_form").accepts(psiElement))
            ) {
            return;
        }

        PsiElement yamlScalar = psiElement.getParent();
        if(!(yamlScalar instanceof YAMLScalar)) {
            return;
        }

        String textValue = ((YAMLScalar) yamlScalar).getTextValue();

        Collection<PhpClass> classesInterface = new ArrayList<>(PhpElementsUtil.getClassesInterface(project, textValue));
        classesInterface.addAll(IndexUtil.getFormClassForId(project, textValue));

        if(classesInterface.size() == 0) {
            return;
        }

        PsiElement yamlKeyValue = yamlScalar.getParent();
        if(!(yamlKeyValue instanceof YAMLKeyValue)) {
            return;
        }

        YAMLMapping parentMapping = ((YAMLKeyValue) yamlKeyValue).getParentMapping();
        if(parentMapping == null) {
            return;
        }

        PsiElement parent = parentMapping.getParent();
        if(!(parent instanceof YAMLKeyValue)) {
            return;
        }

        String keyText = ((YAMLKeyValue) parent).getKeyText();

        if(!"defaults".equals(keyText)) {
            return;
        }

        YAMLMapping parentMapping1 = ((YAMLKeyValue) parent).getParentMapping();
        if(parentMapping1 == null) {
            return;
        }

        PsiElement parent1 = parentMapping1.getParent();
        if(!(parent1 instanceof YAMLKeyValue)) {
            return;
        }

        NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.FORM_TYPE_LINE_MARKER).
            setTargets(classesInterface).
            setTooltipText("Navigate to form");

        results.add(builder.createLineMarkerInfo(parent1));
    }
 
Example 19
public YamlMappingAttributeResolver(@NotNull YAMLMapping yamlMapping) {
    this.yamlMapping = yamlMapping;
}
 
Example 20
Source Project: idea-php-symfony2-plugin   Source File: YamlServiceTag.java    License: MIT License 4 votes vote down vote up
public YamlServiceTag(@NotNull String serviceId, @NotNull String tagName, @NotNull YAMLMapping yamlMapping) {
    this(serviceId, tagName, new YamlMappingAttributeResolver(yamlMapping));
}
 
Example 21
/**
 * Default constructor.
 *
 * @param missingKeys the keys that need to be created.
 * @param mapping the mapping under which to create the keys.
 */
public CreateMissingPropertiesIntentionAction(final Set<String> missingKeys, final YAMLMapping mapping) {
    this.missingKeys = missingKeys;
    this.mapping = mapping;
}
 
Example 22
Source Project: intellij-kubernetes   Source File: KubernetesYamlPsiUtil.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Gets the text of the value held by the given key within a mapping.
 *
 * @param mapping the mapping to search through.
 * @param key the key to search for.
 * @return the trimmed text value of the key, or {@code null} if the mapping is null or the key was not found.
 */
@Nullable
public static String getValueText(@Nullable final YAMLMapping mapping, @NotNull final String key) {
    return Optional.ofNullable(mapping).map(m -> m.getKeyValueByKey(key)).map(YAMLKeyValue::getValueText).map(String::trim).orElse(null);
}