[java-inspections] InlineVariableFix: avoid PSI writes after highlighting

Highlighting causes PSI commit and reformatting. It looks like modifying PSI after that may cause problems. Probably we can fix it later in ModCommand API, but for now we can just reorder things.
Fixes IDEA-355992 "Inline variable" quick-fix doesn't work if the variable initializer is on the next line

GitOrigin-RevId: f0a2418ac5618f91291b1f74ad2168d26c612447
This commit is contained in:
Tagir Valeev
2024-07-31 11:41:55 +00:00
committed by intellij-monorepo-bot
parent c8a7a4ed29
commit 90543d0e24
4 changed files with 33 additions and 3 deletions
@@ -54,9 +54,16 @@ public class InlineVariableFix extends PsiUpdateModCommandQuickFix {
replacedElements.add(expression);
}
for (PsiElement element : replacedElements) {
updater.highlight(element);
}
new CommentTracker().deleteAndRestoreComments(variable);
boolean positioned = false;
for (PsiElement element : replacedElements) {
if (element.isValid()) {
updater.highlight(element);
if (!positioned) {
positioned = true;
updater.moveCaretTo(element);
}
}
}
}
}
@@ -0,0 +1,10 @@
import java.util.List;
public class Demo {
List<String> test(List<String> list) {
return <caret>list.stream()
.map(String::toUpperCase)
.map(String::trim)
.toList();
}
}
@@ -0,0 +1,12 @@
import java.util.List;
public class Demo {
List<String> test(List<String> list) {
List<String> re<caret>sult =
list.stream()
.map(String::toUpperCase)
.map(String::trim)
.toList();
return result;
}
}
@@ -21,4 +21,5 @@ public class InlineVariableFixTest extends IGQuickFixesTestCase {
public void testArrayInitializer() { doTest(); }
public void testCastForOverloads() { assertQuickfixNotAvailable(); }
public void testComment() { doTest(); }
public void testNewLineInInitializer() { doTest(); }
}