com.jetbrains.php.lang.lexer.PhpTokenTypes Java Examples

The following examples show how to use com.jetbrains.php.lang.lexer.PhpTokenTypes. 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: ControllerLineMarkerProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
    if (!PlatformPatterns.psiElement(PhpTokenTypes.IDENTIFIER).withParent(PlatformPatterns.psiElement(Method.class).withName(PlatformPatterns.string().endsWith("Action"))).accepts(element)) {
        return;
    }

    Collection<FluidFile> possibleMatchedTemplates = FluidUtil.findTemplatesForControllerAction((Method) element.getParent());
    if (possibleMatchedTemplates.size() > 0) {
        result.add(
            NavigationGutterIconBuilder
                .create(FluidIcons.TEMPLATE_LINE_MARKER)
                .setTargets(possibleMatchedTemplates)
                .setTooltipText("Navigate to template")
                .createLineMarkerInfo(element)
        );
    }
}
 
Example #2
Source File: PhpAutoPopupSpaceTypedHandler.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if (!(file instanceof PhpFile)) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    if ((c != ' ')) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset() - 2);
    if (psiElement == null || !(PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL).accepts(psiElement) || PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE).accepts(psiElement))) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    scheduleAutoPopup(project, editor, null);

    return TypedHandlerDelegate.Result.CONTINUE;
}
 
Example #3
Source File: RouteLineMarkerProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
private ElementPattern<PsiElement> getPositionPattern() {

    return PlatformPatterns.or(
        PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE).withParent(StringLiteralExpression.class),
        PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL).withParent(StringLiteralExpression.class)
    );
}
 
Example #4
Source File: BxSuperglobalsProvider.java    From bxfs with MIT License 5 votes vote down vote up
public BxSuperglobalsProvider() {
	extend(CompletionType.BASIC, PlatformPatterns.psiElement(PhpTokenTypes.VARIABLE).withLanguage(PhpLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() {
		public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
			String currentFileName = parameters.getOriginalFile().getName();
			for (String variable : bxSuperglobals.keySet()) {
				BxSuperglobal superglobal = bxSuperglobals.get(variable);
				if (superglobal.scopeFileNames == null || superglobal.scopeFileNames.contains(currentFileName))
					resultSet.addElement(LookupElementBuilder.create("$" + variable));
			}
		}
	});
}
 
Example #5
Source File: PhpLineMarkerProvider.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
private void collectSubscriberTargets(@NotNull List<PsiElement> psiElements, final @NotNull Collection<LineMarkerInfo> lineMarkerInfos) {
    Collection<PhpClass> phpClasses = new ArrayList<>();

    for (PsiElement psiElement : psiElements) {
        if(psiElement instanceof PhpClass && PhpElementsUtil.isInstanceOf((PhpClass) psiElement, "Enlight\\Event\\SubscriberInterface")) {
            phpClasses.add((PhpClass) psiElement);
        }
    }

    for (PhpClass phpClass : phpClasses) {
        Method getSubscribedEvents = phpClass.findOwnMethodByName("getSubscribedEvents");
        if(getSubscribedEvents == null) {
            continue;
        }

        Map<String, Pair<String, PsiElement>> methodEvent = new HashMap<>();

        HookSubscriberUtil.visitSubscriberEvents(getSubscribedEvents, (event, methodName, key) ->
            methodEvent.put(methodName, Pair.create(event, key))
        );

        for (Method method : phpClass.getOwnMethods()) {
            if(!methodEvent.containsKey(method.getName()) || !method.getAccess().isPublic()) {
                continue;
            }

            Pair<String, PsiElement> result = methodEvent.get(method.getName());
            NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(ShopwarePluginIcons.SHOPWARE_LINEMARKER).
                setTargets(new MyCollectionNotNullLazyValue(result.getSecond(), result.getFirst())).
                setTooltipText("Related Targets");

            // attach linemarker to leaf item which is our function name for performance reasons
            ASTNode node = method.getNode().findChildByType(PhpTokenTypes.IDENTIFIER);
            if(node != null) {
                lineMarkerInfos.add(builder.createLineMarkerInfo(node.getPsi()));
            }
        }
    }
}
 
