fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper Java Examples

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper. 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: ControllerMethodInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public String getRouteName() {

    YAMLKeyValue defaultKeyValue = PsiTreeUtil.getParentOfType(this.psiElement.getParent(), YAMLKeyValue.class);
    if(defaultKeyValue == null) {
        return null;
    }

    YAMLKeyValue def = PsiTreeUtil.getParentOfType(defaultKeyValue, YAMLKeyValue.class);
    if(def == null) {
        return null;
    }

    return YamlHelper.getYamlKeyName(def);
}
 
Example #2
Source File: YamlHelperLightTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void testGetTaggedServices() {
    PsiFile psiFile = myFixture.configureByText(YAMLFileType.YML, "" +
        "services:\n" +
        "   foobar:\n" +
        "       class: ClassName\\Foo\n" +
        "       tags:\n" +
        "           - { name: crossHint.test_222 }\n" +
        "   foobar2:\n" +
        "       class: ClassName\\Foo\n" +
        "       tags: [ 'test.11' ]\n"
    );

    Collection<YAMLKeyValue> taggedServices1 = YamlHelper.getTaggedServices((YAMLFile) psiFile, "crossHint.test_222");
    assertTrue(taggedServices1.stream().anyMatch(yamlKeyValue -> "foobar".equals(yamlKeyValue.getKey().getText())));

    Collection<YAMLKeyValue> taggedServices2 = YamlHelper.getTaggedServices((YAMLFile) psiFile, "test.11");
    assertTrue(taggedServices2.stream().anyMatch(yamlKeyValue -> "foobar2".equals(yamlKeyValue.getKey().getText())));
}
 
Example #3
Source File: EntityHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static List<DoctrineModelField> getYamlDoctrineFields(String keyName, @Nullable YAMLKeyValue yamlKeyValue) {

    if(yamlKeyValue == null) {
        return null;
    }

    PsiElement yamlCompoundValue = yamlKeyValue.getValue();
    if(yamlCompoundValue == null) {
        return null;
    }

    List<DoctrineModelField> modelFields = new ArrayList<>();
    for(YAMLKeyValue yamlKey: PsiTreeUtil.getChildrenOfTypeAsList(yamlCompoundValue, YAMLKeyValue.class)) {
        String fieldName = YamlHelper.getYamlKeyName(yamlKey);
        if(fieldName != null) {
            DoctrineModelField modelField = new DoctrineModelField(fieldName);
            modelField.addTarget(yamlKey);
            attachYamlFieldTypeName(keyName, modelField, yamlKey);
            modelFields.add(modelField);
        }
    }

    return modelFields;
}
 
Example #4
Source File: EntityHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void attachYamlFieldTypeName(String keyName, DoctrineModelField doctrineModelField, YAMLKeyValue yamlKeyValue) {

        if("fields".equals(keyName) || "id".equals(keyName)) {

            YAMLKeyValue yamlType = YamlHelper.getYamlKeyValue(yamlKeyValue, "type");
            if(yamlType != null) {
                doctrineModelField.setTypeName(yamlType.getValueText());
            }

            YAMLKeyValue yamlColumn = YamlHelper.getYamlKeyValue(yamlKeyValue, "column");
            if(yamlColumn != null) {
                doctrineModelField.setColumn(yamlColumn.getValueText());
            }

            return;
        }

        if(RELATIONS.contains(keyName.toLowerCase())) {
            YAMLKeyValue targetEntity = YamlHelper.getYamlKeyValue(yamlKeyValue, "targetEntity");
            if(targetEntity != null) {
                doctrineModelField.setRelationType(keyName);
                doctrineModelField.setRelation(getOrmClass(yamlKeyValue.getContainingFile(), targetEntity.getValueText()));
            }
        }

    }
 
Example #5
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#insertKeyIntoFile
 * TODO empty file
 */
