-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy patheditors-and-documents-main.ts
453 lines (397 loc) · 16.4 KB
/
editors-and-documents-main.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// *****************************************************************************
// Copyright (C) 2018 Red Hat, Inc. and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { interfaces } from '@theia/core/shared/inversify';
import * as monaco from '@theia/monaco-editor-core';
import { RPCProtocol } from '../../common/rpc-protocol';
import {
MAIN_RPC_CONTEXT,
EditorsAndDocumentsExt,
EditorsAndDocumentsDelta,
ModelAddedData,
TextEditorAddData,
EditorPosition
} from '../../common/plugin-api-rpc';
import { Disposable } from '@theia/core/lib/common/disposable';
import { EditorModelService } from './text-editor-model-service';
import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import { TextEditorMain } from './text-editor-main';
import { DisposableCollection, Emitter, URI } from '@theia/core';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
import { SaveableService } from '@theia/core/lib/browser/saveable-service';
import { TabsMainImpl } from './tabs/tabs-main';
import { NotebookCellEditorService, NotebookEditorWidgetService } from '@theia/notebook/lib/browser';
import { SimpleMonacoEditor } from '@theia/monaco/lib/browser/simple-monaco-editor';
export class EditorsAndDocumentsMain implements Disposable {
private readonly proxy: EditorsAndDocumentsExt;
private readonly stateComputer: EditorAndDocumentStateComputer;
private readonly textEditors = new Map<string, TextEditorMain>();
private readonly modelService: EditorModelService;
private readonly editorManager: EditorManager;
private readonly saveResourceService: SaveableService;
private readonly onTextEditorAddEmitter = new Emitter<TextEditorMain[]>();
private readonly onTextEditorRemoveEmitter = new Emitter<string[]>();
private readonly onDocumentAddEmitter = new Emitter<MonacoEditorModel[]>();
private readonly onDocumentRemoveEmitter = new Emitter<monaco.Uri[]>();
readonly onTextEditorAdd = this.onTextEditorAddEmitter.event;
readonly onTextEditorRemove = this.onTextEditorRemoveEmitter.event;
readonly onDocumentAdd = this.onDocumentAddEmitter.event;
readonly onDocumentRemove = this.onDocumentRemoveEmitter.event;
private readonly toDispose = new DisposableCollection(
Disposable.create(() => this.textEditors.clear())
);
constructor(rpc: RPCProtocol, container: interfaces.Container, tabsMain: TabsMainImpl) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.EDITORS_AND_DOCUMENTS_EXT);
this.editorManager = container.get(EditorManager);
this.modelService = container.get(EditorModelService);
this.saveResourceService = container.get(SaveableService);
this.stateComputer = new EditorAndDocumentStateComputer(d => this.onDelta(d),
this.editorManager,
container.get(NotebookCellEditorService),
container.get(NotebookEditorWidgetService),
this.modelService, tabsMain);
this.toDispose.push(this.stateComputer);
this.toDispose.push(this.onTextEditorAddEmitter);
this.toDispose.push(this.onTextEditorRemoveEmitter);
this.toDispose.push(this.onDocumentAddEmitter);
this.toDispose.push(this.onDocumentRemoveEmitter);
}
listen(): void {
this.stateComputer.listen();
}
dispose(): void {
this.toDispose.dispose();
}
private onDelta(delta: EditorAndDocumentStateDelta): void {
const removedEditors = new Array<string>();
const addedEditors = new Array<TextEditorMain>();
const removedDocuments = delta.removedDocuments.map(d => d.textEditorModel.uri);
for (const editor of delta.addedEditors) {
const textEditorMain = new TextEditorMain(editor.id, editor.editor.getControl().getModel()!, editor.editor);
this.textEditors.set(editor.id, textEditorMain);
this.toDispose.push(textEditorMain);
addedEditors.push(textEditorMain);
}
for (const { id } of delta.removedEditors) {
const textEditorMain = this.textEditors.get(id);
if (textEditorMain) {
textEditorMain.dispose();
this.textEditors.delete(id);
removedEditors.push(id);
}
}
const deltaExt: EditorsAndDocumentsDelta = {};
let empty = true;
if (delta.newActiveEditor !== undefined) {
empty = false;
deltaExt.newActiveEditor = delta.newActiveEditor;
}
if (removedDocuments.length > 0) {
empty = false;
deltaExt.removedDocuments = removedDocuments;
}
if (removedEditors.length > 0) {
empty = false;
deltaExt.removedEditors = removedEditors;
}
if (delta.addedDocuments.length > 0) {
empty = false;
deltaExt.addedDocuments = delta.addedDocuments.map(d => this.toModelAddData(d));
}
if (delta.addedEditors.length > 0) {
empty = false;
deltaExt.addedEditors = addedEditors.map(e => this.toTextEditorAddData(e));
}
if (!empty) {
this.proxy.$acceptEditorsAndDocumentsDelta(deltaExt);
this.onDocumentRemoveEmitter.fire(removedDocuments);
this.onDocumentAddEmitter.fire(delta.addedDocuments);
this.onTextEditorRemoveEmitter.fire(removedEditors);
this.onTextEditorAddEmitter.fire(addedEditors);
}
}
private toModelAddData(model: MonacoEditorModel): ModelAddedData {
return {
uri: model.textEditorModel.uri,
versionId: model.textEditorModel.getVersionId(),
lines: model.textEditorModel.getLinesContent(),
languageId: model.getLanguageId(),
EOL: model.textEditorModel.getEOL(),
modeId: model.languageId,
isDirty: model.dirty
};
}
private toTextEditorAddData(textEditor: TextEditorMain): TextEditorAddData {
const properties = textEditor.getProperties();
return {
id: textEditor.getId(),
documentUri: textEditor.getModel().uri,
options: properties!.options,
selections: properties!.selections,
visibleRanges: properties!.visibleRanges,
editorPosition: this.findEditorPosition(textEditor)
};
}
private findEditorPosition(editor: TextEditorMain): EditorPosition | undefined {
return EditorPosition.ONE; // TODO: fix this when Theia has support splitting editors
}
getEditor(id: string): TextEditorMain | undefined {
return this.textEditors.get(id);
}
async save(uri: URI): Promise<URI | undefined> {
const editor = await this.editorManager.getByUri(uri);
if (!editor) {
return undefined;
}
return this.saveResourceService.save(editor);
}
async saveAs(uri: URI): Promise<URI | undefined> {
const editor = await this.editorManager.getByUri(uri);
if (!editor) {
return undefined;
}
if (!this.saveResourceService.canSaveAs(editor)) {
return undefined;
}
return this.saveResourceService.saveAs(editor);
}
saveAll(includeUntitled?: boolean): Promise<boolean> {
return this.modelService.saveAll(includeUntitled);
}
hideEditor(id: string): Promise<void> {
for (const editorWidget of this.editorManager.all) {
const monacoEditor = MonacoEditor.get(editorWidget);
if (monacoEditor) {
if (id === new EditorSnapshot(monacoEditor).id) {
editorWidget.close();
break;
}
}
}
return Promise.resolve();
}
}
class EditorAndDocumentStateComputer implements Disposable {
private currentState: EditorAndDocumentState | undefined;
private readonly editors = new Map<string, DisposableCollection>();
private readonly toDispose = new DisposableCollection(
Disposable.create(() => this.currentState = undefined)
);
constructor(
private callback: (delta: EditorAndDocumentStateDelta) => void,
private readonly editorService: EditorManager,
private readonly cellEditorService: NotebookCellEditorService,
private readonly notebookWidgetService: NotebookEditorWidgetService,
private readonly modelService: EditorModelService,
private readonly tabsMain: TabsMainImpl
) { }
listen(): void {
if (this.toDispose.disposed) {
return;
}
this.toDispose.push(this.editorService.onCreated(async widget => {
await this.tabsMain.waitForWidget(widget);
this.onTextEditorAdd(widget);
this.update();
}));
this.toDispose.push(this.editorService.onCurrentEditorChanged(async widget => {
if (widget) {
await this.tabsMain.waitForWidget(widget);
}
this.update();
}));
this.toDispose.push(this.modelService.onModelAdded(this.onModelAdded, this));
this.toDispose.push(this.modelService.onModelRemoved(() => this.update()));
this.toDispose.push(this.cellEditorService.onDidChangeCellEditors(() => this.update()));
this.toDispose.push(this.notebookWidgetService.onDidChangeCurrentEditor(() => {
this.currentState = this.currentState && new EditorAndDocumentState(
this.currentState.documents,
this.currentState.editors,
undefined
);
}));
for (const widget of this.editorService.all) {
this.onTextEditorAdd(widget);
}
this.update();
}
dispose(): void {
this.toDispose.dispose();
}
private onModelAdded(model: MonacoEditorModel): void {
if (!this.currentState) {
this.update();
return;
}
this.currentState = new EditorAndDocumentState(
this.currentState.documents.add(model),
this.currentState.editors,
this.currentState.activeEditor);
this.callback(new EditorAndDocumentStateDelta(
[],
[model],
[],
[],
undefined,
undefined
));
}
private onTextEditorAdd(widget: EditorWidget): void {
const editor = MonacoEditor.get(widget);
if (!editor) {
return;
}
const id = editor.getControl().getId();
const toDispose = new DisposableCollection(
editor.onDispose(() => this.onTextEditorRemove(editor)),
Disposable.create(() => this.editors.delete(id))
);
this.editors.set(id, toDispose);
this.toDispose.push(toDispose);
}
private onTextEditorRemove(e: MonacoEditor): void {
const toDispose = this.editors.get(e.getControl().getId());
if (toDispose) {
toDispose.dispose();
this.update();
}
}
private update(): void {
const models = new Set<MonacoEditorModel>();
for (const model of this.modelService.getModels()) {
models.add(model);
}
let activeId: string | null = null;
const activeEditor = MonacoEditor.getCurrent(this.editorService) ?? this.cellEditorService.getActiveCell();
const editors = new Map<string, EditorSnapshot>();
for (const widget of this.editorService.all) {
const editor = MonacoEditor.get(widget);
// VS Code tracks only visible widgets
if (!editor || !widget.isVisible) {
continue;
}
const model = editor.getControl().getModel();
if (model && !model.isDisposed()) {
const editorSnapshot = new EditorSnapshot(editor);
editors.set(editorSnapshot.id, editorSnapshot);
if (activeEditor === editor) {
activeId = editorSnapshot.id;
}
}
}
for (const editor of this.cellEditorService.allCellEditors) {
if (editor.getControl()?.getModel()) {
const editorSnapshot = new EditorSnapshot(editor);
editors.set(editorSnapshot.id, editorSnapshot);
if (activeEditor === editor) {
activeId = editorSnapshot.id;
}
}
};
const newState = new EditorAndDocumentState(models, editors, activeId);
const delta = EditorAndDocumentState.compute(this.currentState, newState);
if (!delta.isEmpty) {
this.currentState = newState;
this.callback(delta);
}
}
}
class EditorAndDocumentStateDelta {
readonly isEmpty: boolean;
constructor(
readonly removedDocuments: MonacoEditorModel[],
readonly addedDocuments: MonacoEditorModel[],
readonly removedEditors: EditorSnapshot[],
readonly addedEditors: EditorSnapshot[],
readonly oldActiveEditor: string | null | undefined,
readonly newActiveEditor: string | null | undefined
) {
this.isEmpty = this.removedDocuments.length === 0
&& this.addedDocuments.length === 0
&& this.addedEditors.length === 0
&& this.removedEditors.length === 0
&& this.newActiveEditor === this.oldActiveEditor;
}
}
class EditorAndDocumentState {
constructor(
readonly documents: Set<MonacoEditorModel>,
readonly editors: Map<string, EditorSnapshot>,
readonly activeEditor: string | null | undefined) {
}
static compute(before: EditorAndDocumentState | undefined, after: EditorAndDocumentState): EditorAndDocumentStateDelta {
if (!before) {
return new EditorAndDocumentStateDelta(
[],
Array.from(after.documents),
[],
Array.from(after.editors.values()),
undefined,
after.activeEditor
);
}
const documentDelta = Delta.ofSets(before.documents, after.documents);
const editorDelta = Delta.ofMaps(before.editors, after.editors);
const oldActiveEditor = before.activeEditor !== after.activeEditor ? before.activeEditor : undefined;
const newActiveEditor = before.activeEditor !== after.activeEditor ? after.activeEditor : undefined;
return new EditorAndDocumentStateDelta(
documentDelta.removed,
documentDelta.added,
editorDelta.removed,
editorDelta.added,
oldActiveEditor,
newActiveEditor
);
}
}
class EditorSnapshot {
readonly id: string;
constructor(readonly editor: MonacoEditor | SimpleMonacoEditor) {
this.id = `${editor.getControl().getId()},${editor.getControl().getModel()!.id}`;
}
}
namespace Delta {
export function ofSets<T>(before: Set<T>, after: Set<T>): { removed: T[], added: T[] } {
const removed: T[] = [];
const added: T[] = [];
before.forEach(element => {
if (!after.has(element)) {
removed.push(element);
}
});
after.forEach(element => {
if (!before.has(element)) {
added.push(element);
}
});
return { removed, added };
}
export function ofMaps<K, V>(before: Map<K, V>, after: Map<K, V>): { removed: V[], added: V[] } {
const removed: V[] = [];
const added: V[] = [];
before.forEach((value, index) => {
if (!after.has(index)) {
removed.push(value);
}
});
after.forEach((value, index) => {
if (!before.has(index)) {
added.push(value);
}
});
return { removed, added };
}
}