Java Code Examples for com.jetbrains.php.lang.psi.elements.StringLiteralExpression#getContents()

The following examples show how to use com.jetbrains.php.lang.psi.elements.StringLiteralExpression#getContents() . 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: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachFromIndexGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {

        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 2)
            .withSignature("\\Doctrine\\ORM\\QueryBuilder", "from")
            .match();

        if(methodMatchParameter == null) {
            return;
        }

        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser(methodMatchParameter.getMethodReference());
        if(qb == null) {
            return;
        }

        QueryBuilderScopeContext collect = qb.collect();
        String propertyContent = psiElement.getContents();
        for(Map.Entry<String, QueryBuilderPropertyAlias> entry: collect.getPropertyAliasMap().entrySet()) {
            if(entry.getKey().equals(propertyContent)) {
                targets.addAll(entry.getValue().getPsiTargets());
            }
        }

    }
 
Example 2
Source File: CreateMissingTranslationQuickFix.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
/**
 * Called to apply the fix.
 *
 * @param project    {@link Project}
 * @param descriptor problem reported by the tool which provided this quick fix action
 */
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement = descriptor.getPsiElement();
    if (psiElement instanceof StringLiteralExpression) {
        StringLiteralExpression stringElement = (StringLiteralExpression) psiElement;
        String contents = stringElement.getContents();

        String fileName = TranslationUtil.extractResourceFilenameFromTranslationString(contents);
        String key = TranslationUtil.extractTranslationKeyTranslationString(contents);
        if (fileName != null) {
            PsiElement[] elementsForKey = ResourcePathIndex.findElementsForKey(project, fileName);
            if (elementsForKey.length > 0) {
                // TranslationUtil.add();
            }
        }
    }
}
 
Example 3
Source File: PhpFoldingBuilder.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachTemplateShortcuts(List<FoldingDescriptor> descriptors, final StringLiteralExpression stringLiteralExpression) {

        if (MethodMatcher.getMatchedSignatureWithDepth(stringLiteralExpression, SymfonyPhpReferenceContributor.TEMPLATE_SIGNATURES) == null) {
            return;
        }

        String content = stringLiteralExpression.getContents();

        String templateShortcutName = TwigUtil.getFoldingTemplateName(content);
        if(templateShortcutName == null) {
            return;
        }

        final String finalTemplateShortcutName = templateShortcutName;
        descriptors.add(new FoldingDescriptor(stringLiteralExpression.getNode(),
            new TextRange(stringLiteralExpression.getTextRange().getStartOffset() + 1, stringLiteralExpression.getTextRange().getEndOffset() - 1)) {
            @Nullable
            @Override
            public String getPlaceholderText() {
                return finalTemplateShortcutName;
            }
        });

    }
 
Example 4
Source File: RouteFoldingBuilder.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Route");

    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);

    for (final StringLiteralExpression literalExpression : literalExpressions) {
        for (PsiReference reference : literalExpression.getReferences()) {
            if (reference instanceof RouteReference) {
                String value = literalExpression.getContents();

                FoldingDescriptor descriptor = foldRouteReferenceString(reference, value, group);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                }
            }
        }


    }

    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example 5
Source File: QueryBuilderGotoDeclarationHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachExprGoto(StringLiteralExpression psiElement, List<PsiElement> targets) {


        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 0)
            .withSignature(QueryBuilderCompletionContributor.EXPR)
            .match();

        if(methodMatchParameter == null) {
            return;
        }

        // simple resolve query inline instance usage
        // $qb->expr()->in('')
        MethodReference methodReference = methodMatchParameter.getMethodReference();
        PsiElement methodReferenceChild = methodReference.getFirstChild();
        if(!(methodReferenceChild instanceof MethodReference)) {
            return;
        }
        QueryBuilderMethodReferenceParser qb = QueryBuilderCompletionContributor.getQueryBuilderParser((MethodReference) methodReferenceChild);
        if(qb == null) {
            return;
        }

        String propertyContent = psiElement.getContents();
        QueryBuilderScopeContext collect = qb.collect();
        for(Map.Entry<String, QueryBuilderPropertyAlias> entry: collect.getPropertyAliasMap().entrySet()) {
            if(entry.getKey().equals(propertyContent)) {
                targets.addAll(entry.getValue().getPsiTargets());
            }
        }
    }
 