public void skipTestInsertKeyIntoEmptyFile() {
    YAMLFile yamlFile = (YAMLFile) myFixture.configureByText(YAMLFileType.YML, "");

    YamlHelper.insertKeyIntoFile(yamlFile, "value", "car", "bar", "apple");

    assertEquals("" +
            "foo:\n" +
            "   bar:\n" +
            "       car: test\n" +
            "car:\n" +
            "  bar:\n" +
            "    apple: value",
        yamlFile.getText()
    );
}
 
Example #6
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PhpClass getPhpClassFromXmlTag(@NotNull XmlTag xmlTag, @NotNull ContainerCollectionResolver.LazyServiceCollector collector) {
    String className = xmlTag.getAttributeValue("class");
    if(className == null) {
        String id = xmlTag.getAttributeValue("id");
        if(id == null || !YamlHelper.isClassServiceId(id)) {
            return null;
        }

        className = id;
    }

    // @TODO: cache defs
    PhpClass resolvedClassDefinition = ServiceUtil.getResolvedClassDefinition(xmlTag.getProject(), className, collector);
    if(resolvedClassDefinition == null) {
        return null;
    }

    return resolvedClassDefinition;
}
 
Example #7
Source File: ServiceUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Resolve "@service" to its class + proxy ServiceCollector for iteration
 */
@Nullable
public static PhpClass getServiceClass(@NotNull Project project, @NotNull String serviceName, @NotNull ContainerCollectionResolver.ServiceCollector collector) {

    serviceName = YamlHelper.trimSpecialSyntaxServiceName(serviceName);
    if(serviceName.length() == 0) {
        return null;
    }

    String resolve = collector.resolve(serviceName);
    if(resolve == null) {
        return null;
    }

    return PhpElementsUtil.getClassInterface(project, resolve);
}
 
Example #8
Source File: DoctrineYamlMappingDriver.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments args) {

    PsiFile psiFile = args.getPsiFile();
    if(!(psiFile instanceof YAMLFile)) {
        return null;
    }

    Collection<DoctrineModelField> fields = new ArrayList<>();
    DoctrineMetadataModel model = new DoctrineMetadataModel(fields);

    for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues((YAMLFile) psiFile)) {
        // first line is class name; check of we are right
        if(args.isEqualClass(YamlHelper.getYamlKeyName(yamlKeyValue))) {
            model.setTable(YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "table"));
            fields.addAll(EntityHelper.getModelFieldsSet(yamlKeyValue));
        }
    }

    if(model.isEmpty()) {
        return null;
    }

    return model;
}
 
Example #9
Source File: ServiceUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Resolve "@service" to its class
 */
@Nullable
public static PhpClass getServiceClass(@NotNull Project project, @NotNull String serviceName) {

    serviceName = YamlHelper.trimSpecialSyntaxServiceName(serviceName);

    if(serviceName.length() == 0) {
        return null;
    }

    ContainerService containerService = ContainerCollectionResolver.getService(project, serviceName);
    if(containerService == null) {
        return null;
    }

    String serviceClass = containerService.getClassName();
    if(serviceClass == null) {
        return null;
    }

    return PhpElementsUtil.getClassInterface(project, serviceClass);
}
 
Example #10
Source File: ServiceContainerUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void visitFile(@NotNull YAMLFile psiFile, @NotNull Consumer<ServiceConsumer> consumer) {
    ServiceFileDefaults defaults = null;

    for (YAMLKeyValue keyValue : YamlHelper.getQualifiedKeyValuesInFile(psiFile, "services")) {
        if(defaults == null) {
            defaults = createDefaults(psiFile);
        }

        String serviceId = keyValue.getKeyText();
        if(StringUtils.isBlank(serviceId) || "_defaults".equals(serviceId)) {
            continue;
        }

        consumer.consume(new ServiceConsumer(keyValue, serviceId, new YamlKeyValueAttributeValue(keyValue), defaults));
    }
}
 
Example #11
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 #12
Source File: FileResourceVisitorUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * foo:
 *   resources: 'FOO'
 */
