/
updatePathsOnRename.ts
208 lines (179 loc) · 5.51 KB
/
updatePathsOnRename.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import * as languageIds from '../utils/languageModeIds';
import * as typeConverters from '../utils/typeConverters';
import FileConfigurationManager from './fileConfigurationManager';
const localize = nls.loadMessageBundle();
const updateImportsOnFileMoveName = 'updateImportsOnFileMove.enabled';
enum UpdateImportsOnFileMoveSetting {
Prompt = 'prompt',
Always = 'always',
Never = 'never',
}
export class UpdateImportsOnFileRenameHandler {
private readonly _onDidRenameSub: vscode.Disposable;
public constructor(
private readonly client: ITypeScriptServiceClient,
private readonly fileConfigurationManager: FileConfigurationManager,
private readonly handles: (uri: vscode.Uri) => Promise<boolean>,
) {
this._onDidRenameSub = vscode.workspace.onDidRenameResource(e => {
console.log(e.newResource);
this.doRename(e.oldResource, e.newResource);
});
}
public dispose() {
this._onDidRenameSub.dispose();
}
private async doRename(
oldResource: vscode.Uri,
newResource: vscode.Uri,
): Promise<void> {
if (!this.client.apiVersion.gte(API.v290)) {
return;
}
const handles = await this.handles(newResource);
console.log(handles);
if (!handles) {
return;
}
const newFile = this.client.toPath(newResource);
if (!newFile) {
return;
}
const oldFile = this.client.toPath(oldResource);
if (!oldFile) {
return;
}
const document = await vscode.workspace.openTextDocument(newResource);
const config = this.getConfiguration(document);
const setting = config.get<UpdateImportsOnFileMoveSetting>(updateImportsOnFileMoveName);
if (setting === UpdateImportsOnFileMoveSetting.Never) {
return;
}
// Make sure TS knows about file
this.client.bufferSyncSupport.closeResource(oldResource);
this.client.bufferSyncSupport.openTextDocument(document);
const edits = await this.getEditsForFileRename(document, oldFile, newFile);
if (!edits || !edits.size) {
return;
}
console.log('x');
if (await this.confirmActionWithUser(document)) {
await vscode.workspace.applyEdit(edits);
}
}
private async confirmActionWithUser(
newDocument: vscode.TextDocument
): Promise<boolean> {
const config = this.getConfiguration(newDocument);
const setting = config.get<UpdateImportsOnFileMoveSetting>(updateImportsOnFileMoveName);
switch (setting) {
case UpdateImportsOnFileMoveSetting.Always:
return true;
case UpdateImportsOnFileMoveSetting.Never:
return false;
case UpdateImportsOnFileMoveSetting.Prompt:
default:
return this.promptUser(newDocument);
}
}
private getConfiguration(newDocument: vscode.TextDocument) {
return vscode.workspace.getConfiguration(isTypeScriptDocument(newDocument) ? 'typescript' : 'javascript', newDocument.uri);
}
private async promptUser(
newDocument: vscode.TextDocument
): Promise<boolean> {
enum Choice {
None = 0,
Accept = 1,
Reject = 2,
Always = 3,
Never = 4,
}
interface Item extends vscode.MessageItem {
choice: Choice;
}
const response = await vscode.window.showInformationMessage<Item>(
localize('prompt', "Automatically update imports for moved file: '{0}'?", path.basename(newDocument.fileName)), {
modal: true,
},
{
title: localize('reject.title', "No"),
choice: Choice.Reject,
isCloseAffordance: true,
},
{
title: localize('accept.title', "Yes"),
choice: Choice.Accept,
},
{
title: localize('always.title', "Yes, always update imports"),
choice: Choice.Always,
},
{
title: localize('never.title', "No, never update imports"),
choice: Choice.Never,
});
if (!response) {
return false;
}
switch (response.choice) {
case Choice.Accept:
{
return true;
}
case Choice.Reject:
{
return false;
}
case Choice.Always:
{
const config = this.getConfiguration(newDocument);
config.update(
updateImportsOnFileMoveName,
UpdateImportsOnFileMoveSetting.Always,
vscode.ConfigurationTarget.Global);
return true;
}
case Choice.Never:
{
const config = this.getConfiguration(newDocument);
config.update(
updateImportsOnFileMoveName,
UpdateImportsOnFileMoveSetting.Never,
vscode.ConfigurationTarget.Global);
return false;
}
}
return false;
}
private async getEditsForFileRename(
document: vscode.TextDocument,
oldFile: string,
newFile: string,
) {
await this.fileConfigurationManager.ensureConfigurationForDocument(document, undefined);
const args: Proto.GetEditsForFileRenameRequestArgs = {
file: newFile,
oldFilePath: oldFile,
newFilePath: newFile,
};
const response = await this.client.execute('getEditsForFileRename', args);
if (!response || !response.body) {
return;
}
return typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body);
}
}
function isTypeScriptDocument(document: vscode.TextDocument) {
return document.languageId === languageIds.typescript || document.languageId === languageIds.typescriptreact;
}