Skip to content

Commit af7560d

Browse files
committed
fix 442
1 parent 441c344 commit af7560d

3 files changed

Lines changed: 53 additions & 21 deletions

File tree

pythonFiles/refactor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ def __init__(self):
166166
self.default_sys_path = sys.path
167167
self._input = io.open(sys.stdin.fileno(), encoding='utf-8')
168168

169-
def _rename(self, filePath, start, newName):
169+
def _rename(self, filePath, start, newName, indent_size):
170170
"""
171171
Extracts a variale
172172
"""
173173
project = rope.base.project.Project(
174-
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False)
174+
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False, indent_size=indent_size)
175175
resourceToRefactor = libutils.path_to_resource(project, filePath)
176176
refactor = RenameRefactor(
177177
project, resourceToRefactor, startOffset=start, newName=newName)
@@ -183,12 +183,12 @@ def _rename(self, filePath, start, newName):
183183
valueToReturn.append({'diff': change.diff})
184184
return valueToReturn
185185

186-
def _extractVariable(self, filePath, start, end, newName):
186+
def _extractVariable(self, filePath, start, end, newName, indent_size):
187187
"""
188188
Extracts a variale
189189
"""
190190
project = rope.base.project.Project(
191-
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False)
191+
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False, indent_size=indent_size)
192192
resourceToRefactor = libutils.path_to_resource(project, filePath)
193193
refactor = ExtractVariableRefactor(
194194
project, resourceToRefactor, startOffset=start, endOffset=end, newName=newName, similar=True)
@@ -200,12 +200,12 @@ def _extractVariable(self, filePath, start, end, newName):
200200
valueToReturn.append({'diff': change.diff})
201201
return valueToReturn
202202

203-
def _extractMethod(self, filePath, start, end, newName):
203+
def _extractMethod(self, filePath, start, end, newName, indent_size):
204204
"""
205205
Extracts a method
206206
"""
207207
project = rope.base.project.Project(
208-
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False)
208+
WORKSPACE_ROOT, ropefolder=ROPE_PROJECT_FOLDER, save_history=False, indent_size=indent_size)
209209
resourceToRefactor = libutils.path_to_resource(project, filePath)
210210
refactor = ExtractMethodRefactor(
211211
project, resourceToRefactor, startOffset=start, endOffset=end, newName=newName, similar=True)
@@ -244,15 +244,15 @@ def _process_request(self, request):
244244
pass
245245
elif lookup == 'rename':
246246
changes = self._rename(request['file'], int(
247-
request['start']), request['name'])
247+
request['start']), request['name'], int(request['indent_size']))
248248
return self._write_response(self._serialize(request['id'], changes))
249249
elif lookup == 'extract_variable':
250250
changes = self._extractVariable(request['file'], int(
251-
request['start']), int(request['end']), request['name'])
251+
request['start']), int(request['end']), request['name'], int(request['indent_size']))
252252
return self._write_response(self._serialize(request['id'], changes))
253253
elif lookup == 'extract_method':
254254
changes = self._extractMethod(request['file'], int(
255-
request['start']), int(request['end']), request['name'])
255+
request['start']), int(request['end']), request['name'], int(request['indent_size']))
256256
return self._write_response(self._serialize(request['id'], changes))
257257

258258
def _write_response(self, response):

