|
4 | 4 | import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
5 | 5 | import com.intellij.patterns.PlatformPatterns;
|
6 | 6 | import com.intellij.psi.PsiElement;
|
| 7 | +import com.jetbrains.php.lang.psi.elements.ArrayCreationExpression; |
| 8 | +import com.jetbrains.php.lang.psi.elements.NewExpression; |
| 9 | +import com.jetbrains.php.lang.psi.elements.ParameterList; |
7 | 10 | import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
|
8 | 11 | import com.jetbrains.twig.elements.TwigElementTypes;
|
9 | 12 | import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
|
|
15 | 18 | import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
|
16 | 19 | import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil;
|
17 | 20 | import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
|
| 21 | +import fr.adrienbrault.idea.symfony2plugin.util.ParameterBag; |
18 | 22 | import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
|
19 | 23 | import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
|
20 | 24 | import org.apache.commons.lang.StringUtils;
|
@@ -54,6 +58,12 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
|
54 | 58 | PlatformPatterns.psiElement().withParent(StringLiteralExpression.class),
|
55 | 59 | new MyPhpTranslationCompletionContributor("transChoice", 2, 3)
|
56 | 60 | );
|
| 61 | + |
| 62 | + // $x->trans('symfony.great', ['%fo<caret>obar%', null], 'symfony') |
| 63 | + registrar.register( |
| 64 | + PlatformPatterns.psiElement().withParent(StringLiteralExpression.class), |
| 65 | + new MyPhpTranslateableMessageCompletionContributor() |
| 66 | + ); |
57 | 67 | }
|
58 | 68 |
|
59 | 69 | private static class MyTranslationPlaceholderGotoCompletionProvider extends GotoCompletionProvider {
|
@@ -131,6 +141,67 @@ public GotoCompletionProvider getProvider(@NotNull PsiElement psiElement) {
|
131 | 141 | }
|
132 | 142 | }
|
133 | 143 |
|
| 144 | + /** |
| 145 | + * new \Symfony\Component\Translation\TranslatableMessage('symfony.great', ['test' => '%fo<caret>obar%'], 'symfony'); |
| 146 | + * new \Symfony\Component\Translation\TranslatableMessage('symfony.great', ['%fo<caret>obar%', null], 'symfony'); |
| 147 | + */ |
| 148 | + private static class MyPhpTranslateableMessageCompletionContributor implements GotoCompletionContributor { |
| 149 | + @Nullable |
| 150 | + @Override |
| 151 | + public GotoCompletionProvider getProvider(@NotNull PsiElement psiElement) { |
| 152 | + PsiElement context = psiElement.getContext(); |
| 153 | + if (!(context instanceof StringLiteralExpression)) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(context); |
| 158 | + if (arrayCreationExpression == null) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + PsiElement parameterList = arrayCreationExpression.getContext(); |
| 163 | + if (!(parameterList instanceof ParameterList)) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + PsiElement[] parameters = ((ParameterList) parameterList).getParameters(); |
| 168 | + int placeHolderParameter = 1; |
| 169 | + if (parameters.length < placeHolderParameter) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + PsiElement newEx = parameterList.getContext(); |
| 174 | + if (!(newEx instanceof NewExpression)) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(arrayCreationExpression); |
| 179 | + if (currentIndex == null || currentIndex.getIndex() != placeHolderParameter) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + if (!PhpElementsUtil.isNewExpressionPhpClassWithInstance((NewExpression) newEx, TranslationUtil.PHP_TRANSLATION_TRANSLATABLE_MESSAGE)) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + String key = PhpElementsUtil.getStringValue(parameters[0]); |
| 188 | + if (key == null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + String domain = "messages"; |
| 193 | + int domainParameter = 2; |
| 194 | + if (parameters.length > domainParameter) { |
| 195 | + domain = PhpElementsUtil.getStringValue(parameters[domainParameter]); |
| 196 | + if(domain == null) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return new MyTranslationPlaceholderGotoCompletionProvider(psiElement, key, domain); |
| 202 | + } |
| 203 | + } |
| 204 | + |
134 | 205 | /**
|
135 | 206 | * {{ 'symfony.great'|trans({'fo<caret>f'}, 'symfony')) }}
|
136 | 207 | */
|
|
0 commit comments