Skip to content

Commit

Permalink
Deprecated ParameterLanguageInjector in favour of StringLiteralLangua…
Browse files Browse the repository at this point in the history
…geInjector
  • Loading branch information
adamwojs committed Jul 8, 2021
1 parent e70d375 commit 6133ea9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public Builder matchingMethodCallArgument(@NotNull String classFQN, @NotNull Str
return matchingPattern(LanguageInjectionPatterns.getMethodCallArgumentPattern(classFQN, methodName, argumentName, argumentIndex));
}

public Builder matchingVariableAssigment(@NotNull String variableName) {
return matchingPattern(LanguageInjectionPatterns.getVariableAssignmentPattern(variableName));
}

public LanguageInjection build() {
return new LanguageInjection(languageId, prefix, suffix, StandardPatterns.or(patterns.toArray(new ElementPattern[0])));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public static ElementPattern<? extends PsiElement> getFunctionCallArgumentPatter
);
}

public static ElementPattern<? extends PsiElement> getVariableAssignmentPattern(@NotNull String variableName) {
return PlatformPatterns.psiElement()
.withParent(PlatformPatterns
.psiElement(AssignmentExpression.class)
.withFirstChild(PlatformPatterns
.psiElement(Variable.class)
.withName(variableName)
)
);
}

