@@ -6,6 +6,7 @@ import utils = require("./utils");
6
6
import os = require( "os" ) ;
7
7
import { ILogger } from "./logging" ;
8
8
import untildify from "untildify" ;
9
+ import path = require( "path" ) ;
9
10
10
11
// TODO: Quite a few of these settings are unused in the client and instead
11
12
// exist just for the server. Those settings do not need to be represented in
@@ -214,47 +215,66 @@ export async function changeSetting(
214
215
}
215
216
216
217
// We don't want to query the user more than once, so this is idempotent.
217
- let hasPrompted = false ;
218
- export let chosenWorkspace : vscode . WorkspaceFolder | undefined = undefined ;
219
-
220
- export async function validateCwdSetting ( logger : ILogger | undefined ) : Promise < string > {
221
- let cwd : string | undefined = utils . stripQuotePair (
222
- vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" ) ) ;
223
-
224
- // Only use the cwd setting if it exists.
225
- if ( cwd !== undefined ) {
226
- cwd = untildify ( cwd ) ;
227
- if ( await utils . checkIfDirectoryExists ( cwd ) ) {
228
- return cwd ;
229
- }
218
+ let hasChosen = false ;
219
+ let chosenWorkspace : vscode . WorkspaceFolder | undefined = undefined ;
220
+ export async function getChosenWorkspace ( logger : ILogger | undefined ) : Promise < vscode . WorkspaceFolder | undefined > {
221
+ if ( hasChosen ) {
222
+ return chosenWorkspace ;
230
223
}
231
224
232
225
// If there is no workspace, or there is but it has no folders, fallback.
233
226
if ( vscode . workspace . workspaceFolders === undefined
234
227
|| vscode . workspace . workspaceFolders . length === 0 ) {
235
- cwd = undefined ;
228
+ chosenWorkspace = undefined ;
236
229
// If there is exactly one workspace folder, use that.
237
230
} else if ( vscode . workspace . workspaceFolders . length === 1 ) {
238
- cwd = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
231
+ chosenWorkspace = vscode . workspace . workspaceFolders [ 0 ] ;
239
232
// If there is more than one workspace folder, prompt the user once.
240
- } else if ( vscode . workspace . workspaceFolders . length > 1 && ! hasPrompted ) {
241
- hasPrompted = true ;
233
+ } else if ( vscode . workspace . workspaceFolders . length > 1 ) {
242
234
const options : vscode . WorkspaceFolderPickOptions = {
243
235
placeHolder : "Select a workspace folder to use for the PowerShell Extension." ,
244
236
} ;
237
+
245
238
chosenWorkspace = await vscode . window . showWorkspaceFolderPick ( options ) ;
246
- cwd = chosenWorkspace ?. uri . fsPath ;
247
- // Save the picked 'cwd' to the workspace settings.
248
- // We have to check again because the user may not have picked.
249
- if ( cwd !== undefined && await utils . checkIfDirectoryExists ( cwd ) ) {
250
- await changeSetting ( "cwd" , cwd , undefined , logger ) ;
239
+ logger ?. writeVerbose ( `User selected workspace: '${ chosenWorkspace ?. name } '` ) ;
240
+ }
241
+
242
+ // NOTE: We don't rely on checking if `chosenWorkspace` is undefined because
243
+ // that may be the case if the user dismissed the prompt, and we don't want
244
+ // to show it again this session.
245
+ hasChosen = true ;
246
+ return chosenWorkspace ;
247
+ }
248
+
249
+ export async function validateCwdSetting ( logger : ILogger | undefined ) : Promise < string > {
250
+ let cwd : string | undefined = utils . stripQuotePair (
251
+ vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" ) ) ;
252
+
253
+ // Use the resolved cwd setting if it exists. We're checking truthiness
254
+ // because it could be an empty string, in which case we ignore it.
255
+ if ( cwd ) {
256
+ cwd = path . resolve ( untildify ( cwd ) ) ;
257
+ if ( await utils . checkIfDirectoryExists ( cwd ) ) {
258
+ return cwd ;
251
259
}
252
260
}
253
261
262
+ // Otherwise get a cwd from the workspace, if possible.
263
+ const workspace = await getChosenWorkspace ( logger ) ;
264
+ cwd = workspace ?. uri . fsPath ;
265
+
266
+ // Save the picked 'cwd' to the workspace settings.
267
+ if ( cwd && await utils . checkIfDirectoryExists ( cwd ) ) {
268
+ // TODO: Stop saving this to settings! Instead, save the picked
269
+ // workspace (or save this, but in a cache).
270
+ await changeSetting ( "cwd" , cwd , undefined , logger ) ;
271
+ }
272
+
254
273
// If there were no workspace folders, or somehow they don't exist, use
255
274
// the home directory.
256
- if ( cwd === undefined || ! await utils . checkIfDirectoryExists ( cwd ) ) {
275
+ if ( ! cwd || ! await utils . checkIfDirectoryExists ( cwd ) ) {
257
276
return os . homedir ( ) ;
258
277
}
278
+
259
279
return cwd ;
260
280
}
0 commit comments