private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<FileResourceConsumer> consumer) {
    for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues(yamlFile)) {
        YAMLKeyValue resourceKey = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource", true);
        if(resourceKey == null) {
            continue;
        }

        String resource = PsiElementUtils.trimQuote(resourceKey.getValueText());
        if(StringUtils.isBlank(resource)) {
            continue;
        }

        consumer.consume(new FileResourceConsumer(resourceKey, yamlKeyValue, normalize(resource)));
    }
}
 
Example #13
Source File: RouteHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Find controller definition in yaml structure
 *
 * foo:
 *   defaults: { _controller: "Bundle:Foo:Bar" }
 *   defaults:
 *      _controller: "Bundle:Foo:Bar"
 *   controller: "Bundle:Foo:Bar"
 */
@Nullable
public static String getYamlController(@NotNull YAMLKeyValue psiElement) {
    YAMLKeyValue yamlKeyValue = YamlHelper.getYamlKeyValue(psiElement, "defaults");
    if(yamlKeyValue != null) {
        final YAMLValue container = yamlKeyValue.getValue();
        if(container instanceof YAMLMapping) {
            YAMLKeyValue yamlKeyValueController = YamlHelper.getYamlKeyValue(container, "_controller", true);
            if(yamlKeyValueController != null) {
                String valueText = yamlKeyValueController.getValueText();
                if(StringUtils.isNotBlank(valueText)) {
                    return valueText;
                }
            }
        }
    }

    String controller = YamlHelper.getYamlKeyValueAsString(psiElement, "controller");
    if(controller != null && StringUtils.isNotBlank(controller)) {
        return controller;
    }

    return null;
}
 
Example #14
Source File: TranslationInsertUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Remove TODO; moved to core
 */
@Deprecated
@NotNull
public static String findEol(@NotNull PsiElement psiElement) {

    for(PsiElement child: YamlHelper.getChildrenFix(psiElement)) {
        if(PlatformPatterns.psiElement(YAMLTokenTypes.EOL).accepts(child)) {
            return child.getText();
        }
    }

    PsiElement[] indentPsiElements = PsiTreeUtil.collectElements(psiElement.getContainingFile(), element ->
        PlatformPatterns.psiElement(YAMLTokenTypes.EOL).accepts(element)
    );

    if(indentPsiElements.length > 0) {
        return indentPsiElements[0].getText();
    }

    return "\n";
}
 
Example #15
Source File: DuplicateLocalRouteInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitFile(PsiFile file) {
    // @TODO: detection of routing files in right way
    // routing.yml
    // comment.routing.yml
    // routing/foo.yml
    if(!YamlHelper.isRoutingFile(file)) {
        return;
    }

    YAMLDocument document = PsiTreeUtil.findChildOfType(file, YAMLDocument.class);
    if(document == null) {
        return;
    }

    YAMLValue topLevelValue = document.getTopLevelValue();
    if(topLevelValue != null) {
        YamlHelper.attachDuplicateKeyInspection(topLevelValue, holder);
    }
}
 
Example #16
Source File: ServiceContainerUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static String getServiceClassFromServiceMapping(@NotNull YAMLMapping yamlMapping) {
    YAMLKeyValue classKeyValue = yamlMapping.getKeyValueByKey("class");

    // Symfony 3.3: "class" is optional; use service id for class
    // Foo\Bar:
    //   arguments: ~
    if(classKeyValue != null) {
        return classKeyValue.getValueText();
    }

    PsiElement parent = yamlMapping.getParent();
    if(parent instanceof YAMLKeyValue) {
        String keyText = ((YAMLKeyValue) parent).getKeyText();
        if(YamlHelper.isClassServiceId(keyText)) {
            return keyText;
        }
    }

    return null;
}
 
