Skip to content

Commit 441c344

Browse files
committed
fix #441, fix #443 (prompt to save for refactor)
1 parent 51581e9 commit 441c344

1 file changed

Lines changed: 39 additions & 16 deletions

File tree

src/client/providers/simpleRefactorProvider.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

33
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';
77

88
interface RenameResponse {
99
results: [{ diff: string }];
@@ -31,26 +31,49 @@ export function activateSimplePythonRefactorProvider(context: vscode.ExtensionCo
3131
export function extractVariable(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range,
3232
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath,
3333
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-
});
3934

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+
});
4144
}
4245

4346
// Exported for unit testing
4447
export function extractMethod(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range,
4548
outputChannel: vscode.OutputChannel, workspaceRoot: string = vscode.workspace.rootPath,
4649
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);
5159
});
60+
}
61+
62+
function validateDocumentForRefactor(textEditor: vscode.TextEditor): Promise<any> {
63+
if (!textEditor.document.isDirty) {
64+
return Promise.resolve();
65+
}
5266

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+
});
5477
}
5578

5679
function extractName(extensionDir: string, textEditor: vscode.TextEditor, range: vscode.Range, newName: string,
@@ -90,9 +113,9 @@ function extractName(extensionDir: string, textEditor: vscode.TextEditor, range:
90113
return newWordPosition;
91114
}
92115
return null;
93-
}).then(newWordPosition => {
116+
}).then(newWordPosition => {
94117
if (newWordPosition) {
95-
return textEditor.document.save().then(()=>{
118+
return textEditor.document.save().then(() => {
96119
// Now that we have selected the new variable, lets invoke the rename command
97120
return vscode.commands.executeCommand('editor.action.rename');
98121
});

0 commit comments

Comments
 (0)