-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinlineEditTriggerer.ts
More file actions
363 lines (298 loc) · 14.1 KB
/
inlineEditTriggerer.ts
File metadata and controls
363 lines (298 loc) · 14.1 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import { TextDocumentChangeReason } from 'vscode';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { DocumentId } from '../../../platform/inlineEdits/common/dataTypes/documentId';
import { DocumentSwitchTriggerStrategy } from '../../../platform/inlineEdits/common/dataTypes/triggerOptions';
import { ILogger, ILogService } from '../../../platform/log/common/logService';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { isNotebookCell } from '../../../util/common/notebooks';
import { Emitter } from '../../../util/vs/base/common/event';
import { Disposable, DisposableMap, IDisposable, MutableDisposable } from '../../../util/vs/base/common/lifecycle';
import { generateUuid } from '../../../util/vs/base/common/uuid';
import { createTimeout } from '../common/common';
import { NesChangeHint, NesTriggerReason } from '../common/nesTriggerHint';
import { NesOutcome, NextEditProvider } from '../node/nextEditProvider';
import { VSCodeWorkspace } from './parts/vscodeWorkspace';
export const TRIGGER_INLINE_EDIT_AFTER_CHANGE_LIMIT = 10000; // 10 seconds
export const TRIGGER_INLINE_EDIT_ON_SAME_LINE_COOLDOWN = 5000; // milliseconds
export const TRIGGER_INLINE_EDIT_REJECTION_COOLDOWN = 5000; // 5s
class LastChange extends Disposable {
public lastEditedTimestamp: number;
public lineNumberTriggers: Map<number /* lineNumber */, number /* timestamp */>;
public readonly timeout = this._register(new MutableDisposable<IDisposable>());
private _nConsecutiveSelectionChanges = 0;
public get nConsecutiveSelectionChanges(): number {
return this._nConsecutiveSelectionChanges;
}
public incrementSelectionChangeEventCount(): void {
this._nConsecutiveSelectionChanges++;
}
constructor(public documentTrigger: vscode.TextDocument) {
super();
this.lastEditedTimestamp = Date.now();
this.lineNumberTriggers = new Map();
}
}
export class InlineEditTriggerer extends Disposable {
private _onChangeEmitter = this._register(new Emitter<NesChangeHint>());
public readonly onChange = this._onChangeEmitter.event;
private readonly docToLastChangeMap = this._register(new DisposableMap<DocumentId, LastChange>());
private lastDocWithSelectionUri: string | undefined;
/**
* Timestamp of the last edit in any document.
*/
private lastEditTimestamp: number | undefined;
private readonly _logger: ILogger;
constructor(
private readonly workspace: VSCodeWorkspace,
private readonly nextEditProvider: NextEditProvider,
@ILogService private readonly _logService: ILogService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IExperimentationService private readonly _expService: IExperimentationService,
@IWorkspaceService private readonly _workspaceService: IWorkspaceService
) {
super();
this._logger = this._logService.createSubLogger(['NES', 'Triggerer']);
this.registerListeners();
}
private registerListeners() {
this._registerDocumentChangeListener();
this._registerSelectionChangeListener();
}
private _shouldIgnoreDoc(doc: vscode.TextDocument): boolean {
return doc.uri.scheme === 'output'; // ignore output pane documents
}
private _registerDocumentChangeListener() {
this._register(this._workspaceService.onDidChangeTextDocument(e => {
if (this._shouldIgnoreDoc(e.document)) {
return;
}
this.lastEditTimestamp = Date.now();
const logger = this._logger.createSubLogger('onDidChangeTextDocument');
if (e.reason === TextDocumentChangeReason.Undo || e.reason === TextDocumentChangeReason.Redo) { // ignore
logger.trace('Return: undo/redo');
return;
}
const doc = this.workspace.getDocumentByTextDocument(e.document);
if (!doc) { // doc is likely copilot-ignored
logger.trace('Return: ignored document');
return;
}
this.docToLastChangeMap.set(doc.id, new LastChange(e.document));
logger.trace(`Return: updated last edit timestamp and cleared line triggers for document for ${doc.id.uri}`);
}));
}
private _registerSelectionChangeListener() {
this._register(this._workspaceService.onDidChangeTextEditorSelection(e => this._handleSelectionChange(e)));
}
private _handleSelectionChange(e: vscode.TextEditorSelectionChangeEvent) {
if (this._shouldIgnoreDoc(e.textEditor.document)) {
return;
}
const isSameDoc = this.lastDocWithSelectionUri === e.textEditor.document.uri.toString();
this.lastDocWithSelectionUri = e.textEditor.document.uri.toString();
const logger = this._logger.createSubLogger('onDidChangeTextEditorSelection');
if (e.selections.length !== 1) { // ignore multi-selection case
logger.trace('Return: multiple selections');
return;
}
if (!e.selections[0].isEmpty) { // ignore non-empty selection
logger.trace('Return: not empty selection');
return;
}
const doc = this.workspace.getDocumentByTextDocument(e.textEditor.document);
if (!doc) { // doc is likely copilot-ignored
return;
}
if (this._isWithinRejectionCooldown()) {
// the cursor has moved within 5s of the last rejection, don't auto-trigger until another doc modification
this.docToLastChangeMap.deleteAndDispose(doc.id);
logger.trace('Return: rejection cooldown');
return;
}
const mostRecentChange = this.docToLastChangeMap.get(doc.id);
if (!mostRecentChange) {
if (!this._maybeTriggerOnDocumentSwitch(e, isSameDoc, logger)) {
logger.trace('Return: document not tracked - does not have recent changes');
}
return;
}
const hadRecentEdit = this._hasRecentEdit(mostRecentChange);
if (!hadRecentEdit || !this._hasRecentTrigger()) {
// The edit is too old or the provider was not triggered recently (we might be
// observing a cursor change following an external edit) — try document switch.
const reason = hadRecentEdit ? 'no recent trigger' : 'no recent edit';
if (!this._maybeTriggerOnDocumentSwitch(e, isSameDoc, logger)) {
logger.trace(`Return: ${reason}`);
}
return;
}
this._handleTrackedDocSelectionChange(e, doc, mostRecentChange, logger);
}
/**
* Handles a selection change in a document that has a recent tracked edit and a recent NES trigger.
* Checks same-line cooldown, cleans up stale triggers, and fires the trigger (possibly debounced).
*/
private _handleTrackedDocSelectionChange(
e: vscode.TextEditorSelectionChangeEvent,
doc: NonNullable<ReturnType<VSCodeWorkspace['getDocumentByTextDocument']>>,
mostRecentChange: LastChange,
logger: ILogger,
) {
const range = doc.toRange(e.textEditor.document, e.selections[0]);
if (!range) {
logger.trace('Return: no range');
return;
}
const selectionLine = range.start.line;
if (this.nextEditProvider.consumeShouldSuppressSelectionChangeTrigger(doc.id, selectionLine, range.start.character)) {
logger.trace('Return: suppressed after NES acceptance');
return;
}
if (this._isSameLineCooldownActive(mostRecentChange, selectionLine, e.textEditor.document)) {
logger.trace('Return: same line cooldown');
return;
}
// TODO: Do not trigger if there is an existing valid request now running, ie don't use just last-trigger timestamp
this._cleanupStaleLineTriggers(mostRecentChange);
mostRecentChange.lineNumberTriggers.set(selectionLine, Date.now());
mostRecentChange.documentTrigger = e.textEditor.document;
logger.trace('Return: triggering inline edit');
this._triggerWithDebounce(mostRecentChange);
}
// #region Helper predicates
private _isWithinRejectionCooldown(): boolean {
return (Date.now() - this.nextEditProvider.lastRejectionTime) < TRIGGER_INLINE_EDIT_REJECTION_COOLDOWN;
}
private _hasRecentEdit(mostRecentChange: LastChange): boolean {
return (Date.now() - mostRecentChange.lastEditedTimestamp) < TRIGGER_INLINE_EDIT_AFTER_CHANGE_LIMIT;
}
private _hasRecentTrigger(): boolean {
return (Date.now() - this.nextEditProvider.lastTriggerTime) < TRIGGER_INLINE_EDIT_AFTER_CHANGE_LIMIT;
}
/**
* Returns true if the same-line cooldown is active and we should skip triggering.
*
* The cooldown is bypassed when:
* - `triggerOnActiveEditorChange` is configured, OR
* - we're in a notebook cell and the current document differs from the one that
* originally triggered the change (user moved to a different cell).
*/
private _isSameLineCooldownActive(mostRecentChange: LastChange, selectionLine: number, currentDocument: vscode.TextDocument): boolean {
const triggerOnActiveEditorChange = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.InlineEditsTriggerOnEditorChangeAfterSeconds, this._expService);
if (triggerOnActiveEditorChange) {
return false; // cooldown bypassed
}
// In a notebook, if the user moved to a different cell, bypass the cooldown
if (isNotebookCell(currentDocument.uri) && currentDocument !== mostRecentChange.documentTrigger) {
return false; // cooldown bypassed
}
const lastTriggerTimestampForLine = mostRecentChange.lineNumberTriggers.get(selectionLine);
return lastTriggerTimestampForLine !== undefined
&& (Date.now() - lastTriggerTimestampForLine) < TRIGGER_INLINE_EDIT_ON_SAME_LINE_COOLDOWN;
}
// #endregion
// #region Trigger helpers
/**
* Removes line triggers older than {@link TRIGGER_INLINE_EDIT_AFTER_CHANGE_LIMIT}
* when the map grows beyond 100 entries.
*/
private _cleanupStaleLineTriggers(mostRecentChange: LastChange): void {
if (mostRecentChange.lineNumberTriggers.size <= 100) {
return;
}
const now = Date.now();
for (const [lineNumber, timestamp] of mostRecentChange.lineNumberTriggers.entries()) {
if (now - timestamp > TRIGGER_INLINE_EDIT_AFTER_CHANGE_LIMIT) {
mostRecentChange.lineNumberTriggers.delete(lineNumber);
}
}
}
/**
* Fires a selection-change trigger, applying debounce when configured.
*
* The first 2 selection changes after an edit fire immediately (the 1st is caused by
* the edit itself, the 2nd is the user intentionally moving to the next edit location).
* Subsequent changes are debounced to avoid excessive triggering during rapid navigation.
*/
private _triggerWithDebounce(mostRecentChange: LastChange): void {
const debounceMs = this._configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.InlineEditsDebounceOnSelectionChange, this._expService);
if (debounceMs === undefined) {
this._triggerInlineEdit(NesTriggerReason.SelectionChange);
return;
}
const N_ALLOWED_IMMEDIATE_SELECTION_CHANGE_EVENTS = 2;
if (mostRecentChange.nConsecutiveSelectionChanges < N_ALLOWED_IMMEDIATE_SELECTION_CHANGE_EVENTS) {
this._triggerInlineEdit(NesTriggerReason.SelectionChange);
} else {
mostRecentChange.timeout.value = createTimeout(debounceMs, () => this._triggerInlineEdit(NesTriggerReason.SelectionChange));
}
mostRecentChange.incrementSelectionChangeEventCount();
}
// #endregion
private _maybeTriggerOnDocumentSwitch(e: vscode.TextEditorSelectionChangeEvent, isSameDoc: boolean, parentLogger: ILogger): boolean {
const logger = parentLogger.createSubLogger('editorSwitch');
const triggerAfterSeconds = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.InlineEditsTriggerOnEditorChangeAfterSeconds, this._expService);
if (triggerAfterSeconds === undefined) {
logger.trace('document switch disabled');
return false;
}
if (isSameDoc) {
logger.trace(`Return: document switch didn't happen`);
return false;
}
if (this.lastEditTimestamp === undefined) {
logger.trace('Return: no last edit timestamp');
return false;
}
const now = Date.now();
const triggerThresholdMs = triggerAfterSeconds * 1000;
const timeSinceLastEdit = now - this.lastEditTimestamp;
if (timeSinceLastEdit > triggerThresholdMs) {
logger.trace('Return: too long since last edit');
return false;
}
// Require a recent NES trigger before triggering on document switch.
// lastTriggerTime === 0 means NES was never triggered in this session.
const timeSinceLastTrigger = now - this.nextEditProvider.lastTriggerTime;
if (this.nextEditProvider.lastTriggerTime === 0 || timeSinceLastTrigger > triggerThresholdMs) {
logger.trace('Return: no recent NES trigger');
return false;
}
const strategy = this._configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.InlineEditsTriggerOnEditorChangeStrategy, this._expService);
if (strategy === DocumentSwitchTriggerStrategy.AfterAcceptance && this.nextEditProvider.lastOutcome !== NesOutcome.Accepted) {
// When the afterAcceptance strategy is active, only trigger on document switch
// if the most recent NES was accepted. A pending outcome (undefined) is treated
// as not-accepted to avoid racing with the UI's accept/reject/ignore callback.
logger.trace('Return: afterAcceptance strategy requires last NES to be accepted');
return false;
}
const doc = this.workspace.getDocumentByTextDocument(e.textEditor.document);
if (!doc) { // doc is likely copilot-ignored
logger.trace('Return: ignored document');
return false;
}
const range = doc.toRange(e.textEditor.document, e.selections[0]);
if (!range) {
logger.trace('Return: no range');
return false;
}
const selectionLine = range.start.line;
// mark as touched such that NES gets triggered on cursor move; otherwise, user may get a single NES then move cursor and never get the suggestion back
const lastChange = new LastChange(e.textEditor.document);
lastChange.lineNumberTriggers.set(selectionLine, Date.now());
this.docToLastChangeMap.set(doc.id, lastChange);
this._triggerInlineEdit(NesTriggerReason.ActiveDocumentSwitch);
return true;
}
private _triggerInlineEdit(reason: NesTriggerReason) {
const uuid = generateUuid();
this._logger.trace(`Triggering inline edit: ${reason}`);
this._onChangeEmitter.fire({ data: { uuid, reason } });
}
}