Java Code Examples for fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#trimQuote()

The following examples show how to use fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils#trimQuote() . 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: ExtJsTemplateLineMarkerProvider.java    From idea-php-shopware-plugin with MIT License 6 votes vote down vote up
private void attachControllerAction(PsiElement sourceElement, Collection<LineMarkerInfo> lineMarkerInfos) {

        if(!ShopwareProjectComponent.isValidForProject(sourceElement)) {
            return;
        }

        String text = PsiElementUtils.trimQuote(sourceElement.getText());
        if(text.startsWith("{") && text.endsWith("}")) {

            List<PsiElement> controllerTargets = ExtJsUtil.getControllerTargets(sourceElement, text);
            if(controllerTargets.size() > 0) {
                NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(ShopwarePluginIcons.SHOPWARE_LINEMARKER).
                    setTargets(controllerTargets).
                    setTooltipText("Navigate to action");

                lineMarkerInfos.add(builder.createLineMarkerInfo(sourceElement));
            }
        }

    }
 
Example 2
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 3
Source File: EventMethodCallInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void registerMethodProblem(final @NotNull PsiElement psiElement, @NotNull ProblemsHolder holder, @Nullable PhpClass phpClass) {
    if(phpClass == null) {
        return;
    }

    final String methodName = PsiElementUtils.trimQuote(psiElement.getText());
    if(phpClass.findMethodByName(methodName) != null) {
        return;
    }

    holder.registerProblem(
        psiElement,
        "Missing Method",
        ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
        new CreateMethodQuickFix(phpClass, methodName, new MyCreateMethodQuickFix())
    );
}
 
Example 4
Source File: ServiceDeprecatedClassesInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    boolean serviceArgumentAccepted = XmlHelper.getArgumentServiceIdPattern().accepts(element);
    if(serviceArgumentAccepted || XmlHelper.getServiceClassAttributeWithIdPattern().accepts(element)) {
        String text = PsiElementUtils.trimQuote(element.getText());
        PsiElement[] psiElements = element.getChildren();

        // we need to attach to child because else strike out equal and quote char
        if(StringUtils.isNotBlank(text) && psiElements.length > 2) {
            this.problemRegistrar.attachDeprecatedProblem(psiElements[1], text, holder);

            // check service arguments for "deprecated" defs
            if(serviceArgumentAccepted) {
                this.problemRegistrar.attachServiceDeprecatedProblem(psiElements[1], text, holder);
            }
        }
    }

    super.visitElement(element);
}
 
Example 5
Source File: BlockGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
public Collection<PsiElement> getPsiTargets(PsiElement element) {
    String blockName = PsiElementUtils.trimQuote(element.getText());
    if(StringUtils.isBlank(blockName)) {
        return Collections.emptyList();
    }

    Collection<PsiElement> psiElements = new HashSet<>();

    psiElements.addAll(
        TwigBlockUtil.getBlockOverwriteTargets(element.getContainingFile(), blockName, true)
    );

    psiElements.addAll(
        TwigBlockUtil.getBlockImplementationTargets(element)
    );

    // filter self navigation
    return psiElements.stream()
        .filter(psiElement -> psiElement != element)
        .collect(Collectors.toSet());
}
 
Example 6
Source File: ServiceReferenceProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {

    if(!Symfony2ProjectComponent.isEnabled(psiElement)) {
        return new PsiReference[0];
    }

    // get the service name "service_container"
    String text = PsiElementUtils.trimQuote(psiElement.getText());

    return new PsiReference[]{ new ServiceXmlReference(psiElement, text) };
}
 
Example 7
Source File: ExtJsTemplateLineMarkerProvider.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
private void attachDefineTargets(PsiElement psiElement, Collection<LineMarkerInfo> lineMarkerInfos, boolean attachController) {
    if(!ShopwareProjectComponent.isValidForProject(psiElement)) {
        return;
    }

    String text = PsiElementUtils.trimQuote(psiElement.getText());
    if(!text.startsWith("Shopware.apps.")) {
        return;
    }

    String[] namespaces = StringUtils.split(text, ".");
    if(namespaces.length < 3) {
        return;
    }

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

    if(attachController) {
        attachController(psiElement.getProject(), namespaces, psiElementList);
    }

    attachModels(psiElement.getProject(), namespaces, psiElementList);

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

    NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(ShopwarePluginIcons.SHOPWARE_LINEMARKER).
        setTargets(psiElementList).
        setTooltipText("Navigate");

    lineMarkerInfos.add(builder.createLineMarkerInfo(psiElement));
}
 
Example 8
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<PsiElement> getArrayMethodGoto(@NotNull PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    if(StringUtils.isBlank(text)) {
        return Collections.emptyList();
    }

    String service = YamlHelper.getPreviousSequenceItemAsText(psiElement);
    if (service == null) {
        return Collections.emptyList();
    }

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

    Collection<PsiElement> results = new ArrayList<>();

    for (Method method : phpClass.getMethods()) {
        if(text.equals(method.getName())) {
            results.add(method);
        }
    }

    return results;
}
 
Example 9
Source File: EventMethodCallInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void visitYamlMethodTagKey(@NotNull final PsiElement psiElement, @NotNull ProblemsHolder holder, ContainerCollectionResolver.LazyServiceCollector collector) {

        String methodName = PsiElementUtils.trimQuote(psiElement.getText());
        if(StringUtils.isBlank(methodName)) {
            return;
        }

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

        registerMethodProblem(psiElement, holder, classValue, collector);
    }
 
