[java-injection] Allow reassigning STR to language-specific constant (IDEA-333006)

GitOrigin-RevId: 29656ea1e3c85f350fe969c1b7025c1d18f0a2b9
This commit is contained in:
Tagir Valeev
2024-01-26 14:58:00 +00:00
committed by intellij-monorepo-bot
parent 89181f4bcc
commit b145cd2784
2 changed files with 28 additions and 5 deletions
@@ -16,7 +16,9 @@
package org.intellij.plugins.intelliLang.inject.java.validation;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
@@ -30,6 +32,11 @@ public class LanguageMismatchTest extends LightJavaCodeInsightFixtureTestCase {
myFixture.enableInspections(inspection);
}
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_21_ANNOTATED;
}
public void testParenthesesHighlighting() {
highlightTest("""
import org.intellij.lang.annotations.Language;
@@ -66,6 +73,16 @@ public class LanguageMismatchTest extends LightJavaCodeInsightFixtureTestCase {
}
""", "Annotate field 'JS_CODE' as '@Language'");
}
public void testProcessorReassigned() {
highlightTest("""
import org.intellij.lang.annotations.Language;
class Hello {
@Language("JAVA")
public static final StringTemplate.Processor<String, RuntimeException> JAVA = STR;
}""");
}
public void testEmptyArrayConstant() {
highlightTest("""
@@ -24,7 +24,9 @@ import com.intellij.codeInspection.options.OptPane;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.HtmlChunk;
import com.intellij.psi.*;
import com.intellij.psi.util.JavaPsiStringTemplateUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.SmartList;
import com.siyeh.ig.psiutils.CollectionUtils;
@@ -99,20 +101,24 @@ public class LanguageMismatch extends LocalInspectionTool {
}
}
else if (CHECK_NON_ANNOTATED_REFERENCES) {
final PsiElement var =
final PsiElement decl =
PsiTreeUtil.getParentOfType(expression, PsiVariable.class, PsiExpressionList.class, PsiAssignmentExpression.class);
// only nag about direct assignment or passing the reference as parameter
if (var instanceof PsiVariable) {
if (((PsiVariable)var).getInitializer() != expression) {
if (decl instanceof PsiVariable variable) {
if (variable.getInitializer() != expression) {
return;
}
if (JavaPsiStringTemplateUtil.isStrTemplate(expression)) {
// Allow reassigning STR processor to apply language for standard concatenation
return;
}
}
else if (var instanceof PsiExpressionList list) {
else if (decl instanceof PsiExpressionList list) {
if (!ArrayUtil.contains(expression, list.getExpressions())) {
return;
}
}
else if (var instanceof PsiAssignmentExpression a) {
else if (decl instanceof PsiAssignmentExpression a) {
if (a.getRExpression() != expression) {
return;
}