[java-highlighting] Avoid highlighting outside of PsiField psi element

Fixes IDEA-368916 IAE in error highlighting when several fields are declared with shared modifier list

GitOrigin-RevId: 012b6564f9ca1a775894a3fc2b003c4feb2477e6
This commit is contained in:
Tagir Valeev
2025-03-10 21:59:42 +00:00
committed by intellij-monorepo-bot
parent f3ac3c8925
commit 830860950b
3 changed files with 7 additions and 1 deletions
@@ -158,7 +158,8 @@ final class JavaErrorFormatUtil {
static @NotNull TextRange getFieldDeclarationTextRange(@NotNull PsiField field) {
PsiModifierList modifierList = field.getModifierList();
TextRange range = field.getTextRange();
int start = modifierList == null ? range.getStartOffset() : stripAnnotationsFromModifierList(modifierList);
int start = modifierList == null || modifierList.getParent() != field ?
range.getStartOffset() : stripAnnotationsFromModifierList(modifierList);
int end = field.getNameIdentifier().getTextRange().getEndOffset();
return new TextRange(start, end).shiftLeft(range.getStartOffset());
}
@@ -0,0 +1,3 @@
public final class A {
<error descr="Field 'x' might not have been initialized">private final int x</error>, <error descr="Field 'y' might not have been initialized">y</error>;
}
@@ -469,4 +469,6 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testReferenceToImplicitClass() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_22_PREVIEW, () -> doTest(false));
}
public void testUninitializedFields() { doTest(false); }
}