Java: avoid IAE on Introduce Variable (IDEA-369326)

GitOrigin-RevId: 9fa79fc70578d8ff14bda2051341b6d22ece6677
This commit is contained in:
Bas Leijdekkers
2025-03-20 18:24:49 +00:00
committed by intellij-monorepo-bot
parent 33d037c1fd
commit 35aab21776
4 changed files with 94 additions and 1 deletions
@@ -35,6 +35,7 @@ import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.AbstractJavaInplaceIntroducer;
import com.intellij.refactoring.IntroduceVariableUtil;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.refactoring.introduceField.ElementToWorkOn;
import com.intellij.refactoring.rename.ResolveSnapshotProvider;
import com.intellij.refactoring.rename.inplace.SelectableInlayPresentation;
import com.intellij.refactoring.rename.inplace.TemplateInlayUtil;
@@ -138,6 +139,18 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
super.restoreState(psiField);
}
@Override
protected RangeMarker createMarker(PsiElement element) {
int length = myEditor.getDocument().getTextLength();
if (element.getTextRange().getEndOffset() > length) {
RangeMarker marker = ElementToWorkOn.TEXT_RANGE.get(element);
if (marker != null) {
return marker;
}
}
return super.createMarker(element);
}
@Override
protected boolean ensureValid() {
final PsiVariable variable = getVariable();
@@ -0,0 +1,35 @@
public class GraphQLIntrospectionQuery {
String INTROSPECTION_QUERY = <selection>"fragment TypeRef on __Type {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"\n"</selection>;}
@@ -0,0 +1,41 @@
public class GraphQLIntrospectionQuery {
String INTROSPECTION_QUERY;
{
String s = "fragment TypeRef on __Type {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"\n";
INTROSPECTION_QUERY = s;
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
@@ -348,6 +348,10 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
public void testNoExternalTypeAnnotations() {
doTest(null);
}
public void testNoIllegalArgumentException() {
doTest(null);
}
public void testSplitMutuallyExclusiveIf() { doTestReplaceChoice("Replace all 0 occurrences"); }