Example #17
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 #18
Source File: XmlReferenceContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
    if(!Symfony2ProjectComponent.isEnabled(psiElement) || !(psiElement instanceof XmlAttributeValue)) {
        return new PsiReference[0];
    }

    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof XmlAttribute) && YamlHelper.isClassServiceId(parent.getText())) {
        return new PsiReference[0];
    }

    // invalidate on class attribute
    PsiElement xmlTag = parent.getParent();
    if(!(xmlTag instanceof XmlTag) || ((XmlTag) xmlTag).getAttribute("class") != null) {
        return new PsiReference[0];
    }

    return new PsiReference[] {
        new ServiceIdWithoutParameterReference((XmlAttributeValue) psiElement)
    };
}
 
Example #19
Source File: TwigUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Extract template name from embed tag
 *
 * {% embed "teasers_skeleton.html.twig" %}
 */
@Nullable
private static String getTemplateNameForEmbedTag(@NotNull PsiElement embedTag) {
    if(embedTag.getNode().getElementType() == TwigElementTypes.EMBED_TAG) {
        PsiElement fileReference = ContainerUtil.find(YamlHelper.getChildrenFix(embedTag), psiElement ->
            TwigPattern.getTemplateFileReferenceTagPattern().accepts(psiElement)
        );

        if(fileReference != null && TwigUtil.isValidStringWithoutInterpolatedOrConcat(fileReference)) {
            String text = fileReference.getText();
            if(StringUtils.isNotBlank(text)) {
                return text;
            }
        }
    }

    return null;
}
 
Example #20
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static Map<String, String> getInlineCommentDocsVars(@NotNull PsiElement twigCompositeElement) {
    Map<String, String> variables = new HashMap<>();

    for(PsiElement psiComment: YamlHelper.getChildrenFix(twigCompositeElement)) {
        if(!(psiComment instanceof PsiComment)) {
            continue;
        }

        String text = psiComment.getText();
        if(StringUtils.isBlank(text)) {
            continue;
        }

        for (Pattern pattern : INLINE_DOC_REGEX) {
            Matcher matcher = pattern.matcher(text);
            while (matcher.find()) {
                variables.put(matcher.group("var"), matcher.group("class"));
            }
        }
    }

    return variables;
}
 
Example #21
Source File: ServiceIndexUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static List<PsiElement> findParameterDefinitions(@NotNull PsiFile psiFile, @NotNull String parameterName) {

        List<PsiElement> items = new ArrayList<>();

        if(psiFile instanceof YAMLFile) {
            PsiElement servicePsiElement = YamlHelper.getLocalParameterMap(psiFile, parameterName);
            if(servicePsiElement != null) {
                items.add(servicePsiElement);
            }
        }

        if(psiFile instanceof XmlFile) {
            PsiElement localParameterName = XmlHelper.getLocalParameterName(psiFile, parameterName);
            if(localParameterName != null) {
                items.add(localParameterName);
            }
        }

        return items;
    }
 
Example #22
Source File: ContainerParameterStubIndex.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, String, FileContent> getIndexer() {
    return inputData -> {
        Map<String, String> map = new HashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if(!Symfony2ProjectComponent.isEnabledForIndex(psiFile.getProject())) {
            return map;
        }

        if(!ServicesDefinitionStubIndex.isValidForIndex(inputData, psiFile)) {
            return map;
        }

        if(psiFile instanceof YAMLFile) {
            attachTHashMapNullable(YamlHelper.getLocalParameterMap(psiFile), map);
        } else if(psiFile instanceof XmlFile) {
            attachTHashMapNullable(XmlHelper.getFileParameterMap((XmlFile) psiFile), map);
        }

        return map;
    };
}
 
Example #23
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
private Collection<PsiElement> getTagMethodGoto(@NotNull PsiElement psiElement) {
    String methodName = PsiElementUtils.trimQuote(psiElement.getText());
    if(StringUtils.isBlank(methodName)) {
        return Collections.emptyList();
    }

    String classValue = YamlHelper.getServiceDefinitionClassFromTagMethod(psiElement);
    if(classValue == null) {
        return Collections.emptyList();
    }

    PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), classValue);
    if(phpClass == null) {
        return Collections.emptyList();
    }

    Method method = phpClass.findMethodByName(methodName);
    if(method != null) {
        return Collections.singletonList(method);
    }

    return Collections.emptyList();
}
 
