From 8c51490eae994c191310903fc46fb3400b70f7cd Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Sat, 23 Mar 2024 10:30:50 +0100 Subject: [PATCH] IJPL-846 cleanup, prefer serviceAsync GitOrigin-RevId: 219325e0230cb61fec5e3dd09ac37e88320635f0 --- .../daemon/impl/HighlightingMarkupGrave.java | 14 +-- .../fusCollectors/FileEditorCollector.kt | 10 +- .../fileEditor/impl/EditorsSplitters.kt | 3 +- .../fileEditor/impl/FileEditorManagerImpl.kt | 93 +++++++++---------- .../wm/ex/IdeFocusTraversalPolicy.java | 6 +- 5 files changed, 62 insertions(+), 64 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightingMarkupGrave.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightingMarkupGrave.java index 23c2077df21c..3c58c3b66052 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightingMarkupGrave.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HighlightingMarkupGrave.java @@ -271,7 +271,7 @@ public class HighlightingMarkupGrave { } HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); if (info != null && - (info.getSeverity().compareTo(HighlightSeverity.INFORMATION) > 0 // either warning/error or symbol type (e.g. field text attribute) + (info.getSeverity().compareTo(HighlightSeverity.INFORMATION) > 0 // either warning/error or symbol type (e.g., field text attribute) || info.getSeverity() == HighlightInfoType.SYMBOL_TYPE_SEVERITY)) { return true; } @@ -362,9 +362,9 @@ public class HighlightingMarkupGrave { } private void writeTextAttributesKey(@NotNull DataOutput out) throws IOException { - boolean attributesKeyExists = textAttributesKey != null; - out.writeBoolean(attributesKeyExists); - if (attributesKeyExists) { + boolean attributeKeyExists = textAttributesKey != null; + out.writeBoolean(attributeKeyExists); + if (attributeKeyExists) { IOUtil.writeUTF(out, textAttributesKey.getExternalName()); } } @@ -413,7 +413,7 @@ public class HighlightingMarkupGrave { return STORE_NEW; } if (oldMarkup == null) { - // no a limb to put in grave + // no, a limb to put in grave return KEEP_OLD; } if (oldMarkup.contentHash() != newMarkup.contentHash() && !newMarkup.isEmpty()) { @@ -429,7 +429,7 @@ public class HighlightingMarkupGrave { return KEEP_OLD; } if (isNewMoreRelevant == null) { - // should never happen. file is closed without being opened before + // should never happen. the file is closed without being opened before return STORE_NEW; } if (isNewMoreRelevant) { @@ -482,6 +482,6 @@ public class HighlightingMarkupGrave { } private void logFusStatistic(@NotNull VirtualFileWithId file, @NotNull MarkupGraveEvent event, int restoredCount) { - FileEditorCollector.logEditorMarkupGrave(myProject, (VirtualFile) file, event, restoredCount); + FileEditorCollector.INSTANCE.logEditorMarkupGrave(myProject, (VirtualFile) file, event, restoredCount); } } diff --git a/platform/platform-impl/src/com/intellij/featureStatistics/fusCollectors/FileEditorCollector.kt b/platform/platform-impl/src/com/intellij/featureStatistics/fusCollectors/FileEditorCollector.kt index 642321bffb52..779afb85120d 100644 --- a/platform/platform-impl/src/com/intellij/featureStatistics/fusCollectors/FileEditorCollector.kt +++ b/platform/platform-impl/src/com/intellij/featureStatistics/fusCollectors/FileEditorCollector.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.featureStatistics.fusCollectors import com.intellij.internal.statistic.eventLog.EventLogGroup @@ -22,17 +22,17 @@ object FileEditorCollector : CounterUsagesCollector() { EventFields.Enum("markup_grave_event"), EventFields.Int("restored_highlighters")) - @JvmStatic fun logAlternativeFileEditorSelected(project: Project, file: VirtualFile, editor: FileEditor) { - ALTERNATIVE_FILE_EDITOR_SELECTED.log(project, FILE_EDITOR_FIELD.with(editor.javaClass), EventFields.AnonymizedPath.with(file.path)) + ALTERNATIVE_FILE_EDITOR_SELECTED.log( + project = project, + pairs = listOf(FILE_EDITOR_FIELD.with(editor.javaClass), EventFields.AnonymizedPath.with(file.path)), + ) } - @JvmStatic fun logEditorEmptyState(project: Project, cause: EmptyStateCause) { EDITOR_EMPTY_STATE_SHOWN.log(project, cause) } - @JvmStatic fun logEditorMarkupGrave(project: Project, file: VirtualFile, graveEvent: MarkupGraveEvent, restoredCount: Int) { EDITOR_MARKUP_RESTORED.log(project, file.path, graveEvent, restoredCount) } diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorsSplitters.kt b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorsSplitters.kt index 28a5589a58b2..efbd70840937 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorsSplitters.kt +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorsSplitters.kt @@ -91,7 +91,7 @@ private const val IDE_FINGERPRINT: @NonNls String = "ideFingerprint" @DirtyUI open class EditorsSplitters internal constructor( val manager: FileEditorManagerImpl, - internal val coroutineScope: CoroutineScope, + @JvmField internal val coroutineScope: CoroutineScope, ) : JPanel(BorderLayout()), UISettingsListener { companion object { const val SPLITTER_KEY: @NonNls String = "EditorsSplitters" @@ -103,7 +103,6 @@ open class EditorsSplitters internal constructor( } } - @JvmStatic fun findDefaultComponentInSplitters(project: Project?): JComponent? { return getSplittersToFocus(project)?.currentWindow?.selectedComposite?.preferredFocusedComponent } diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.kt b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.kt index afe9a7623aa2..465318b2b243 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.kt +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileEditorManagerImpl.kt @@ -91,7 +91,6 @@ import com.intellij.util.messages.impl.MessageListenerList import com.intellij.util.ui.EDT import com.intellij.util.ui.UIUtil import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet -import kotlinx.collections.immutable.persistentSetOf import kotlinx.coroutines.* import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.* @@ -220,37 +219,38 @@ open class FileEditorManagerImpl( } .stateIn(coroutineScope, SharingStarted.Eagerly, null) - val publisher = project.messageBus.syncPublisher(FileEditorManagerListener.FILE_EDITOR_MANAGER) + coroutineScope.launch { + val publisher = project.messageBus.syncPublisher(FileEditorManagerListener.FILE_EDITOR_MANAGER) - // not using collectLatest() to ensure that the listeners miss no selection update - selectionFlow - .zipWithNext { oldState, state -> - val oldEditorWithProvider = oldState?.fileEditorProvider - val newEditorWithProvider = state?.fileEditorProvider - if (oldEditorWithProvider == newEditorWithProvider) { - return@zipWithNext - } + // not using collectLatest() to ensure that the listeners miss no selection update + selectionFlow + .zipWithNext { oldState, state -> + val oldEditorWithProvider = oldState?.fileEditorProvider + val newEditorWithProvider = state?.fileEditorProvider + if (oldEditorWithProvider == newEditorWithProvider) { + return@zipWithNext + } - // expected in EDT - withContext(Dispatchers.EDT) { - blockingContext { - kotlin.runCatching { - fireSelectionChanged(newComposite = state?.composite, - oldEditorWithProvider = oldEditorWithProvider, - newEditorWithProvider = newEditorWithProvider, - publisher = publisher) + // expected in EDT + withContext(Dispatchers.EDT) { + runCatching { + fireSelectionChanged( + newComposite = state?.composite, + oldEditorWithProvider = oldEditorWithProvider, + newEditorWithProvider = newEditorWithProvider, + publisher = publisher, + ) }.getOrLogException(LOG) } } - } - .launchIn(coroutineScope) + .collect() + } currentFileEditorFlow = selectionFlow .map { it?.fileEditorProvider?.fileEditor } .stateIn(coroutineScope, SharingStarted.Eagerly, null) - val dumbModeFinishedScope = coroutineScope.childScope() - dumbModeFinishedScope.launch(start = CoroutineStart.UNDISPATCHED) { + coroutineScope.launch { dumbModeFinishedFlow.collectLatest { dumbModeFinished(project) } @@ -424,7 +424,7 @@ open class FileEditorManagerImpl( getAllSplitters() } - val providerManager = FileEditorProviderManager.getInstance() + val providerManager = serviceAsync() // predictable order of iteration val fileToNewProviders = openedComposites.groupByTo(LinkedHashMap()) { it.file }.entries.mapNotNull { entry -> val composites = entry.value @@ -435,13 +435,15 @@ open class FileEditorManagerImpl( null } else { - file to composites.map { composite -> composite to newProviders } + file to composites.map { it to newProviders } } } for ((file, toOpen) in fileToNewProviders) { withContext(Dispatchers.EDT) { for ((composite, providers) in toOpen) { - if (!openedComposites.contains(composite)) continue + if (!openedComposites.contains(composite)) { + continue + } for (provider in providers) { val editor = provider.createEditor(project, file) @@ -472,10 +474,11 @@ open class FileEditorManagerImpl( fun getAllSplitters(): Set { // ordered - var result = persistentSetOf(mainSplitters) + val result = LinkedHashSet() + result.add(mainSplitters) for (container in DockManager.getInstance(project).containers) { if (container is DockableEditorTabbedContainer) { - result = result.add(container.splitters) + result.add(container.splitters) } } return result @@ -485,10 +488,7 @@ open class FileEditorManagerImpl( val result = CompletableDeferred() val focusManager = IdeFocusManager.getGlobalInstance() focusManager.doWhenFocusSettlesDown { - result.complete( - if (project.isDisposed) null - else getDockContainer(focusManager.focusOwner)?.splitters ?: mainSplitters - ) + result.complete(if (project.isDisposed) null else getDockContainer(focusManager.focusOwner)?.splitters ?: mainSplitters) } return result } @@ -508,8 +508,8 @@ open class FileEditorManagerImpl( } private fun getDockContainer(focusOwner: Component?): DockableEditorTabbedContainer? { - return DockManager.getInstance(project).getContainerFor( - focusOwner) { it is DockableEditorTabbedContainer } as DockableEditorTabbedContainer? + return DockManager.getInstance(project) + .getContainerFor(focusOwner) { it is DockableEditorTabbedContainer } as DockableEditorTabbedContainer? } override val preferredFocusedComponent: JComponent? @@ -1728,23 +1728,22 @@ open class FileEditorManagerImpl( ?: allClientFileEditorManagers.firstNotNullOfOrNull { it.getComposite(editor) } } - private fun fireSelectionChanged(newComposite: EditorComposite?, - oldEditorWithProvider: FileEditorWithProvider?, - newEditorWithProvider: FileEditorWithProvider?, - publisher: FileEditorManagerListener) { - val task = { - oldEditorWithProvider?.fileEditor?.deselectNotify() - - val newEditor = newEditorWithProvider?.fileEditor - if (newEditor != null) { + private suspend fun fireSelectionChanged( + newComposite: EditorComposite?, + oldEditorWithProvider: FileEditorWithProvider?, + newEditorWithProvider: FileEditorWithProvider?, + publisher: FileEditorManagerListener, + ) { + oldEditorWithProvider?.fileEditor?.deselectNotify() + val newEditor = newEditorWithProvider?.fileEditor + if (newEditor != null) { + blockingContext { newEditor.selectNotify() - FileEditorCollector.logAlternativeFileEditorSelected(project, newComposite!!.file, newEditor) - (FileEditorProviderManager.getInstance() as FileEditorProviderManagerImpl).providerSelected(newComposite) } - IdeDocumentHistory.getInstance(project).onSelectionChanged() + FileEditorCollector.logAlternativeFileEditorSelected(project = project, file = newComposite!!.file, editor = newEditor) + (serviceAsync() as FileEditorProviderManagerImpl).providerSelected(newComposite) } - - task() + project.serviceAsync().onSelectionChanged() val newFile = newComposite?.file if (newFile != null) { diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/ex/IdeFocusTraversalPolicy.java b/platform/platform-impl/src/com/intellij/openapi/wm/ex/IdeFocusTraversalPolicy.java index a56d36c89be7..74d4dee96e75 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/ex/IdeFocusTraversalPolicy.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/ex/IdeFocusTraversalPolicy.java @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.openapi.wm.ex; import com.intellij.openapi.editor.impl.EditorComponentImpl; @@ -52,7 +52,7 @@ public class IdeFocusTraversalPolicy extends LayoutFocusTraversalPolicy { } if (siblingComponent instanceof EditorsSplitters) { - Component defaultFocusableComponent = EditorsSplitters.findDefaultComponentInSplitters(getProject()); + Component defaultFocusableComponent = EditorsSplitters.Companion.findDefaultComponentInSplitters(getProject()); if (defaultFocusableComponent != null) { return defaultFocusableComponent; } @@ -164,7 +164,7 @@ public class IdeFocusTraversalPolicy extends LayoutFocusTraversalPolicy { component instanceof JComboBox; } - // Create our own subclass and change accept to public so that we can call accept. + // Create our own subclass and change accepts to public so that we can call accept. private static final class SwingDefaultFocusTraversalPolicy extends DefaultFocusTraversalPolicy { @Override public boolean accept(Component aComponent) {