Example 6
Source File: RoutingGotoCompletionRegistrar.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(StringLiteralExpression element) {
    String contents = element.getContents();
    if(StringUtils.isBlank(contents)) {
        return Collections.emptyList();
    }

    return RoutingUtil.getRoutesAsTargets(element.getProject(), contents);
}
 
Example 7
Source File: PhpFoldingBuilder.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void attachModelShortcuts(List<FoldingDescriptor> descriptors, final StringLiteralExpression stringLiteralExpression) {

        if (MethodMatcher.getMatchedSignatureWithDepth(stringLiteralExpression, SymfonyPhpReferenceContributor.REPOSITORY_SIGNATURES) == null) {
            return;
        }

        String content = stringLiteralExpression.getContents();

        for(String lastChar: new String[] {":", "\\"}) {
            if(content.contains(lastChar)) {
                final String replace = content.substring(content.lastIndexOf(lastChar) + 1);
                if(replace.length() > 0) {
                    descriptors.add(new FoldingDescriptor(stringLiteralExpression.getNode(),
                        new TextRange(stringLiteralExpression.getTextRange().getStartOffset() + 1, stringLiteralExpression.getTextRange().getEndOffset() - 1)) {
                        @Nullable
                        @Override
                        public String getPlaceholderText() {
                            return replace;
                        }
                    });
                }

                return;
            }
        }

    }
 
Example 8
Source File: GeneralUtilityServiceTypeProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb() || !TYPO3CMSProjectSettings.isEnabled(psiElement)) {
        return null;
    }

    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstanceService")) {
        return null;
    }

    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }

    PsiElement firstParam = methodReference.getParameters()[0];

    if (firstParam instanceof StringLiteralExpression) {
        StringLiteralExpression ref = (StringLiteralExpression) firstParam;
        String serviceId = ref.getContents();

        return new PhpType().add("#" + this.getKey() + serviceId);
    }

    return null;
}
 
Example 9
Source File: DicCompletionRegistrar.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(StringLiteralExpression element) {
    String contents = element.getContents();
    if(StringUtils.isBlank(contents)) {
        return Collections.emptyList();
    }

    return LaravelDicUtil.getDicTargets(getProject(), contents);
}
 
Example 10
Source File: AppConfigReferences.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(StringLiteralExpression element) {

    final Set<PsiElement> targets = new HashSet<>();

    final String contents = element.getContents();
    if(StringUtils.isBlank(contents)) {
        return targets;
    }

    FileBasedIndex.getInstance().getFilesWithKey(ConfigKeyStubIndex.KEY, Collections.singleton(contents), virtualFile -> {
        PsiFile psiFileTarget = PsiManager.getInstance(getProject()).findFile(virtualFile);
        if(psiFileTarget == null) {
            return true;
        }

        psiFileTarget.acceptChildren(new ArrayReturnPsiRecursiveVisitor(ConfigFileUtil.matchConfigFile(getProject(), virtualFile).getKeyPrefix(), (key, psiKey, isRootElement) -> {
            if(!isRootElement && key.equals(contents)) {
                targets.add(psiKey);
            }
        }));

        return true;
    }, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(getProject()), PhpFileType.INSTANCE));

    return targets;
}
 
Example 11
Source File: RouteAnnotator.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {
    if (!TYPO3CMSProjectSettings.getInstance(psiElement.getProject()).pluginEnabled) {
        return;
    }

    if (!TYPO3CMSProjectSettings.getInstance(psiElement.getProject()).routeAnnotatorEnabled) {
        return;
    }

    if (!(psiElement instanceof StringLiteralExpression)) {
        return;
    }

    StringLiteralExpression literalExpression = (StringLiteralExpression) psiElement;
    String value = literalExpression.getContents();

    if (value.isEmpty()) {
        return;
    }

    for (PsiReference psiReference : literalExpression.getReferences()) {
        if (psiReference instanceof RouteReference) {
            annotateRoute(psiElement, annotationHolder, value);
        }
    }
}
 
