Skip to content

Commit 7c82b49

Browse files
committed
fix #440
1 parent 3a324cd commit 7c82b49

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

pythonFiles/refactor.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,10 @@ def watch(self):
264264
while True:
265265
try:
266266
self._process_request(self._input.readline())
267-
except Exception as ex:
268-
message = ""
269-
try:
270-
message = ex.message
271-
except:
272-
pass
273-
274-
message = message + ' \n' + traceback.format_exc()
275-
jsonMessage = {'error': True, 'message': message,
276-
'traceback': traceback.format_exc()}
267+
except:
268+
exc_type, exc_value, exc_tb = sys.exc_info()
269+
tb_info = traceback.extract_tb(exc_tb)
270+
jsonMessage = {'error': True, 'message': str(exc_value), 'traceback': str(tb_info), 'type': str(exc_type)}
277271
sys.stderr.write(json.dumps(jsonMessage))
278272
sys.stderr.flush()
279273

src/client/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function activate(context: vscode.ExtensionContext) {
6161
]
6262
});
6363

64-
context.subscriptions.push(vscode.languages.registerRenameProvider(PYTHON, new PythonRenameProvider()));
64+
context.subscriptions.push(vscode.languages.registerRenameProvider(PYTHON, new PythonRenameProvider(formatOutChannel)));
6565
context.subscriptions.push(vscode.languages.registerHoverProvider(PYTHON, new PythonHoverProvider(context)));
6666
const definitionProvider = new PythonDefinitionProvider(context);
6767
const jediProx = definitionProvider.JediProxy;
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22

33
import * as vscode from 'vscode';
4-
import * as telemetryContracts from "../common/telemetryContracts";
5-
import {RefactorProxy} from '../refactor/proxy';
6-
import {getWorkspaceEditsFromPatch, getTextEdits} from '../common/editor';
4+
import { RefactorProxy } from '../refactor/proxy';
5+
import { getWorkspaceEditsFromPatch } from '../common/editor';
76
import * as path from 'path';
8-
import {PythonSettings} from '../common/configSettings';
7+
import { PythonSettings } from '../common/configSettings';
98

109
const pythonSettings = PythonSettings.getInstance();
1110
const EXTENSION_DIR = path.join(__dirname, '..', '..', '..');
@@ -14,24 +13,24 @@ interface RenameResponse {
1413
}
1514

1615
export class PythonRenameProvider implements vscode.RenameProvider {
16+
constructor(private outputChannel: vscode.OutputChannel) {
17+
}
1718
public provideRenameEdits(document: vscode.TextDocument, position: vscode.Position, newName: string, token: vscode.CancellationToken): Thenable<vscode.WorkspaceEdit> {
1819
return vscode.workspace.saveAll(false).then(() => {
1920
return this.doRename(document, position, newName, token);
2021
});
2122
}
2223

2324
private doRename(document: vscode.TextDocument, position: vscode.Position, newName: string, token: vscode.CancellationToken): Thenable<vscode.WorkspaceEdit> {
24-
var filename = document.fileName;
2525
if (document.lineAt(position.line).text.match(/^\s*\/\//)) {
2626
return;
2727
}
2828
if (position.character <= 0) {
2929
return;
3030
}
3131

32-
var source = document.getText();
3332
var range = document.getWordRangeAtPosition(position);
34-
if (range == undefined || range == null || range.isEmpty) {
33+
if (!range || range.isEmpty) {
3534
return;
3635
}
3736
const oldName = document.getText(range);
@@ -40,12 +39,14 @@ export class PythonRenameProvider implements vscode.RenameProvider {
4039
}
4140

4241
let proxy = new RefactorProxy(EXTENSION_DIR, pythonSettings, vscode.workspace.rootPath);
43-
return new Promise<vscode.WorkspaceEdit>(resolve => {
44-
proxy.rename<RenameResponse>(document, newName, document.uri.fsPath, range).then(response => {
45-
//return response.results[0].diff;
46-
const workspaceEdit = getWorkspaceEditsFromPatch(response.results.map(fileChanges => fileChanges.diff));
47-
resolve(workspaceEdit);
48-
});
42+
return proxy.rename<RenameResponse>(document, newName, document.uri.fsPath, range).then(response => {
43+
//return response.results[0].diff;
44+
const workspaceEdit = getWorkspaceEditsFromPatch(response.results.map(fileChanges => fileChanges.diff));
45+
return workspaceEdit;
46+
}).catch(reason => {
47+
vscode.window.showErrorMessage(reason);
48+
this.outputChannel.appendLine(reason);
49+
return;
4950
});
5051
}
5152
}

src/client/refactor/proxy.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
import * as vscode from 'vscode';
44
import * as path from 'path';
5-
import * as fs from 'fs';
65
import * as child_process from 'child_process';
7-
import { ExtractResult } from './contracts';
8-
import { execPythonFile } from '../common/utils';
96
import { IPythonSettings } from '../common/configSettings';
107
import { REFACTOR } from '../common/telemetryContracts';
118
import { sendTelemetryEvent, Delays } from '../common/telemetry';
129

13-
const ROPE_PYTHON_VERSION = 'Currently code refactoring is only supported in Python 2.x';
14-
const ERROR_PREFIX = '$ERROR';
15-
1610
export class RefactorProxy extends vscode.Disposable {
1711
private _process: child_process.ChildProcess;
1812
private _extensionDir: string;
@@ -46,7 +40,7 @@ export class RefactorProxy extends vscode.Disposable {
4640
extractMethod<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range): Promise<T> {
4741
// Ensure last line is an empty line
4842
if (!document.lineAt(document.lineCount - 1).isEmptyOrWhitespace && range.start.line === document.lineCount - 1) {
49-
return Promise.reject<T>('Missing blank line at the end of document (PEP8).')
43+
return Promise.reject<T>('Missing blank line at the end of document (PEP8).');
5044
}
5145
let command = { "lookup": "extract_method", "file": filePath, "start": document.offsetAt(range.start).toString(), "end": document.offsetAt(range.end).toString(), "id": "1", "name": name };
5246
return this.sendCommand<T>(JSON.stringify(command), REFACTOR.ExtractMethod);
@@ -109,6 +103,7 @@ export class RefactorProxy extends vscode.Disposable {
109103
this._previousStdErrData = '';
110104
}
111105
catch (ex) {
106+
console.error(ex);
112107
// Possible we've only received part of the data, hence don't clear previousData
113108
return;
114109
}

0 commit comments

Comments
 (0)