Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/editTelemetry/browser/helpers/annotatedDocuments.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable } from '../../../../../base/common/lifecycle.js';
7
import { IObservable, mapObservableArrayCached, derived, derivedObservableWithCache, observableFromEvent, observableSignalFromEvent } from '../../../../../base/common/observable.js';
8
import { isDefined } from '../../../../../base/common/types.js';
9
import { URI } from '../../../../../base/common/uri.js';
10
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
11
import { EditorResourceAccessor } from '../../../../common/editor.js';
12
import { IEditorGroupsService } from '../../../../services/editor/common/editorGroupsService.js';
13
import { IDocumentWithAnnotatedEdits, EditSourceData, DocumentWithSourceAnnotatedEdits, CombineStreamedChanges, MinimizeEditsProcessor } from './documentWithAnnotatedEdits.js';
14
import { ObservableWorkspace, IObservableDocument } from './observableWorkspace.js';
15
16
export class AnnotatedDocuments extends Disposable {
17
public readonly documents: IObservable<readonly AnnotatedDocument[]>;
18
19
constructor(
20
private readonly _workspace: ObservableWorkspace,
21
@IEditorGroupsService private readonly _editorGroupsService: IEditorGroupsService,
22
@IInstantiationService private readonly _instantiationService: IInstantiationService
23
) {
24
super();
25
26
const onDidAddGroupSignal = observableSignalFromEvent(this, this._editorGroupsService.onDidAddGroup);
27
const onDidRemoveGroupSignal = observableSignalFromEvent(this, this._editorGroupsService.onDidRemoveGroup);
28
const groups = derived(this, reader => {
29
onDidAddGroupSignal.read(reader);
30
onDidRemoveGroupSignal.read(reader);
31
return this._editorGroupsService.groups;
32
});
33
const visibleUris: IObservable<Map<string, URI>> = mapObservableArrayCached(this, groups, g => {
34
const editors = observableFromEvent(this, g.onDidModelChange, () => g.editors);
35
return editors.map(e => e.map(editor => EditorResourceAccessor.getCanonicalUri(editor)));
36
}).map((editors, reader) => {
37
const map = new Map<string, URI>();
38
for (const urisObs of editors) {
39
for (const uri of urisObs.read(reader)) {
40
if (isDefined(uri)) {
41
map.set(uri.toString(), uri);
42
}
43
}
44
}
45
return map;
46
});
47
48
const states = mapObservableArrayCached(this, this._workspace.documents, (doc, store) => {
49
const docIsVisible = derived(reader => visibleUris.read(reader).has(doc.uri.toString()));
50
const wasEverVisible = derivedObservableWithCache<boolean>(this, (reader, lastVal) => lastVal || docIsVisible.read(reader));
51
return wasEverVisible.map(v => v ? store.add(this._instantiationService.createInstance(AnnotatedDocument, doc, docIsVisible)) : undefined);
52
});
53
54
this.documents = states.map((vals, reader) => vals.map(v => v.read(reader)).filter(isDefined));
55
56
this.documents.recomputeInitiallyAndOnChange(this._store);
57
}
58
}
59
60
export class AnnotatedDocument extends Disposable {
61
public readonly documentWithAnnotations;
62
63
constructor(
64
public readonly document: IObservableDocument,
65
public readonly isVisible: IObservable<boolean>,
66
@IInstantiationService private readonly _instantiationService: IInstantiationService
67
) {
68
super();
69
70
let processedDoc: IDocumentWithAnnotatedEdits<EditSourceData> = this._store.add(new DocumentWithSourceAnnotatedEdits(document));
71
// Combine streaming edits into one and make edit smaller
72
processedDoc = this._store.add(this._instantiationService.createInstance((CombineStreamedChanges<EditSourceData>), processedDoc));
73
// Remove common suffix and prefix from edits
74
processedDoc = this._store.add(new MinimizeEditsProcessor(processedDoc));
75
76
this.documentWithAnnotations = processedDoc;
77
}
78
}
79
80