@@ -10,7 +10,7 @@ import {
10
10
} from "vscode-languageclient" ;
11
11
import { LanguageClient } from "vscode-languageclient/node" ;
12
12
import { Logger } from "../logging" ;
13
- import { getSettings } from "../settings" ;
13
+ import { getSettings , validateCwdSetting } from "../settings" ;
14
14
import { LanguageClientConsumer } from "../languageClientConsumer" ;
15
15
16
16
export interface IExtensionCommand {
@@ -368,32 +368,20 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
368
368
return EditorOperationResponse . Completed ;
369
369
}
370
370
371
- private openFile ( openFileDetails : IOpenFileDetails ) : Thenable < EditorOperationResponse > {
372
- const filePath = this . normalizeFilePath ( openFileDetails . filePath ) ;
373
-
374
- const promise =
375
- vscode . workspace . openTextDocument ( filePath )
376
- . then ( ( doc ) => vscode . window . showTextDocument (
377
- doc ,
378
- { preview : openFileDetails . preview } ) )
379
- . then ( ( _ ) => EditorOperationResponse . Completed ) ;
380
-
381
- return promise ;
371
+ private async openFile ( openFileDetails : IOpenFileDetails ) : Promise < EditorOperationResponse > {
372
+ const filePath = await this . normalizeFilePath ( openFileDetails . filePath ) ;
373
+ const doc = await vscode . workspace . openTextDocument ( filePath ) ;
374
+ await vscode . window . showTextDocument ( doc , { preview : openFileDetails . preview } ) ;
375
+ return EditorOperationResponse . Completed ;
382
376
}
383
377
384
- private closeFile ( filePath : string ) : Thenable < EditorOperationResponse > {
385
- let promise : Thenable < EditorOperationResponse > ;
386
- if ( this . findTextDocument ( this . normalizeFilePath ( filePath ) ) ) {
387
- promise =
388
- vscode . workspace . openTextDocument ( filePath )
389
- . then ( ( doc ) => vscode . window . showTextDocument ( doc ) )
390
- . then ( ( _ ) => vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) )
391
- . then ( ( _ ) => EditorOperationResponse . Completed ) ;
392
- } else {
393
- promise = Promise . resolve ( EditorOperationResponse . Completed ) ;
378
+ private async closeFile ( filePath : string ) : Promise < EditorOperationResponse > {
379
+ if ( this . findTextDocument ( await this . normalizeFilePath ( filePath ) ) ) {
380
+ const doc = await vscode . workspace . openTextDocument ( filePath ) ;
381
+ await vscode . window . showTextDocument ( doc ) ;
382
+ await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
394
383
}
395
-
396
- return promise ;
384
+ return EditorOperationResponse . Completed ;
397
385
}
398
386
399
387
/**
@@ -413,7 +401,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
413
401
switch ( currentFileUri . scheme ) {
414
402
case "file" : {
415
403
// If the file to save can't be found, just complete the request
416
- if ( ! this . findTextDocument ( this . normalizeFilePath ( currentFileUri . fsPath ) ) ) {
404
+ if ( ! this . findTextDocument ( await this . normalizeFilePath ( currentFileUri . fsPath ) ) ) {
417
405
void this . logger . writeAndShowError ( `File to save not found: ${ currentFileUri . fsPath } .` ) ;
418
406
return EditorOperationResponse . Completed ;
419
407
}
@@ -449,23 +437,8 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
449
437
if ( path . isAbsolute ( saveFileDetails . newPath ) ) {
450
438
newFileAbsolutePath = saveFileDetails . newPath ;
451
439
} else {
452
- // In fresh contexts, workspaceFolders is not defined...
453
- if ( ! vscode . workspace . workspaceFolders || vscode . workspace . workspaceFolders . length === 0 ) {
454
- void this . logger . writeAndShowWarning ( "Cannot save file to relative path: no workspaces are open. " +
455
- "Try saving to an absolute path, or open a workspace." ) ;
456
- return EditorOperationResponse . Completed ;
457
- }
458
-
459
- // If not, interpret the path as relative to the workspace root
460
- const workspaceRootUri = vscode . workspace . workspaceFolders [ 0 ] . uri ;
461
- // We don't support saving to a non-file URI-schemed workspace
462
- if ( workspaceRootUri . scheme !== "file" ) {
463
- void this . logger . writeAndShowWarning (
464
- "Cannot save untitled file to a relative path in an untitled workspace. " +
465
- "Try saving to an absolute path or opening a workspace folder." ) ;
466
- return EditorOperationResponse . Completed ;
467
- }
468
- newFileAbsolutePath = path . join ( workspaceRootUri . fsPath , saveFileDetails . newPath ) ;
440
+ const cwd = await validateCwdSetting ( this . logger ) ;
441
+ newFileAbsolutePath = path . join ( cwd , saveFileDetails . newPath ) ;
469
442
}
470
443
break ; }
471
444
@@ -511,24 +484,21 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
511
484
await vscode . window . showTextDocument ( newFile , { preview : true } ) ;
512
485
}
513
486
514
- private normalizeFilePath ( filePath : string ) : string {
487
+ private async normalizeFilePath ( filePath : string ) : Promise < string > {
488
+ const cwd = await validateCwdSetting ( this . logger ) ;
515
489
const platform = os . platform ( ) ;
516
490
if ( platform === "win32" ) {
517
491
// Make sure the file path is absolute
518
492
if ( ! path . win32 . isAbsolute ( filePath ) ) {
519
- filePath = path . win32 . resolve (
520
- vscode . workspace . rootPath ! ,
521
- filePath ) ;
493
+ filePath = path . win32 . resolve ( cwd , filePath ) ;
522
494
}
523
495
524
496
// Normalize file path case for comparison for Windows
525
497
return filePath . toLowerCase ( ) ;
526
498
} else {
527
499
// Make sure the file path is absolute
528
500
if ( ! path . isAbsolute ( filePath ) ) {
529
- filePath = path . resolve (
530
- vscode . workspace . rootPath ! ,
531
- filePath ) ;
501
+ filePath = path . resolve ( cwd , filePath ) ;
532
502
}
533
503
534
504
// macOS is case-insensitive
0 commit comments