|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | import * as vscode from 'vscode'; |
4 | | -import {RefactorProxy} from '../refactor/proxy'; |
5 | | -import {getTextEditsFromPatch} from '../common/editor'; |
6 | | -import {PythonSettings, IPythonSettings} from '../common/configSettings'; |
| 4 | +import { RefactorProxy } from '../refactor/proxy'; |
| 5 | +import { getTextEditsFromPatch } from '../common/editor'; |
| 6 | +import { PythonSettings, IPythonSettings } from '../common/configSettings'; |
7 | 7 |
|
8 | 8 | interface RenameResponse { |
9 | 9 | results: [{ diff: string }]; |
@@ -31,26 +31,49 @@ export function activateSimplePythonRefactorProvider(context: vscode.ExtensionCo |
31 | 31 | export function extractVariable(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range, |
32 | 32 | outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath, |
33 | 33 | pythonSettings: IPythonSettings = PythonSettings.getInstance()): Promise<any> { |
34 | | - let newName = 'newvariable' + new Date().getMilliseconds().toString(); |
35 | | - let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot); |
36 | | - let rename = proxy.extractVariable<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => { |
37 | | - return response.results[0].diff; |
38 | | - }); |
39 | 34 |
|
40 | | - return extractName(extensionDir, textEditor, range, newName, rename, outputChannel); |
| 35 | + return validateDocumentForRefactor(textEditor).then(() => { |
| 36 | + let newName = 'newvariable' + new Date().getMilliseconds().toString(); |
| 37 | + let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot); |
| 38 | + let rename = proxy.extractVariable<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => { |
| 39 | + return response.results[0].diff; |
| 40 | + }); |
| 41 | + |
| 42 | + return extractName(extensionDir, textEditor, range, newName, rename, outputChannel); |
| 43 | + }); |
41 | 44 | } |
42 | 45 |
|
43 | 46 | // Exported for unit testing |
44 | 47 | export function extractMethod(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range, |
45 | 48 | outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath, |
46 | 49 | pythonSettings: IPythonSettings = PythonSettings.getInstance()): Promise<any> { |
47 | | - let newName = 'newmethod' + new Date().getMilliseconds().toString(); |
48 | | - let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot); |
49 | | - let rename = proxy.extractMethod<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => { |
50 | | - return response.results[0].diff; |
| 50 | + |
| 51 | + return validateDocumentForRefactor(textEditor).then(() => { |
| 52 | + let newName = 'newmethod' + new Date().getMilliseconds().toString(); |
| 53 | + let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot); |
| 54 | + let rename = proxy.extractMethod<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => { |
| 55 | + return response.results[0].diff; |
| 56 | + }); |
| 57 | + |
| 58 | + return extractName(extensionDir, textEditor, range, newName, rename, outputChannel); |
51 | 59 | }); |
| 60 | +} |
| 61 | + |
| 62 | +function validateDocumentForRefactor(textEditor: vscode.TextEditor): Promise<any> { |
| 63 | + if (!textEditor.document.isDirty) { |
| 64 | + return Promise.resolve(); |
| 65 | + } |
52 | 66 |
|
53 | | - return extractName(extensionDir, textEditor, range, newName, rename, outputChannel); |
| 67 | + return new Promise<any>((resolve, reject) => { |
| 68 | + vscode.window.showInformationMessage('Please save changes before refactoring', 'Save').then(item => { |
| 69 | + if (item === 'Save') { |
| 70 | + textEditor.document.save().then(resolve, reject); |
| 71 | + } |
| 72 | + else { |
| 73 | + return reject(); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
54 | 77 | } |
55 | 78 |
|
56 | 79 | function extractName(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range, newName: string, |
@@ -90,9 +113,9 @@ function extractName(extensionDir: string, textEditor: vscode.TextEditor, range: |
90 | 113 | return newWordPosition; |
91 | 114 | } |
92 | 115 | return null; |
93 | | - }).then(newWordPosition => { |
| 116 | + }).then(newWordPosition => { |
94 | 117 | if (newWordPosition) { |
95 | | - return textEditor.document.save().then(()=>{ |
| 118 | + return textEditor.document.save().then(() => { |
96 | 119 | // Now that we have selected the new variable, lets invoke the rename command |
97 | 120 | return vscode.commands.executeCommand('editor.action.rename'); |
98 | 121 | }); |
|
0 commit comments