Example #24
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
private Collection<PsiElement> getMethodGoto(@NotNull PsiElement psiElement) {
    Collection<PsiElement> results = new ArrayList<>();

    PsiElement parent = psiElement.getParent();

    if(parent instanceof YAMLScalar) {
        YamlHelper.visitServiceCall((YAMLScalar) parent, clazz -> {
            PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(),clazz);
            if(phpClass != null) {
                for(Method method: PhpElementsUtil.getClassPublicMethod(phpClass)) {
                    if(method.getName().equals(PsiElementUtils.trimQuote(psiElement.getText()))) {
                        results.add(method);
                    }
                }
            }
        });
    }

    return results;
}
 
Example #25
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 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;
    }

    String service = YamlHelper.getPreviousSequenceItemAsText(position);
    if (service == null) {
        return;
    }

    PhpClass phpClass = ServiceUtil.getServiceClass(position.getProject(), service);
    if(phpClass == null) {
        return;
    }

    for (Method method : phpClass.getMethods()) {
        if(method.getAccess().isPublic() && !(method.getName().startsWith("__"))) {
            completionResultSet.addElement(new PhpLookupElement(method));
        }
    }

}
 
Example #26
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 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 serviceDefinition = position.getParent();
    if(serviceDefinition instanceof YAMLKeyValue) {
        YAMLKeyValue aClass = YamlHelper.getYamlKeyValue((YAMLKeyValue) serviceDefinition, "class");
        if(aClass == null) {
            PhpClassCompletionProvider.addClassCompletion(parameters, completionResultSet, position, false);
        }
    }
}
 
Example #27
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;
    }

    PsiElement psiElement = completionParameters.getPosition();

    String serviceDefinitionClassFromTagMethod = YamlHelper.getServiceDefinitionClassFromTagMethod(psiElement);

    if(serviceDefinitionClassFromTagMethod != null) {
        PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceDefinitionClassFromTagMethod);
        if(phpClass != null) {
            PhpElementsUtil.addClassPublicMethodCompletion(completionResultSet, phpClass);
        }
    }
}
 
Example #28
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 #29
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
private Collection<PsiElement> serviceGoToDeclaration(@NotNull PsiElement psiElement, @NotNull String serviceId) {
    serviceId = YamlHelper.trimSpecialSyntaxServiceName(serviceId).toLowerCase();

    String serviceClass = ContainerCollectionResolver.resolveService(psiElement.getProject(), serviceId);

    if (serviceClass != null) {
        Collection<PhpClass> phpClasses = PhpIndex.getInstance(psiElement.getProject()).getAnyByFQN(serviceClass);
        if(phpClasses.size() > 0) {
            return new ArrayList<>(phpClasses);
        }
    }

    // get container target on indexes
    return ServiceIndexUtil.findServiceDefinitions(psiElement.getProject(), serviceId);

}
 
Example #30
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    YAMLKeyValue yamlKeyValue = PsiTreeUtil.getParentOfType(completionParameters.getOriginalPosition(), YAMLKeyValue.class);
    if(yamlKeyValue != null) {
        PsiElement compoundValue = yamlKeyValue.getParent();
        if(compoundValue instanceof YAMLCompoundValue) {

            // path and pattern are valid
            String pattern = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) compoundValue, "path", false);
            if(pattern == null) {
                pattern = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) compoundValue, "pattern", false);
            }

            if(pattern != null) {
                Matcher matcher = Pattern.compile("\\{(\\w+)}").matcher(pattern);
                while(matcher.find()){
                    completionResultSet.addElement(LookupElementBuilder.create(matcher.group(1)));
                }
            }

        }
    }
}