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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { alert } from 'vs/base/browser/ui/aria/aria';
import { IdleValue, raceCancellation } from 'vs/base/common/async';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { onUnexpectedError } from 'vs/base/common/errors';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { assertType } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { CodeEditorStateFlag, EditorStateCancellationTokenSource } from 'vs/editor/contrib/editorState/browser/editorState';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction, EditorCommand, registerEditorAction, registerEditorCommand, registerEditorContribution, registerModelAndPositionCommand, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ITextModel } from 'vs/editor/common/model';
import { Rejection, RenameLocation, RenameProvider, WorkspaceEdit } from 'vs/editor/common/languages';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfiguration';
import { MessageController } from 'vs/editor/contrib/message/browser/messageController';
import * as nls from 'vs/nls';
import { ConfigurationScope, Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ILogService } from 'vs/platform/log/common/log';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IEditorProgressService } from 'vs/platform/progress/common/progress';
import { Registry } from 'vs/platform/registry/common/platform';
import { CONTEXT_RENAME_INPUT_VISIBLE, RenameInputField } from './renameInputField';
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
class RenameSkeleton {
private readonly _providers: RenameProvider[];
private _providerRenameIdx: number = 0;
constructor(
private readonly model: ITextModel,
private readonly position: Position,
registry: LanguageFeatureRegistry<RenameProvider>
) {
this._providers = registry.ordered(model);
}
hasProvider() {
return this._providers.length > 0;
}
async resolveRenameLocation(token: CancellationToken): Promise<RenameLocation & Rejection | undefined> {
const rejects: string[] = [];
for (this._providerRenameIdx = 0; this._providerRenameIdx < this._providers.length; this._providerRenameIdx++) {
const provider = this._providers[this._providerRenameIdx];
if (!provider.resolveRenameLocation) {
break;
}
let res = await provider.resolveRenameLocation(this.model, this.position, token);
if (!res) {
continue;
}
if (res.rejectReason) {
rejects.push(res.rejectReason);
continue;
}
return res;
}
const word = this.model.getWordAtPosition(this.position);
if (!word) {
return {
range: Range.fromPositions(this.position),
text: '',
rejectReason: rejects.length > 0 ? rejects.join('\n') : undefined
};
}
return {
range: new Range(this.position.lineNumber, word.startColumn, this.position.lineNumber, word.endColumn),
text: word.word,
rejectReason: rejects.length > 0 ? rejects.join('\n') : undefined
};
}
async provideRenameEdits(newName: string, token: CancellationToken): Promise<WorkspaceEdit & Rejection> {
return this._provideRenameEdits(newName, this._providerRenameIdx, [], token);
}
private async _provideRenameEdits(newName: string, i: number, rejects: string[], token: CancellationToken): Promise<WorkspaceEdit & Rejection> {
const provider = this._providers[i];
if (!provider) {
return {
edits: [],
rejectReason: rejects.join('\n')
};
}
const result = await provider.provideRenameEdits(this.model, this.position, newName, token);
if (!result) {
return this._provideRenameEdits(newName, i + 1, rejects.concat(nls.localize('no result', "No result.")), token);
} else if (result.rejectReason) {
return this._provideRenameEdits(newName, i + 1, rejects.concat(result.rejectReason), token);
}
return result;
}
}
export async function rename(registry: LanguageFeatureRegistry<RenameProvider>, model: ITextModel, position: Position, newName: string): Promise<WorkspaceEdit & Rejection> {
const skeleton = new RenameSkeleton(model, position, registry);
const loc = await skeleton.resolveRenameLocation(CancellationToken.None);
if (loc?.rejectReason) {
return { edits: [], rejectReason: loc.rejectReason };
}
return skeleton.provideRenameEdits(newName, CancellationToken.None);
}
// --- register actions and commands
class RenameController implements IEditorContribution {
public static readonly ID = 'editor.contrib.renameController';
static get(editor: ICodeEditor): RenameController | null {
return editor.getContribution<RenameController>(RenameController.ID);
}
private readonly _renameInputField: IdleValue<RenameInputField>;
private readonly _dispoableStore = new DisposableStore();
private _cts: CancellationTokenSource = new CancellationTokenSource();
constructor(
private readonly editor: ICodeEditor,
@IInstantiationService private readonly _instaService: IInstantiationService,
@INotificationService private readonly _notificationService: INotificationService,
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
@IEditorProgressService private readonly _progressService: IEditorProgressService,
@ILogService private readonly _logService: ILogService,
@ITextResourceConfigurationService private readonly _configService: ITextResourceConfigurationService,
@ILanguageFeaturesService private readonly _languageFeaturesService: ILanguageFeaturesService,
) {
this._renameInputField = this._dispoableStore.add(new IdleValue(() => this._dispoableStore.add(this._instaService.createInstance(RenameInputField, this.editor, ['acceptRenameInput', 'acceptRenameInputWithPreview']))));
}
dispose(): void {
this._dispoableStore.dispose();
this._cts.dispose(true);
}
async run(): Promise<void> {
this._cts.dispose(true);
if (!this.editor.hasModel()) {
return undefined;
}
const position = this.editor.getPosition();
const skeleton = new RenameSkeleton(this.editor.getModel(), position, this._languageFeaturesService.renameProvider);
if (!skeleton.hasProvider()) {
return undefined;
}
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value);
// resolve rename location
let loc: RenameLocation & Rejection | undefined;
try {
const resolveLocationOperation = skeleton.resolveRenameLocation(this._cts.token);
this._progressService.showWhile(resolveLocationOperation, 250);
loc = await resolveLocationOperation;
} catch (e) {
MessageController.get(this.editor)?.showMessage(e || nls.localize('resolveRenameLocationFailed', "An unknown error occurred while resolving rename location"), position);
return undefined;
}
if (!loc) {
return undefined;
}
if (loc.rejectReason) {
MessageController.get(this.editor)?.showMessage(loc.rejectReason, position);
return undefined;
}
if (this._cts.token.isCancellationRequested) {
return undefined;
}
this._cts.dispose();
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, loc.range);
// do rename at location
let selection = this.editor.getSelection();
let selectionStart = 0;
let selectionEnd = loc.text.length;
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(loc.range, selection)) {
selectionStart = Math.max(0, selection.startColumn - loc.range.startColumn);
selectionEnd = Math.min(loc.range.endColumn, selection.endColumn) - loc.range.startColumn;
}
const supportPreview = this._bulkEditService.hasPreviewHandler() && this._configService.getValue<boolean>(this.editor.getModel().uri, 'editor.rename.enablePreview');
const inputFieldResult = await this._renameInputField.value.getInput(loc.range, loc.text, selectionStart, selectionEnd, supportPreview, this._cts.token);
// no result, only hint to focus the editor or not
if (typeof inputFieldResult === 'boolean') {
if (inputFieldResult) {
this.editor.focus();
}
return undefined;
}
this.editor.focus();
const renameOperation = raceCancellation(skeleton.provideRenameEdits(inputFieldResult.newName, this._cts.token), this._cts.token).then(async renameResult => {
if (!renameResult || !this.editor.hasModel()) {
return;
}
if (renameResult.rejectReason) {
this._notificationService.info(renameResult.rejectReason);
return;
}
// collapse selection to active end
this.editor.setSelection(Range.fromPositions(this.editor.getSelection().getPosition()));
this._bulkEditService.apply(ResourceEdit.convert(renameResult), {
editor: this.editor,
showPreview: inputFieldResult.wantsPreview,
label: nls.localize('label', "Renaming '{0}' to '{1}'", loc?.text, inputFieldResult.newName),
code: 'undoredo.rename',
quotableLabel: nls.localize('quotableLabel', "Renaming {0} to {1}", loc?.text, inputFieldResult.newName),
respectAutoSaveConfig: true
}).then(result => {
if (result.ariaSummary) {
alert(nls.localize('aria', "Successfully renamed '{0}' to '{1}'. Summary: {2}", loc!.text, inputFieldResult.newName, result.ariaSummary));
}
}).catch(err => {
this._notificationService.error(nls.localize('rename.failedApply', "Rename failed to apply edits"));
this._logService.error(err);
});
}, err => {
this._notificationService.error(nls.localize('rename.failed', "Rename failed to compute edits"));
this._logService.error(err);
});
this._progressService.showWhile(renameOperation, 250);
return renameOperation;
}
acceptRenameInput(wantsPreview: boolean): void {
this._renameInputField.value.acceptInput(wantsPreview);
}
cancelRenameInput(): void {
this._renameInputField.value.cancelInput(true);
}
}
// ---- action implementation
export class RenameAction extends EditorAction {
constructor() {
super({
id: 'editor.action.rename',
label: nls.localize('rename.label', "Rename Symbol"),
alias: 'Rename Symbol',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasRenameProvider),
kbOpts: {
kbExpr: EditorContextKeys.editorTextFocus,
primary: KeyCode.F2,
weight: KeybindingWeight.EditorContrib
},
contextMenuOpts: {
group: '1_modification',
order: 1.1
}
});
}
override runCommand(accessor: ServicesAccessor, args: [URI, IPosition]): void | Promise<void> {
const editorService = accessor.get(ICodeEditorService);
const [uri, pos] = Array.isArray(args) && args || [undefined, undefined];
if (URI.isUri(uri) && Position.isIPosition(pos)) {
return editorService.openCodeEditor({ resource: uri }, editorService.getActiveCodeEditor()).then(editor => {
if (!editor) {
return;
}
editor.setPosition(pos);
editor.invokeWithinContext(accessor => {
this.reportTelemetry(accessor, editor);
return this.run(accessor, editor);
});
}, onUnexpectedError);
}
return super.runCommand(accessor, args);
}
run(accessor: ServicesAccessor, editor: ICodeEditor): Promise<void> {
const controller = RenameController.get(editor);
if (controller) {
return controller.run();
}
return Promise.resolve();
}
}
registerEditorContribution(RenameController.ID, RenameController);
registerEditorAction(RenameAction);
const RenameCommand = EditorCommand.bindToContribution<RenameController>(RenameController.get);
registerEditorCommand(new RenameCommand({
id: 'acceptRenameInput',
precondition: CONTEXT_RENAME_INPUT_VISIBLE,
handler: x => x.acceptRenameInput(false),
kbOpts: {
weight: KeybindingWeight.EditorContrib + 99,
kbExpr: EditorContextKeys.focus,
primary: KeyCode.Enter
}
}));
registerEditorCommand(new RenameCommand({
id: 'acceptRenameInputWithPreview',
precondition: ContextKeyExpr.and(CONTEXT_RENAME_INPUT_VISIBLE, ContextKeyExpr.has('config.editor.rename.enablePreview')),
handler: x => x.acceptRenameInput(true),
kbOpts: {
weight: KeybindingWeight.EditorContrib + 99,
kbExpr: EditorContextKeys.focus,
primary: KeyMod.Shift + KeyCode.Enter
}
}));
registerEditorCommand(new RenameCommand({
id: 'cancelRenameInput',
precondition: CONTEXT_RENAME_INPUT_VISIBLE,
handler: x => x.cancelRenameInput(),
kbOpts: {
weight: KeybindingWeight.EditorContrib + 99,
kbExpr: EditorContextKeys.focus,
primary: KeyCode.Escape,
secondary: [KeyMod.Shift | KeyCode.Escape]
}
}));
// ---- api bridge command
registerModelAndPositionCommand('_executeDocumentRenameProvider', function (accessor, model, position, ...args) {
const [newName] = args;
assertType(typeof newName === 'string');
const { renameProvider } = accessor.get(ILanguageFeaturesService);
return rename(renameProvider, model, position, newName);
});
registerModelAndPositionCommand('_executePrepareRename', async function (accessor, model, position) {
const { renameProvider } = accessor.get(ILanguageFeaturesService);
const skeleton = new RenameSkeleton(model, position, renameProvider);
const loc = await skeleton.resolveRenameLocation(CancellationToken.None);
if (loc?.rejectReason) {
throw new Error(loc.rejectReason);
}
return loc;
});
//todo@jrieken use editor options world
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
id: 'editor',
properties: {
'editor.rename.enablePreview': {
scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
description: nls.localize('enablePreview', "Enable/disable the ability to preview changes before renaming"),
default: true,
type: 'boolean'
}
}
});
|