IJPL-163258 Redesign NavBarModelExtension.getData

(cherry picked from commit 1c678c439106f6b962d1763a982928d444a6d181)

IJ-CR-158811

GitOrigin-RevId: 6ea9f4a8ae5eb99b90035d22ed3dd8e0fe787de2
This commit is contained in:
Gregory.Shrago
2024-06-26 21:35:34 +04:00
committed by intellij-monorepo-bot
parent 1189cb9281
commit 35b6af5202
8 changed files with 75 additions and 81 deletions

View File

@@ -14,7 +14,6 @@ import com.intellij.platform.execution.serviceView.ServiceModel.ServiceViewItem;
import com.intellij.ui.ClientProperty;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.tabs.JBTabs;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.tree.TreeModelAdapter;
@@ -120,10 +119,6 @@ final class ServiceViewActionProvider {
return getSelectedView(dataContext.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT));
}
static @Nullable ServiceView getSelectedView(@NotNull DataProvider provider) {
return getSelectedView(ObjectUtils.tryCast(provider.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT.getName()), Component.class));
}
static @NotNull List<ServiceViewItem> getSelectedItems(@NotNull AnActionEvent e) {
List<ServiceViewItem> items = e.getData(SERVICES_SELECTED_ITEMS);
return items != null ? items : Collections.emptyList();

View File

@@ -2,14 +2,14 @@
package com.intellij.platform.execution.serviceView;
import com.intellij.ide.navigationToolbar.AbstractNavBarModelExtension;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.DataContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class ServiceViewNavBarExtension extends AbstractNavBarModelExtension {
@Override
public @Nullable String getPopupMenuGroup(@NotNull DataProvider provider) {
ServiceView serviceView = ServiceViewActionProvider.getSelectedView(provider);
public @Nullable String getPopupMenuGroup(@NotNull DataContext dataContext) {
ServiceView serviceView = ServiceViewActionProvider.getSelectedView(dataContext);
return serviceView == null ? null : ServiceViewActionProvider.SERVICE_VIEW_ITEM_POPUP;
}

View File

@@ -1,6 +1,7 @@
// 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.platform.navbar.backend.impl
import com.intellij.ide.navigationToolbar.NavBarModelExtension
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.ide.util.DeleteHandler.DefaultDeleteProvider
import com.intellij.model.Pointer
@@ -11,7 +12,6 @@ import com.intellij.openapi.roots.ui.configuration.actions.ModuleDeleteProvider
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.navbar.NavBarVmItem
import com.intellij.platform.navbar.backend.NavBarItem
import com.intellij.platform.navbar.impl.extensionData
import com.intellij.pom.Navigatable
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
@@ -26,34 +26,33 @@ internal class NavBarBgtDataRule : UiDataRule {
val pointers = selection.mapNotNull { (it as? IdeNavBarVmItem)?.pointer }
if (pointers.isEmpty()) return
sink[PlatformDataKeys.SELECTED_ITEM] = selection.firstOrNull()
sink[PlatformDataKeys.SELECTED_ITEMS] = selection.toTypedArray()
sink.lazy(LangDataKeys.IDE_VIEW) {
NavBarIdeView(pointers)
}
sink[PlatformCoreDataKeys.BGT_DATA_PROVIDER] = DataProvider { dataId ->
val provider = DataProvider {
getBgData(project, pointers, it)
}
extensionData(dataId, provider)
defaultSnapshot(project, sink, pointers)
NavBarModelExtension.EP_NAME.forEachExtensionSafe {
it.uiDataSnapshot(sink, snapshot)
}
}
}
private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarItem>>, dataId: String): Any? {
val seq = selectedItems.asSequence().mapNotNull { it.dereference() }
return when (dataId) {
CommonDataKeys.PROJECT.name -> project
PlatformDataKeys.SELECTED_ITEM.name -> seq.firstOrNull()
PlatformDataKeys.SELECTED_ITEMS.name -> seq.toList().toTypedArray()
PlatformCoreDataKeys.MODULE.name -> {
seq.firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data }
?: seq.firstNotNullOfOrNull {
private fun defaultSnapshot(project: Project,
sink: DataSink, pointers:
List<Pointer<out NavBarItem>>) {
val seq = { pointers.asSequence().mapNotNull { it.dereference() } }
sink.lazy(PlatformCoreDataKeys.MODULE) {
seq().firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data }
?: seq().firstNotNullOfOrNull {
(it as? PsiNavBarItem)?.data?.let { psi ->
ModuleUtilCore.findModuleForPsiElement(psi)
}
}
}
LangDataKeys.MODULE_CONTEXT.name -> {
val dir = seq.firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data as? PsiDirectory }
sink.lazy(LangDataKeys.MODULE_CONTEXT) {
val dir = seq().firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data as? PsiDirectory }
if (dir != null && ProjectRootsUtil.isModuleContentRoot(dir.virtualFile, project)) {
ModuleUtilCore.findModuleForPsiElement(dir)
}
@@ -61,12 +60,17 @@ private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarIt
null
}
}
CommonDataKeys.PSI_ELEMENT.name -> seq.firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data }
PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.name -> seq.mapNotNull { (it as? PsiNavBarItem)?.data }
sink.lazy(CommonDataKeys.PSI_ELEMENT) {
seq().firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data }
}
sink.lazy(PlatformCoreDataKeys.PSI_ELEMENT_ARRAY) {
seq().mapNotNull { (it as? PsiNavBarItem)?.data }
.toList()
.ifEmpty { null }
?.toArray(PsiElement.EMPTY_ARRAY)
CommonDataKeys.VIRTUAL_FILE_ARRAY.name -> seq.mapNotNull {
}
sink.lazy(CommonDataKeys.VIRTUAL_FILE_ARRAY) {
seq().mapNotNull {
(it as? PsiNavBarItem)?.data?.let { psi ->
PsiUtilCore.getVirtualFile(psi)
}
@@ -74,14 +78,17 @@ private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarIt
.toSet()
.ifEmpty { null }
?.toArray(VirtualFile.EMPTY_ARRAY)
CommonDataKeys.NAVIGATABLE_ARRAY.name -> seq.mapNotNull {
}
sink.lazy(PlatformDataKeys.NAVIGATABLE_ARRAY) {
seq().mapNotNull {
(it as? DefaultNavBarItem<*>)?.data as? Navigatable
}
.toList()
.ifEmpty { null }
?.toArray(Navigatable.EMPTY_NAVIGATABLE_ARRAY)
PlatformDataKeys.DELETE_ELEMENT_PROVIDER.name -> (
seq.firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data } != null).let { hasModule ->
}
sink.lazy(PlatformDataKeys.DELETE_ELEMENT_PROVIDER) {
val hasModule = seq().firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data } != null
if (hasModule) {
ModuleDeleteProvider.getInstance()
}
@@ -89,6 +96,4 @@ private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarIt
DefaultDeleteProvider()
}
}
else -> null
}
}

