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.ClientProperty;
import com.intellij.ui.PopupHandler; import com.intellij.ui.PopupHandler;
import com.intellij.ui.tabs.JBTabs; import com.intellij.ui.tabs.JBTabs;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.EmptyIcon; import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.tree.TreeModelAdapter; import com.intellij.util.ui.tree.TreeModelAdapter;
@@ -120,10 +119,6 @@ final class ServiceViewActionProvider {
return getSelectedView(dataContext.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT)); 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) { static @NotNull List<ServiceViewItem> getSelectedItems(@NotNull AnActionEvent e) {
List<ServiceViewItem> items = e.getData(SERVICES_SELECTED_ITEMS); List<ServiceViewItem> items = e.getData(SERVICES_SELECTED_ITEMS);
return items != null ? items : Collections.emptyList(); return items != null ? items : Collections.emptyList();

View File

@@ -2,14 +2,14 @@
package com.intellij.platform.execution.serviceView; package com.intellij.platform.execution.serviceView;
import com.intellij.ide.navigationToolbar.AbstractNavBarModelExtension; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public final class ServiceViewNavBarExtension extends AbstractNavBarModelExtension { public final class ServiceViewNavBarExtension extends AbstractNavBarModelExtension {
@Override @Override
public @Nullable String getPopupMenuGroup(@NotNull DataProvider provider) { public @Nullable String getPopupMenuGroup(@NotNull DataContext dataContext) {
ServiceView serviceView = ServiceViewActionProvider.getSelectedView(provider); ServiceView serviceView = ServiceViewActionProvider.getSelectedView(dataContext);
return serviceView == null ? null : ServiceViewActionProvider.SERVICE_VIEW_ITEM_POPUP; 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. // 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 package com.intellij.platform.navbar.backend.impl
import com.intellij.ide.navigationToolbar.NavBarModelExtension
import com.intellij.ide.projectView.impl.ProjectRootsUtil import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.ide.util.DeleteHandler.DefaultDeleteProvider import com.intellij.ide.util.DeleteHandler.DefaultDeleteProvider
import com.intellij.model.Pointer 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.openapi.vfs.VirtualFile
import com.intellij.platform.navbar.NavBarVmItem import com.intellij.platform.navbar.NavBarVmItem
import com.intellij.platform.navbar.backend.NavBarItem import com.intellij.platform.navbar.backend.NavBarItem
import com.intellij.platform.navbar.impl.extensionData
import com.intellij.pom.Navigatable import com.intellij.pom.Navigatable
import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
@@ -26,47 +26,51 @@ internal class NavBarBgtDataRule : UiDataRule {
val pointers = selection.mapNotNull { (it as? IdeNavBarVmItem)?.pointer } val pointers = selection.mapNotNull { (it as? IdeNavBarVmItem)?.pointer }
if (pointers.isEmpty()) return if (pointers.isEmpty()) return
sink[PlatformDataKeys.SELECTED_ITEM] = selection.firstOrNull()
sink[PlatformDataKeys.SELECTED_ITEMS] = selection.toTypedArray()
sink.lazy(LangDataKeys.IDE_VIEW) { sink.lazy(LangDataKeys.IDE_VIEW) {
NavBarIdeView(pointers) NavBarIdeView(pointers)
} }
sink[PlatformCoreDataKeys.BGT_DATA_PROVIDER] = DataProvider { dataId -> defaultSnapshot(project, sink, pointers)
val provider = DataProvider { NavBarModelExtension.EP_NAME.forEachExtensionSafe {
getBgData(project, pointers, it) it.uiDataSnapshot(sink, snapshot)
}
extensionData(dataId, provider)
} }
} }
} }
private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarItem>>, dataId: String): Any? { private fun defaultSnapshot(project: Project,
val seq = selectedItems.asSequence().mapNotNull { it.dereference() } sink: DataSink, pointers:
return when (dataId) { List<Pointer<out NavBarItem>>) {
CommonDataKeys.PROJECT.name -> project val seq = { pointers.asSequence().mapNotNull { it.dereference() } }
PlatformDataKeys.SELECTED_ITEM.name -> seq.firstOrNull() sink.lazy(PlatformCoreDataKeys.MODULE) {
PlatformDataKeys.SELECTED_ITEMS.name -> seq.toList().toTypedArray() seq().firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data }
PlatformCoreDataKeys.MODULE.name -> { ?: seq().firstNotNullOfOrNull {
seq.firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data } (it as? PsiNavBarItem)?.data?.let { psi ->
?: seq.firstNotNullOfOrNull { ModuleUtilCore.findModuleForPsiElement(psi)
(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) {
if (dir != null && ProjectRootsUtil.isModuleContentRoot(dir.virtualFile, project)) { val dir = seq().firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data as? PsiDirectory }
ModuleUtilCore.findModuleForPsiElement(dir) if (dir != null && ProjectRootsUtil.isModuleContentRoot(dir.virtualFile, project)) {
} ModuleUtilCore.findModuleForPsiElement(dir)
else {
null
}
} }
CommonDataKeys.PSI_ELEMENT.name -> seq.firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data } else {
PlatformCoreDataKeys.PSI_ELEMENT_ARRAY.name -> seq.mapNotNull { (it as? PsiNavBarItem)?.data } null
}
}
sink.lazy(CommonDataKeys.PSI_ELEMENT) {
seq().firstNotNullOfOrNull { (it as? PsiNavBarItem)?.data }
}
sink.lazy(PlatformCoreDataKeys.PSI_ELEMENT_ARRAY) {
seq().mapNotNull { (it as? PsiNavBarItem)?.data }
.toList() .toList()
.ifEmpty { null } .ifEmpty { null }
?.toArray(PsiElement.EMPTY_ARRAY) ?.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 -> (it as? PsiNavBarItem)?.data?.let { psi ->
PsiUtilCore.getVirtualFile(psi) PsiUtilCore.getVirtualFile(psi)
} }
@@ -74,21 +78,22 @@ private fun getBgData(project: Project, selectedItems: List<Pointer<out NavBarIt
.toSet() .toSet()
.ifEmpty { null } .ifEmpty { null }
?.toArray(VirtualFile.EMPTY_ARRAY) ?.toArray(VirtualFile.EMPTY_ARRAY)
CommonDataKeys.NAVIGATABLE_ARRAY.name -> seq.mapNotNull { }
sink.lazy(PlatformDataKeys.NAVIGATABLE_ARRAY) {
seq().mapNotNull {
(it as? DefaultNavBarItem<*>)?.data as? Navigatable (it as? DefaultNavBarItem<*>)?.data as? Navigatable
} }
.toList() .toList()
.ifEmpty { null } .ifEmpty { null }
?.toArray(Navigatable.EMPTY_NAVIGATABLE_ARRAY) ?.toArray(Navigatable.EMPTY_NAVIGATABLE_ARRAY)
PlatformDataKeys.DELETE_ELEMENT_PROVIDER.name -> (
seq.firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data } != null).let { hasModule ->
if (hasModule) {
ModuleDeleteProvider.getInstance()
}
else {
DefaultDeleteProvider()
}
}
else -> null
} }
} sink.lazy(PlatformDataKeys.DELETE_ELEMENT_PROVIDER) {
val hasModule = seq().firstNotNullOfOrNull { (it as? ModuleNavBarItem)?.data } != null
if (hasModule) {
ModuleDeleteProvider.getInstance()
}
else {
DefaultDeleteProvider()
}
}
}

View File

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

View File

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

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 - getIcon(java.lang.Object):javax.swing.Icon
- getLeafElement(com.intellij.openapi.actionSystem.DataContext):com.intellij.psi.PsiElement - getLeafElement(com.intellij.openapi.actionSystem.DataContext):com.intellij.psi.PsiElement
- a:getParent(com.intellij.psi.PsiElement):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 - getPopupMenuGroup(com.intellij.openapi.actionSystem.DataProvider):java.lang.String
- a:getPresentableText(java.lang.Object):java.lang.String - a:getPresentableText(java.lang.Object):java.lang.String
- getPresentableText(java.lang.Object,Z):java.lang.String - getPresentableText(java.lang.Object,Z):java.lang.String
- normalizeChildren():Z - normalizeChildren():Z
- processChildren(java.lang.Object,java.lang.Object,com.intellij.util.Processor):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 f:com.intellij.ide.navigationToolbar.NavBarRootPaneExtension
- <init>():V - <init>():V
a:com.intellij.ide.navigationToolbar.NavBarRootPaneExtension$NavBarWrapperPanel 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.DataContext;
import com.intellij.openapi.actionSystem.DataProvider; 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.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Key;
@@ -49,8 +51,24 @@ public interface NavBarModelExtension {
@NotNull @NotNull
Collection<VirtualFile> additionalRoots(Project project); 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; } 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 @Nullable String getPopupMenuGroup(@NotNull DataProvider provider) { return null; }
default PsiElement getLeafElement(@NotNull DataContext dataContext) { default PsiElement getLeafElement(@NotNull DataContext dataContext) {