org.jetbrains.yaml.psi.YAMLMapping Java Examples

The following examples show how to use org.jetbrains.yaml.psi.YAMLMapping. 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 File: EelHelperUtil.java    From intellij-neos with 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 #2
Source File: DuplicateKeyAnnotator.java    From intellij-spring-assistant with 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 #3
Source File: PropertyNotInModelAnnotator.java    From intellij-kubernetes with 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;
    }
    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
Source File: MissingRequiredPropertiesAnnotator.java    From intellij-kubernetes with 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;
    }
    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 File: DuplicateKeyAnnotator.java    From intellij-kubernetes with 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
Source File: YamlServiceTagTest.java    From idea-php-symfony2-plugin with 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 #7
Source File: YamlServiceArgumentInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
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 #8
Source File: YamlDuplicateServiceKeyInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
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 #9
Source File: YamlGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@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 #10
Source File: YamlGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@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 #11
Source File: Unity3dMetaIndexExtension.java    From consulo-unity3d with 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 #12
Source File: KubernetesYamlPsiUtil.java    From intellij-kubernetes with 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 #13
Source File: KubernetesYamlPsiUtil.java    From intellij-kubernetes with 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 #14
Source File: DataTypeCheckerAnnotator.java    From intellij-kubernetes with Apache License 2.0 5 votes vote down vote up
@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 #15
Source File: MissingRequiredPropertiesAnnotator.java    From intellij-kubernetes with Apache License 2.0 5 votes vote down vote up
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 #16
Source File: PathFinder.java    From intellij-swagger with 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 #17
Source File: PrototypeLineMarkerProvider.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
@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 #18
Source File: RouteFormLineMarkerProvider.java    From idea-php-drupal-symfony2-bridge with MIT License 4 votes vote down vote up
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
Source File: YamlMappingAttributeResolver.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public YamlMappingAttributeResolver(@NotNull YAMLMapping yamlMapping) {
    this.yamlMapping = yamlMapping;
}
 
Example #20
Source File: YamlServiceTag.java    From idea-php-symfony2-plugin with 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
Source File: KubernetesYamlPsiUtil.java    From intellij-kubernetes with 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);
}
 
Example #22
Source File: CreateMissingPropertiesIntentionAction.java    From intellij-kubernetes with Apache License 2.0 2 votes vote down vote up
/**
 * 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;
}