org.jetbrains.yaml.psi.YAMLScalar Java Examples

The following examples show how to use org.jetbrains.yaml.psi.YAMLScalar. 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: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentMethodIndexForNamedServices() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    Foo\\Bar:\n" +
        "       calls:\n" +
        "           - [ 'setBar', ['@foo', @f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<Parameter> parameters = new ArrayList<>();
    YamlHelper.visitServiceCallArgumentMethodIndex(parent, parameters::add);

    assertNotNull(ContainerUtil.find(parameters, parameter -> "arg2".equals(parameter.getName())));
}
 
Example #2
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentMethodIndex() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    foobar:\n" +
        "       class: Foo\\Bar\n" +
        "       calls:\n" +
        "           - [ 'setBar', [@f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<Parameter> parameters = new ArrayList<>();
    YamlHelper.visitServiceCallArgumentMethodIndex(parent, parameters::add);

    assertNotNull(ContainerUtil.find(parameters, parameter -> "arg1".equals(parameter.getName())));
}
 
Example #3
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgument
 */
public void testVisitServiceCallArgumentAsNamedService() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    Foo\\Bar:\n" +
        "       calls:\n" +
        "           - [ 'setBar', [@f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<String> values = new ArrayList<>();
    YamlHelper.visitServiceCallArgument(parent, parameterVisitor ->
        values.add(parameterVisitor.getClassName() + ":" + parameterVisitor.getMethod() + ":" + parameterVisitor.getParameterIndex())
    );

    assertContainsElements(values, "Foo\\Bar:setBar:0");
}
 
Example #4
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCallArgument
 */
public void testVisitServiceCallArgument() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    foobar:\n" +
        "       class: Foo\\Bar\n" +
        "       calls:\n" +
        "           - [ 'setBar', [@f<caret>oo] ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<String> values = new ArrayList<>();
    YamlHelper.visitServiceCallArgument(parent, parameterVisitor ->
        values.add(parameterVisitor.getClassName() + ":" + parameterVisitor.getMethod() + ":" + parameterVisitor.getParameterIndex())
    );

    assertContainsElements(values, "Foo\\Bar:setBar:0");
}
 
Example #5
Source File: YamlXmlServiceInstanceInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * class: FooClass
 * tags:
 *  - [ setFoo, [@args_bar] ]
 */
private void visitCall(PsiElement psiElement) {
    PsiElement yamlScalar = psiElement.getContext();
    if(!(yamlScalar instanceof YAMLScalar)) {
        return;
    }

    YamlHelper.visitServiceCallArgument((YAMLScalar) yamlScalar, visitor -> {
        PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), visitor.getClassName(), getLazyServiceCollector(psiElement.getProject()));
        if(serviceClass == null) {
            return;
        }

        Method method = serviceClass.findMethodByName(visitor.getMethod());
        if (method == null) {
            return;
        }

        YamlXmlServiceInstanceInspection.registerInstanceProblem(psiElement, holder, visitor.getParameterIndex(), method, getLazyServiceCollector(psiElement.getProject()));
    });
}
 
Example #6
Source File: ContainerConstantInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void yamlVisitor(@NotNull ProblemsHolder holder, @NotNull PsiFile psiFile) {
    psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
        @Override
        public void visitElement(PsiElement psiElement) {
            if(psiElement instanceof YAMLScalar) {
                String textValue = ((YAMLScalar) psiElement).getTextValue();
                if(textValue.length() > 0 && textValue.startsWith("!php/const:")) {
                    String constantName = textValue.substring(11);
                    if(StringUtils.isNotBlank(constantName) && ServiceContainerUtil.getTargetsForConstant(psiElement.getProject(), constantName).size() == 0) {
                        holder.registerProblem(psiElement, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                    }
                }
            }

            super.visitElement(psiElement);
        }
    });
}
 
Example #7
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCall
 */
public void testVisitServiceCallForNamedServices() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    Foo\\Bar:\n" +
        "       calls:\n" +
        "           - [ '<caret>' ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<String> values = new ArrayList<>();
    YamlHelper.visitServiceCall(parent, values::add);

    assertContainsElements(values, "Foo\\Bar");
}
 
Example #8
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    if(!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
        return;
    }

    // - [ setContainer, [ @service_container ] ]
    PsiElement psiElement = completionParameters.getPosition();

    PsiElement yamlScalar = psiElement.getParent();
    if(yamlScalar instanceof YAMLScalar) {
        YamlHelper.visitServiceCall((YAMLScalar) yamlScalar, clazz -> {
            PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), clazz);
            if(phpClass != null) {
                PhpElementsUtil.addClassPublicMethodCompletion(completionResultSet, phpClass);
            }
        });
    }
}
 
Example #9
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper#visitServiceCall
 */
