forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameProvider.ts
More file actions
91 lines (85 loc) · 3.69 KB
/
renameProvider.ts
File metadata and controls
91 lines (85 loc) · 3.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
CancellationToken,
OutputChannel,
Position,
ProviderResult,
RenameProvider,
TextDocument,
window,
workspace,
WorkspaceEdit
} from 'vscode';
import { EXTENSION_ROOT_DIR, STANDARD_OUTPUT_CHANNEL } from '../common/constants';
import { getWorkspaceEditsFromPatch } from '../common/editor';
import { traceError } from '../common/logger';
import { IFileSystem } from '../common/platform/types';
import { IConfigurationService, IInstaller, IOutputChannel, Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { RefactorProxy } from '../refactor/proxy';
import { captureTelemetry } from '../telemetry';
import { EventName } from '../telemetry/constants';
type RenameResponse = {
results: [{ diff: string }];
};
export class PythonRenameProvider implements RenameProvider {
private readonly outputChannel: OutputChannel;
private readonly configurationService: IConfigurationService;
constructor(private serviceContainer: IServiceContainer) {
this.outputChannel = serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
this.configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
}
@captureTelemetry(EventName.REFACTOR_RENAME)
public provideRenameEdits(
document: TextDocument,
position: Position,
newName: string,
_token: CancellationToken
): ProviderResult<WorkspaceEdit> {
return workspace.saveAll(false).then(() => {
return this.doRename(document, position, newName);
});
}
private doRename(document: TextDocument, position: Position, newName: string): ProviderResult<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 = workspace.getWorkspaceFolder(document.uri);
if (!workspaceFolder && Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
workspaceFolder = workspace.workspaceFolders[0];
}
const workspaceRoot = workspaceFolder ? workspaceFolder.uri.fsPath : __dirname;
const pythonSettings = this.configurationService.getSettings(workspaceFolder ? workspaceFolder.uri : undefined);
const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings, workspaceRoot, this.serviceContainer);
return proxy
.rename<RenameResponse>(document, newName, document.uri.fsPath, range)
.then(response => {
const fileDiffs = response.results.map(fileChanges => fileChanges.diff);
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
return getWorkspaceEditsFromPatch(fileDiffs, workspaceRoot, fs);
})
.catch(reason => {
if (reason === 'Not installed') {
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
installer
.promptToInstall(Product.rope, document.uri)
.catch(ex => traceError('Python Extension: promptToInstall', ex));
return Promise.reject('');
} else {
window.showErrorMessage(reason);
this.outputChannel.appendLine(reason);
}
return Promise.reject(reason);
});
}
}