mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
terminal: revert excess ToolWindow API changes; bug fixed: once a selected file gets stuck in ToolWindow instance, a terminal always used it on opening
This commit is contained in:
@@ -18,7 +18,6 @@ package com.intellij.openapi.wm;
|
||||
import com.intellij.openapi.util.ActionCallback;
|
||||
import com.intellij.openapi.util.BusyObject;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.ui.content.ContentManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -27,7 +26,6 @@ import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ToolWindow extends BusyObject {
|
||||
|
||||
@@ -48,24 +46,6 @@ public interface ToolWindow extends BusyObject {
|
||||
|
||||
void activate(@Nullable Runnable runnable, boolean autoFocusContents, boolean forced);
|
||||
|
||||
/**
|
||||
* Activates the tool window.
|
||||
*
|
||||
* @param runnable A command to execute right after the window gets activated. The call is asynchronous since it may require animation.
|
||||
* @param selectedFile A file that user have selected to open the window.
|
||||
*/
|
||||
default void activate(@Nullable Runnable runnable, @Nullable VirtualFile selectedFile) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file that user have opened the window for.
|
||||
*
|
||||
* @return the file, selected by user or {@link Optional#empty()}, if the user have selected nothing or file info is unavailable.
|
||||
*/
|
||||
default Optional<VirtualFile> getSelectedFile() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the tool window is visible or not.
|
||||
* @exception IllegalStateException if tool window isn't installed.
|
||||
|
||||
@@ -28,7 +28,6 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.ActionCallback;
|
||||
import com.intellij.openapi.util.BusyObject;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.*;
|
||||
import com.intellij.openapi.wm.ex.ToolWindowEx;
|
||||
import com.intellij.openapi.wm.ex.WindowManagerEx;
|
||||
@@ -55,7 +54,6 @@ import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -81,9 +79,6 @@ public final class ToolWindowImpl implements ToolWindowEx {
|
||||
private boolean myPlaceholderMode;
|
||||
private ToolWindowFactory myContentFactory;
|
||||
|
||||
@Nullable
|
||||
private VirtualFile mySelectedFile;
|
||||
|
||||
private static final Set<KeyStroke> FORWARD_TRAVERSAL_KEYSTROKES = new HashSet<>(Arrays.asList(
|
||||
new KeyStroke[]{
|
||||
KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)
|
||||
@@ -215,17 +210,6 @@ public final class ToolWindowImpl implements ToolWindowEx {
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void activate(@Nullable Runnable runnable, @Nullable VirtualFile selectedFile) {
|
||||
this.mySelectedFile = selectedFile;
|
||||
activate(runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VirtualFile> getSelectedFile() {
|
||||
return Optional.ofNullable(mySelectedFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isActive() {
|
||||
ApplicationManager.getApplication().assertIsDispatchThread();
|
||||
|
||||
@@ -32,7 +32,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
@@ -86,15 +85,10 @@ public abstract class AbstractTerminalRunner<T extends Process> {
|
||||
openSessionInDirectory(widget.getFirst(), widget.getSecond());
|
||||
return true;
|
||||
}, parent);
|
||||
final ToolWindow window = getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
|
||||
openSessionForFile(terminalWidget, window.getSelectedFile().orElse(null));
|
||||
openSessionForFile(terminalWidget, TerminalView.getInstance(myProject).getFileToOpen());
|
||||
return terminalWidget;
|
||||
}
|
||||
|
||||
private ToolWindow getToolWindow(String windowId) {
|
||||
return ToolWindowManager.getInstance(myProject).getToolWindow(windowId);
|
||||
}
|
||||
|
||||
private void initConsoleUI(final T process) {
|
||||
final Executor defaultExecutor = DefaultRunExecutor.getRunExecutorInstance();
|
||||
final DefaultActionGroup toolbarActions = new DefaultActionGroup();
|
||||
@@ -156,7 +150,10 @@ public abstract class AbstractTerminalRunner<T extends Process> {
|
||||
ExecutionManager.getInstance(myProject).getContentManager().showRunContent(defaultExecutor, myDescriptor);
|
||||
|
||||
// Request focus
|
||||
getToolWindow(defaultExecutor.getId()).activate(() -> IdeFocusManager.getInstance(myProject).requestFocus(toFocus, true));
|
||||
ToolWindow toolWindow = ToolWindowManager.getInstance(myProject).getToolWindow(defaultExecutor.getId());
|
||||
if (toolWindow != null) {
|
||||
toolWindow.activate(() -> IdeFocusManager.getInstance(myProject).requestFocus(toFocus, true));
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -167,21 +164,13 @@ public abstract class AbstractTerminalRunner<T extends Process> {
|
||||
public abstract String runningTargetName();
|
||||
|
||||
public void openSessionForFile(@NotNull TerminalWidget terminalWidget, @Nullable VirtualFile file) {
|
||||
openSessionInDirectory(terminalWidget, Optional.ofNullable(file).map(this::getClosestParentFolderPath).orElse(null));
|
||||
openSessionInDirectory(terminalWidget, getParentDirectoryPath(file));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getClosestParentFolderPath(@Nullable VirtualFile selectedFile) {
|
||||
if (selectedFile != null) {
|
||||
if (selectedFile.isDirectory()) {
|
||||
return selectedFile.getPath();
|
||||
}
|
||||
VirtualFile parent = selectedFile.getParent();
|
||||
if (parent != null) {
|
||||
return parent.getPath();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
private static String getParentDirectoryPath(@Nullable VirtualFile file) {
|
||||
VirtualFile dir = file != null && !file.isDirectory() ? file.getParent() : file;
|
||||
return dir != null ? dir.getPath() : null;
|
||||
}
|
||||
|
||||
private void openSessionInDirectory(@NotNull TerminalWidget terminalWidget, @Nullable String directory) {
|
||||
|
||||
@@ -45,7 +45,6 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author traff
|
||||
@@ -60,7 +59,7 @@ public class TerminalView {
|
||||
private TerminalDockContainer myDockContainer;
|
||||
|
||||
@Nullable
|
||||
private VirtualFile myPreviousSelectedFile;
|
||||
private VirtualFile myFileToOpen;
|
||||
|
||||
public TerminalView(Project project) {
|
||||
myProject = project;
|
||||
@@ -70,7 +69,6 @@ public class TerminalView {
|
||||
return project.getComponent(TerminalView.class);
|
||||
}
|
||||
|
||||
|
||||
public void initTerminal(final ToolWindow toolWindow) {
|
||||
LocalTerminalDirectRunner terminalRunner = LocalTerminalDirectRunner.createTerminalRunner(myProject);
|
||||
|
||||
@@ -91,15 +89,13 @@ public class TerminalView {
|
||||
if (window != null) {
|
||||
boolean visible = window.isVisible();
|
||||
if (visible) {
|
||||
VirtualFile selectedFile = window.getSelectedFile().orElse(null);
|
||||
if (toolWindow.getContentManager().getContentCount() == 0) {
|
||||
initTerminal(window);
|
||||
myPreviousSelectedFile = selectedFile;
|
||||
}
|
||||
else if (!Objects.equals(myPreviousSelectedFile, selectedFile)) {
|
||||
terminalRunner.openSessionForFile(myTerminalWidget, selectedFile);
|
||||
myPreviousSelectedFile = selectedFile;
|
||||
else if (myFileToOpen != null) {
|
||||
terminalRunner.openSessionForFile(myTerminalWidget, myFileToOpen);
|
||||
}
|
||||
myFileToOpen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +126,6 @@ public class TerminalView {
|
||||
final Content content = ContentFactory.SERVICE.getInstance().createContent(panel, "", false);
|
||||
content.setCloseable(true);
|
||||
|
||||
myPreviousSelectedFile = toolWindow.getSelectedFile().orElse(null);
|
||||
myTerminalWidget = terminalRunner.createTerminalWidget(content);
|
||||
myTerminalWidget.addTabListener(new TabbedTerminalWidget.TabListener() {
|
||||
@Override
|
||||
@@ -178,6 +173,15 @@ public class TerminalView {
|
||||
return myTerminalWidget != null ? myTerminalWidget.getComponent() : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public VirtualFile getFileToOpen() {
|
||||
return myFileToOpen;
|
||||
}
|
||||
|
||||
public void setFileToOpen(@Nullable VirtualFile fileToOpen) {
|
||||
myFileToOpen = fileToOpen;
|
||||
}
|
||||
|
||||
public void openLocalSession(Project project, ToolWindow terminal) {
|
||||
LocalTerminalDirectRunner terminalRunner = LocalTerminalDirectRunner.createTerminalRunner(project);
|
||||
openSession(terminal, terminalRunner);
|
||||
|
||||
+13
-8
@@ -23,33 +23,38 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.ToolWindow;
|
||||
import com.intellij.openapi.wm.ToolWindowManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory;
|
||||
import org.jetbrains.plugins.terminal.TerminalView;
|
||||
|
||||
/**
|
||||
* An action that activates the terminal window for file, selected by user.
|
||||
*
|
||||
* @see <a href="https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FIDEA-149976">Corresponding ticket</a>
|
||||
*/
|
||||
public class RevealFileInTerminalAction extends DumbAwareAction {
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
e.getPresentation().setEnabledAndVisible(getSelectedFile(e) != null);
|
||||
Project project = getEventProject(e);
|
||||
e.getPresentation().setEnabledAndVisible(project != null && getSelectedFile(e) != null);
|
||||
}
|
||||
|
||||
private static VirtualFile getSelectedFile(AnActionEvent e) {
|
||||
return ShowFilePathAction.findLocalFile(CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext()));
|
||||
@Nullable
|
||||
private static VirtualFile getSelectedFile(@NotNull AnActionEvent e) {
|
||||
return ShowFilePathAction.findLocalFile(e.getData(CommonDataKeys.VIRTUAL_FILE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
Project project = getEventProject(e);
|
||||
if (project == null) {
|
||||
VirtualFile selectedFile = getSelectedFile(e);
|
||||
if (project == null || selectedFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
|
||||
ToolWindow window = ToolWindowManager.getInstance(project).getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
|
||||
if (window != null && window.isAvailable()) {
|
||||
window.activate(null, getSelectedFile(e));
|
||||
TerminalView.getInstance(project).setFileToOpen(selectedFile);
|
||||
window.activate(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user