@@ -4,6 +4,7 @@ import * as vscode from 'vscode';
44import { OutputChannel , TextEdit , Uri } from 'vscode' ;
55import { IWorkspaceService } from '../common/application/types' ;
66import { STANDARD_OUTPUT_CHANNEL } from '../common/constants' ;
7+ import '../common/extensions' ;
78import { isNotInstalledError } from '../common/helpers' ;
89import { IPythonToolExecutionService } from '../common/process/types' ;
910import { IInstaller , IOutputChannel , Product } from '../common/types' ;
@@ -50,37 +51,32 @@ export abstract class BaseFormatter {
5051 // However they don't support returning the diff of the formatted text when reading data from the input stream.
5152 // Yes getting text formatted that way avoids having to create a temporary file, however the diffing will have
5253 // to be done here in node (extension), i.e. extension cpu, i.e. les responsive solution.
53- const tmpFileCreated = document . isDirty ;
54- const filePromise = tmpFileCreated ? getTempFileWithDocumentContents ( document ) : Promise . resolve ( document . fileName ) ;
55- const filePath = await filePromise ;
56- if ( token && token . isCancellationRequested ) {
54+ const tempFile = await this . createTempFile ( document ) ;
55+ if ( this . checkCancellation ( document . fileName , tempFile , token ) ) {
5756 return [ ] ;
5857 }
5958
6059 const executionInfo = this . helper . getExecutionInfo ( this . product , args , document . uri ) ;
61- executionInfo . args . push ( filePath ) ;
60+ executionInfo . args . push ( tempFile ) ;
6261 const pythonToolsExecutionService = this . serviceContainer . get < IPythonToolExecutionService > ( IPythonToolExecutionService ) ;
6362 const promise = pythonToolsExecutionService . exec ( executionInfo , { cwd, throwOnStdErr : true , token } , document . uri )
6463 . then ( output => output . stdout )
6564 . then ( data => {
66- if ( token && token . isCancellationRequested ) {
65+ if ( this . checkCancellation ( document . fileName , tempFile , token ) ) {
6766 return [ ] as TextEdit [ ] ;
6867 }
6968 return getTextEditsFromPatch ( document . getText ( ) , data ) ;
7069 } )
7170 . catch ( error => {
72- if ( token && token . isCancellationRequested ) {
71+ if ( this . checkCancellation ( document . fileName , tempFile , token ) ) {
7372 return [ ] as TextEdit [ ] ;
7473 }
7574 // tslint:disable-next-line:no-empty
7675 this . handleError ( this . Id , error , document . uri ) . catch ( ( ) => { } ) ;
7776 return [ ] as TextEdit [ ] ;
7877 } )
7978 . then ( edits => {
80- // Delete the temporary file created
81- if ( tmpFileCreated ) {
82- fs . unlinkSync ( filePath ) ;
83- }
79+ this . deleteTempFile ( document . fileName , tempFile ) . ignoreErrors ( ) ;
8480 return edits ;
8581 } ) ;
8682 vscode . window . setStatusBarMessage ( `Formatting with ${ this . Id } ` , promise ) ;
@@ -101,4 +97,25 @@ export abstract class BaseFormatter {
10197
10298 this . outputChannel . appendLine ( `\n${ customError } \n${ error } ` ) ;
10399 }
100+
101+ private async createTempFile ( document : vscode . TextDocument ) : Promise < string > {
102+ return document . isDirty
103+ ? await getTempFileWithDocumentContents ( document )
104+ : document . fileName ;
105+ }
106+
107+ private deleteTempFile ( originalFile : string , tempFile : string ) : Promise < void > {
108+ if ( originalFile !== tempFile ) {
109+ return fs . unlink ( tempFile ) ;
110+ }
111+ return Promise . resolve ( ) ;
112+ }
113+
114+ private checkCancellation ( originalFile : string , tempFile : string , token ?: vscode . CancellationToken ) : boolean {
115+ if ( token && token . isCancellationRequested ) {
116+ this . deleteTempFile ( originalFile , tempFile ) . ignoreErrors ( ) ;
117+ return true ;
118+ }
119+ return false ;
120+ }
104121}
0 commit comments