-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy patheditors-and-documents.ts
172 lines (150 loc) · 7.21 KB
/
editors-and-documents.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
// *****************************************************************************
// 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 { inject, injectable } from '@theia/core/shared/inversify';
import { EditorsAndDocumentsExt, EditorsAndDocumentsDelta, PLUGIN_RPC_CONTEXT } from '../common/plugin-api-rpc';
import { TextEditorExt } from './text-editor';
import { RPCProtocol } from '../common/rpc-protocol';
import { Emitter, Event } from '@theia/core/lib/common/event';
import { DocumentDataExt } from './document-data';
import { ok } from '../common/assert';
import * as Converter from './type-converters';
import { dispose } from '../common/disposable-util';
import { URI } from './types-impl';
@injectable()
export class EditorsAndDocumentsExtImpl implements EditorsAndDocumentsExt {
@inject(RPCProtocol)
protected readonly rpc: RPCProtocol;
private activeEditorId: string | null = null;
private readonly _onDidAddDocuments = new Emitter<DocumentDataExt[]>();
private readonly _onDidRemoveDocuments = new Emitter<DocumentDataExt[]>();
private readonly _onDidChangeVisibleTextEditors = new Emitter<TextEditorExt[]>();
private readonly _onDidChangeActiveTextEditor = new Emitter<TextEditorExt | undefined>();
readonly onDidAddDocuments: Event<DocumentDataExt[]> = this._onDidAddDocuments.event;
readonly onDidRemoveDocuments: Event<DocumentDataExt[]> = this._onDidRemoveDocuments.event;
readonly onDidChangeVisibleTextEditors: Event<TextEditorExt[]> = this._onDidChangeVisibleTextEditors.event;
readonly onDidChangeActiveTextEditor: Event<TextEditorExt | undefined> = this._onDidChangeActiveTextEditor.event;
private readonly documents = new Map<string, DocumentDataExt>();
private readonly editors = new Map<string, TextEditorExt>();
async $acceptEditorsAndDocumentsDelta(delta: EditorsAndDocumentsDelta): Promise<void> {
const removedDocuments = new Array<DocumentDataExt>();
const addedDocuments = new Array<DocumentDataExt>();
const removedEditors = new Array<TextEditorExt>();
if (delta.removedDocuments) {
for (const uriComponent of delta.removedDocuments) {
const uri = URI.revive(uriComponent);
const id = uri.toString();
const data = this.documents.get(id);
this.documents.delete(id);
if (data) {
removedDocuments.push(data);
}
}
}
if (delta.addedDocuments) {
for (const data of delta.addedDocuments) {
const resource = URI.revive(data.uri);
ok(!this.documents.has(resource.toString()), `document '${resource}' already exists!`);
const documentData = new DocumentDataExt(
this.rpc.getProxy(PLUGIN_RPC_CONTEXT.DOCUMENTS_MAIN),
resource,
data.lines,
data.EOL,
data.modeId,
data.versionId,
data.isDirty
);
this.documents.set(resource.toString(), documentData);
addedDocuments.push(documentData);
}
}
if (delta.removedEditors) {
for (const id of delta.removedEditors) {
const editor = this.editors.get(id);
this.editors.delete(id);
if (editor) {
removedEditors.push(editor);
}
}
}
if (delta.addedEditors) {
for (const data of delta.addedEditors) {
const resource = URI.revive(data.documentUri);
ok(this.documents.has(resource.toString()), `document '${resource}' doesn't exist`);
ok(!this.editors.has(data.id), `editor '${data.id}' already exists!`);
const documentData = this.documents.get(resource.toString());
const editor = new TextEditorExt(
this.rpc.getProxy(PLUGIN_RPC_CONTEXT.TEXT_EDITORS_MAIN),
data.id,
documentData!,
data.selections.map(Converter.toSelection),
data.options,
data.visibleRanges.map(Converter.toRange),
Converter.toViewColumn(data.editorPosition)
);
this.editors.set(data.id, editor);
}
}
// TODO investigate how to get rid of it to align with VS Code extension host code
if (this.activeEditorId && delta.removedEditors && delta.removedEditors.indexOf(this.activeEditorId) !== -1 && this.editors.size !== 0) {
// to be compatible with VSCode, when active editor is closed onDidChangeActiveTextEditor
// should be triggered with undefined before next editor, if any, become active.
this.activeEditorId = null;
this._onDidChangeActiveTextEditor.fire(undefined);
}
if (delta.newActiveEditor !== undefined) {
ok(delta.newActiveEditor === null || this.editors.has(delta.newActiveEditor), `active editor '${delta.newActiveEditor}' does not exist`);
this.activeEditorId = delta.newActiveEditor;
}
dispose(removedDocuments);
dispose(removedEditors);
// now that the internal state is complete, fire events
if (delta.removedDocuments) {
this._onDidRemoveDocuments.fire(removedDocuments);
}
if (delta.addedDocuments) {
this._onDidAddDocuments.fire(addedDocuments);
}
if (delta.removedEditors || delta.addedEditors) {
this._onDidChangeVisibleTextEditors.fire(this.allEditors());
}
if (delta.newActiveEditor !== undefined) {
this._onDidChangeActiveTextEditor.fire(this.activeEditor());
}
}
allEditors(): TextEditorExt[] {
const result = new Array<TextEditorExt>();
this.editors.forEach(editor => result.push(editor));
return result;
}
activeEditor(): TextEditorExt | undefined {
if (!this.activeEditorId) {
return undefined;
} else {
return this.editors.get(this.activeEditorId);
}
}
allDocuments(): DocumentDataExt[] {
const result = new Array<DocumentDataExt>();
this.documents.forEach(data => result.push(data));
return result;
}
getDocument(uri: string): DocumentDataExt | undefined {
return this.documents.get(uri);
}
getEditor(id: string): TextEditorExt | undefined {
return this.editors.get(id);
}
}