From a7e14187eac0dd5f26ccc760555661420d481ebf Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Wed, 19 May 2021 12:13:46 -0400 Subject: [PATCH] Bug 453696: Save CWD in terminal to restore it Change-Id: I6a432fcbd40f6b4e991e49ba582b8b9f7cae823e --- .../local/launcher/LocalLauncherDelegate.java | 85 ++++++++++--------- .../ui/view/TerminalsViewMementoHandler.java | 18 ++++ 2 files changed, 62 insertions(+), 41 deletions(-) diff --git a/terminal/plugins/org.eclipse.tm.terminal.connector.local/src/org/eclipse/tm/terminal/connector/local/launcher/LocalLauncherDelegate.java b/terminal/plugins/org.eclipse.tm.terminal.connector.local/src/org/eclipse/tm/terminal/connector/local/launcher/LocalLauncherDelegate.java index 624e344db5a..a5c775b06b0 100644 --- a/terminal/plugins/org.eclipse.tm.terminal.connector.local/src/org/eclipse/tm/terminal/connector/local/launcher/LocalLauncherDelegate.java +++ b/terminal/plugins/org.eclipse.tm.terminal.connector.local/src/org/eclipse/tm/terminal/connector/local/launcher/LocalLauncherDelegate.java @@ -106,52 +106,55 @@ public class LocalLauncherDelegate extends AbstractLauncherDelegate { } // Initialize the local terminal working directory. - // By default, start the local terminal in the users home directory - String initialCwd = org.eclipse.tm.terminal.view.ui.activator.UIPlugin.getScopedPreferences() - .getString(IPreferenceKeys.PREF_LOCAL_TERMINAL_INITIAL_CWD); - String cwd = null; - if (initialCwd == null || IPreferenceKeys.PREF_INITIAL_CWD_USER_HOME.equals(initialCwd) - || "".equals(initialCwd.trim())) { //$NON-NLS-1$ - cwd = System.getProperty("user.home"); //$NON-NLS-1$ - } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_HOME.equals(initialCwd)) { - String eclipseHomeLocation = System.getProperty("eclipse.home.location"); //$NON-NLS-1$ - if (eclipseHomeLocation != null) { + if (!properties.containsKey(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR)) { + // By default, start the local terminal in the users home directory + String initialCwd = org.eclipse.tm.terminal.view.ui.activator.UIPlugin.getScopedPreferences() + .getString(IPreferenceKeys.PREF_LOCAL_TERMINAL_INITIAL_CWD); + String cwd = null; + if (initialCwd == null || IPreferenceKeys.PREF_INITIAL_CWD_USER_HOME.equals(initialCwd) + || "".equals(initialCwd.trim())) { //$NON-NLS-1$ + cwd = System.getProperty("user.home"); //$NON-NLS-1$ + } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_HOME.equals(initialCwd)) { + String eclipseHomeLocation = System.getProperty("eclipse.home.location"); //$NON-NLS-1$ + if (eclipseHomeLocation != null) { + try { + URI uri = URIUtil.fromString(eclipseHomeLocation); + File f = URIUtil.toFile(uri); + cwd = f.getAbsolutePath(); + } catch (URISyntaxException ex) { + /* ignored on purpose */ } + } + } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_WS.equals(initialCwd)) { + Bundle bundle = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$ + if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) { + if (org.eclipse.core.resources.ResourcesPlugin.getWorkspace() != null + && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot() != null + && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot() + .getLocation() != null) { + cwd = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation() + .toOSString(); + } + } + } else { try { - URI uri = URIUtil.fromString(eclipseHomeLocation); - File f = URIUtil.toFile(uri); - cwd = f.getAbsolutePath(); - } catch (URISyntaxException ex) { - /* ignored on purpose */ } - } - } else if (IPreferenceKeys.PREF_INITIAL_CWD_ECLIPSE_WS.equals(initialCwd)) { - Bundle bundle = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$ - if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) { - if (org.eclipse.core.resources.ResourcesPlugin.getWorkspace() != null - && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot() != null - && org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation() != null) { - cwd = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation() - .toOSString(); - } - } - } else { - try { - // Resolve possible dynamic variables - IStringVariableManager vm = VariablesPlugin.getDefault().getStringVariableManager(); - String resolved = vm.performStringSubstitution(initialCwd); + // Resolve possible dynamic variables + IStringVariableManager vm = VariablesPlugin.getDefault().getStringVariableManager(); + String resolved = vm.performStringSubstitution(initialCwd); - IPath p = new Path(resolved); - if (p.toFile().canRead() && p.toFile().isDirectory()) { - cwd = p.toOSString(); - } - } catch (CoreException ex) { - if (Platform.inDebugMode()) { - UIPlugin.getDefault().getLog().log(ex.getStatus()); + IPath p = new Path(resolved); + if (p.toFile().canRead() && p.toFile().isDirectory()) { + cwd = p.toOSString(); + } + } catch (CoreException ex) { + if (Platform.inDebugMode()) { + UIPlugin.getDefault().getLog().log(ex.getStatus()); + } } } - } - if (cwd != null && !"".equals(cwd)) { //$NON-NLS-1$ - properties.put(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR, cwd); + if (cwd != null && !"".equals(cwd)) { //$NON-NLS-1$ + properties.put(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR, cwd); + } } // If the current selection resolved to an folder, default the working directory diff --git a/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsViewMementoHandler.java b/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsViewMementoHandler.java index 4bf1edac28f..36cbfd4b131 100644 --- a/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsViewMementoHandler.java +++ b/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsViewMementoHandler.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import org.eclipse.core.runtime.Assert; import org.eclipse.swt.custom.CTabItem; @@ -113,6 +114,17 @@ public class TerminalsViewMementoHandler { connectionMemento.putString(ITerminalsConnectorConstants.PROP_ENCODING, encoding); } + // Store the current working directory, or if not available, the initial working directory + if (terminal != null) { + encoding = terminal.getEncoding(); + Optional workingDirectory = terminal.getTerminalConnector().getWorkingDirectory(); + String cwd = workingDirectory + .orElse((String) properties.get(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR)); + if (cwd != null) { + connectionMemento.putString(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR, cwd); + } + } + // Pass on to the memento handler mementoHandler.saveState(connectionMemento, properties); } @@ -164,6 +176,12 @@ public class TerminalsViewMementoHandler { connection.getString(ITerminalsConnectorConstants.PROP_ENCODING)); } + // Restore the working directory + if (connection.getString(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR) != null) { + properties.put(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR, + connection.getString(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR)); + } + // Get the terminal launcher delegate String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID); ILauncherDelegate delegate = delegateId != null