Package fr.adrienbrault.idea.symfony2plugin.dic

Source Code of fr.adrienbrault.idea.symfony2plugin.dic.ConstraintPropertyReference

package fr.adrienbrault.idea.symfony2plugin.dic;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyVariantReferenceBase;
import com.intellij.psi.ResolveResult;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.resolve.PhpResolveResult;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ConstraintPropertyReference extends PsiPolyVariantReferenceBase<PsiElement> {

    private final PhpClass constraintPhpClass;

    public ConstraintPropertyReference(StringLiteralExpression psiElement, PhpClass constraintPhpClass) {
        super(psiElement);
        this.constraintPhpClass = constraintPhpClass;
    }

    @NotNull
    @Override
    public ResolveResult[] multiResolve(boolean b) {

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

        String content = ((StringLiteralExpression) getElement()).getContents();
        for(Field field: constraintPhpClass.getFields()) {
            String name = field.getName();
            if(!field.isConstant() && field.getModifier().isPublic() && content.equals(name)) {
                psiElements.add(field);
            }
        }

        return PhpResolveResult.create(psiElements);
    }

    @NotNull
    @Override
    public Object[] getVariants() {

        List<LookupElement> lookupElements = new ArrayList<LookupElement>();

        for(Field field: constraintPhpClass.getFields()) {
            if(!field.isConstant() && field.getModifier().isPublic()) {

                LookupElementBuilder lookupElement = LookupElementBuilder.create(field.getName()).withIcon(Symfony2Icons.SYMFONY);

                String defaultValue = PhpElementsUtil.getStringValue(field.getDefaultValue());
                if(defaultValue != null) {
                    lookupElement = lookupElement.withTypeText(defaultValue, true);
                }

                lookupElements.add(lookupElement);
            }
        }

        return lookupElements.toArray();
    }

}
TOP

Related Classes of fr.adrienbrault.idea.symfony2plugin.dic.ConstraintPropertyReference

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.