-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
editor.ts
223 lines (199 loc) Β· 5.93 KB
/
editor.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
import type { LanguageService } from '@volar/language-service';
import type { editor as _editor, IDisposable, Uri } from 'monaco-editor-core';
import { markers } from './utils/markers';
import * as protocol2monaco from './utils/protocol2monaco';
import * as monaco2protocol from './utils/monaco2protocol';
interface IInternalEditorModel extends _editor.IModel {
onDidChangeAttached(listener: () => void): IDisposable;
isAttachedToEditor(): boolean;
}
export namespace editor {
export function activateMarkers(
worker: _editor.MonacoWebWorker<LanguageService>,
languages: string[],
markersOwn: string,
getSyncUris: () => Uri[],
editor: typeof import('monaco-editor-core').editor,
): IDisposable {
const disposables: IDisposable[] = [];
const listener = new Map<string, IDisposable>();
disposables.push(
editor.onDidCreateModel((model) => hostingMarkers(model)),
editor.onWillDisposeModel(stopHostingMarkers),
editor.onDidChangeModelLanguage((event) => {
stopHostingMarkers(event.model);
hostingMarkers(event.model);
}),
{
dispose: () => {
for (const model of editor.getModels()) {
stopHostingMarkers(model);
}
}
},
);
for (const model of editor.getModels()) {
hostingMarkers(model);
}
return { dispose: () => disposables.forEach((d) => d.dispose()) };
function stopHostingMarkers(model: _editor.IModel): void {
editor.setModelMarkers(model, markersOwn, []);
const key = model.uri.toString();
if (listener.has(key)) {
listener.get(key)?.dispose();
listener.delete(key);
}
}
function hostingMarkers(model: IInternalEditorModel): void {
if (!languages.includes(model.getLanguageId?.() ?? (model as any).getModeId?.())) {
return;
}
let timer: number | undefined;
const changeSubscription = model.onDidChangeContent(() => {
clearTimeout(timer);
timer = setTimeout(() => doValidation(model), 250);
});
const visibleSubscription = model.onDidChangeAttached(() => {
if (model.isAttachedToEditor()) {
doValidation(model);
} else {
editor.setModelMarkers(model, markersOwn, []);
}
});
listener.set(
model.uri.toString(),
{
dispose: () => {
changeSubscription.dispose();
visibleSubscription.dispose();
clearTimeout(timer);
}
},
);
doValidation(model);
}
async function doValidation(model: _editor.ITextModel) {
if (model.isDisposed()) {
return;
}
if (!model.isAttachedToEditor()) {
return;
}
const version = model.getVersionId();
const languageService = await worker.withSyncedResources(getSyncUris());
const diagnostics = await languageService.doValidation(
model.uri.toString(),
'all',
);
if (model.getVersionId() !== version) {
return;
}
const result = diagnostics.map(error => {
const marker = protocol2monaco.asMarkerData(error);
markers.set(marker, error);
return marker;
});
editor.setModelMarkers(model, markersOwn, result);
}
}
export function activateAutoInsertion(
worker: _editor.MonacoWebWorker<LanguageService>,
languages: string[],
getSyncUris: () => Uri[],
editor: typeof import('monaco-editor-core').editor,
): IDisposable {
const disposables: IDisposable[] = [];
const listener = new Map<_editor.IModel, IDisposable>();
let timeout: number | undefined;
disposables.push(
editor.onDidCreateModel((model) => hostingAutoInsertion(model)),
editor.onWillDisposeModel(stopHostingAutoInsertion),
editor.onDidChangeModelLanguage((event) => {
stopHostingAutoInsertion(event.model);
hostingAutoInsertion(event.model);
}),
{
dispose: () => {
for (const disposable of listener.values()) {
disposable.dispose();
}
listener.clear();
}
},
);
for (const model of editor.getModels()) {
hostingAutoInsertion(model);
}
return { dispose: () => disposables.forEach((d) => d.dispose()) };
function stopHostingAutoInsertion(model: _editor.IModel): void {
if (listener.has(model)) {
listener.get(model)?.dispose();
listener.delete(model);
}
}
function hostingAutoInsertion(model: IInternalEditorModel) {
if (!languages.includes(model.getLanguageId?.() ?? (model as any).getModeId?.())) {
return;
}
listener.set(model, model.onDidChangeContent((e) => {
if (model.isDisposed()) {
return;
}
if (!model.isAttachedToEditor()) {
return;
}
if (e.changes.length === 0 || e.isUndoing || e.isRedoing) {
return;
}
const lastChange = e.changes[e.changes.length - 1];
doAutoInsert(model, lastChange);
}));
}
async function doAutoInsert(
model: _editor.ITextModel,
lastChange: _editor.IModelContentChange,
) {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
const version = model.getVersionId();
timeout = setTimeout(() => {
(async () => {
if (model.getVersionId() !== version) {
return;
}
const languageService = await worker.withSyncedResources(getSyncUris());
const edit = await languageService.doAutoInsert(
model.uri.toString(),
monaco2protocol.asPosition({
lineNumber: lastChange.range.startLineNumber,
column: lastChange.range.startColumn + lastChange.text.length,
}),
{
lastChange: {
range: monaco2protocol.asRange(lastChange.range),
rangeLength: lastChange.rangeLength,
text: lastChange.text,
rangeOffset: lastChange.rangeOffset,
},
},
);
if (model.getVersionId() !== version) {
return;
}
const codeEditor = editor.getEditors().find((e) => e.getModel() === model);
if (codeEditor && edit && model.getVersionId() === version) {
if (typeof edit === 'string') {
(codeEditor?.getContribution('snippetController2') as any)?.insert(edit);
}
else {
model.pushEditOperations([], [protocol2monaco.asTextEdit(edit)], () => []);
}
}
})();
timeout = undefined;
}, 100);
}
}
}