|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.lang; |
| 2 | + |
| 3 | +import com.intellij.lang.Language; |
| 4 | +import com.intellij.lang.injection.MultiHostInjector; |
| 5 | +import com.intellij.lang.injection.MultiHostRegistrar; |
| 6 | +import com.intellij.openapi.util.TextRange; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiLanguageInjectionHost; |
| 9 | +import com.jetbrains.php.lang.psi.elements.*; |
| 10 | +import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class ParameterLanguageInjector implements MultiHostInjector { |
| 20 | + |
| 21 | + private static final MethodMatcher.CallToSignature[] CSS_SELECTOR_SIGNATURES = { |
| 22 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\DomCrawler\\Crawler", "filter"), |
| 23 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\DomCrawler\\Crawler", "children"), |
| 24 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\CssSelector\\CssSelectorConverter", "toXPath"), |
| 25 | + }; |
| 26 | + |
| 27 | + private static final MethodMatcher.CallToSignature[] XPATH_SIGNATURES = { |
| 28 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\DomCrawler\\Crawler", "filterXPath"), |
| 29 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\DomCrawler\\Crawler", "evaluate"), |
| 30 | + }; |
| 31 | + |
| 32 | + private static final MethodMatcher.CallToSignature[] JSON_SIGNATURES = { |
| 33 | + //new MethodMatcher.CallToSignature("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "__construct"), |
| 34 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "fromJsonString"), |
| 35 | + new MethodMatcher.CallToSignature("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "setJson"), |
| 36 | + }; |
| 37 | + |
| 38 | + private static final MethodMatcher.CallToSignature[] DQL_SIGNATURES = { |
| 39 | + new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\EntityManager", "createQuery"), |
| 40 | + new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\Query", "setDQL"), |
| 41 | + }; |
| 42 | + |
| 43 | + private final MethodLanguageInjection[] LANGUAGE_INJECTIONS = { |
| 44 | + new MethodLanguageInjection(LANGUAGE_ID_CSS, "@media all { ", " }", CSS_SELECTOR_SIGNATURES), |
| 45 | + new MethodLanguageInjection(LANGUAGE_ID_XPATH, null, null, XPATH_SIGNATURES), |
| 46 | + new MethodLanguageInjection(LANGUAGE_ID_JSON, null, null, JSON_SIGNATURES), |
| 47 | + new MethodLanguageInjection(LANGUAGE_ID_DQL, null, null, DQL_SIGNATURES), |
| 48 | + }; |
| 49 | + |
| 50 | + public static final String LANGUAGE_ID_CSS = "CSS"; |
| 51 | + public static final String LANGUAGE_ID_XPATH = "XPath"; |
| 52 | + public static final String LANGUAGE_ID_JSON = "JSON"; |
| 53 | + public static final String LANGUAGE_ID_DQL = "DQL"; |
| 54 | + |
| 55 | + private static final String DQL_VARIABLE_NAME = "dql"; |
| 56 | + |
| 57 | + public ParameterLanguageInjector() { |
| 58 | + } |
| 59 | + |
| 60 | + @NotNull |
| 61 | + @Override |
| 62 | + public List<? extends Class<? extends PsiElement>> elementsToInjectIn() { |
| 63 | + return Collections.singletonList(StringLiteralExpressionImpl.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement element) { |
| 68 | + if (!(element instanceof StringLiteralExpression) || !((PsiLanguageInjectionHost) element).isValidHost()) { |
| 69 | + return; |
| 70 | + } |
| 71 | + if (!Symfony2ProjectComponent.isEnabled(element.getProject())) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + final StringLiteralExpressionImpl expr = (StringLiteralExpressionImpl) element; |
| 76 | + |
| 77 | + PsiElement parent = expr.getParent(); |
| 78 | + |
| 79 | + final boolean isParameter = parent instanceof ParameterList && expr.getPrevPsiSibling() == null; // 1st parameter |
| 80 | + final boolean isAssignment = parent instanceof AssignmentExpression; |
| 81 | + |
| 82 | + if (!isParameter && !isAssignment) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (isParameter) { |
| 87 | + parent = parent.getParent(); |
| 88 | + } |
| 89 | + |
| 90 | + for (MethodLanguageInjection languageInjection : LANGUAGE_INJECTIONS) { |
| 91 | + Language language = languageInjection.getLanguage(); |
| 92 | + if (language == null) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + // $crawler->filter('...') |
| 96 | + // $em->createQuery('...') |
| 97 | + // JsonResponse::fromJsonString('...') |
| 98 | + if (parent instanceof MethodReference) { |
| 99 | + if (PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) parent, languageInjection.getSignatures())) { |
| 100 | + injectLanguage(registrar, expr, language, languageInjection); |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + // $dql = "..."; |
| 105 | + else if (parent instanceof AssignmentExpression) { |
| 106 | + if (LANGUAGE_ID_DQL.equals(language.getID())) { |
| 107 | + PhpPsiElement variable = ((AssignmentExpression) parent).getVariable(); |
| 108 | + if (variable instanceof Variable) { |
| 109 | + if (DQL_VARIABLE_NAME.equals(variable.getName())) { |
| 110 | + injectLanguage(registrar, expr, language, languageInjection); |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private void injectLanguage(@NotNull MultiHostRegistrar registrar, @NotNull StringLiteralExpressionImpl element, Language language, MethodLanguageInjection languageInjection) { |
| 121 | + final int length = ((StringLiteralExpression) element).getContents().length(); |
| 122 | + final TextRange range = TextRange.create(1, length + 1); |
| 123 | + |
| 124 | + registrar.startInjecting(language) |
| 125 | + .addPlace(languageInjection.getPrefix(), languageInjection.getSuffix(), element, range) |
| 126 | + .doneInjecting(); |
| 127 | + } |
| 128 | + |
| 129 | + private class MethodLanguageInjection { |
| 130 | + private final Language language; |
| 131 | + private final String prefix; |
| 132 | + private final String suffix; |
| 133 | + private final MethodMatcher.CallToSignature[] signatures; |
| 134 | + |
| 135 | + MethodLanguageInjection(@NotNull String languageId, String prefix, String suffix, MethodMatcher.CallToSignature[] signatures) { |
| 136 | + |
| 137 | + this.language = Language.findLanguageByID(languageId); |
| 138 | + this.prefix = prefix; |
| 139 | + this.suffix = suffix; |
| 140 | + this.signatures = signatures; |
| 141 | + } |
| 142 | + |
| 143 | + public Language getLanguage() { |
| 144 | + return language; |
| 145 | + } |
| 146 | + |
| 147 | + public String getPrefix() { |
| 148 | + return prefix; |
| 149 | + } |
| 150 | + |
| 151 | + public String getSuffix() { |
| 152 | + return suffix; |
| 153 | + } |
| 154 | + |
| 155 | + public MethodMatcher.CallToSignature[] getSignatures() { |
| 156 | + return signatures; |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments