Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed injection range for heredoc/nowdoc literals in automatic injectors #1337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ else if (parent instanceof AssignmentExpression) {
}

private void injectLanguage(@NotNull MultiHostRegistrar registrar, @NotNull StringLiteralExpressionImpl element, Language language, MethodLanguageInjection languageInjection) {
final int length = ((StringLiteralExpression) element).getContents().length();
final TextRange range = TextRange.create(1, length + 1);

registrar.startInjecting(language)
.addPlace(languageInjection.getPrefix(), languageInjection.getSuffix(), element, range)
.addPlace(languageInjection.getPrefix(), languageInjection.getSuffix(), element, element.getValueRange())
.doneInjecting();
}

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

import com.intellij.openapi.fileTypes.LanguageFileType;
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 static fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector.*;

Expand Down Expand Up @@ -56,7 +58,9 @@ public void testDqlLanguageInjections() {
String base = "<?php $em = new \\Doctrine\\ORM\\EntityManager();\n";
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$em->createQuery('SELECT b FR<caret>OM \\Foo\\Bar b');", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$em->createQuery('<caret>');", LANGUAGE_ID_DQL);

assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$em->createQuery(<<caret><<AAA\n \nAAA\n);", null);
assertInjectedFragmentText(PhpFileType.INSTANCE, base + "$em->createQuery(<<<AAA\nSELEC<caret>T\nAAA\n);", "SELECT");
assertInjectedFragmentText(PhpFileType.INSTANCE, base + "$em->createQuery(<<<'AAA'\nSELEC<caret>T a\nAAA\n);", "SELECT a");
base = "<?php $q = new \\Doctrine\\ORM\\Query();\n";
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$q->setDQL('SELECT b FR<caret>OM \\Foo\\Bar b');", LANGUAGE_ID_DQL);
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$q->setDQL('<caret>');", LANGUAGE_ID_DQL);
Expand All @@ -69,4 +73,11 @@ private void assertInjectedLangAtCaret(LanguageFileType fileType, String configu
myFixture.configureByText(fileType, configureByText);
injectionTestFixture.assertInjectedLangAtCaret(lang);
}

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