Example #6
Source File: ShopwareBoostrapInspection.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    PsiFile psiFile = holder.getFile();
    if(!ShopwareProjectComponent.isValidForProject(psiFile)) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    if(!"Bootstrap.php".equals(psiFile.getName())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if(element instanceof Method) {
                String methodName = ((Method) element).getName();
                if(INSTALL_METHODS.contains(((Method) element).getName())) {
                    if(PsiTreeUtil.collectElementsOfType(element, PhpReturn.class).size() == 0) {
                        PsiElement psiElement = PsiElementUtils.getChildrenOfType(element, PlatformPatterns.psiElement(PhpTokenTypes.IDENTIFIER).withText(methodName));
                        if(psiElement != null) {
                            holder.registerProblem(psiElement, "Shopware need return statement", ProblemHighlightType.GENERIC_ERROR);
                        }
                    }
                }

            }

            super.visitElement(element);
        }
    };
}
 
Example #7
Source File: VoterUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static void visitAttributeForeach(@NotNull Method method, @NotNull Consumer<Pair<String, PsiElement>> consumer) {
    Parameter[] parameters = method.getParameters();
    if(parameters.length < 3) {
        return;
    }

    for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[2])) {
        // foreach ($attributes as $attribute)
        PsiElement psiElement = PsiTreeUtil.nextVisibleLeaf(variable);
        if(psiElement != null && psiElement.getNode().getElementType() == PhpTokenTypes.kwAS) {
            PsiElement parent = variable.getParent();
            if(!(parent instanceof ForeachStatement)) {
                continue;
            }

            PhpPsiElement variableDecl = variable.getNextPsiSibling();
            if(variableDecl instanceof Variable) {
                for (Variable variable1 : PhpElementsUtil.getVariablesInScope(parent, (Variable) variableDecl)) {
                    visitVariable(variable1, consumer);
                }
            }
        }

        // in_array('foobar', $attributes)
        PsiElement parameterList = variable.getParent();
        if(parameterList instanceof ParameterList && PsiElementUtils.getParameterIndexValue(variable) == 1) {
            PsiElement functionCall = parameterList.getParent();
            if(functionCall instanceof FunctionReference && "in_array".equalsIgnoreCase(((FunctionReference) functionCall).getName())) {
                PsiElement[] functionParameter = ((ParameterList) parameterList).getParameters();
                if(functionParameter.length > 0) {
                    String stringValue = PhpElementsUtil.getStringValue(functionParameter[0]);
                    if(stringValue != null && StringUtils.isNotBlank(stringValue)) {
                        consumer.accept(Pair.create(stringValue, functionParameter[0]));
                    }
                }
            }
        }
    }
}
 
Example #8
Source File: VoterUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * null == null, null != null, null === null
 */
private static boolean isIfOperand(@NotNull IElementType node) {
    return
        node == PhpTokenTypes.opIDENTICAL ||
            node == PhpTokenTypes.opEQUAL ||
            node == PhpTokenTypes.opNOT_EQUAL ||
            node == PhpTokenTypes.opNOT_IDENTICAL
        ;
}
 
Example #9
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * class "Foo" extends
 */
static public PsiElementPattern.Capture<PsiElement> getClassNamePattern() {
    return PlatformPatterns
        .psiElement(PhpTokenTypes.IDENTIFIER)
        .afterLeafSkipping(
            PlatformPatterns.psiElement(PsiWhiteSpace.class),
            PlatformPatterns.psiElement(PhpTokenTypes.kwCLASS)
        )
        .withParent(PhpClass.class)
        .withLanguage(PhpLanguage.INSTANCE);
}
 
Example #10
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * class Foo { function "test" {} }
 */
static public PsiElementPattern.Capture<PsiElement> getClassMethodNamePattern() {
    return PlatformPatterns
        .psiElement(PhpTokenTypes.IDENTIFIER)
        .afterLeafSkipping(
            PlatformPatterns.psiElement(PsiWhiteSpace.class),
            PlatformPatterns.psiElement(PhpTokenTypes.kwFUNCTION)
        )
        .withParent(Method.class)
        .withLanguage(PhpLanguage.INSTANCE);
}
 
Example #11
Source File: AnnotationUsageLineMarkerProvider.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * class "Foo" extends
 */
private static PsiElementPattern.Capture<PsiElement> getClassNamePattern() {
    return PlatformPatterns
        .psiElement(PhpTokenTypes.IDENTIFIER)
        .afterLeafSkipping(
            PlatformPatterns.psiElement(PsiWhiteSpace.class),
            PlatformPatterns.psiElement(PhpTokenTypes.kwCLASS)
        )
        .withParent(PhpClass.class)
        .withLanguage(PhpLanguage.INSTANCE);
}
 
Example #12
Source File: RepositoryMagicMethodsCompletionContributor.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
public RepositoryMagicMethodsCompletionContributor() {
    this.extend(CompletionType.BASIC, PlatformPatterns.psiElement(PhpTokenTypes.IDENTIFIER), new ExtbaseRepositoryMagicMethodsCompletionProvider());
}
 
