3
3
import com .intellij .codeInspection .LocalInspectionTool ;
4
4
import com .intellij .codeInspection .ProblemHighlightType ;
5
5
import com .intellij .codeInspection .ProblemsHolder ;
6
+ import com .intellij .lang .ASTNode ;
6
7
import com .intellij .psi .PsiElement ;
7
8
import com .intellij .psi .PsiElementVisitor ;
9
+ import com .intellij .psi .formatter .FormatterUtil ;
10
+ import com .jetbrains .php .lang .lexer .PhpTokenTypes ;
8
11
import com .jetbrains .php .lang .psi .elements .*;
9
12
import fr .adrienbrault .idea .symfony2plugin .Symfony2ProjectComponent ;
10
13
import fr .adrienbrault .idea .symfony2plugin .translation .dict .TranslationUtil ;
@@ -40,23 +43,41 @@ public void visitElement(PsiElement element) {
40
43
}
41
44
42
45
private void invoke (@ NotNull ProblemsHolder holder , @ NotNull PsiElement psiElement ) {
43
- if (!(psiElement instanceof StringLiteralExpression ) || !( psiElement . getContext () instanceof ParameterList ) ) {
46
+ if (!(psiElement instanceof StringLiteralExpression )) {
44
47
return ;
45
48
}
46
49
47
- ParameterList parameterList = (ParameterList ) psiElement .getContext ();
50
+ PsiElement parameterList = psiElement .getContext ();
51
+ if (!(parameterList instanceof ParameterList )) {
52
+ return ;
53
+ }
48
54
49
- int domainParameter = -1 ;
50
- PsiElement methodReference = parameterList .getContext ();
51
- if (methodReference instanceof MethodReference && PhpElementsUtil .isMethodReferenceInstanceOf ((MethodReference ) methodReference , TranslationUtil .PHP_TRANSLATION_SIGNATURES )) {
52
- domainParameter = 2 ;
53
- if ("transChoice" .equals (((MethodReference ) methodReference ).getName ())) {
54
- domainParameter = 3 ;
55
+ PsiElement methodReferenceOrNewExpression = parameterList .getContext ();
56
+ if (!(methodReferenceOrNewExpression instanceof MethodReference ) && !(methodReferenceOrNewExpression instanceof NewExpression )) {
57
+ return ;
58
+ }
59
+
60
+ ASTNode previousNonWhitespaceSibling = FormatterUtil .getPreviousNonWhitespaceSibling (psiElement .getNode ());
61
+
62
+ if (previousNonWhitespaceSibling != null && previousNonWhitespaceSibling .getElementType () == PhpTokenTypes .opCOLON ) {
63
+ ASTNode previousNonWhitespaceSibling1 = FormatterUtil .getPreviousNonWhitespaceSibling (previousNonWhitespaceSibling );
64
+ if (previousNonWhitespaceSibling1 != null && previousNonWhitespaceSibling1 .getElementType () == PhpTokenTypes .IDENTIFIER ) {
65
+ String text = previousNonWhitespaceSibling1 .getText ();
66
+ boolean isSupportedAttributeInsideContext = "domain" .equals (text ) && (
67
+ (methodReferenceOrNewExpression instanceof MethodReference && PhpElementsUtil .isMethodReferenceInstanceOf ((MethodReference ) methodReferenceOrNewExpression , TranslationUtil .PHP_TRANSLATION_SIGNATURES ))
68
+ || (methodReferenceOrNewExpression instanceof NewExpression && PhpElementsUtil .isNewExpressionPhpClassWithInstance ((NewExpression ) methodReferenceOrNewExpression , TranslationUtil .PHP_TRANSLATION_TRANSLATABLE_MESSAGE ))
69
+ );
70
+
71
+ if (isSupportedAttributeInsideContext ) {
72
+ annotateTranslationDomain ((StringLiteralExpression ) psiElement , holder );
73
+ }
55
74
}
56
- } else if ( methodReference instanceof NewExpression && PhpElementsUtil . isNewExpressionPhpClassWithInstance (( NewExpression ) methodReference , TranslationUtil . PHP_TRANSLATION_TRANSLATABLE_MESSAGE )) {
57
- domainParameter = 2 ;
75
+
76
+ return ;
58
77
}
59
78
79
+ int domainParameter = getDomainParameter (methodReferenceOrNewExpression );
80
+
60
81
if (domainParameter >= 0 ) {
61
82
ParameterBag currentIndex = PsiElementUtils .getCurrentParameterIndex (psiElement );
62
83
if (currentIndex != null && currentIndex .getIndex () == domainParameter ) {
@@ -65,6 +86,22 @@ private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiEleme
65
86
}
66
87
}
67
88
89
+ public static int getDomainParameter (@ NotNull PsiElement methodReferenceOrNewExpression ) {
90
+ if (methodReferenceOrNewExpression instanceof MethodReference && PhpElementsUtil .isMethodReferenceInstanceOf ((MethodReference ) methodReferenceOrNewExpression , TranslationUtil .PHP_TRANSLATION_SIGNATURES )) {
91
+ int domainParameter = 2 ;
92
+
93
+ if ("transChoice" .equals (((MethodReference ) methodReferenceOrNewExpression ).getName ())) {
94
+ domainParameter = 3 ;
95
+ }
96
+
97
+ return domainParameter ;
98
+ } else if (methodReferenceOrNewExpression instanceof NewExpression && PhpElementsUtil .isNewExpressionPhpClassWithInstance ((NewExpression ) methodReferenceOrNewExpression , TranslationUtil .PHP_TRANSLATION_TRANSLATABLE_MESSAGE )) {
99
+ return 2 ;
100
+ }
101
+
102
+ return -1 ;
103
+ }
104
+
68
105
private void annotateTranslationDomain (StringLiteralExpression psiElement , @ NotNull ProblemsHolder holder ) {
69
106
String contents = psiElement .getContents ();
70
107
if (StringUtils .isBlank (contents ) || TranslationUtil .hasDomain (psiElement .getProject (), contents )) {
0 commit comments