-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
lineTracker.ts
428 lines (343 loc) · 11.9 KB
/
lineTracker.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import type { Event, Selection, TextEditor, TextEditorSelectionChangeEvent } from 'vscode';
import { Disposable, EventEmitter, window } from 'vscode';
import type { Container } from '../container';
import type { GitCommit } from '../git/models/commit';
import { debug } from '../system/decorators/log';
import type { Deferrable } from '../system/function';
import { debounce } from '../system/function';
import { getLogScope, setLogScopeExit } from '../system/logger.scope';
import { isTrackableTextEditor } from '../system/vscode/utils';
import type {
DocumentBlameStateChangeEvent,
DocumentContentChangeEvent,
DocumentDirtyIdleTriggerEvent,
DocumentDirtyStateChangeEvent,
GitDocumentTracker,
} from './documentTracker';
export interface LinesChangeEvent {
readonly editor: TextEditor | undefined;
readonly selections: LineSelection[] | undefined;
readonly reason: 'editor' | 'selection';
readonly pending?: boolean;
readonly suspended?: boolean;
}
export interface LineSelection {
anchor: number;
active: number;
}
export interface LineState {
commit: GitCommit;
}
export class LineTracker {
private _onDidChangeActiveLines = new EventEmitter<LinesChangeEvent>();
get onDidChangeActiveLines(): Event<LinesChangeEvent> {
return this._onDidChangeActiveLines.event;
}
protected _disposable: Disposable | undefined;
private _editor: TextEditor | undefined;
private readonly _state = new Map<number, LineState | undefined>();
private _subscriptions = new Map<unknown, Disposable[]>();
private _subscriptionOnlyWhenTracking: Disposable | undefined;
constructor(
private readonly container: Container,
private readonly documentTracker: GitDocumentTracker,
) {}
dispose() {
for (const subscriber of this._subscriptions.keys()) {
this.unsubscribe(subscriber);
}
}
private onActiveTextEditorChanged(editor: TextEditor | undefined) {
if (editor === this._editor) return;
if (editor != null && !isTrackableTextEditor(editor)) return;
this._editor = editor;
this._selections = toLineSelections(editor?.selections);
if (this._suspended) {
this.resume({ force: true });
} else {
this.notifyLinesChanged('editor');
}
}
@debug<LineTracker['onBlameStateChanged']>({
args: {
0: e => `editor/doc=${e.editor?.document.uri.toString(true)}, blameable=${e.blameable}`,
},
})
private onBlameStateChanged(_e: DocumentBlameStateChangeEvent) {
this.notifyLinesChanged('editor');
}
@debug<LineTracker['onContentChanged']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}`,
},
})
private onContentChanged(e: DocumentContentChangeEvent) {
if (
this.selections?.length &&
e.contentChanges.some(c =>
this.selections!.some(
selection =>
(c.range.end.line >= selection.active && selection.active >= c.range.start.line) ||
(c.range.start.line >= selection.active && selection.active >= c.range.end.line),
),
)
) {
this.notifyLinesChanged('editor');
}
}
@debug<LineTracker['onDirtyIdleTriggered']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}`,
},
})
private onDirtyIdleTriggered(_e: DocumentDirtyIdleTriggerEvent) {
this.resume();
}
@debug<LineTracker['onDirtyStateChanged']>({
args: {
0: e => `editor/doc=${e.editor.document.uri.toString(true)}, dirty=${e.dirty}`,
},
})
private onDirtyStateChanged(e: DocumentDirtyStateChangeEvent) {
if (e.dirty) {
this.suspend();
} else {
this.resume({ force: true });
}
}
private onTextEditorSelectionChanged(e: TextEditorSelectionChangeEvent) {
// If this isn't for our cached editor and its not a real editor -- kick out
if (this._editor !== e.textEditor && !isTrackableTextEditor(e.textEditor)) return;
const selections = toLineSelections(e.selections);
if (this._editor === e.textEditor && this.includes(selections)) return;
this._editor = e.textEditor;
this._selections = selections;
this.notifyLinesChanged(this._editor === e.textEditor ? 'selection' : 'editor');
}
private _selections: LineSelection[] | undefined;
get selections(): LineSelection[] | undefined {
return this._selections;
}
private _suspended = false;
get suspended() {
return this._suspended;
}
getState(line: number): LineState | undefined {
return this._state.get(line);
}
resetState(line?: number) {
if (line != null) {
this._state.delete(line);
return;
}
this._state.clear();
}
setState(line: number, state: LineState | undefined) {
this._state.set(line, state);
}
includes(selections: LineSelection[]): boolean;
includes(line: number, options?: { activeOnly: boolean }): boolean;
includes(lineOrSelections: number | LineSelection[], options?: { activeOnly: boolean }): boolean {
if (typeof lineOrSelections !== 'number') {
return isIncluded(lineOrSelections, this._selections);
}
if (this._selections == null || this._selections.length === 0) return false;
const line = lineOrSelections;
const activeOnly = options?.activeOnly ?? true;
for (const selection of this._selections) {
if (
line === selection.active ||
(!activeOnly &&
((selection.anchor >= line && line >= selection.active) ||
(selection.active >= line && line >= selection.anchor)))
) {
return true;
}
}
return false;
}
refresh() {
this.notifyLinesChanged('editor');
}
@debug()
resume(options?: { force?: boolean; silent?: boolean }) {
if (!options?.force && !this._suspended) return;
this._suspended = false;
this._subscriptionOnlyWhenTracking ??= this.documentTracker.onDidChangeContent(this.onContentChanged, this);
if (!options?.silent) {
this.notifyLinesChanged('editor');
}
}
@debug()
suspend(options?: { force?: boolean; silent?: boolean }) {
if (!options?.force && this._suspended) return;
this._suspended = true;
this._subscriptionOnlyWhenTracking?.dispose();
this._subscriptionOnlyWhenTracking = undefined;
if (!options?.silent) {
this.notifyLinesChanged('editor');
}
}
subscribed(subscriber: unknown) {
return this._subscriptions.has(subscriber);
}
@debug({ args: false, singleLine: true })
subscribe(subscriber: unknown, subscription: Disposable): Disposable {
const scope = getLogScope();
const disposable = {
dispose: () => this.unsubscribe(subscriber),
};
const first = this._subscriptions.size === 0;
let subs = this._subscriptions.get(subscriber);
if (subs == null) {
subs = [subscription];
this._subscriptions.set(subscriber, subs);
} else {
subs.push(subscription);
}
if (first) {
setLogScopeExit(scope, ' \u2022 starting line tracker...');
this.resume({ force: true, silent: true });
this._disposable = Disposable.from(
{ dispose: () => this.suspend({ force: true, silent: true }) },
window.onDidChangeActiveTextEditor(debounce(this.onActiveTextEditorChanged, 0), this),
window.onDidChangeTextEditorSelection(this.onTextEditorSelectionChanged, this),
this.documentTracker.onDidChangeBlameState(this.onBlameStateChanged, this),
this.documentTracker.onDidChangeDirtyState(this.onDirtyStateChanged, this),
this.documentTracker.onDidTriggerDirtyIdle(this.onDirtyIdleTriggered, this),
);
queueMicrotask(() => this.onActiveTextEditorChanged(window.activeTextEditor));
} else {
setLogScopeExit(scope, ' \u2022 already started...');
}
return disposable;
}
@debug({ args: false, singleLine: true })
unsubscribe(subscriber: unknown) {
const subs = this._subscriptions.get(subscriber);
if (subs == null) return;
this._subscriptions.delete(subscriber);
for (const sub of subs) {
sub.dispose();
}
if (this._subscriptions.size !== 0) return;
this._fireLinesChangedDebounced?.cancel();
this._disposable?.dispose();
this._disposable = undefined;
}
private async fireLinesChanged(e: LinesChangeEvent) {
let updated = false;
if (!this.suspended && !e.pending && e.selections != null && e.editor != null) {
updated = await this.updateState(e.selections, e.editor);
}
this._onDidChangeActiveLines.fire(updated ? e : { ...e, selections: undefined, suspended: this.suspended });
}
private _fireLinesChangedDebounced: Deferrable<(e: LinesChangeEvent) => void> | undefined;
private notifyLinesChanged(reason: 'editor' | 'selection') {
if (reason === 'editor') {
this.resetState();
}
const e: LinesChangeEvent = { editor: this._editor, selections: this.selections, reason: reason };
if (e.selections == null) {
queueMicrotask(() => {
if (e.editor !== window.activeTextEditor) return;
this._fireLinesChangedDebounced?.cancel();
void this.fireLinesChanged(e);
});
return;
}
if (this._fireLinesChangedDebounced == null) {
this._fireLinesChangedDebounced = debounce((e: LinesChangeEvent) => {
if (e.editor !== window.activeTextEditor) return;
// Make sure we are still on the same lines
if (!isIncluded(e.selections, toLineSelections(e.editor?.selections))) {
return;
}
void this.fireLinesChanged(e);
}, 250);
}
// If we have no pending moves, then fire an immediate pending event, and defer the real event
if (!this._fireLinesChangedDebounced.pending()) {
void this.fireLinesChanged({ ...e, pending: true });
}
this._fireLinesChangedDebounced(e);
}
@debug<LineTracker['updateState']>({
args: { 0: selections => selections?.map(s => s.active).join(','), 1: e => e.document.uri.toString(true) },
exit: true,
})
private async updateState(selections: LineSelection[], editor: TextEditor): Promise<boolean> {
const scope = getLogScope();
if (!this.includes(selections)) {
setLogScopeExit(scope, ` \u2022 lines no longer match`);
return false;
}
const document = await this.documentTracker.getOrAdd(editor.document);
let status = await document.getStatus();
if (!status.blameable) {
setLogScopeExit(scope, ` \u2022 document is not blameable`);
return false;
}
if (selections.length === 1) {
const blameLine = await this.container.git.getBlameForLine(
document.uri,
selections[0].active,
editor?.document,
);
if (blameLine == null) {
setLogScopeExit(scope, ` \u2022 blame failed`);
return false;
}
if (blameLine.commit != null && blameLine.commit.file == null) {
debugger;
}
this.setState(blameLine.line.line - 1, { commit: blameLine.commit });
} else {
const blame = await this.container.git.getBlame(document.uri, editor.document);
if (blame == null) {
setLogScopeExit(scope, ` \u2022 blame failed`);
return false;
}
for (const selection of selections) {
const commitLine = blame.lines[selection.active];
const commit = blame.commits.get(commitLine.sha);
if (commit != null && commit.file == null) {
debugger;
}
if (commit == null) {
debugger;
this.resetState(selection.active);
} else {
this.setState(selection.active, { commit: commit });
}
}
}
// Check again because of the awaits above
if (!this.includes(selections)) {
setLogScopeExit(scope, ` \u2022 lines no longer match`);
return false;
}
status = await document.getStatus();
if (!status.blameable) {
setLogScopeExit(scope, ` \u2022 document is not blameable`);
return false;
}
if (editor.document.isDirty) {
document.setForceDirtyStateChangeOnNextDocumentChange();
}
return true;
}
}
function isIncluded(selections: LineSelection[] | undefined, within: LineSelection[] | undefined): boolean {
if (selections == null && within == null) return true;
if (selections == null || within == null || selections.length !== within.length) return false;
return selections.every((s, i) => {
const match = within[i];
return s.active === match.active && s.anchor === match.anchor;
});
}
function toLineSelections(selections: readonly Selection[]): LineSelection[];
function toLineSelections(selections: readonly Selection[] | undefined): LineSelection[] | undefined;
function toLineSelections(selections: readonly Selection[] | undefined) {
return selections?.map(s => ({ active: s.active.line, anchor: s.anchor.line }));
}