Example #13
Source File: PhpLineMarkerProvider.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
private void collectBootstrapSubscriber(@NotNull List<PsiElement> psiElements, @NotNull Collection<LineMarkerInfo> lineMarkerInfos, PsiFile containingFile) {
    Map<String, Method> methods = new HashMap<>();

    // we dont want multiple line markers, so wrap all into one here
    Map<String, Set<PsiElement>> methodTargets = new HashMap<>();

    for(PsiElement psiElement: psiElements) {
        if(psiElement instanceof Method && ((Method) psiElement).getModifier().isPublic()) {
            methods.put(((Method) psiElement).getName(), (Method) psiElement);
        }
    }

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

    final Project project = containingFile.getProject();
    containingFile.accept(new PsiRecursiveElementWalkingVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if(!(element instanceof MethodReference)) {
                super.visitElement(element);
                return;
            }

            MethodReference methodReference = (MethodReference) element;
            String name = methodReference.getName();
            if(name != null && (name.equals("subscribeEvent") || name.equals("createEvent"))) {
                PsiElement[] parameters = methodReference.getParameters();
                if(parameters.length >= 2 && parameters[0] instanceof StringLiteralExpression && parameters[1] instanceof StringLiteralExpression) {
                    String subscriberName = ((StringLiteralExpression) parameters[0]).getContents();
                    String methodName = ((StringLiteralExpression) parameters[1]).getContents();

                    if(StringUtils.isNotBlank(subscriberName) && StringUtils.isNotBlank(methodName)) {

                        if(methods.containsKey(methodName)) {

                            if(!methodTargets.containsKey(methodName)) {
                                methodTargets.put(methodName, new HashSet<>());
                            }

                            methodTargets.get(methodName).add(parameters[1]);
                            methodTargets.get(methodName).addAll(HookSubscriberUtil.getAllHookTargets(project, subscriberName));
                        }
                    }
                }
            }

            super.visitElement(element);
        }
    });

    // we allow multiple events
    for(Map.Entry<String, Set<PsiElement>> method: methodTargets.entrySet()) {
        if(methods.containsKey(method.getKey())) {

            NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(ShopwarePluginIcons.SHOPWARE_LINEMARKER).
                setTargets(method.getValue()).
                setTooltipText("Related Targets");

            Method psiMethod = methods.get(method.getKey());

            // attach linemarker to leaf item which is our function name for performance reasons
            ASTNode node = psiMethod.getNode().findChildByType(PhpTokenTypes.IDENTIFIER);
            if(node != null) {
                lineMarkerInfos.add(builder.createLineMarkerInfo(node.getPsi()));
            }
        }
    }
}
 
Example #14
Source File: ControllerMethodLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
public LineMarkerInfo collect(PsiElement psiElement) {
    if(!Symfony2ProjectComponent.isEnabled(psiElement) || psiElement.getNode().getElementType() != PhpTokenTypes.IDENTIFIER) {
        return null;
    }

    PsiElement method = psiElement.getParent();
    if(!(method instanceof Method) || !((Method) method).getAccess().isPublic()) {
        return null;
    }

    List<GotoRelatedItem> gotoRelatedItems = getGotoRelatedItems((Method) method);

    if(gotoRelatedItems.size() == 0) {
        return null;
    }

    // only one item dont need popover
    if(gotoRelatedItems.size() == 1) {

        GotoRelatedItem gotoRelatedItem = gotoRelatedItems.get(0);

        // hell: find any possible small icon
        Icon icon = null;
        if(gotoRelatedItem instanceof RelatedPopupGotoLineMarker.PopupGotoRelatedItem) {
            icon = ((RelatedPopupGotoLineMarker.PopupGotoRelatedItem) gotoRelatedItem).getSmallIcon();
        }

        if(icon == null) {
           icon = Symfony2Icons.SYMFONY_LINE_MARKER;
        }

        NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(icon).
            setTargets(gotoRelatedItems.get(0).getElement());

        String customName = gotoRelatedItems.get(0).getCustomName();
        if(customName != null) {
            builder.setTooltipText(customName);
        }

        return builder.createLineMarkerInfo(psiElement);
    }

    return new LineMarkerInfo<>(
        psiElement,
        psiElement.getTextRange(),
        Symfony2Icons.SYMFONY_LINE_MARKER,
        6,
        new ConstantFunction<>("Related Files"),
        new RelatedPopupGotoLineMarker.NavigationHandler(gotoRelatedItems),
        GutterIconRenderer.Alignment.RIGHT
    );
}