public void testVisitServiceCall() {
    myFixture.configureByText(YAMLFileType.YML, "services:\n" +
        "    foobar:\n" +
        "       class: Foo\\Bar\n" +
        "       calls:\n" +
        "           - [ '<caret>' ]\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    Collection<String> values = new ArrayList<>();
    YamlHelper.visitServiceCall(parent, values::add);

    assertContainsElements(values, "Foo\\Bar");
}
 
Example #10
Source File: ServiceContainerUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil#getYamlConstructorTypeHint
 */
public void testGetYamlConstructorTypeHint() {
    myFixture.configureByText("test.yml", "" +
        "services:\n" +
        "   NamedArgument\\Foobar:\n" +
        "       arguments: ['<caret>']\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    ServiceTypeHint typeHint = ServiceContainerUtil.getYamlConstructorTypeHint(
        parent,
        new ContainerCollectionResolver.LazyServiceCollector(getProject())
    );

    assertEquals(0, typeHint.getIndex());
    assertEquals("__construct", typeHint.getMethod().getName());
}
 
Example #11
Source File: ServiceContainerUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil#getYamlConstructorTypeHint
 */
public void testGetYamlConstructorTypeHintForNamedArgument() {
    myFixture.configureByText("test.yml", "" +
        "services:\n" +
        "   NamedArgument\\Foobar:\n" +
        "       arguments:\n" +
        "           $foobar: '<caret>'\n"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    YAMLScalar parent = (YAMLScalar) psiElement.getParent();

    ServiceTypeHint typeHint = ServiceContainerUtil.getYamlConstructorTypeHint(
        parent,
        new ContainerCollectionResolver.LazyServiceCollector(getProject())
    );

    assertEquals(0, typeHint.getIndex());
    assertEquals("__construct", typeHint.getMethod().getName());
}
 
Example #12
Source File: TranslationUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * Extract common placeholder pattern from translation content
 */
@NotNull
public static Set<String> getPlaceholderFromTranslation(@NotNull Project project, @NotNull String key, @NotNull String domain) {
    Set<String> placeholder = new HashSet<>();
    Set<VirtualFile> visitedXlf = new HashSet<>();

    for (PsiElement element : TranslationUtil.getTranslationPsiElements(project, key, domain)) {
        if (element instanceof YAMLScalar) {
            String textValue = ((YAMLScalar) element).getTextValue();
            if(StringUtils.isBlank(textValue)) {
                continue;
            }

            placeholder.addAll(
                TranslationUtil.getPlaceholderFromTranslation(textValue)
            );
        } else if("xlf".equalsIgnoreCase(element.getContainingFile().getVirtualFile().getExtension()) || "xliff".equalsIgnoreCase(element.getContainingFile().getVirtualFile().getExtension())) {
            VirtualFile virtualFile = element.getContainingFile().getVirtualFile();

            // visiting on file scope because we dont rely on xlf and xliff registered as XML file
            // dont visit file twice
            if(!visitedXlf.contains(virtualFile)) {
                try {
                    visitXliffTranslations(
                        element.getContainingFile().getVirtualFile().getInputStream(),
                        new MyXlfTranslationConsumer(placeholder, key)
                    );
                } catch (IOException ignored) {
                }
            }

            visitedXlf.add(virtualFile);
        }
    }

    return placeholder;
}
 
Example #13
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 #14
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement position = parameters.getPosition();
    if(!Symfony2ProjectComponent.isEnabled(position)) {
        return;
    }

    PsiElement parent = position.getParent();
    if(!(parent instanceof YAMLScalar)) {
        return;
    }

    String textValue = ((YAMLScalar) parent).getTextValue();
    String[] split = textValue.split(":");
    if(split.length < 2) {
        new ServiceCompletionProvider().addCompletions(parameters, processingContext, completionResultSet);
        return;
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(position.getProject(), split[0]);
    if(phpClass == null) {
        return;
    }

    for (Method method : phpClass.getMethods()) {
        if(method.getAccess().isPublic() && !(method.getName().startsWith("__"))) {
            completionResultSet.addElement(
                LookupElementBuilder.create(split[0] + ":" + method.getName())
                .withIcon(method.getIcon()).withTypeText(phpClass.getName(), true)
            );
        }
    }
}
 
Example #15
Source File: GotoCompletionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static String getTextValueForElement(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();

    String value = null;
    if(parent instanceof StringLiteralExpression) {
        value = ((StringLiteralExpression) parent).getContents();
    } else if(parent instanceof XmlAttributeValue) {
        // <foo attr="FOO"/>
        value = ((XmlAttributeValue) parent).getValue();
    } else if(parent instanceof XmlText) {
        // <foo>FOO</foo>
        value = ((XmlText) parent).getValue();
    } else if(parent instanceof YAMLScalar) {
        // foo: foo, foo: 'foo', foo: "foo"
        value = ((YAMLScalar) parent).getTextValue();
    } else if(psiElement.getNode().getElementType() == TwigTokenTypes.STRING_TEXT) {
        // twig: 'foobar'
        value = psiElement.getText();
    }

    if(StringUtils.isBlank(value)) {
        return null;
    }

    return value;
}
 
Example #16
Source File: TaggedExtendsInterfaceClassInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {

    if(YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(element)) {

        // class: '\Foo'
        String text = PsiElementUtils.trimQuote(element.getText());
        if(StringUtils.isBlank(text)) {
            super.visitElement(element);
            return;
        }

        PsiElement yamlScalar = element.getParent();
        if(!(yamlScalar instanceof YAMLScalar)) {
            super.visitElement(element);
            return;
        }

        PsiElement classKey = yamlScalar.getParent();
        if(classKey instanceof YAMLKeyValue) {
            PsiElement yamlCompoundValue = classKey.getParent();
            if(yamlCompoundValue instanceof YAMLCompoundValue) {
                PsiElement serviceKeyValue = yamlCompoundValue.getParent();
                if(serviceKeyValue instanceof YAMLKeyValue) {
                    Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue);
                    if(tags.size() > 0) {
                        registerTaggedProblems(element, tags, text, holder, this.lazyServiceCollector);
                    }
                }
            }
        }
    }

    super.visitElement(element);
}
 
Example #17
Source File: YamlRegistrarUtil.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
@Nullable
public static String getYamlScalarKey(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();

    if(!(parent instanceof YAMLScalar)) {
        return null;
    }

    String text = ((YAMLScalar) parent).getTextValue();
    if(StringUtils.isBlank(text)) {
        return null;
    }

    return text;
}
 
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));
    }