Example 12
Source File: RouteLineMarkerProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
    if (!TYPO3CMSProjectSettings.isEnabled(element)) {
        return;
    }

    if (!getPositionPattern().accepts(element)) {
        return;
    }

    StringLiteralExpression literalExpression = (StringLiteralExpression) element.getParent();
    String value = literalExpression.getContents();

    if (value.isEmpty()) {
        return;
    }

    PsiElement methodReference = PsiTreeUtil.getParentOfType(element, MethodReference.class);
    if (PhpElementsUtil.isMethodWithFirstStringOrFieldReference(methodReference, "getAjaxUrl") || PhpElementsUtil.isMethodWithFirstStringOrFieldReference(methodReference, "buildUriFromRoute")) {

        if (RouteIndex.hasRoute(element.getProject(), value)) {
            Collection<RouteStub> routes = RouteIndex.getRoute(element.getProject(), value);
            routes.forEach(def -> {
                PsiElement[] routeDefinitionElements = RouteHelper.getRouteDefinitionElements(element.getProject(), value);
                NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder
                    .create(TYPO3CMSIcons.ROUTE_ICON)
                    .setTargets(routeDefinitionElements);

                if (def.getPath() != null) {
                    builder.setTooltipTitle("Path: " + def.getPath());
                }

                result.add(builder.createLineMarkerInfo(element));
            });

        }
    }
}
 
Example 13
Source File: RouteParameterReference.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public RouteParameterReference(@NotNull StringLiteralExpression element, String routeName) {
    super(element);
    this.routeName = routeName;
    this.parameterName = element.getContents();
}
 
Example 14
Source File: ResourceReference.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
public ResourceReference(StringLiteralExpression psiElement) {
    super(psiElement);

    this.resourceName = psiElement.getContents();
}
 
Example 15
Source File: SignalSlotMethodReference.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
public SignalSlotMethodReference(@NotNull StringLiteralExpression className, @NotNull StringLiteralExpression subject) {
    super(subject);

    this.classFqn = className.getContents();
    this.methodName = subject.getContents();
}
 
Example 16
Source File: TagReference.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public TagReference(@NotNull StringLiteralExpression element) {
    super(element);
    tagName = element.getContents();
}
 
Example 17
Source File: ServiceReference.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public ServiceReference(@NotNull StringLiteralExpression element) {
    super(element);
    this.serviceId = element.getContents();
}
 
Example 18
Source File: TranslationDomainReference.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public TranslationDomainReference(@NotNull StringLiteralExpression element) {
    super(element);
    this.domainName = element.getContents();
}
 
Example 19
Source File: IconReference.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
public IconReference(StringLiteralExpression psiElement) {
    super(psiElement);

    iconName = psiElement.getContents();
}
 
Example 20
Source File: RouterReference.java    From Thinkphp5-Plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(StringLiteralExpression element) {

    final Set<PsiElement> targets = new HashSet<>();

    String contents = element.getContents();
    if (StringUtils.isBlank(contents)) {
        return targets;
    }

    //忽略大小写
    Collection<String> allKeys = FileBasedIndex.getInstance().getAllKeys(RouteValStubIndex.KEY, getElement().getProject());

    final String contents1 = Util.getKeyWithCase(allKeys, contents);

    FileBasedIndex.getInstance().getFilesWithKey(RouteValStubIndex.KEY, new HashSet<>(Collections.singletonList(contents1)), new Processor<VirtualFile>() {
        @Override
        public boolean process(VirtualFile virtualFile) {
            PsiFile psiFileTarget = PsiManager.getInstance(RouteProvider.this.getProject()).findFile(virtualFile);
            if (psiFileTarget == null) {
                return true;
            }
            String[] split = contents.split("/");
            if (split.length == 2)
                targets.add(psiFileTarget);
            psiFileTarget.acceptChildren(new PhpControllerVisitor(RouteUtil.matchControllerFile(RouteProvider.this.getProject(), virtualFile).getKeyPrefix(),
                    new ArrayKeyVisitor() {
                        @Override
                        public void visit(String key, PsiElement psiKey, boolean isRootElement) {
                            if (!isRootElement && key.equals(contents1)) {
                                targets.add(psiKey);
                            }
                        }
                    }));

            return true;
        }
    }, GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(getProject()), PhpFileType.INSTANCE));

    return targets;
}