forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameProvider.ts
More file actions
68 lines (61 loc) · 2.89 KB
/
renameProvider.ts
File metadata and controls
68 lines (61 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import { PythonSettings } from '../common/configSettings';
import { getWorkspaceEditsFromPatch } from '../common/editor';
import { Installer, Product } from '../common/installer';
import { RefactorProxy } from '../refactor/proxy';
import { captureTelemetry } from '../telemetry';
import { REFACTOR_RENAME } from '../telemetry/constants';
const EXTENSION_DIR = path.join(__dirname, '..', '..', '..');
interface RenameResponse {
results: [{ diff: string }];
}
export class PythonRenameProvider implements vscode.RenameProvider {
private installer: Installer;
constructor(private outputChannel: vscode.OutputChannel) {
this.installer = new Installer(outputChannel);
}
@captureTelemetry(REFACTOR_RENAME)
public provideRenameEdits(document: vscode.TextDocument, position: vscode.Position, newName: string, token: vscode.CancellationToken): Thenable<vscode.WorkspaceEdit> {
return vscode.workspace.saveAll(false).then(() => {
return this.doRename(document, position, newName, token);
});
}
private doRename(document: vscode.TextDocument, position: vscode.Position, newName: string, token: vscode.CancellationToken): Thenable<vscode.WorkspaceEdit> {
if (document.lineAt(position.line).text.match(/^\s*\/\//)) {
return;
}
if (position.character <= 0) {
return;
}
const range = document.getWordRangeAtPosition(position);
if (!range || range.isEmpty) {
return;
}
const oldName = document.getText(range);
if (oldName === newName) {
return;
}
let workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
if (!workspaceFolder && Array.isArray(vscode.workspace.workspaceFolders) && vscode.workspace.workspaceFolders.length > 0) {
workspaceFolder = vscode.workspace.workspaceFolders[0];
}
const workspaceRoot = workspaceFolder ? workspaceFolder.uri.fsPath : __dirname;
const pythonSettings = PythonSettings.getInstance(workspaceFolder ? workspaceFolder.uri : undefined);
const proxy = new RefactorProxy(EXTENSION_DIR, pythonSettings, workspaceRoot);
return proxy.rename<RenameResponse>(document, newName, document.uri.fsPath, range).then(response => {
const fileDiffs = response.results.map(fileChanges => fileChanges.diff);
return getWorkspaceEditsFromPatch(fileDiffs, workspaceRoot);
}).catch(reason => {
if (reason === 'Not installed') {
this.installer.promptToInstall(Product.rope, document.uri);
return Promise.reject('');
} else {
vscode.window.showErrorMessage(reason);
this.outputChannel.appendLine(reason);
}
return Promise.reject(reason);
});
}
}