@@ -10,48 +10,50 @@ interface RenameResponse {
1010 results : [ { diff : string } ] ;
1111}
1212
13- let pythonOutputChannel : vscode . OutputChannel ;
14- let extensionContext : vscode . ExtensionContext ;
15-
1613export function activateSimplePythonRefactorProvider ( context : vscode . ExtensionContext , outputChannel : vscode . OutputChannel ) {
17- pythonOutputChannel = outputChannel ;
18- extensionContext = context ;
1914 let disposable = vscode . commands . registerCommand ( 'python.refactorExtractVariable' , ( ) => {
2015 extractVariable ( context . extensionPath ,
2116 vscode . window . activeTextEditor ,
22- vscode . window . activeTextEditor . selection ) ;
17+ vscode . window . activeTextEditor . selection ,
18+ outputChannel ) ;
2319 } ) ;
2420 context . subscriptions . push ( disposable ) ;
2521
2622 disposable = vscode . commands . registerCommand ( 'python.refactorExtractMethod' , ( ) => {
2723 extractMethod ( context . extensionPath ,
2824 vscode . window . activeTextEditor ,
29- vscode . window . activeTextEditor . selection ) ;
25+ vscode . window . activeTextEditor . selection ,
26+ outputChannel ) ;
3027 } ) ;
3128 context . subscriptions . push ( disposable ) ;
3229}
3330
34- function extractVariable ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range ) : Promise < any > {
31+ // Exported for unit testing
32+ export function extractVariable ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range ,
33+ outputChannel : vscode . OutputChannel , workspaceRoot : string = vscode . workspace . rootPath , renameAfterExtration : boolean = true ) : Promise < any > {
3534 let newName = 'newvariable' + new Date ( ) . getMilliseconds ( ) . toString ( ) ;
36- let proxy = new RefactorProxy ( extensionContext ) ;
35+ let proxy = new RefactorProxy ( extensionDir , workspaceRoot ) ;
3736 let rename = proxy . extractVariable < RenameResponse > ( textEditor . document , newName , textEditor . document . uri . fsPath , range ) . then ( response => {
3837 return response . results [ 0 ] . diff ;
3938 } ) ;
4039
41- return extractName ( extensionDir , textEditor , range , newName , rename ) ;
40+ return extractName ( extensionDir , textEditor , range , newName , rename , outputChannel , renameAfterExtration ) ;
4241}
4342
44- function extractMethod ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range ) : Promise < any > {
43+ // Exported for unit testing
44+ export function extractMethod ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range ,
45+ outputChannel : vscode . OutputChannel , workspaceRoot : string = vscode . workspace . rootPath , renameAfterExtration : boolean = true ) : Promise < any > {
4546 let newName = 'newmethod' + new Date ( ) . getMilliseconds ( ) . toString ( ) ;
46- let proxy = new RefactorProxy ( extensionContext ) ;
47+ let proxy = new RefactorProxy ( extensionDir , workspaceRoot ) ;
4748 let rename = proxy . extractMethod < RenameResponse > ( textEditor . document , newName , textEditor . document . uri . fsPath , range ) . then ( response => {
4849 return response . results [ 0 ] . diff ;
4950 } ) ;
5051
51- return extractName ( extensionDir , textEditor , range , newName , rename ) ;
52+ return extractName ( extensionDir , textEditor , range , newName , rename , outputChannel , renameAfterExtration ) ;
5253}
5354
54- function extractName ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range , newName : string , renameResponse : Promise < string > ) : Promise < any > {
55+ function extractName ( extensionDir : string , textEditor : vscode . TextEditor , range : vscode . Range , newName : string ,
56+ renameResponse : Promise < string > , outputChannel : vscode . OutputChannel , renameAfterExtration : boolean = true ) : Promise < any > {
5557 let changeStartsAtLine = - 1 ;
5658 return renameResponse . then ( diff => {
5759 if ( diff . length === 0 ) {
@@ -69,7 +71,7 @@ function extractName(extensionDir: string, textEditor: vscode.TextEditor, range:
6971 } ) ;
7072 } ) ;
7173 } ) . then ( done => {
72- if ( done && changeStartsAtLine >= 0 ) {
74+ if ( done && changeStartsAtLine >= 0 && renameAfterExtration ) {
7375 let newWordPosition : vscode . Position ;
7476 for ( let lineNumber = changeStartsAtLine ; lineNumber < textEditor . document . lineCount ; lineNumber ++ ) {
7577 let line = textEditor . document . lineAt ( lineNumber ) ;
@@ -96,8 +98,8 @@ function extractName(extensionDir: string, textEditor: vscode.TextEditor, range:
9698 if ( typeof error === 'object' && error . message ) {
9799 errorMessage = `Refactor failed, ${ error . message } ` ;
98100 }
99- pythonOutputChannel . appendLine ( '#' . repeat ( 10 ) + 'Refactor Output' + '#' . repeat ( 10 ) ) ;
100- pythonOutputChannel . appendLine ( 'Error in refactoring:\n' + errorMessage ) ;
101+ outputChannel . appendLine ( '#' . repeat ( 10 ) + 'Refactor Output' + '#' . repeat ( 10 ) ) ;
102+ outputChannel . appendLine ( 'Error in refactoring:\n' + errorMessage ) ;
101103 console . error ( error ) ;
102104 vscode . window . showErrorMessage ( errorMessage ) ;
103105 } ) ;
0 commit comments