From bd34e1c80fd97da8d0eb29c67a7de64dda69d83b Mon Sep 17 00:00:00 2001 From: Yuriy Artamonov Date: Tue, 19 Mar 2024 15:07:57 +0100 Subject: [PATCH] [completion] DocumentationAutoPopupListener can be registered as declarative project listener We are trying to avoid unnecessary code in startup activities and one of the ways to eliminate those are declarative message bus listeners GitOrigin-RevId: 1bfa56ac820ff506271ede9fe8933a7835184cf2 --- .../InlineCompletionLookupManagerListener.kt | 2 +- .../inline/completion/TabEnterUsageDetector.kt | 18 ++++++++---------- .../ide/impl/DocumentationAutoPopup.kt | 8 -------- .../ide/impl/DocumentationManager.kt | 14 ++++++++------ .../ide/impl/DocumentationTargetHoverInfo.kt | 2 +- .../ide/impl/DocumentationToolWindowManager.kt | 5 +++-- .../ide/ui/DocumentationPopupUI.kt | 2 +- .../src/META-INF/LangExtensions.xml | 3 ++- 8 files changed, 24 insertions(+), 30 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/InlineCompletionLookupManagerListener.kt b/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/InlineCompletionLookupManagerListener.kt index 7e6fe3b6f64f..6427f3b5a72e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/InlineCompletionLookupManagerListener.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/InlineCompletionLookupManagerListener.kt @@ -6,7 +6,7 @@ import com.intellij.codeInsight.lookup.LookupEvent import com.intellij.codeInsight.lookup.LookupListener import com.intellij.codeInsight.lookup.LookupManagerListener -class InlineCompletionLookupManagerListener : LookupManagerListener { +private class InlineCompletionLookupManagerListener : LookupManagerListener { override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) { newLookup?.addLookupListener(object : LookupListener { override fun currentItemChanged(event: LookupEvent) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/TabEnterUsageDetector.kt b/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/TabEnterUsageDetector.kt index 2e9ebb9f0fda..dcfce191b52c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/TabEnterUsageDetector.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/inline/completion/TabEnterUsageDetector.kt @@ -7,7 +7,13 @@ import com.intellij.codeInsight.lookup.LookupListener import com.intellij.codeInsight.lookup.LookupManagerListener import com.intellij.ide.util.PropertiesComponent -class TabEnterUsageDetector : LookupManagerListener { +private const val TAB_SELECTION_COUNT_KEY = "tab_selection_count" +private const val ENTER_SELECTION_COUNT_KEY = "enter_selection_count" + +private const val SELECTION_COUNT_THRESHOLD = 5 +private const val TAB_RATIO_THRESHOLD = 0.1 + +private class TabEnterUsageDetector : LookupManagerListener { private val properties = PropertiesComponent.getInstance() private var tabCount @@ -39,13 +45,5 @@ class TabEnterUsageDetector : LookupManagerListener { return if (tabRatio > TAB_RATIO_THRESHOLD) '\t' else '\n' } - fun detectionFinished() = totalCount >= SELECTION_COUNT_THRESHOLD - - companion object { - const val TAB_SELECTION_COUNT_KEY = "tab_selection_count" - const val ENTER_SELECTION_COUNT_KEY = "enter_selection_count" - - const val SELECTION_COUNT_THRESHOLD = 5 - const val TAB_RATIO_THRESHOLD = 0.1 - } + fun detectionFinished(): Boolean = totalCount >= SELECTION_COUNT_THRESHOLD } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationAutoPopup.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationAutoPopup.kt index 93c7540ce720..2afda28115bd 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationAutoPopup.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationAutoPopup.kt @@ -4,14 +4,6 @@ package com.intellij.lang.documentation.ide.impl import com.intellij.codeInsight.lookup.Lookup import com.intellij.codeInsight.lookup.LookupEx import com.intellij.codeInsight.lookup.LookupManagerListener -import com.intellij.openapi.project.Project -import com.intellij.openapi.startup.ProjectActivity - -private class DocumentationAutoPopup : ProjectActivity { - override suspend fun execute(project: Project) { - project.messageBus.simpleConnect().subscribe(LookupManagerListener.TOPIC, DocumentationAutoPopupListener()) - } -} private class DocumentationAutoPopupListener : LookupManagerListener { override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) { diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationManager.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationManager.kt index 380f0fd241a7..7dd4a7f8306e 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationManager.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationManager.kt @@ -60,8 +60,6 @@ class DocumentationManager(private val project: Project, private val cs: Corouti cs.cancel() } - private val toolWindowManager: DocumentationToolWindowManager get() = DocumentationToolWindowManager.instance(project) - fun actionPerformed(dataContext: DataContext, popupDependencies: Disposable? = null) { EDT.assertIsEdt() @@ -79,16 +77,18 @@ class DocumentationManager(private val project: Project, private val cs: Corouti } ?: quickSearchPopupContext(project)?.also { FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKJAVADOC_CTRLN_FEATURE) } + + val toolWindowManager = DocumentationToolWindowManager.getInstanceIfCreated(project) if (secondaryPopupContext == null) { // no popups - if (toolWindowManager.focusVisibleReusableTab()) { + if (toolWindowManager?.focusVisibleReusableTab() == true) { // Explicit invocation moves focus to a visible preview tab. return } } else { // some popup is already visible - if (toolWindowManager.hasVisibleAutoUpdatingTab()) { + if (toolWindowManager?.hasVisibleAutoUpdatingTab() == true) { // don't show another popup is a preview tab is visible, it will be updated return } @@ -142,6 +142,7 @@ class DocumentationManager(private val project: Project, private val cs: Corouti private fun showDocumentation(requests: List, popupContext: PopupContext, popupDependencies: Disposable? = null) { + val toolWindowManager = DocumentationToolWindowManager.getInstance(project) val initial = requests.first() if (skipPopup) { toolWindowManager.showInToolWindow(requests) @@ -208,7 +209,7 @@ class DocumentationManager(private val project: Project, private val cs: Corouti // the user might've explicitly invoked the action during the delay return // return here to not compute the request unnecessarily } - if (toolWindowManager.hasVisibleAutoUpdatingTab()) { + if (DocumentationToolWindowManager.getInstanceIfCreated(project)?.hasVisibleAutoUpdatingTab() == true) { return // don't show a documentation popup if an auto-updating tab is visible, it will be updated } val request = withContext(Dispatchers.Default) { @@ -260,7 +261,8 @@ class DocumentationManager(private val project: Project, private val cs: Corouti editor: Editor, popupPosition: Point, ): Boolean = coroutineScope { - val pauseAutoUpdateHandle = toolWindowManager.getVisibleAutoUpdatingContent()?.toolWindowUI?.pauseAutoUpdate() + val toolWindowManager = DocumentationToolWindowManager.getInstanceIfCreated(project) + val pauseAutoUpdateHandle = toolWindowManager?.getVisibleAutoUpdatingContent()?.toolWindowUI?.pauseAutoUpdate() try { val result = withContext(Dispatchers.Default) { resolveLink(targetSupplier, url) diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationTargetHoverInfo.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationTargetHoverInfo.kt index 7f7c44c0649c..5fe8fa423535 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationTargetHoverInfo.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationTargetHoverInfo.kt @@ -45,7 +45,7 @@ internal fun calcTargetDocumentationInfo(project: Project, hostEditor: Editor, h } if (!LightEdit.owns(project)) { val preview = withContext(Dispatchers.EDT) { - DocumentationToolWindowManager.instance(project).updateVisibleAutoUpdatingTab(requests.first()) + DocumentationToolWindowManager.getInstance(project).updateVisibleAutoUpdatingTab(requests.first()) } if (preview) { return@runBlockingCancellable null diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationToolWindowManager.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationToolWindowManager.kt index fe9ebd3769cf..b566833caa29 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationToolWindowManager.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationToolWindowManager.kt @@ -41,11 +41,12 @@ internal class DocumentationToolWindowManager( private val project: Project, private val cs: CoroutineScope, ) { - companion object { const val TOOL_WINDOW_ID: String = "documentation.v2" - fun instance(project: Project): DocumentationToolWindowManager = project.service() + fun getInstance(project: Project): DocumentationToolWindowManager = project.service() + + fun getInstanceIfCreated(project: Project): DocumentationToolWindowManager? = project.serviceIfCreated() private var autoUpdate_: Boolean by propComponentProperty(name = "documentation.auto.update", defaultValue = true) diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/ui/DocumentationPopupUI.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/ui/DocumentationPopupUI.kt index 41eecfc614f2..1ac9d8630252 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/ui/DocumentationPopupUI.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/ui/DocumentationPopupUI.kt @@ -193,7 +193,7 @@ internal class DocumentationPopupUI( override fun actionPerformed(e: AnActionEvent) { val documentationUI = detachUI() myPopup.cancel() - DocumentationToolWindowManager.instance(project).showInToolWindow(documentationUI) + DocumentationToolWindowManager.getInstance(project).showInToolWindow(documentationUI) } } diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index b111b1d6b83c..65faa3769a13 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -1750,7 +1750,6 @@ - @@ -1798,6 +1797,8 @@ topic="com.intellij.codeInsight.lookup.LookupManagerListener"/> +