[terminal] IDEA-334674 Add custom show documentation action for the case when auto showing is disabled

Also, cancel the popup scope when documentation popup is closed for any reason (not only when it is closed manually by our code).

GitOrigin-RevId: 623cc68d101c3a7d12f88a3c785a41ca7d296886
This commit is contained in:
Konstantin Hudyakov
2023-10-12 11:32:18 +03:00
committed by intellij-monorepo-bot
parent 5b03b2191e
commit 0e191bdfad
4 changed files with 42 additions and 3 deletions

View File

@@ -121,6 +121,8 @@
<action id="Terminal.InsertInlineCompletionItem" class="org.jetbrains.plugins.terminal.exp.TerminalInsertInlineCompletionAction">
<keyboard-shortcut first-keystroke="RIGHT" keymap="$default"/>
</action>
<action id="Terminal.ShowDocumentation" class="org.jetbrains.plugins.terminal.action.TerminalShowDocAction"
use-shortcut-of="QuickJavaDoc"/>
<action id="Terminal.Copy" class="org.jetbrains.plugins.terminal.action.TerminalCopyAction"
use-shortcut-of="$Copy" icon="AllIcons.Actions.Copy"/>

View File

@@ -25,6 +25,7 @@ action.Terminal.Find.text=Find
action.Terminal.ClearBuffer.text=Clear Terminal
action.Terminal.CommandCompletion.text=Command Completion
action.Terminal.InsertInlineCompletionItem.text=Insert Inline Completion Item
action.Terminal.ShowDocumentation.text=Show Documentation
action.Terminal.Copy.text=Copy
action.Terminal.SelectLastBlock.text=Select Last Block
action.Terminal.SelectPrompt.text=Select Prompt

View File

@@ -0,0 +1,35 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.terminal.action
import com.intellij.codeInsight.hint.HintManagerImpl
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.DumbAwareAction
import org.jetbrains.plugins.terminal.exp.CommandHistoryPresenter.Companion.IS_COMMAND_HISTORY_LOOKUP_KEY
import org.jetbrains.plugins.terminal.exp.TerminalDataContextUtils.editor
import org.jetbrains.plugins.terminal.exp.TerminalDataContextUtils.isPromptEditor
import org.jetbrains.plugins.terminal.exp.documentation.TerminalDocumentationManager
class TerminalShowDocAction : DumbAwareAction(), HintManagerImpl.ActionToIgnore {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val lookup = LookupManager.getActiveLookup(e.editor) as? LookupImpl ?: return
val currentItem = lookup.currentItem ?: return
TerminalDocumentationManager.getInstance(project).showDocumentationForItem(lookup, currentItem, parentDisposable = lookup)
}
override fun update(e: AnActionEvent) {
val editor = e.editor
val lookup = LookupManager.getActiveLookup(editor) as? LookupImpl
// enable this action only in the terminal command completion popup
e.presentation.isEnabledAndVisible = e.project != null
&& editor?.isPromptEditor == true
&& lookup != null
&& lookup.getUserData(IS_COMMAND_HISTORY_LOOKUP_KEY) != true
&& lookup.currentItem != null
}
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT
}

View File

@@ -75,6 +75,7 @@ internal class TerminalDocumentationManager(private val project: Project, privat
Disposer.register(popup) {
EDT.assertIsEdt()
currentPopup = null
popupScope.coroutineContext.job.cancelChildren()
}
}
@@ -86,7 +87,7 @@ internal class TerminalDocumentationManager(private val project: Project, privat
val lookupElementFlow = lookup.elementFlow()
val showDocJob = scope.launch(Dispatchers.EDT + ModalityState.current().asContextElement()) {
lookupElementFlow.collectLatest {
handleLookupElementChange(lookup, it, parentDisposable)
showDocumentationForItem(lookup, it, parentDisposable)
}
}
Disposer.register(parentDisposable) {
@@ -94,7 +95,8 @@ internal class TerminalDocumentationManager(private val project: Project, privat
}
}
private fun handleLookupElementChange(lookup: LookupEx, element: LookupElement, parentDisposable: Disposable) {
@RequiresEdt
fun showDocumentationForItem(lookup: LookupEx, element: LookupElement, parentDisposable: Disposable) {
if (getCurrentPopup() != null) {
return
}
@@ -141,7 +143,6 @@ internal class TerminalDocumentationManager(private val project: Project, privat
private fun cancelPopup() {
EDT.assertIsEdt()
getCurrentPopup()?.cancel()
popupScope.coroutineContext.job.cancelChildren()
}
private fun createDocPopupComponent(docComponent: DocumentationComponent, parentDisposable: Disposable): JComponent {