Example 10
Source File: ParameterPercentWrapInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    String insertText = null;
    if((lookupElement.getObject() instanceof PsiElement)) {
        return;
    }

    if((lookupElement.getObject() instanceof String)) {
        insertText = (String) lookupElement.getObject();
    }

    if(insertText == null) {
        return;
    }

    // "<caret>", '<caret>'
    insertText = PsiElementUtils.trimQuote(insertText);

    if(!insertText.startsWith("%")) {
        context.getDocument().insertString(context.getStartOffset(), "%");
    }

    // %| is also fired
    if(!insertText.endsWith("%") || insertText.length() == 1) {
        context.getDocument().insertString(context.getTailOffset(), "%");
        context.getEditor().getCaretModel().moveCaretRelatively(1, 0, false, false, true);
    }

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

    return new ArrayList<>(ServiceUtil.getTaggedClassesWithCompiled(psiElement.getProject(), tagName));
}
 
Example 12
Source File: TwigFoldingBuilder.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachPathFoldingDescriptors(PsiElement psiElement, List<FoldingDescriptor> descriptors) {

        // find path calls in file
        PsiElement[] psiElements = PsiTreeUtil.collectElements(psiElement, psiElement12 ->
            TwigPattern.getAutocompletableRoutePattern().accepts(psiElement12)
        );

        if(psiElements.length == 0) {
            return;
        }

        Map<String,Route> routes = null;
        for(PsiElement psiElement1: psiElements) {

            // cache routes if we need them
            if(routes == null) {
                routes = RouteHelper.getAllRoutes(psiElement.getProject());
            }

            String contents = PsiElementUtils.trimQuote(psiElement1.getText());
            if(contents.length() > 0 && routes.containsKey(contents)) {
                final Route route = routes.get(contents);

                final String url = RouteHelper.getRouteUrl(route);
                if(url != null) {
                    descriptors.add(new FoldingDescriptor(psiElement1.getNode(),
                        new TextRange(psiElement1.getTextRange().getStartOffset(), psiElement1.getTextRange().getEndOffset())) {
                        @Nullable
                        @Override
                        public String getPlaceholderText() {
                            return url;
                        }
                    });
                }
            }

        }

    }
 
Example 13
Source File: RouteSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
    String s = PsiElementUtils.trimQuote(element.getKeyText());
    if("pattern".equals(s) && YamlHelper.isRoutingFile(element.getContainingFile())) {
        // pattern: foo
        holder.registerProblem(element.getKey(), "Pattern is deprecated; use path instead", ProblemHighlightType.LIKE_DEPRECATED);

    } else if(("_method".equals(s) || "_scheme".equals(s)) && YamlHelper.isRoutingFile(element.getContainingFile())) {
        // requirements: { _method: 'foo', '_scheme': 'foo' }
        YAMLKeyValue parentOfType = PsiTreeUtil.getParentOfType(element, YAMLKeyValue.class);
        if(parentOfType != null && "requirements".equals(parentOfType.getKeyText())) {
            holder.registerProblem(element.getKey(), String.format("The '%s' requirement is deprecated", s), ProblemHighlightType.LIKE_DEPRECATED);
        }
    }
}
 
Example 14
Source File: ContainerSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
    String s = PsiElementUtils.trimQuote(element.getKeyText());
    if(("factory_class".equals(s) || "factory_method".equals(s) || "factory_service".equals(s)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
        // services:
        //   foo:
        //      factory_*:
        registerProblem(holder, element.getKey());
    }
}
 
Example 15
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 16
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private Collection<PsiElement> getControllerGoto(@NotNull PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    if(StringUtils.isBlank(text)) {
        return Collections.emptyList();
    }

    return Arrays.asList(RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), text));
}
 
Example 17
Source File: ConfigSchemaIndex.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {
    return inputData -> {
        Map<String, Set<String>> map = new THashMap<>();

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

        if(!(psiFile instanceof YAMLFile) || !isValidForIndex(psiFile)) {
            return map;
        }

        for(YAMLKeyValue yamlKeyValue: YamlHelper.getTopLevelKeyValues((YAMLFile) psiFile)) {
            String key = PsiElementUtils.trimQuote(yamlKeyValue.getKeyText());

            if(StringUtils.isBlank(key) || key.contains("*")) {
                continue;
            }

            Set<String> mappings = new HashSet<>();
            YAMLKeyValue mapping = YamlHelper.getYamlKeyValue(yamlKeyValue, "mapping");
            if(mapping == null) {
                continue;
            }

            Set<String> keySet = YamlHelper.getKeySet(mapping);
            if(keySet != null) {
                mappings.addAll(keySet);
            }

            map.put(key, mappings);
        }

        return map;
    };

}
 
Example 18
Source File: ExtJsGoToDeclarationHandler.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
private void attachControllerActionNameGoto(PsiElement sourceElement, final List<PsiElement> psiElements) {

        String text = PsiElementUtils.trimQuote(sourceElement.getText());
        if(text.startsWith("{") && text.endsWith("}")) {
            psiElements.addAll(ExtJsUtil.getControllerTargets(sourceElement, text));
        }

    }
 
Example 19
Source File: TwigTemplateGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
private Collection<PsiElement> getControllerGoTo(@NotNull  PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    return Arrays.asList(RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), text));
}
 
Example 20
Source File: YamlGoToDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@NotNull
private Collection<PsiElement> getClassGoto(@NotNull PsiElement psiElement) {
    String text = PsiElementUtils.trimQuote(psiElement.getText());
    return new ArrayList<>(PhpElementsUtil.getClassesInterface(psiElement.getProject(), text));
}