From f63b9b34e44afcbb71585ddfc578f87a9e8472a3 Mon Sep 17 00:00:00 2001 From: Konstantin Hudyakov Date: Fri, 22 Nov 2024 12:19:56 +0200 Subject: [PATCH] IJPL-172124 Allow to listen for command search popup events IJ-CR-148020 (cherry picked from commit d1a449823a5c68a5ca82f16c12ccb5d938e70e79) GitOrigin-RevId: 00b2cb07cb5e385f841ca6410a7893c911a03a4d --- .../block/history/CommandSearchListener.kt | 18 ++++++++++++++++++ .../block/history/CommandSearchPresenter.kt | 10 ++++++++++ 2 files changed, 28 insertions(+) create mode 100644 plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchListener.kt diff --git a/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchListener.kt b/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchListener.kt new file mode 100644 index 0000000000000..d30f1f13524e0 --- /dev/null +++ b/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchListener.kt @@ -0,0 +1,18 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.terminal.block.history + +import com.intellij.util.messages.Topic +import org.jetbrains.annotations.ApiStatus +import org.jetbrains.plugins.terminal.block.prompt.TerminalPromptModel + +@ApiStatus.Internal +interface CommandSearchListener { + fun commandSearchShown(promptModel: TerminalPromptModel) {} + + fun commandSearchAborted(promptModel: TerminalPromptModel) {} + + companion object { + @Topic.ProjectLevel + val TOPIC: Topic = Topic(CommandSearchListener::class.java, Topic.BroadcastDirection.NONE) + } +} diff --git a/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchPresenter.kt b/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchPresenter.kt index 8cc7c91d22836..008f081094e31 100644 --- a/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchPresenter.kt +++ b/plugins/terminal/src/org/jetbrains/plugins/terminal/block/history/CommandSearchPresenter.kt @@ -2,6 +2,8 @@ package org.jetbrains.plugins.terminal.block.history import com.intellij.codeInsight.lookup.Lookup +import com.intellij.codeInsight.lookup.LookupEvent +import com.intellij.codeInsight.lookup.LookupListener import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key @@ -20,8 +22,16 @@ internal class CommandSearchPresenter( val lookup = CommandHistoryUtil.createLookup(project, editor, command, history.asReversed()) lookup.putUserData(IS_COMMAND_SEARCH_LOOKUP_KEY, true) + lookup.addLookupListener(object : LookupListener { + override fun lookupCanceled(event: LookupEvent) { + project.messageBus.syncPublisher(CommandSearchListener.TOPIC).commandSearchAborted(promptModel) + } + }) + if (lookup.showLookup()) { lookup.ensureSelectionVisible(false) + + project.messageBus.syncPublisher(CommandSearchListener.TOPIC).commandSearchShown(promptModel) } }