1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-03 22:35:43 +02:00

Bug 453696: Save CWD in terminal to restore it

Change-Id: I6a432fcbd40f6b4e991e49ba582b8b9f7cae823e
This commit is contained in:
Jonah Graham 2021-05-19 12:13:46 -04:00
parent fe2dfb7dd2
commit a7e14187ea
2 changed files with 62 additions and 41 deletions

View file

@ -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

View file

@ -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<String> 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