/
text-editors.ts
150 lines (127 loc) · 6.57 KB
/
text-editors.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
// *****************************************************************************
// 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 { TextEditorsExt, EditorChangedPropertiesData, TextEditorPositionData, TextEditorsMain, PLUGIN_RPC_CONTEXT } from '../common/plugin-api-rpc';
import { RPCProtocol } from '../common/rpc-protocol';
import * as theia from '@theia/plugin';
import { Emitter, Event } from '@theia/core/lib/common/event';
import { EditorsAndDocumentsExtImpl } from './editors-and-documents';
import { TextEditorExt } from './text-editor';
import * as Converters from './type-converters';
import { TextEditorSelectionChangeKind } from './types-impl';
import { IdGenerator } from '../common/id-generator';
export class TextEditorsExtImpl implements TextEditorsExt {
private readonly _onDidChangeTextEditorSelection = new Emitter<theia.TextEditorSelectionChangeEvent>();
private readonly _onDidChangeTextEditorOptions = new Emitter<theia.TextEditorOptionsChangeEvent>();
private readonly _onDidChangeTextEditorVisibleRanges = new Emitter<theia.TextEditorVisibleRangesChangeEvent>();
private readonly _onDidChangeTextEditorViewColumn = new Emitter<theia.TextEditorViewColumnChangeEvent>();
private readonly _onDidChangeActiveTextEditor = new Emitter<theia.TextEditor | undefined>();
private readonly _onDidChangeVisibleTextEditors = new Emitter<theia.TextEditor[]>();
readonly onDidChangeTextEditorSelection: Event<theia.TextEditorSelectionChangeEvent> = this._onDidChangeTextEditorSelection.event;
readonly onDidChangeTextEditorOptions = this._onDidChangeTextEditorOptions.event;
readonly onDidChangeTextEditorVisibleRanges = this._onDidChangeTextEditorVisibleRanges.event;
readonly onDidChangeTextEditorViewColumn = this._onDidChangeTextEditorViewColumn.event;
readonly onDidChangeActiveTextEditor = this._onDidChangeActiveTextEditor.event;
readonly onDidChangeVisibleTextEditors = this._onDidChangeVisibleTextEditors.event;
private proxy: TextEditorsMain;
constructor(rpc: RPCProtocol, private editorsAndDocuments: EditorsAndDocumentsExtImpl) {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.TEXT_EDITORS_MAIN);
this.editorsAndDocuments.onDidChangeActiveTextEditor(e => this._onDidChangeActiveTextEditor.fire(e));
this.editorsAndDocuments.onDidChangeVisibleTextEditors(e => this._onDidChangeVisibleTextEditors.fire(e));
}
$acceptEditorPropertiesChanged(id: string, props: EditorChangedPropertiesData): void {
const textEditor = this.editorsAndDocuments.getEditor(id);
if (!textEditor) {
return;
}
if (props.options) {
textEditor.acceptOptions(props.options);
}
if (props.selections) {
const selections = props.selections.selections.map(Converters.toSelection);
textEditor.acceptSelections(selections);
}
if (props.visibleRanges) {
const visibleRanges = props.visibleRanges.map(Converters.toRange);
textEditor.acceptVisibleRanges(visibleRanges);
}
if (props.options) {
this._onDidChangeTextEditorOptions.fire({
textEditor,
options: props.options
});
}
if (props.selections) {
const kind = TextEditorSelectionChangeKind.fromValue(props.selections.source);
const selections = props.selections.selections.map(Converters.toSelection);
this._onDidChangeTextEditorSelection.fire({
textEditor,
selections,
kind
});
}
if (props.visibleRanges) {
const visibleRanges = props.visibleRanges.map(Converters.toRange);
this._onDidChangeTextEditorVisibleRanges.fire({
textEditor,
visibleRanges
});
}
}
$acceptEditorPositionData(data: TextEditorPositionData): void {
for (const id in data) {
if (data.hasOwnProperty(id)) {
const textEditor = this.editorsAndDocuments.getEditor(id);
const viewColumn = Converters.toViewColumn(data[id]);
if (textEditor && viewColumn) {
if (textEditor.viewColumn !== viewColumn) {
textEditor.acceptViewColumn(viewColumn);
this._onDidChangeTextEditorViewColumn.fire({ textEditor, viewColumn });
}
}
}
}
}
getActiveEditor(): TextEditorExt | undefined {
return this.editorsAndDocuments.activeEditor();
}
getVisibleTextEditors(): theia.TextEditor[] {
return this.editorsAndDocuments.allEditors();
}
createTextEditorDecorationType(options: theia.DecorationRenderOptions): theia.TextEditorDecorationType {
return new TextEditorDecorationType(this.proxy, options);
}
applyWorkspaceEdit(edit: theia.WorkspaceEdit, metadata?: theia.WorkspaceEditMetadata): Promise<boolean> {
const dto = Converters.fromWorkspaceEdit(edit, this.editorsAndDocuments);
return this.proxy.$tryApplyWorkspaceEdit(dto, metadata);
}
saveAll(includeUntitled?: boolean): PromiseLike<boolean> {
return this.proxy.$saveAll(includeUntitled);
}
}
export class TextEditorDecorationType implements theia.TextEditorDecorationType {
private static readonly Keys = new IdGenerator('TextEditorDecorationType');
private proxy: TextEditorsMain;
public key: string;
constructor(proxy: TextEditorsMain, options: theia.DecorationRenderOptions) {
this.key = TextEditorDecorationType.Keys.nextId();
this.proxy = proxy;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.proxy.$registerTextEditorDecorationType(this.key, Converters.DecorationRenderOptions.from(options));
}
dispose(): void {
this.proxy.$removeTextEditorDecorationType(this.key);
}
}