src/client/providers/simpleRefactorProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function extractVariable(extensionDir: string, textEditor: vscode.TextEdi
3535
return validateDocumentForRefactor(textEditor).then(() => {
3636
let newName = 'newvariable' + new Date().getMilliseconds().toString();
3737
let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot);
38-
let rename = proxy.extractVariable<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => {
38+
let rename = proxy.extractVariable<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range, textEditor.options).then(response => {
3939
return response.results[0].diff;
4040
});
4141

@@ -51,7 +51,7 @@ export function extractMethod(extensionDir: string, textEditor: vscode.TextEdito
5151
return validateDocumentForRefactor(textEditor).then(() => {
5252
let newName = 'newmethod' + new Date().getMilliseconds().toString();
5353
let proxy = new RefactorProxy(extensionDir, pythonSettings, workspaceRoot);
54-
let rename = proxy.extractMethod<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range).then(response => {
54+
let rename = proxy.extractMethod<RenameResponse>(textEditor.document, newName, textEditor.document.uri.fsPath, range, textEditor.options).then(response => {
5555
return response.results[0].diff;
5656
});
5757

src/client/refactor/proxy.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as child_process from 'child_process';
66
import { IPythonSettings } from '../common/configSettings';
77
import { REFACTOR } from '../common/telemetryContracts';
88
import { sendTelemetryEvent, Delays } from '../common/telemetry';
9-
import {IS_WINDOWS} from '../common/utils';
9+
import { IS_WINDOWS } from '../common/utils';
1010

1111
export class RefactorProxy extends vscode.Disposable {
1212
private _process: child_process.ChildProcess;
@@ -30,8 +30,8 @@ export class RefactorProxy extends vscode.Disposable {
3030
}
3131
this._process = null;
3232
}
33-
private getOffsetAt(document: vscode.TextDocument, position:vscode.Position):number {
34-
if (!IS_WINDOWS){
33+
private getOffsetAt(document: vscode.TextDocument, position: vscode.Position): number {
34+
if (!IS_WINDOWS) {
3535
return document.offsetAt(position);
3636
}
3737

@@ -41,21 +41,53 @@ export class RefactorProxy extends vscode.Disposable {
4141
const offset = document.offsetAt(position);
4242
return offset - position.line;
4343
}
44-
rename<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range): Promise<T> {
45-
let command = { "lookup": "rename", "file": filePath, "start": this.getOffsetAt(document, range.start).toString(), "id": "1", "name": name };
46-
44+
rename<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {
45+
if (!options) {
46+
options = vscode.window.activeTextEditor.options;
47+
}
48+
let command = {
49+
"lookup": "rename",
50+
"file": filePath,
51+
"start": this.getOffsetAt(document, range.start).toString(),
52+
"id": "1",
53+
"name": name,
54+
"indent_size": options.tabSize
55+
};
56+
4757
return this.sendCommand<T>(JSON.stringify(command), REFACTOR.Rename);
4858
}
49-
extractVariable<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range): Promise<T> {
50-
let command = { "lookup": "extract_variable", "file": filePath, "start": this.getOffsetAt(document, range.start).toString(), "end": this.getOffsetAt(document, range.end).toString(), "id": "1", "name": name };
59+
extractVariable<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {
60+
if (!options) {
61+
options = vscode.window.activeTextEditor.options;
62+
}
63+
let command = {
64+
"lookup": "extract_variable",
65+
"file": filePath,
66+
"start": this.getOffsetAt(document, range.start).toString(),
67+
"end": this.getOffsetAt(document, range.end).toString(),
68+
"id": "1",
69+
"name": name,
70+
"indent_size": options.tabSize
71+
};
5172
return this.sendCommand<T>(JSON.stringify(command), REFACTOR.ExtractVariable);
5273
}
53-
extractMethod<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range): Promise<T> {
74+
extractMethod<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {
75+
if (!options) {
76+
options = vscode.window.activeTextEditor.options;
77+
}
5478
// Ensure last line is an empty line
5579
if (!document.lineAt(document.lineCount - 1).isEmptyOrWhitespace && range.start.line === document.lineCount - 1) {
5680
return Promise.reject<T>('Missing blank line at the end of document (PEP8).');
5781
}
58-
let command = { "lookup": "extract_method", "file": filePath, "start": this.getOffsetAt(document, range.start).toString(), "end": this.getOffsetAt(document, range.end).toString(), "id": "1", "name": name };
82+
let command = {
83+
"lookup": "extract_method",
84+
"file": filePath,
85+
"start": this.getOffsetAt(document, range.start).toString(),
86+
"end": this.getOffsetAt(document, range.end).toString(),
87+
"id": "1",
88+
"name": name,
89+
"indent_size": options.tabSize
90+
};
5991
return this.sendCommand<T>(JSON.stringify(command), REFACTOR.ExtractMethod);
6092
}
6193
private sendCommand<T>(command: string, telemetryEvent: string): Promise<T> {

0 commit comments

Comments
 (0)