@@ -10,7 +10,7 @@ import {
1010} from "vscode-languageclient" ;
1111import { LanguageClient } from "vscode-languageclient/node" ;
1212import { Logger } from "../logging" ;
13- import { getSettings } from "../settings" ;
13+ import { getSettings , validateCwdSetting } from "../settings" ;
1414import { LanguageClientConsumer } from "../languageClientConsumer" ;
1515
1616export interface IExtensionCommand {
@@ -368,32 +368,20 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
368368 return EditorOperationResponse . Completed ;
369369 }
370370
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 ;
382376 }
383377
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" ) ;
394383 }
395-
396- return promise ;
384+ return EditorOperationResponse . Completed ;
397385 }
398386
399387 /**
@@ -413,7 +401,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
413401 switch ( currentFileUri . scheme ) {
414402 case "file" : {
415403 // 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 ) ) ) {
417405 void this . logger . writeAndShowError ( `File to save not found: ${ currentFileUri . fsPath } .` ) ;
418406 return EditorOperationResponse . Completed ;
419407 }
@@ -449,23 +437,8 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
449437 if ( path . isAbsolute ( saveFileDetails . newPath ) ) {
450438 newFileAbsolutePath = saveFileDetails . newPath ;
451439 } 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 ) ;
469442 }
470443 break ; }
471444
@@ -511,24 +484,21 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
511484 await vscode . window . showTextDocument ( newFile , { preview : true } ) ;
512485 }
513486
514- private normalizeFilePath ( filePath : string ) : string {
487+ private async normalizeFilePath ( filePath : string ) : Promise < string > {
488+ const cwd = await validateCwdSetting ( this . logger ) ;
515489 const platform = os . platform ( ) ;
516490 if ( platform === "win32" ) {
517491 // Make sure the file path is absolute
518492 if ( ! path . win32 . isAbsolute ( filePath ) ) {
519- filePath = path . win32 . resolve (
520- vscode . workspace . rootPath ! ,
521- filePath ) ;
493+ filePath = path . win32 . resolve ( cwd , filePath ) ;
522494 }
523495
524496 // Normalize file path case for comparison for Windows
525497 return filePath . toLowerCase ( ) ;
526498 } else {
527499 // Make sure the file path is absolute
528500 if ( ! path . isAbsolute ( filePath ) ) {
529- filePath = path . resolve (
530- vscode . workspace . rootPath ! ,
531- filePath ) ;
501+ filePath = path . resolve ( cwd , filePath ) ;
532502 }
533503
534504 // macOS is case-insensitive
0 commit comments