@@ -4,57 +4,38 @@ import * as vscode from "vscode";
44import * as path from "path" ;
55import * as fs from "fs" ;
66import * as child_process from "child_process" ;
7+ import { getTextEditsFromPatch } from "../common/editor" ;
78
89export class PythonImportSortProvider {
9- public sortImports ( extensionDir : string , document : vscode . TextDocument ) : Thenable < vscode . TextEdit [ ] > {
10+ public sortImports ( extensionDir : string , document : vscode . TextDocument ) : Promise < vscode . TextEdit [ ] > {
11+ if ( document . lineCount === 1 ) {
12+ return Promise . resolve ( [ ] ) ;
13+ }
14+ let filePath = document . uri . fsPath ;
15+ let importScript = path . join ( extensionDir , "pythonFiles" , "sortImports.py" ) ;
1016 return new Promise < vscode . TextEdit [ ] > ( ( resolve , reject ) => {
11- let filePath = document . uri . fsPath ;
12- let importScript = path . join ( extensionDir , "pythonFiles" , "sortImports.py" ) ;
13- if ( ! fs . existsSync ( filePath ) ) {
14- vscode . window . showErrorMessage ( `File ${ filePath } does not exist` ) ;
15- return resolve ( [ ] ) ;
16- }
17-
1817 let ext = path . extname ( filePath ) ;
1918 let tmp = require ( "tmp" ) ;
20- tmp . file ( { postfix : ext } , function _tempFileCreated ( err , tmpFilePath , fd ) {
19+ tmp . file ( { postfix : ext } , function ( err , tmpFilePath , fd ) {
2120 if ( err ) {
22- reject ( err ) ;
23- return ;
21+ return reject ( err ) ;
2422 }
25- let documentText = document . getText ( ) ;
26- fs . writeFile ( tmpFilePath , documentText , ex => {
23+ fs . writeFile ( tmpFilePath , document . getText ( ) , ex => {
2724 if ( ex ) {
28- vscode . window . showErrorMessage ( `Failed to create a temporary file, ${ ex . message } ` ) ;
29- return ;
25+ return reject ( `Failed to create a temporary file, ${ ex . message } ` ) ;
3026 }
3127
32- child_process . exec ( `python "${ importScript } " "${ tmpFilePath } "` , ( error , stdout , stderr ) => {
33- if ( error || stderr ) {
34- vscode . window . showErrorMessage ( `File ${ filePath } does not exist` ) ;
35- return resolve ( [ ] ) ;
28+ child_process . exec ( `python "${ importScript } " "${ tmpFilePath } " --diff` , ( error , stdout , stderr ) => {
29+ if ( error || ( stderr && stderr . length > 0 ) ) {
30+ return reject ( error ? error : stderr ) ;
3631 }
3732
38- fs . readFile ( tmpFilePath , ( ex , data ) => {
39- if ( ex ) {
40- vscode . window . showErrorMessage ( `Failed to create a temporary file for sorting, ${ ex . message } ` ) ;
41- return ;
42- }
43-
44- let formattedText = data . toString ( "utf-8" ) ;
45- // Nothing to do
46- if ( document . getText ( ) === formattedText ) {
47- return resolve ( [ ] ) ;
48- }
49-
50- let range = new vscode . Range ( document . lineAt ( 0 ) . range . start , document . lineAt ( document . lineCount - 1 ) . range . end ) ;
51- let txtEdit = new vscode . TextEdit ( range , formattedText ) ;
52- resolve ( [ txtEdit ] ) ;
53- } ) ;
33+ let formattedText = stdout ;
34+ let edits = getTextEditsFromPatch ( document . getText ( ) , stdout ) ;
35+ resolve ( edits ) ;
5436 } ) ;
5537 } ) ;
5638 } ) ;
57-
5839 } ) ;
5940 }
60- }
41+ }
0 commit comments