-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathPhpTranslationDomainInspection.java
More file actions
106 lines (88 loc) · 4.91 KB
/
PhpTranslationDomainInspection.java
File metadata and controls
106 lines (88 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fr.adrienbrault.idea.symfony2plugin.translation;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.formatter.FormatterUtil;
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
import com.jetbrains.php.lang.psi.elements.*;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil;
import fr.adrienbrault.idea.symfony2plugin.translation.inspection.TranslationDomainGuessTypoQuickFix;
import fr.adrienbrault.idea.symfony2plugin.translation.inspection.TwigTranslationDomainInspection;
import fr.adrienbrault.idea.symfony2plugin.util.ParameterBag;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class PhpTranslationDomainInspection extends LocalInspectionTool {
public static final String MESSAGE = TwigTranslationDomainInspection.MESSAGE;
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof StringLiteralExpression stringLiteralExpression) {
invoke(holder, stringLiteralExpression);
}
super.visitElement(element);
}
};
}
private void invoke(@NotNull ProblemsHolder holder, @NotNull StringLiteralExpression psiElement) {
ParameterListOwner methodReferenceOrNewExpression = TranslationUtil.getTranslationFunctionContext(psiElement);
if (methodReferenceOrNewExpression == null) {
return;
}
ASTNode previousNonWhitespaceSibling = FormatterUtil.getPreviousNonWhitespaceSibling(psiElement.getNode());
if (previousNonWhitespaceSibling != null && previousNonWhitespaceSibling.getElementType() == PhpTokenTypes.opCOLON) {
ASTNode previousNonWhitespaceSibling1 = FormatterUtil.getPreviousNonWhitespaceSibling(previousNonWhitespaceSibling);
if (previousNonWhitespaceSibling1 != null && previousNonWhitespaceSibling1.getElementType() == PhpTokenTypes.IDENTIFIER) {
String text = previousNonWhitespaceSibling1.getText();
boolean isSupportedAttributeInsideContext = "domain".equals(text)
&& TranslationUtil.isTranslationReference(methodReferenceOrNewExpression);
if (isSupportedAttributeInsideContext) {
annotateTranslationDomain(psiElement, holder);
}
}
return;
}
int domainParameter = getDomainParameter(methodReferenceOrNewExpression);
if (domainParameter >= 0) {
ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(psiElement);
if(currentIndex != null && currentIndex.getIndex() == domainParameter) {
annotateTranslationDomain(psiElement, holder);
}
}
}
public static int getDomainParameter(@NotNull PsiElement methodReferenceOrNewExpression) {
if (methodReferenceOrNewExpression instanceof MethodReference methodReference && PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, TranslationUtil.PHP_TRANSLATION_SIGNATURES)) {
int domainParameter = 2;
if("transChoice".equals(methodReference.getName())) {
domainParameter = 3;
}
return domainParameter;
} else if(methodReferenceOrNewExpression instanceof NewExpression newExpression && PhpElementsUtil.isNewExpressionPhpClassWithInstance(newExpression, TranslationUtil.PHP_TRANSLATION_TRANSLATABLE_MESSAGE)) {
return 2;
} else if(methodReferenceOrNewExpression instanceof FunctionReference functionReference && TranslationUtil.isFunctionReferenceTranslationTFunction(functionReference)) {
return 2;
}
return -1;
}
private void annotateTranslationDomain(StringLiteralExpression psiElement, @NotNull ProblemsHolder holder) {
String contents = psiElement.getContents();
if(StringUtils.isBlank(contents) || TranslationUtil.hasDomain(psiElement.getProject(), contents)) {
return;
}
holder.registerProblem(psiElement, MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new TranslationDomainGuessTypoQuickFix(contents));
}
}