Skip to content

Commit 4473bd3

Browse files
committed
Add multi-root choice experience to powershell.cwd
1 parent 08fd2b7 commit 4473bd3

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/main.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,25 @@ const documentSelector: DocumentSelector = [
5252
{ language: "powershell", scheme: "untitled" },
5353
];
5454

55-
export function activate(context: vscode.ExtensionContext): IPowerShellExtensionClient {
56-
// create telemetry reporter on extension activation
55+
// NOTE: Now that this is async, we can probably improve a lot!
56+
export async function activate(context: vscode.ExtensionContext): Promise<IPowerShellExtensionClient> {
5757
telemetryReporter = new TelemetryReporter(PackageJSON.name, PackageJSON.version, AI_KEY);
5858

59-
// If both extensions are enabled, this will cause unexpected behavior since both register the same commands
59+
// If both extensions are enabled, this will cause unexpected behavior since both register the same commands.
60+
// TODO: Merge extensions and use preview channel in marketplace instead.
6061
if (PackageJSON.name.toLowerCase() === "powershell-preview"
6162
&& vscode.extensions.getExtension("ms-vscode.powershell")) {
6263
vscode.window.showWarningMessage(
6364
"'PowerShell' and 'PowerShell Preview' are both enabled. Please disable one for best performance.");
6465
}
6566

67+
// This displays a popup and a changelog after an update.
6668
checkForUpdatedVersion(context, PackageJSON.version);
6769

70+
// Load and validate settings (will prompt for 'cwd' if necessary).
71+
await Settings.validateCwdSetting();
72+
const extensionSettings = Settings.load();
73+
6874
vscode.languages.setLanguageConfiguration(
6975
PowerShellLanguageId,
7076
{
@@ -118,11 +124,8 @@ export function activate(context: vscode.ExtensionContext): IPowerShellExtension
118124
],
119125
});
120126

121-
// Create the logger
127+
// Setup the logger.
122128
logger = new Logger();
123-
124-
// Set the log level
125-
const extensionSettings = Settings.load();
126129
logger.MinimumLogLevel = LogLevel[extensionSettings.developer.editorServicesLogLevel];
127130

128131
sessionManager =

src/settings.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ export interface INotebooksSettings {
134134
saveMarkdownCellsAs?: CommentType;
135135
}
136136

137+
// TODO: This could probably be async, and call `validateCwdSetting()` directly.
137138
export function load(): ISettings {
138139
const configuration: vscode.WorkspaceConfiguration =
139-
vscode.workspace.getConfiguration(
140-
utils.PowerShellLanguageId);
140+
vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
141141

142142
const defaultBugReportingSettings: IBugReportingSettings = {
143143
project: "https://github.com/PowerShell/vscode-powershell",
@@ -265,17 +265,17 @@ export function load(): ISettings {
265265
// is the reason terminals on macOS typically run login shells by default which set up
266266
// the environment. See http://unix.stackexchange.com/a/119675/115410"
267267
configuration.get<IStartAsLoginShellSettings>("startAsLoginShell", defaultStartAsLoginShellSettings),
268-
cwd: // TODO: Should we resolve this path and/or default to a workspace folder?
269-
configuration.get<string>("cwd", null),
268+
cwd: // NOTE: This must be validated at startup via `validateCwdSetting()`. There's probably a better way to do this.
269+
configuration.get<string>("cwd", undefined),
270270
};
271271
}
272272

273273
// Get the ConfigurationTarget (read: scope) of where the *effective* setting value comes from
274274
export async function getEffectiveConfigurationTarget(settingName: string): Promise<vscode.ConfigurationTarget> {
275275
const configuration = vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
276-
277276
const detail = configuration.inspect(settingName);
278277
let configurationTarget = null;
278+
279279
if (typeof detail.workspaceFolderValue !== "undefined") {
280280
configurationTarget = vscode.ConfigurationTarget.WorkspaceFolder;
281281
}
@@ -294,7 +294,6 @@ export async function change(
294294
configurationTarget?: vscode.ConfigurationTarget | boolean): Promise<void> {
295295

296296
const configuration = vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
297-
298297
await configuration.update(settingName, newValue, configurationTarget);
299298
}
300299

@@ -312,3 +311,30 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
312311
}
313312
return defaultSettings;
314313
}
314+
315+
export async function validateCwdSetting(): Promise<string> {
316+
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", null);
317+
318+
// Only use the cwd setting if it exists.
319+
if (utils.checkIfDirectoryExists(cwd)) {
320+
return cwd;
321+
} else {
322+
// Otherwise use a workspace folder, prompting if necessary.
323+
if (vscode.workspace.workspaceFolders?.length > 1) {
324+
const options: vscode.WorkspaceFolderPickOptions = {
325+
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
326+
}
327+
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
328+
// Save the picked 'cwd' to the workspace settings.
329+
await change("cwd", cwd);
330+
} else {
331+
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
332+
}
333+
// If there were no workspace folders, or somehow they don't exist, use
334+
// the home directory.
335+
if (cwd === undefined || !utils.checkIfDirectoryExists(cwd)) {
336+
return process.cwd();
337+
}
338+
return cwd;
339+
}
340+
}

0 commit comments

Comments
 (0)