[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
This commit is contained in:
Yuriy Artamonov
2024-03-20 13:46:51 +00:00
committed by intellij-monorepo-bot
parent 40d645b620
commit bd34e1c80f
8 changed files with 24 additions and 30 deletions
@@ -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) {
@@ -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
}
@@ -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?) {
@@ -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<DocumentationRequest>,
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)
@@ -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
@@ -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)
@@ -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)
}
}
@@ -1750,7 +1750,6 @@
<recoveryAction implementation="com.intellij.util.indexing.ReindexAction"/>
<recoveryAction implementation="com.intellij.util.indexing.RefreshIndexableFilesAction"/>
<postStartupActivity implementation="com.intellij.lang.documentation.ide.impl.DocumentationAutoPopup"/>
<postStartupActivity implementation="com.intellij.codeInsight.documentation.DocumentationSettingsListener"/>
<projectService serviceInterface="com.intellij.lang.documentation.ide.IdeDocumentationTargetProvider"
serviceImplementation="com.intellij.lang.documentation.ide.impl.IdeDocumentationTargetProviderImpl"/>
@@ -1798,6 +1797,8 @@
topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>
<listener class="com.intellij.codeInsight.inline.completion.TabEnterUsageDetector"
topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>
<listener class="com.intellij.lang.documentation.ide.impl.DocumentationAutoPopupListener"
topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>
<listener class="com.intellij.psi.impl.file.impl.PsiVFSListener$MyModuleRootListener"
topic="com.intellij.openapi.roots.ModuleRootListener"/>
<listener class="com.intellij.workspaceModel.ide.impl.legacyBridge.facet.FacetEntityChangeListener$WorkspaceModelListener"