From 5809e089ab9db296b4a4342f9bbbd90c69577fae Mon Sep 17 00:00:00 2001 From: Sergei Tachenov Date: Tue, 15 Nov 2022 12:05:08 +0200 Subject: [PATCH] [platform UI] IDEA-304888 Use inactive selected editor for Select In only in Project View The last fix broke a lot of Alt+F1 actions in tool windows unrelated to Project View, such as Git (select a branch in the log). To fix this, only fall back to the selected but not focused editor if the context component is in the Project View. To avoid an ugly `instanceof ProjectViewTree` in SelectInContextImpl, we reverse this dependency a bit: we declare a nested interface to retrieve what the context component considers the active file editor, and then we put it into a client property to avoid implementing it directly in the context component, which would be possible in this case but generally not desirable as the component could be of a more or less standard type. This way components now can specify what they consider the active editor. It's only implemented for ProjectViewTree now, other components preserve their original behavior. GitOrigin-RevId: 27ebb6bb096401dd32c6721e065bafb7acb35035 --- .../ide/actions/SelectInContextImpl.java | 24 +++++++++++++++---- .../ide/projectView/impl/ProjectViewTree.java | 11 +++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/ide/actions/SelectInContextImpl.java b/platform/lang-impl/src/com/intellij/ide/actions/SelectInContextImpl.java index a59b169c3c0a..b5173a587180 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/SelectInContextImpl.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/SelectInContextImpl.java @@ -11,16 +11,21 @@ import com.intellij.ide.structureView.StructureView; import com.intellij.ide.structureView.StructureViewBuilder; import com.intellij.ide.structureView.StructureViewModel; import com.intellij.ide.structureView.TreeBasedStructureViewBuilder; -import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.PlatformCoreDataKeys; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.*; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.Navigatable; import com.intellij.psi.*; import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider; +import com.intellij.ui.ClientProperty; import com.intellij.util.ArrayUtil; import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.NotNull; @@ -31,6 +36,7 @@ import java.awt.*; import java.awt.event.InputEvent; public final class SelectInContextImpl extends FileSelectInContext { + public static final Key CONTEXT_EDITOR_PROVIDER_KEY = Key.create("CONTEXT_EDITOR_PROVIDER"); private final Object mySelector; private SelectInContextImpl(@NotNull PsiFile psiFile, @NotNull Object selector) { @@ -47,9 +53,14 @@ public final class SelectInContextImpl extends FileSelectInContext { @Nullable public static SelectInContext createContext(AnActionEvent event) { Project project = event.getProject(); - FileEditor editor = event.getData(PlatformCoreDataKeys.FILE_EDITOR); - if (editor == null) { - editor = event.getData(PlatformDataKeys.LAST_ACTIVE_FILE_EDITOR); + FileEditor editor; + final var contextComponent = event.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT); + final var contextEditorProvider = ClientProperty.get(contextComponent, CONTEXT_EDITOR_PROVIDER_KEY); + if (contextEditorProvider != null) { + editor = contextEditorProvider.getContextEditor(event); + } + else { + editor = event.getData(PlatformCoreDataKeys.FILE_EDITOR); } VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE); @@ -210,4 +221,9 @@ public final class SelectInContextImpl extends FileSelectInContext { return component instanceof JComponent ? (JComponent)component : null; } } + + public interface ContextEditorProvider { + @Nullable FileEditor getContextEditor(@NotNull AnActionEvent event); + } + } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewTree.java b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewTree.java index f697d30a56bb..c1e5bc284488 100644 --- a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewTree.java +++ b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewTree.java @@ -1,12 +1,16 @@ // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.ide.projectView.impl; +import com.intellij.ide.actions.SelectInContextImpl; import com.intellij.ide.dnd.aware.DnDAwareTree; import com.intellij.ide.util.treeView.PresentableNodeDescriptor; +import com.intellij.openapi.actionSystem.PlatformCoreDataKeys; +import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.presentation.FilePresentationService; import com.intellij.psi.PsiElement; +import com.intellij.ui.ClientProperty; import com.intellij.ui.popup.HintUpdateSupply; import com.intellij.ui.tabs.FileColorManagerImpl; import com.intellij.util.ObjectUtils; @@ -42,6 +46,13 @@ public class ProjectViewTree extends DnDAwareTree { setModel(model); setCellRenderer(createCellRenderer()); HintUpdateSupply.installDataContextHintUpdateSupply(this); + ClientProperty.put(this, SelectInContextImpl.CONTEXT_EDITOR_PROVIDER_KEY, event -> { + var editor = event.getData(PlatformCoreDataKeys.FILE_EDITOR); + if (editor == null) { + editor = event.getData(PlatformDataKeys.LAST_ACTIVE_FILE_EDITOR); + } + return editor; + }); } /**