diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java index 564f6ababe49..0bc1ea8b3150 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CompletionProgressIndicator.java @@ -76,6 +76,8 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import static com.intellij.codeInsight.completion.CompletionPhase.CUSTOM_CODE_COMPLETION_ACTION_ID; + @ApiStatus.Internal public final class CompletionProgressIndicator extends ProgressIndicatorBase implements CompletionProcessEx, Disposable { private static final int TEST_COMPLETION_TIMEOUT = 100 * 1000; @@ -843,7 +845,12 @@ public final class CompletionProgressIndicator extends ProgressIndicatorBase imp if (handler.isTestingMode() && !TestModeFlags.is(CompletionAutoPopupHandler.ourTestingAutopopup)) { closeAndFinish(false); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); - new CodeCompletionHandlerBase(myCompletionType, false, false, true).invokeCompletion(getProject(), myEditor, myInvocationCount); + String customId = myEditor.getUserData(CUSTOM_CODE_COMPLETION_ACTION_ID); + if (customId == null) { + customId = "CodeCompletion"; + } + CodeCompletionHandlerBase handler = CodeCompletionHandlerBase.createHandler(myCompletionType, false, false, true, customId); + handler.invokeCompletion(getProject(), myEditor, myInvocationCount); return; } diff --git a/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionFixture.kt b/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionFixture.kt index 93c7dd822a9f..ef56dd768670 100644 --- a/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionFixture.kt +++ b/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionFixture.kt @@ -82,6 +82,28 @@ class TerminalCompletionFixture(val project: Project, testRootDisposable: Dispos runActionById("Terminal.UpCommandCompletion") } + /** + * Simulates a key press in the active completion popup. + * @param keyCode (VK_LEFT or VK_ENTER) + */ + fun pressKey(keycode: Int) { + if (keycode != KeyEvent.VK_LEFT && keycode != KeyEvent.VK_RIGHT) { + throw IllegalArgumentException("keycode must be VK_LEFT or VK_RIGHT") + } + val keyPressEvent = KeyEvent( + view.outputEditor.component, + KeyEvent.KEY_PRESSED, + System.currentTimeMillis(), + 0, + keycode, + KeyEvent.CHAR_UNDEFINED, + KeyEvent.KEY_LOCATION_STANDARD + ) + view.outputEditorEventsHandler.keyPressed(TimedKeyEvent(keyPressEvent, TimeSource.Monotonic.markNow())) + val commonOffset = view.outputEditor.getCaretModel().offset + view.outputModel.updateCursorPosition(view.outputModel.relativeOffset(commonOffset)) + } + private fun runActionById(actionId: String) { val action = ActionManager.getInstance().getAction(actionId) val context = SimpleDataContext.builder() diff --git a/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionTest.kt b/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionTest.kt index 1bb60fe3095f..34105f47764b 100644 --- a/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionTest.kt +++ b/plugins/terminal/tests/src/com/intellij/terminal/tests/reworked/frontend/TerminalCompletionTest.kt @@ -5,6 +5,8 @@ import org.junit.Assert.assertNotEquals import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import java.awt.event.KeyEvent.VK_LEFT +import java.awt.event.KeyEvent.VK_RIGHT @RunWith(JUnit4::class) class TerminalCompletionTest : BasePlatformTestCase() { @@ -42,4 +44,43 @@ class TerminalCompletionTest : BasePlatformTestCase() { assertEquals(firstElement, fistAfterElement) assertNotEquals(secondElement, fistAfterElement) } + + @Test + fun `test terminal completion with left right`() { + val terminalCompletionFixture = TerminalCompletionFixture(project, testRootDisposable) + terminalCompletionFixture.type("git ") + terminalCompletionFixture.callCompletionPopup() + + terminalCompletionFixture.type("st") + val startResult = terminalCompletionFixture.getLookupElements() + + terminalCompletionFixture.pressKey(VK_LEFT) + val afterFirstLeftResult = terminalCompletionFixture.getLookupElements() + terminalCompletionFixture.pressKey(VK_LEFT) + + val afterSecondLeftResult = terminalCompletionFixture.getLookupElements() + assertNotEquals(afterFirstLeftResult.size, afterSecondLeftResult.size) + + terminalCompletionFixture.pressKey(VK_RIGHT) + val afterRightResult = terminalCompletionFixture.getLookupElements() + assertEquals(afterFirstLeftResult.size, afterRightResult.size) + + terminalCompletionFixture.pressKey(VK_RIGHT) + val afterSecondRightResult = terminalCompletionFixture.getLookupElements() + assertEquals(startResult.size, afterSecondRightResult.size) + } + + @Test + fun `test terminal completion with left right reopening`() { + val terminalCompletionFixture = TerminalCompletionFixture(project, testRootDisposable) + terminalCompletionFixture.type("git st") + terminalCompletionFixture.callCompletionPopup() + + val startResult = terminalCompletionFixture.getLookupElements() + assertEquals(3, startResult.size) + terminalCompletionFixture.pressKey(VK_LEFT) + val afterLeftResult = terminalCompletionFixture.getLookupElements() + assertEquals(6, afterLeftResult.size) + } + } \ No newline at end of file