Java Code Examples for org.jetbrains.yaml.psi.YAMLKeyValue#getValue()

The following examples show how to use org.jetbrains.yaml.psi.YAMLKeyValue#getValue() . 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: 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 2
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 3
Source File: TranslationInsertUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static PsiElement invokeTranslation(@NotNull final YAMLFile yamlFile, @NotNull final String keyName, @NotNull final String translation) {
    String[] split = keyName.split("\\.");
    PsiElement psiElement = YamlHelper.insertKeyIntoFile(yamlFile, "'" + translation + "'", split);
    if(psiElement == null) {
        return null;
    }

    // resolve target to get value
    YAMLKeyValue target = YAMLUtil.getQualifiedKeyInFile(yamlFile, split);
    if(target != null && target.getValue() != null) {
        return target.getValue();
    } else if(target != null) {
        return target;
    }

    return yamlFile;
}
 
Example 4
Source File: YamlReferenceInspection.java    From intellij-swagger with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(
    @NotNull final ProblemsHolder holder,
    final boolean isOnTheFly,
    @NotNull final LocalInspectionToolSession session) {
  final PsiFile file = holder.getFile();
  final VirtualFile virtualFile = file.getVirtualFile();
  final Project project = holder.getProject();

  boolean checkRefs =
      indexFacade.isMainSpecFile(virtualFile, project)
          || indexFacade.isPartialSpecFile(virtualFile, project);

  return new YamlPsiElementVisitor() {
    @Override
    public void visitKeyValue(@NotNull YAMLKeyValue keyValue) {
      if (!checkRefs) {
        return;
      }
      if ("$ref".equals(keyValue.getKeyText())) {
        YAMLValue value = keyValue.getValue();

        if (!(value instanceof YAMLScalar)) {
          return;
        }

        final String unquotedValue = StringUtil.unquoteString(value.getText());

        if (!unquotedValue.startsWith("http")) {
          doCheck(holder, value, new CreateYamlReferenceIntentionAction(unquotedValue));
        }
      }
      super.visitKeyValue(keyValue);
    }
  };
}
 
Example 5
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 6
Source File: CorrectClassNameCasingYamlLocalQuickFix.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement1 = descriptor.getPsiElement();
    YAMLKeyValue replacement = YamlPsiElementFactory.createFromText(
            project,
            YAMLKeyValue.class,
            "class: " + replacementFQN
    );

    if (replacement != null && replacement.getValue() != null) {
        psiElement1.replace(replacement.getValue());
    }
}