11import { Diff , diff_match_patch } from 'diff-match-patch' ;
2- import * as fs from 'fs-extra' ;
32import { injectable } from 'inversify' ;
43import * as md5 from 'md5' ;
54import { EOL } from 'os' ;
65import * as path from 'path' ;
76import { Position , Range , TextDocument , TextEdit , Uri , WorkspaceEdit } from 'vscode' ;
7+ import { IFileSystem } from './platform/types' ;
88import { IEditorUtils } from './types' ;
99
1010// Code borrowed from goFormat.ts (Go Extension for VS Code)
@@ -80,7 +80,11 @@ export function getTextEditsFromPatch(before: string, patch: string): TextEdit[]
8080
8181 return textEdits ;
8282}
83- export function getWorkspaceEditsFromPatch ( filePatches : string [ ] , workspaceRoot ?: string ) : WorkspaceEdit {
83+ export function getWorkspaceEditsFromPatch (
84+ filePatches : string [ ] ,
85+ fs : IFileSystem ,
86+ workspaceRoot ?: string
87+ ) : WorkspaceEdit {
8488 const workspaceEdit = new WorkspaceEdit ( ) ;
8589 filePatches . forEach ( patch => {
8690 const indexOfAtAt = patch . indexOf ( '@@' ) ;
@@ -107,7 +111,7 @@ export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot?
107111
108112 let fileName = fileNameLines [ 0 ] . substring ( fileNameLines [ 0 ] . indexOf ( ' a' ) + 3 ) . trim ( ) ;
109113 fileName = workspaceRoot && ! path . isAbsolute ( fileName ) ? path . resolve ( workspaceRoot , fileName ) : fileName ;
110- if ( ! fs . existsSync ( fileName ) ) {
114+ if ( ! fs . fileExistsSync ( fileName ) ) {
111115 return ;
112116 }
113117
@@ -123,7 +127,7 @@ export function getWorkspaceEditsFromPatch(filePatches: string[], workspaceRoot?
123127 throw new Error ( 'Unable to parse Patch string' ) ;
124128 }
125129
126- const fileSource = fs . readFileSync ( fileName ) . toString ( 'utf8' ) ;
130+ const fileSource = fs . readFileSync ( fileName ) ;
127131 const fileUri = Uri . file ( fileName ) ;
128132
129133 // Add line feeds and build the text edits
@@ -226,24 +230,25 @@ function getTextEditsInternal(before: string, diffs: [number, string][], startLi
226230 return edits ;
227231}
228232
229- export function getTempFileWithDocumentContents ( document : TextDocument ) : Promise < string > {
230- return new Promise < string > ( ( resolve , reject ) => {
231- const ext = path . extname ( document . uri . fsPath ) ;
232- // Don't create file in temp folder since external utilities
233- // look into configuration files in the workspace and are not able
234- // to find custom rules if file is saved in a random disk location.
235- // This means temp file has to be created in the same folder
236- // as the original one and then removed.
237-
238- // tslint:disable-next-line:no-require-imports
239- const fileName = `${ document . uri . fsPath } .${ md5 ( document . uri . fsPath ) } ${ ext } ` ;
240- fs . writeFile ( fileName , document . getText ( ) , ex => {
241- if ( ex ) {
242- reject ( `Failed to create a temporary file, ${ ex . message } ` ) ;
243- }
244- resolve ( fileName ) ;
245- } ) ;
246- } ) ;
233+ export async function getTempFileWithDocumentContents (
234+ document : TextDocument ,
235+ fs : IFileSystem
236+ ) : Promise < string > {
237+ // Don't create file in temp folder since external utilities
238+ // look into configuration files in the workspace and are not able
239+ // to find custom rules if file is saved in a random disk location.
240+ // This means temp file has to be created in the same folder
241+ // as the original one and then removed.
242+
243+ const ext = path . extname ( document . uri . fsPath ) ;
244+ const filename = `${ document . uri . fsPath } .${ md5 ( document . uri . fsPath ) } ${ ext } ` ;
245+ await (
246+ fs . writeFile ( filename , document . getText ( ) )
247+ . catch ( err => {
248+ throw Error ( `Failed to create a temporary file, ${ err . message } ` ) ;
249+ } )
250+ ) ;
251+ return filename ;
247252}
248253
249254/**
0 commit comments