From 815ab749c554baed4b32da7c809552befccb95f2 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Fri, 31 Oct 2025 11:48:08 +0100 Subject: [PATCH] [command-completion] IDEA-381534 Some fixes are unavailable after closed brackets (cherry picked from commit 36e9573c3722ae6cfb0119059b579a4e523e109c) IJ-CR-180941 GitOrigin-RevId: aea8e6d4111a024157051510d58f038bafe4b5f2 --- .../commands/JavaCommandsCompletionTest.kt | 48 +++++++++++++++++-- .../DirectIntentionCommandProvider.kt | 2 +- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt index 7106c2998190..5b205e67ef9f 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/commands/JavaCommandsCompletionTest.kt @@ -11,6 +11,7 @@ import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo import com.intellij.codeInsight.lookup.LookupElementPresentation import com.intellij.codeInsight.template.impl.TemplateManagerImpl import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection +import com.intellij.codeInspection.streamMigration.StreamApiMigrationInspection import com.intellij.ide.highlighter.JavaFileType import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.application.ApplicationManager @@ -26,6 +27,7 @@ import com.intellij.platform.backend.documentation.DocumentationData import com.intellij.psi.CommonClassNames.JAVA_LANG_CLASS import com.intellij.psi.JavaPsiFacade import com.intellij.psi.search.GlobalSearchScope +import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.NeedsIndex import com.intellij.testFramework.replaceService import com.siyeh.ig.style.SizeReplaceableByIsEmptyInspection @@ -35,6 +37,10 @@ import javax.swing.JComponent @NeedsIndex.SmartMode(reason = "it requires highlighting") class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { + override fun getProjectDescriptor(): LightProjectDescriptor { + return JAVA_21 + } + override fun setUp() { super.setUp() Registry.get("ide.completion.command.enabled").setValue(false, getTestRootDisposable()) @@ -762,6 +768,40 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { } } + fun testInspectionFromAnotherLine() { + Registry.get("ide.completion.command.force.enabled").setValue(true, getTestRootDisposable()) + myFixture.enableInspections(StreamApiMigrationInspection()) + myFixture.configureByText(JavaFileType.INSTANCE, """ + import java.io.BufferedReader; + import java.io.FileReader; + import java.io.IOException; + + public class Test { + void main() throws IOException { + var reader = new BufferedReader(new FileReader("input.txt")); + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + } + }""".trimIndent()) + myFixture.doHighlighting() + myFixture.type(".") + val elements = myFixture.completeBasic() + selectItem(elements.first { element -> element.lookupString.contains("Collapse loop with stream 'forEach()'", ignoreCase = true) }) + myFixture.checkResult(""" + import java.io.BufferedReader; + import java.io.FileReader; + import java.io.IOException; + + public class Test { + void main() throws IOException { + var reader = new BufferedReader(new FileReader("input.txt")); + reader.lines().forEach(System.out::println); + } + }""".trimIndent()) + } + fun testChangeSignature() { Registry.get("ide.completion.command.force.enabled").setValue(true, getTestRootDisposable()) var text = """ @@ -1079,7 +1119,7 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { public class A { void foo() { - ArrayList strings = new ArrayList(); + ArrayList strings = new ArrayList<>(); } } """.trimIndent()) @@ -1109,7 +1149,7 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { public class A { void foo() { - ArrayList strings = new ArrayList(); + ArrayList strings = new ArrayList<>(); new ArrayList(strings); } } @@ -1142,7 +1182,7 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { public class A { void foo() { - ArrayList strings = new ArrayList(); + ArrayList strings = new ArrayList<>(); } } """.trimIndent()) @@ -1173,7 +1213,7 @@ class JavaCommandsCompletionTest : LightFixtureCompletionTestCase() { public class A { void foo() { - ArrayList strings = new ArrayList(); + ArrayList strings = new ArrayList<>(); } } """.trimIndent()) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/command/commands/DirectIntentionCommandProvider.kt b/platform/lang-impl/src/com/intellij/codeInsight/completion/command/commands/DirectIntentionCommandProvider.kt index 350316c93ca9..512f6c02737b 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/command/commands/DirectIntentionCommandProvider.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/command/commands/DirectIntentionCommandProvider.kt @@ -241,7 +241,7 @@ internal class DirectIntentionCommandProvider : CommandProvider { val profileToUse = getInstance(psiFile.project).getCurrentProfile() val inspectionWrapper = InspectionProfileWrapper(profileToUse) val inspectionTools = getInspectionTools(inspectionWrapper, originalFile) - val lineRange = getLineRange(topLevelFile, topLevelOffset) + val lineRange = getLineRange(topLevelFile, currentOffset) val indicator = EmptyProgressIndicator() val inspectionResult = jobToIndicator(coroutineContext.job, indicator) { if (!isInjected) {