[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
This commit is contained in:
Sergei Tachenov
2022-11-15 11:29:12 +00:00
committed by intellij-monorepo-bot
parent bbba21e772
commit 5809e089ab
2 changed files with 31 additions and 4 deletions
@@ -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<ContextEditorProvider> 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);
}
}
@@ -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;
});
}
/**