@@ -68,9 +68,11 @@ export const GetEditorContextRequestType =
68
68
export interface IGetEditorContextRequestArguments {
69
69
}
70
70
71
+ // NOTE: The server at least now expects this response, but it's not used in any
72
+ // way. In the future we could actually communicate an error to the user.
71
73
enum EditorOperationResponse {
72
- Unsupported = 0 ,
73
74
Completed ,
75
+ Failed
74
76
}
75
77
76
78
export const InsertTextRequestType =
@@ -148,6 +150,7 @@ interface IInvokeRegisteredEditorCommandParameter {
148
150
export class ExtensionCommandsFeature extends LanguageClientConsumer {
149
151
private commands : vscode . Disposable [ ] ;
150
152
private handlers : vscode . Disposable [ ] = [ ] ;
153
+ private statusBarMessages : vscode . Disposable [ ] = [ ] ;
151
154
private extensionCommands : IExtensionCommand [ ] = [ ] ;
152
155
153
156
constructor ( private logger : ILogger ) {
@@ -216,9 +219,7 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
216
219
217
220
this . languageClient . onRequest (
218
221
NewFileRequestType ,
219
- // NOTE: The VS Code API does not support naming a file as it's
220
- // opened, only when it's saved. Hence the argument is not used.
221
- ( _filePath ) => this . newFile ( ) ) ,
222
+ ( _content ) => this . newFile ( _content ) ) ,
222
223
223
224
this . languageClient . onRequest (
224
225
OpenFileRequestType ,
@@ -267,6 +268,9 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
267
268
for ( const handler of this . handlers ) {
268
269
handler . dispose ( ) ;
269
270
}
271
+ for ( const statusBarMessage of this . statusBarMessages ) {
272
+ statusBarMessage . dispose ( ) ;
273
+ }
270
274
}
271
275
272
276
private addExtensionCommand ( command : IExtensionCommandAddedNotificationBody ) : void {
@@ -355,27 +359,35 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
355
359
} ;
356
360
}
357
361
358
- private async newFile ( ) : Promise < EditorOperationResponse > {
359
- const doc = await vscode . workspace . openTextDocument ( { content : "" } ) ;
362
+ private async newFile ( content : string ) : Promise < EditorOperationResponse > {
363
+ const doc = await vscode . workspace . openTextDocument (
364
+ { language : "powershell" , content : content } ) ;
360
365
await vscode . window . showTextDocument ( doc ) ;
361
366
return EditorOperationResponse . Completed ;
362
367
}
363
368
364
369
private async openFile ( openFileDetails : IOpenFileDetails ) : Promise < EditorOperationResponse > {
365
370
const filePath = await this . resolveFilePathWithCwd ( openFileDetails . filePath ) ;
366
- const doc = await vscode . workspace . openTextDocument ( filePath ) ;
367
- await vscode . window . showTextDocument ( doc , { preview : openFileDetails . preview } ) ;
371
+ try {
372
+ const doc = await vscode . workspace . openTextDocument ( filePath ) ;
373
+ await vscode . window . showTextDocument ( doc , { preview : openFileDetails . preview } ) ;
374
+ } catch {
375
+ void this . logger . writeAndShowWarning ( `File to open not found: ${ filePath } ` ) ;
376
+ return EditorOperationResponse . Failed ;
377
+ }
368
378
return EditorOperationResponse . Completed ;
369
379
}
370
380
371
381
private async closeFile ( filePath : string ) : Promise < EditorOperationResponse > {
372
382
filePath = await this . resolveFilePathWithCwd ( filePath ) ;
373
- const doc = vscode . workspace . textDocuments . find ( ( x ) => x . fileName === filePath ) ;
383
+ const doc = vscode . workspace . textDocuments . find ( ( x ) => x . uri . fsPath === filePath ) ;
374
384
if ( doc != undefined && ! doc . isClosed ) {
375
385
await vscode . window . showTextDocument ( doc ) ;
376
386
await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
387
+ return EditorOperationResponse . Completed ;
377
388
}
378
- return EditorOperationResponse . Completed ;
389
+ void this . logger . writeAndShowWarning ( `File to close not found or already closed: ${ filePath } ` ) ;
390
+ return EditorOperationResponse . Failed ;
379
391
}
380
392
381
393
/**
@@ -388,17 +400,17 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
388
400
if ( saveFileDetails . filePath . startsWith ( "untitled" ) || saveFileDetails . filePath . startsWith ( "file" ) ) {
389
401
currentFileUri = vscode . Uri . parse ( saveFileDetails . filePath ) ;
390
402
} else {
391
- currentFileUri = vscode . Uri . file ( saveFileDetails . filePath ) ;
403
+ const filePath = await this . resolveFilePathWithCwd ( saveFileDetails . filePath ) ;
404
+ currentFileUri = vscode . Uri . file ( filePath ) ;
392
405
}
393
406
394
- // If the file to save can't be found, just complete the request
395
- const doc = vscode . workspace . textDocuments . find ( ( x ) => x . uri === currentFileUri ) ;
407
+ const doc = vscode . workspace . textDocuments . find ( ( x ) => x . uri . fsPath === currentFileUri . fsPath ) ;
396
408
if ( doc === undefined ) {
397
- void this . logger . writeAndShowError ( `File to save not found: ${ currentFileUri . fsPath } ` ) ;
398
- return EditorOperationResponse . Completed ;
409
+ void this . logger . writeAndShowWarning ( `File to save not found: ${ currentFileUri . fsPath } ` ) ;
410
+ return EditorOperationResponse . Failed ;
399
411
}
400
412
401
- let newFilePath = saveFileDetails . newPath ;
413
+ let newFilePath = saveFileDetails . newPath ?? undefined ; // Otherwise it's null.
402
414
if ( currentFileUri . scheme === "file" ) {
403
415
// If no newFile is given, just save the current file
404
416
if ( newFilePath === undefined ) {
@@ -416,31 +428,29 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
416
428
} else if ( currentFileUri . scheme === "untitled" ) {
417
429
// We need a new name to save an untitled file
418
430
if ( newFilePath === undefined ) {
419
- void this . logger . writeAndShowError ( "Cannot save untitled file! Try SaveAs(\"path/to/file.ps1\") instead." ) ;
420
- return EditorOperationResponse . Completed ;
431
+ void this . logger . writeAndShowWarning ( "Cannot save untitled file! Try SaveAs(\"path/to/file.ps1\") instead." ) ;
432
+ return EditorOperationResponse . Failed ;
421
433
}
422
434
423
435
newFilePath = await this . resolveFilePathWithCwd ( newFilePath ) ;
424
436
} else {
425
437
// Other URI schemes are not supported
426
438
const msg = JSON . stringify ( saveFileDetails , undefined , 2 ) ;
427
- void this . logger . writeAndShowError (
439
+ void this . logger . writeAndShowWarning (
428
440
`<${ ExtensionCommandsFeature . name } >: Saving a document with scheme '${ currentFileUri . scheme } ' ` +
429
441
`is currently unsupported. Message: '${ msg } '` ) ;
430
- return EditorOperationResponse . Completed ;
442
+ return EditorOperationResponse . Failed ;
431
443
}
432
444
433
- await this . saveFileAs ( doc , newFilePath ) ;
434
- return EditorOperationResponse . Completed ;
435
-
445
+ return await this . saveFileAs ( doc , newFilePath ) ;
436
446
}
437
447
438
448
/**
439
449
* Take a document available to vscode at the given URI and save it to the given absolute path
440
450
* @param documentUri the URI of the vscode document to save
441
451
* @param filePath the absolute path to save the document contents to
442
452
*/
443
- private async saveFileAs ( doc : vscode . TextDocument , filePath : string ) : Promise < void > {
453
+ private async saveFileAs ( doc : vscode . TextDocument , filePath : string ) : Promise < EditorOperationResponse > {
444
454
// Write the old document's contents to the new document path
445
455
const newFileUri = vscode . Uri . file ( filePath ) ;
446
456
try {
@@ -450,12 +460,13 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
450
460
} catch ( err ) {
451
461
void this . logger . writeAndShowWarning ( `<${ ExtensionCommandsFeature . name } >: ` +
452
462
`Unable to save file to path '${ filePath } ': ${ err } ` ) ;
453
- return ;
463
+ return EditorOperationResponse . Failed ;
454
464
}
455
465
456
466
// Finally open the new document
457
467
const newFile = await vscode . workspace . openTextDocument ( newFileUri ) ;
458
468
await vscode . window . showTextDocument ( newFile , { preview : true } ) ;
469
+ return EditorOperationResponse . Completed ;
459
470
}
460
471
461
472
// Resolve file path against user's CWD setting
@@ -474,9 +485,9 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
474
485
asCodePosition ( details . selectionRange . start ) ! ,
475
486
asCodePosition ( details . selectionRange . end ) ! ) ,
476
487
] ;
488
+ return EditorOperationResponse . Completed ;
477
489
}
478
-
479
- return EditorOperationResponse . Completed ;
490
+ return EditorOperationResponse . Failed ;
480
491
}
481
492
482
493
private showInformationMessage ( message : string ) : EditorOperationResponse {
@@ -496,11 +507,12 @@ export class ExtensionCommandsFeature extends LanguageClientConsumer {
496
507
497
508
private setStatusBarMessage ( messageDetails : IStatusBarMessageDetails ) : EditorOperationResponse {
498
509
if ( messageDetails . timeout ) {
499
- vscode . window . setStatusBarMessage ( messageDetails . message , messageDetails . timeout ) ;
510
+ this . statusBarMessages . push (
511
+ vscode . window . setStatusBarMessage ( messageDetails . message , messageDetails . timeout ) ) ;
500
512
} else {
501
- vscode . window . setStatusBarMessage ( messageDetails . message ) ;
513
+ this . statusBarMessages . push (
514
+ vscode . window . setStatusBarMessage ( messageDetails . message ) ) ;
502
515
}
503
-
504
516
return EditorOperationResponse . Completed ;
505
517
}
506
518
}
0 commit comments