private static class IsAnnotationProperty extends PatternCondition<StringLiteralExpression> {
@NotNull
private final String classFQN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collections;
import java.util.List;

@Deprecated
public class ParameterLanguageInjector implements MultiHostInjector {

private static final MethodMatcher.CallToSignature[] CSS_SELECTOR_SIGNATURES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,33 @@

public class StringLiteralLanguageInjector implements MultiHostInjector {

public static final String LANGUAGE_ID_CSS = "CSS";
public static final String LANGUAGE_ID_XPATH = "XPath";
public static final String LANGUAGE_ID_JSON = "JSON";
public static final String LANGUAGE_ID_DQL = "DQL";
public static final String LANGUAGE_ID_EXPRESSION_LANGUAGE = "Symfony Expression Language";

private final LanguageInjection[] LANGUAGE_INJECTIONS = {
new LanguageInjection.Builder(LANGUAGE_ID_CSS)
.withPrefix("@media all { ")
.withSuffix(" }")
.matchingMethodCallArgument("\\Symfony\\Component\\DomCrawler\\Crawler", "filter", "selector", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\DomCrawler\\Crawler", "children", "selector", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\CssSelector\\CssSelectorConverter", "toXPath", "cssExpr", 0)
.build(),
new LanguageInjection.Builder(LANGUAGE_ID_XPATH)
.matchingMethodCallArgument("\\Symfony\\Component\\DomCrawler\\Crawler", "filterXPath", "xpath", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\DomCrawler\\Crawler", "evaluate", "xpath", 0)
.build(),
new LanguageInjection.Builder(LANGUAGE_ID_JSON)
.matchingMethodCallArgument("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "fromJsonString", "data", 0)
.matchingMethodCallArgument("\\Symfony\\Component\\HttpFoundation\\JsonResponse", "setJson", "json", 0)
.build(),
new LanguageInjection.Builder(LANGUAGE_ID_DQL)
.matchingMethodCallArgument("\\Doctrine\\ORM\\EntityManager", "createQuery", "dql", 0)
.matchingMethodCallArgument("\\Doctrine\\ORM\\Query", "setDQL", "dqlQuery", 0)
.matchingVariableAssigment("dql")
.build(),
new LanguageInjection.Builder(LANGUAGE_ID_EXPRESSION_LANGUAGE)
.matchingConstructorCallArgument("\\Symfony\\Component\\ExpressionLanguage\\Expression", "expression", 0)
.matchingFunctionCallArgument("\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.jetbrains.php.lang.PhpFileType;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import junit.framework.TestCase;
import org.junit.Ignore;

import static fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector.*;

@Deprecated
@Ignore("ParameterLanguageInjectorTest is deprecated")
public class ParameterLanguageInjectorTest extends SymfonyLightCodeInsightFixtureTestCase {

private InjectionTestFixture injectionTestFixture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package fr.adrienbrault.idea.symfony2plugin.tests.lang;

import com.intellij.psi.PsiElement;
import com.intellij.testFramework.fixtures.InjectionTestFixture;
import com.jetbrains.php.lang.PhpFileType;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_CSS;
import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_DQL;
import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_EXPRESSION_LANGUAGE;
import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_JSON;
import static fr.adrienbrault.idea.symfony2plugin.lang.StringLiteralLanguageInjector.LANGUAGE_ID_XPATH;

public class StringLiteralLanguageInjectorTest extends SymfonyLightCodeInsightFixtureTestCase {

Expand All @@ -22,6 +29,52 @@ public String getTestDataPath() {
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/lang/fixtures";
}

public void skipTestCssLanguageInjections() {
// skip as we dont have CSS module in >= 2020 test builds
String base = "<?php $c = new \\Symfony\\Component\\DomCrawler\\Crawler();\n";
assertInjectedLangAtCaret(base + "$c->filter('html > bo<caret>dy');", LANGUAGE_ID_CSS);
assertInjectedLangAtCaret(base + "$c->filter('<caret>');", LANGUAGE_ID_CSS);
assertInjectedLangAtCaret(base + "$c->children('html > bo<caret>dy');", LANGUAGE_ID_CSS);
assertInjectedLangAtCaret(base + "$c->children('<caret>');", LANGUAGE_ID_CSS);

base = "<?php $c = new \\Symfony\\Component\\CssSelector\\CssSelectorConverter();\n";
assertInjectedLangAtCaret(base + "$c->toXPath('html > bo<caret>dy');", LANGUAGE_ID_CSS);
assertInjectedLangAtCaret(base + "$c->toXPath('<caret>');", LANGUAGE_ID_CSS);
}

public void testXPathLanguageInjections() {
String base = "<?php $c = new \\Symfony\\Component\\DomCrawler\\Crawler();\n";
assertInjectedLangAtCaret(base + "$c->filterXPath('//dum<caret>my');", LANGUAGE_ID_XPATH);
assertInjectedLangAtCaret(base + "$c->filterXPath('<caret>');", LANGUAGE_ID_XPATH);
assertInjectedLangAtCaret(base + "$c->evaluate('//dum<caret>my');", LANGUAGE_ID_XPATH);
assertInjectedLangAtCaret(base + "$c->evaluate('<caret>');", LANGUAGE_ID_XPATH);
}

public void testJsonLanguageInjections() {
String base = "<?php \\Symfony\\Component\\HttpFoundation\\";
assertInjectedLangAtCaret(base + "JsonResponse::fromJsonString('<caret>');", LANGUAGE_ID_JSON);
assertInjectedLangAtCaret(base + "JsonResponse::fromJsonString('{\"foo\": <caret>}');", LANGUAGE_ID_JSON);

base = "<?php $r = new \\Symfony\\Component\\HttpFoundation\\JsonResponse();\n";
assertInjectedLangAtCaret(base + "$r->setJson('<caret>');", LANGUAGE_ID_JSON);
assertInjectedLangAtCaret(base + "$r->setJson('{\"foo\": <caret>}');", LANGUAGE_ID_JSON);
}

public void testDqlLanguageInjections() {
String base = "<?php $em = new \\Doctrine\\ORM\\EntityManager();\n";
assertInjectedLangAtCaret(base + "$em->createQuery('SELECT b FR<caret>OM \\Foo\\Bar b');", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret(base + "$em->createQuery('<caret>');", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret(base + "$em->createQuery(<<caret><<AAA\n \nAAA\n);", null);
assertInjectedFragmentText(base + "$em->createQuery(<<<AAA\nSELEC<caret>T\nAAA\n);", "SELECT");
assertInjectedFragmentText(base + "$em->createQuery(<<<'AAA'\nSELEC<caret>T a\nAAA\n);", "SELECT a");
base = "<?php $q = new \\Doctrine\\ORM\\Query();\n";
assertInjectedLangAtCaret(base + "$q->setDQL('SELECT b FR<caret>OM \\Foo\\Bar b');", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret(base + "$q->setDQL('<caret>');", LANGUAGE_ID_DQL);

assertInjectedLangAtCaret("<?php $dql = \"SELECT b FR<caret>OM \\Foo\\Bar b\");", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret("<?php $dql = \"<caret>\");", LANGUAGE_ID_DQL);
}

public void testExpressionLanguageLanguageInjections() {
assertInjectedLangAtCaret(
"<?php $expr = new \\Symfony\\Component\\ExpressionLanguage\\Expression('<caret>');",
Expand Down Expand Up @@ -327,8 +380,15 @@ public void testExpressionLanguageLanguageInjections() {
);
}

private void assertInjectedLangAtCaret(@NotNull String configureByText, @NotNull String lang) {
private void assertInjectedLangAtCaret(@NotNull String configureByText, @Nullable String lang) {
myFixture.configureByText(PhpFileType.INSTANCE, configureByText);
injectionTestFixture.assertInjectedLangAtCaret(lang);
}

private void assertInjectedFragmentText(@NotNull String configureByText, String text) {
myFixture.configureByText(PhpFileType.INSTANCE, configureByText);
PsiElement injectedElement = injectionTestFixture.getInjectedElement();
assertNotNull(injectedElement);
TestCase.assertEquals(text, injectedElement.getContainingFile().getText());
}
}

0 comments on commit 6133ea9

Please sign in to comment.