View File

@@ -12,14 +12,14 @@ internal class NavBarContextMenuActionGroup : ActionGroup() {
return EMPTY_ARRAY
}
val dataContext = e.dataContext
val popupGroupId = contextMenuActionGroupId(dataContext::getData)
val popupGroupId = contextMenuActionGroupId(dataContext)
val group = CustomActionsSchema.getInstance().getCorrectedAction(popupGroupId) as ActionGroup?
?: return EMPTY_ARRAY
return group.getChildren(e)
}
}
private fun contextMenuActionGroupId(dataProvider: DataProvider): String {
private fun contextMenuActionGroupId(dataProvider: DataContext): String {
for (modelExtension in NavBarModelExtension.EP_NAME.extensionList) {
return modelExtension.getPopupMenuGroup(dataProvider)
?: continue

View File

@@ -2,9 +2,9 @@
package com.intellij.platform.navbar.frontend.actions
import com.intellij.ide.CopyPasteDelegator
import com.intellij.ide.navigationToolbar.NavBarModelExtension
import com.intellij.openapi.actionSystem.*
import com.intellij.platform.navbar.NavBarVmItem
import com.intellij.platform.navbar.impl.extensionData
import javax.swing.JComponent
internal class NavBarUiDataRule : UiDataRule {
@@ -19,10 +19,8 @@ internal class NavBarUiDataRule : UiDataRule {
sink[PlatformDataKeys.COPY_PROVIDER] = delegator.copyProvider
sink[PlatformDataKeys.PASTE_PROVIDER] = delegator.pasteProvider
DataSink.uiDataSnapshot(sink, DataProvider { dataId ->
extensionData(dataId) { innerDataId ->
snapshot[DataKey.create(innerDataId)]
}
})
NavBarModelExtension.EP_NAME.forEachExtensionSafe {
it.uiDataSnapshot(sink, snapshot)
}
}
}

View File

@@ -1,24 +0,0 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.platform.navbar.impl
import com.intellij.ide.impl.DataValidators
import com.intellij.ide.navigationToolbar.NavBarModelExtension
import com.intellij.openapi.actionSystem.CompositeDataProvider
import com.intellij.openapi.actionSystem.DataProvider
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys
fun extensionData(dataId: String, provider: DataProvider): Any? {
if (PlatformCoreDataKeys.BGT_DATA_PROVIDER.`is`(dataId)) {
val bgtProviders = NavBarModelExtension.EP_NAME.extensionList.mapNotNull {
it.getData(dataId, provider) as? DataProvider
}
return CompositeDataProvider.compose(bgtProviders)
}
for (modelExtension in NavBarModelExtension.EP_NAME.extensionList) {
val data = modelExtension.getData(dataId, provider)
if (data != null) {
return DataValidators.validOrNull(data, dataId, modelExtension)
}
}
return provider.getData(dataId)
}

View File

@@ -3373,11 +3373,13 @@ com.intellij.ide.navigationToolbar.NavBarModelExtension
- getIcon(java.lang.Object):javax.swing.Icon
- getLeafElement(com.intellij.openapi.actionSystem.DataContext):com.intellij.psi.PsiElement
- a:getParent(com.intellij.psi.PsiElement):com.intellij.psi.PsiElement
- getPopupMenuGroup(com.intellij.openapi.actionSystem.DataContext):java.lang.String
- getPopupMenuGroup(com.intellij.openapi.actionSystem.DataProvider):java.lang.String
- a:getPresentableText(java.lang.Object):java.lang.String
- getPresentableText(java.lang.Object,Z):java.lang.String
- normalizeChildren():Z
- processChildren(java.lang.Object,java.lang.Object,com.intellij.util.Processor):Z
- uiDataSnapshot(com.intellij.openapi.actionSystem.DataSink,com.intellij.openapi.actionSystem.DataSnapshot):V
f:com.intellij.ide.navigationToolbar.NavBarRootPaneExtension
- <init>():V
a:com.intellij.ide.navigationToolbar.NavBarRootPaneExtension$NavBarWrapperPanel

View File

@@ -3,6 +3,8 @@ package com.intellij.ide.navigationToolbar;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.DataSink;
import com.intellij.openapi.actionSystem.DataSnapshot;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
@@ -49,8 +51,24 @@ public interface NavBarModelExtension {
@NotNull
Collection<VirtualFile> additionalRoots(Project project);
default void uiDataSnapshot(@NotNull DataSink sink, @NotNull DataSnapshot snapshot) {
}
/**
* @noinspection unused
* @deprecated Unused. Use {@link #uiDataSnapshot} instead
*/
@Deprecated(forRemoval = true)
default @Nullable Object getData(@NotNull String dataId, @NotNull DataProvider provider) { return null; }
/** @noinspection LambdaUnfriendlyMethodOverload*/
default @Nullable String getPopupMenuGroup(@NotNull DataContext dataContext) { return null; }
/**
* @noinspection LambdaUnfriendlyMethodOverload, unused
* @deprecated Unused. Use {@link #uiDataSnapshot} instead
*/
@Deprecated(forRemoval = true)
default @Nullable String getPopupMenuGroup(@NotNull DataProvider provider) { return null; }
default PsiElement getLeafElement(@NotNull DataContext dataContext) {