@theia/monaco 1.53.0-next.55 → 1.53.0-next.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +100 -100
  2. package/data/monaco-nls.json +1379 -1379
  3. package/data/monaco-themes/vscode/dark_plus.json +201 -201
  4. package/data/monaco-themes/vscode/dark_theia.json +5 -5
  5. package/data/monaco-themes/vscode/dark_vs.json +393 -393
  6. package/data/monaco-themes/vscode/hc_black.json +457 -457
  7. package/data/monaco-themes/vscode/hc_light.json +590 -590
  8. package/data/monaco-themes/vscode/hc_theia.json +5 -5
  9. package/data/monaco-themes/vscode/hc_theia_light.json +5 -5
  10. package/data/monaco-themes/vscode/light_plus.json +202 -202
  11. package/data/monaco-themes/vscode/light_theia.json +10 -10
  12. package/data/monaco-themes/vscode/light_vs.json +421 -421
  13. package/lib/browser/monaco-editor-zone-widget.js +1 -1
  14. package/lib/browser/monaco-frontend-application-contribution.js +25 -25
  15. package/lib/browser/simple-monaco-editor.d.ts +3 -1
  16. package/lib/browser/simple-monaco-editor.d.ts.map +1 -1
  17. package/lib/browser/simple-monaco-editor.js +9 -2
  18. package/lib/browser/simple-monaco-editor.js.map +1 -1
  19. package/package.json +8 -8
  20. package/src/browser/index.ts +17 -17
  21. package/src/browser/markdown-renderer/monaco-markdown-renderer.ts +109 -109
  22. package/src/browser/monaco-bulk-edit-service.ts +64 -64
  23. package/src/browser/monaco-color-registry.ts +73 -73
  24. package/src/browser/monaco-command-registry.ts +85 -85
  25. package/src/browser/monaco-command-service.ts +90 -90
  26. package/src/browser/monaco-command.ts +303 -303
  27. package/src/browser/monaco-context-key-service.ts +144 -144
  28. package/src/browser/monaco-context-menu.ts +112 -112
  29. package/src/browser/monaco-diff-editor.ts +141 -141
  30. package/src/browser/monaco-diff-navigator-factory.ts +39 -39
  31. package/src/browser/monaco-editor-model.ts +693 -693
  32. package/src/browser/monaco-editor-peek-view-widget.ts +233 -233
  33. package/src/browser/monaco-editor-provider.ts +455 -455
  34. package/src/browser/monaco-editor-service.ts +152 -152
  35. package/src/browser/monaco-editor-zone-widget.ts +250 -250
  36. package/src/browser/monaco-editor.ts +733 -733
  37. package/src/browser/monaco-formatting-conflicts.ts +116 -116
  38. package/src/browser/monaco-frontend-application-contribution.ts +182 -182
  39. package/src/browser/monaco-frontend-module.ts +319 -319
  40. package/src/browser/monaco-gotoline-quick-access.ts +47 -47
  41. package/src/browser/monaco-gotosymbol-quick-access.ts +53 -53
  42. package/src/browser/monaco-icon-registry.ts +49 -49
  43. package/src/browser/monaco-indexed-db.ts +130 -130
  44. package/src/browser/monaco-init.ts +134 -134
  45. package/src/browser/monaco-keybinding.ts +111 -111
  46. package/src/browser/monaco-keycode-map.ts +171 -171
  47. package/src/browser/monaco-languages.ts +177 -177
  48. package/src/browser/monaco-marker-collection.ts +83 -83
  49. package/src/browser/monaco-menu.ts +147 -147
  50. package/src/browser/monaco-mime-service.ts +71 -71
  51. package/src/browser/monaco-outline-contribution.ts +404 -404
  52. package/src/browser/monaco-outline-decorator.ts +66 -66
  53. package/src/browser/monaco-quick-access-registry.ts +112 -112
  54. package/src/browser/monaco-quick-input-service.ts +701 -701
  55. package/src/browser/monaco-resolved-keybinding.ts +162 -162
  56. package/src/browser/monaco-snippet-suggest-provider.ts +306 -306
  57. package/src/browser/monaco-standalone-theme-service.ts +51 -51
  58. package/src/browser/monaco-status-bar-contribution.ts +110 -110
  59. package/src/browser/monaco-text-model-service.ts +157 -157
  60. package/src/browser/monaco-theming-service.ts +204 -204
  61. package/src/browser/monaco-to-protocol-converter.ts +82 -82
  62. package/src/browser/monaco-undo-redo-handler.ts +64 -64
  63. package/src/browser/monaco-workspace.ts +416 -416
  64. package/src/browser/protocol-to-monaco-converter.ts +158 -158
  65. package/src/browser/simple-monaco-editor.ts +228 -216
  66. package/src/browser/style/index.css +266 -266
  67. package/src/browser/textmate/index.ts +20 -20
  68. package/src/browser/textmate/monaco-textmate-frontend-bindings.ts +90 -90
  69. package/src/browser/textmate/monaco-textmate-service.ts +187 -187
  70. package/src/browser/textmate/monaco-theme-registry.ts +176 -176
  71. package/src/browser/textmate/monaco-theme-types.ts +37 -37
  72. package/src/browser/textmate/textmate-contribution.ts +29 -29
  73. package/src/browser/textmate/textmate-registry.ts +129 -129
  74. package/src/browser/textmate/textmate-snippet-completion-provider.ts +73 -73
  75. package/src/browser/textmate/textmate-tokenizer.ts +84 -84
  76. package/src/browser/workspace-symbol-command.ts +196 -196
  77. package/src/package.spec.ts +28 -28
@@ -1,733 +1,733 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { injectable, inject, unmanaged } from '@theia/core/shared/inversify';
18
- import { ElementExt } from '@theia/core/shared/@phosphor/domutils';
19
- import URI from '@theia/core/lib/common/uri';
20
- import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
21
- import { DisposableCollection, Disposable, Emitter, Event, nullToUndefined, MaybeNull } from '@theia/core/lib/common';
22
- import {
23
- Dimension,
24
- EditorManager,
25
- EditorWidget,
26
- Position,
27
- Range,
28
- TextDocumentContentChangeDelta,
29
- TextDocumentChangeEvent,
30
- TextEditor,
31
- RevealRangeOptions,
32
- RevealPositionOptions,
33
- DeltaDecorationParams,
34
- ReplaceTextParams,
35
- EditorDecoration,
36
- EditorMouseEvent,
37
- EncodingMode,
38
- EditorDecorationOptions,
39
- MouseTargetType
40
- } from '@theia/editor/lib/browser';
41
- import { MonacoEditorModel } from './monaco-editor-model';
42
- import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
43
- import { ProtocolToMonacoConverter } from './protocol-to-monaco-converter';
44
- import { TextEdit } from '@theia/core/shared/vscode-languageserver-protocol';
45
- import { UTF8 } from '@theia/core/lib/common/encodings';
46
- import * as monaco from '@theia/monaco-editor-core';
47
- import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
48
- import { ILanguageService } from '@theia/monaco-editor-core/esm/vs/editor/common/languages/language';
49
- import { IInstantiationService, ServiceIdentifier } from '@theia/monaco-editor-core/esm/vs/platform/instantiation/common/instantiation';
50
- import { ICodeEditor, IMouseTargetMargin } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser';
51
- import { IStandaloneEditorConstructionOptions, StandaloneCodeEditor, StandaloneEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor';
52
- import { ServiceCollection } from '@theia/monaco-editor-core/esm/vs/platform/instantiation/common/serviceCollection';
53
- import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
54
- import { ConfigurationChangedEvent, IEditorOptions } from '@theia/monaco-editor-core/esm/vs/editor/common/config/editorOptions';
55
- import { ICodeEditorService } from '@theia/monaco-editor-core/esm/vs/editor/browser/services/codeEditorService';
56
- import { ICommandService } from '@theia/monaco-editor-core/esm/vs/platform/commands/common/commands';
57
- import { IContextKeyService } from '@theia/monaco-editor-core/esm/vs/platform/contextkey/common/contextkey';
58
- import { IKeybindingService } from '@theia/monaco-editor-core/esm/vs/platform/keybinding/common/keybinding';
59
- import { IThemeService } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
60
- import { INotificationService } from '@theia/monaco-editor-core/esm/vs/platform/notification/common/notification';
61
- import { IAccessibilityService } from '@theia/monaco-editor-core/esm/vs/platform/accessibility/common/accessibility';
62
- import { ILanguageConfigurationService } from '@theia/monaco-editor-core/esm/vs/editor/common/languages/languageConfigurationRegistry';
63
- import { ILanguageFeaturesService } from '@theia/monaco-editor-core/esm/vs/editor/common/services/languageFeatures';
64
- import * as objects from '@theia/monaco-editor-core/esm/vs/base/common/objects';
65
- import { Selection } from '@theia/editor/lib/browser/editor';
66
-
67
- export type ServicePair<T> = [ServiceIdentifier<T>, T];
68
-
69
- export interface EditorServiceOverrides extends Iterable<ServicePair<unknown>> { }
70
-
71
- @injectable()
72
- export class MonacoEditorServices {
73
-
74
- @inject(MonacoToProtocolConverter)
75
- protected readonly m2p: MonacoToProtocolConverter;
76
-
77
- @inject(ProtocolToMonacoConverter)
78
- protected readonly p2m: ProtocolToMonacoConverter;
79
-
80
- @inject(ContextKeyService)
81
- protected readonly contextKeyService: ContextKeyService;
82
-
83
- constructor(@unmanaged() services: MonacoEditorServices) {
84
- Object.assign(this, services);
85
- }
86
- }
87
-
88
- export class MonacoEditor extends MonacoEditorServices implements TextEditor {
89
-
90
- protected readonly toDispose = new DisposableCollection();
91
-
92
- protected readonly autoSizing: boolean;
93
- protected readonly minHeight: number;
94
- protected readonly maxHeight: number;
95
- protected editor: monaco.editor.IStandaloneCodeEditor;
96
-
97
- protected readonly onCursorPositionChangedEmitter = new Emitter<Position>();
98
- protected readonly onSelectionChangedEmitter = new Emitter<Selection>();
99
- protected readonly onFocusChangedEmitter = new Emitter<boolean>();
100
- protected readonly onDocumentContentChangedEmitter = new Emitter<TextDocumentChangeEvent>();
101
- protected readonly onMouseDownEmitter = new Emitter<EditorMouseEvent>();
102
- readonly onDidChangeReadOnly = this.document.onDidChangeReadOnly;
103
- protected readonly onLanguageChangedEmitter = new Emitter<string>();
104
- readonly onLanguageChanged = this.onLanguageChangedEmitter.event;
105
- protected readonly onScrollChangedEmitter = new Emitter<void>();
106
- readonly onEncodingChanged = this.document.onDidChangeEncoding;
107
- protected readonly onResizeEmitter = new Emitter<Dimension | null>();
108
- readonly onDidResize = this.onResizeEmitter.event;
109
-
110
- readonly documents = new Set<MonacoEditorModel>();
111
-
112
- constructor(
113
- readonly uri: URI,
114
- readonly document: MonacoEditorModel,
115
- readonly node: HTMLElement,
116
- services: MonacoEditorServices,
117
- options?: MonacoEditor.IOptions,
118
- override?: EditorServiceOverrides,
119
- readonly parentEditor?: MonacoEditor
120
- ) {
121
- super(services);
122
- this.toDispose.pushAll([
123
- this.onCursorPositionChangedEmitter,
124
- this.onSelectionChangedEmitter,
125
- this.onFocusChangedEmitter,
126
- this.onDocumentContentChangedEmitter,
127
- this.onMouseDownEmitter,
128
- this.onLanguageChangedEmitter,
129
- this.onScrollChangedEmitter
130
- ]);
131
- this.documents.add(document);
132
- this.autoSizing = options && options.autoSizing !== undefined ? options.autoSizing : false;
133
- this.minHeight = options && options.minHeight !== undefined ? options.minHeight : -1;
134
- this.maxHeight = options && options.maxHeight !== undefined ? options.maxHeight : -1;
135
- this.toDispose.push(this.create({
136
- ...MonacoEditor.createReadOnlyOptions(document.readOnly),
137
- ...options
138
- }, override));
139
- this.addHandlers(this.editor);
140
- }
141
-
142
- getEncoding(): string {
143
- return this.document.getEncoding() || UTF8;
144
- }
145
-
146
- setEncoding(encoding: string, mode: EncodingMode): Promise<void> {
147
- return this.document.setEncoding(encoding, mode);
148
- }
149
-
150
- protected create(options?: monaco.editor.IStandaloneEditorConstructionOptions | IStandaloneEditorConstructionOptions, override?: EditorServiceOverrides): Disposable {
151
- const combinedOptions = {
152
- ...options,
153
- lightbulb: { enabled: true },
154
- fixedOverflowWidgets: true,
155
- scrollbar: {
156
- useShadows: false,
157
- verticalHasArrows: false,
158
- horizontalHasArrows: false,
159
- verticalScrollbarSize: 10,
160
- horizontalScrollbarSize: 10,
161
- ...options?.scrollbar,
162
- }
163
- } as IStandaloneEditorConstructionOptions;
164
- const instantiator = this.getInstantiatorWithOverrides(override);
165
- /**
166
- * @monaco-uplift. Should be guaranteed to work.
167
- * Incomparable enums prevent TypeScript from believing that public IStandaloneCodeEditor is satisfied by private StandaloneCodeEditor
168
- */
169
- return this.editor = (this.parentEditor ?
170
- instantiator.createInstance(EmbeddedCodeEditor, this.node, combinedOptions, this.parentEditor.getControl() as unknown as ICodeEditor) :
171
- instantiator.createInstance(StandaloneEditor, this.node, combinedOptions)) as unknown as monaco.editor.IStandaloneCodeEditor;
172
- }
173
-
174
- protected getInstantiatorWithOverrides(override?: EditorServiceOverrides): IInstantiationService {
175
- const instantiator = StandaloneServices.get(IInstantiationService);
176
- if (override) {
177
- const overrideServices = new ServiceCollection(...override);
178
- return instantiator.createChild(overrideServices);
179
- }
180
- return instantiator;
181
- }
182
-
183
- protected addHandlers(codeEditor: monaco.editor.IStandaloneCodeEditor): void {
184
- this.toDispose.push(codeEditor.onDidChangeModelLanguage(e =>
185
- this.fireLanguageChanged(e.newLanguage)
186
- ));
187
- this.toDispose.push(codeEditor.onDidChangeConfiguration(() => this.refresh()));
188
- this.toDispose.push(codeEditor.onDidChangeModel(() => this.refresh()));
189
- this.toDispose.push(codeEditor.onDidChangeModelContent(e => {
190
- this.refresh();
191
- this.onDocumentContentChangedEmitter.fire({ document: this.document, contentChanges: e.changes.map(this.mapModelContentChange.bind(this)) });
192
- }));
193
- this.toDispose.push(codeEditor.onDidChangeCursorPosition(() =>
194
- this.onCursorPositionChangedEmitter.fire(this.cursor)
195
- ));
196
- this.toDispose.push(codeEditor.onDidChangeCursorSelection(event =>
197
- this.onSelectionChangedEmitter.fire({
198
- ...this.m2p.asRange(event.selection),
199
- direction: event.selection.getDirection() === monaco.SelectionDirection.LTR ? 'ltr' : 'rtl'
200
- })
201
- ));
202
- this.toDispose.push(codeEditor.onDidFocusEditorText(() =>
203
- this.onFocusChangedEmitter.fire(this.isFocused())
204
- ));
205
- this.toDispose.push(codeEditor.onDidBlurEditorText(() =>
206
- this.onFocusChangedEmitter.fire(this.isFocused())
207
- ));
208
- this.toDispose.push(codeEditor.onMouseDown(e => {
209
- const { element, position, range } = e.target;
210
- this.onMouseDownEmitter.fire({
211
- target: {
212
- type: e.target.type as unknown as MouseTargetType,
213
- element: element || undefined,
214
- mouseColumn: this.m2p.asPosition(undefined, e.target.mouseColumn).character,
215
- range: range && this.m2p.asRange(range) || undefined,
216
- position: position && this.m2p.asPosition(position.lineNumber, position.column) || undefined,
217
- detail: (e.target as unknown as IMouseTargetMargin).detail || {},
218
- },
219
- event: e.event.browserEvent
220
- });
221
- }));
222
- this.toDispose.push(codeEditor.onDidScrollChange(e => {
223
- this.onScrollChangedEmitter.fire(undefined);
224
- }));
225
- this.toDispose.push(this.onDidChangeReadOnly(readOnly => {
226
- codeEditor.updateOptions(MonacoEditor.createReadOnlyOptions(readOnly));
227
- }));
228
- }
229
-
230
- getVisibleRanges(): Range[] {
231
- return this.editor.getVisibleRanges().map(range => this.m2p.asRange(range));
232
- }
233
-
234
- protected mapModelContentChange(change: monaco.editor.IModelContentChange): TextDocumentContentChangeDelta {
235
- return {
236
- range: this.m2p.asRange(change.range),
237
- rangeLength: change.rangeLength,
238
- text: change.text
239
- };
240
- }
241
-
242
- get onDispose(): Event<void> {
243
- return this.toDispose.onDispose;
244
- }
245
-
246
- get onDocumentContentChanged(): Event<TextDocumentChangeEvent> {
247
- return this.onDocumentContentChangedEmitter.event;
248
- }
249
-
250
- get isReadonly(): boolean | MarkdownString {
251
- return this.document.readOnly;
252
- }
253
-
254
- get cursor(): Position {
255
- const { lineNumber, column } = this.editor.getPosition()!;
256
- return this.m2p.asPosition(lineNumber, column);
257
- }
258
-
259
- set cursor(cursor: Position) {
260
- const position = this.p2m.asPosition(cursor);
261
- this.editor.setPosition(position);
262
- }
263
-
264
- get onCursorPositionChanged(): Event<Position> {
265
- return this.onCursorPositionChangedEmitter.event;
266
- }
267
-
268
- get selection(): Selection {
269
- return this.m2p.asSelection(this.editor.getSelection()!);
270
- }
271
-
272
- set selection(selection: Selection) {
273
- const range = this.p2m.asRange(selection);
274
- this.editor.setSelection(range);
275
- }
276
-
277
- get onSelectionChanged(): Event<Selection> {
278
- return this.onSelectionChangedEmitter.event;
279
- }
280
-
281
- get onScrollChanged(): Event<void> {
282
- return this.onScrollChangedEmitter.event;
283
- }
284
-
285
- revealPosition(raw: Position, options: RevealPositionOptions = { vertical: 'center' }): void {
286
- const position = this.p2m.asPosition(raw);
287
- switch (options.vertical) {
288
- case 'auto':
289
- this.editor.revealPosition(position);
290
- break;
291
- case 'center':
292
- this.editor.revealPositionInCenter(position);
293
- break;
294
- case 'centerIfOutsideViewport':
295
- this.editor.revealPositionInCenterIfOutsideViewport(position);
296
- break;
297
- }
298
- }
299
-
300
- revealRange(raw: Range, options: RevealRangeOptions = { at: 'center' }): void {
301
- const range = this.p2m.asRange(raw);
302
- switch (options.at) {
303
- case 'top':
304
- this.editor.revealRangeAtTop(range!);
305
- break;
306
- case 'center':
307
- this.editor.revealRangeInCenter(range!);
308
- break;
309
- case 'centerIfOutsideViewport':
310
- this.editor.revealRangeInCenterIfOutsideViewport(range!);
311
- break;
312
- case 'auto':
313
- this.editor.revealRange(range!);
314
- break;
315
- }
316
- }
317
-
318
- focus(): void {
319
- /**
320
- * `this.editor.focus` forcefully changes the focus editor state,
321
- * regardless whether the textarea actually received the focus.
322
- * It could lead to issues like https://github.com/eclipse-theia/theia/issues/7902
323
- * Instead we focus the underlying textarea.
324
- */
325
- const node = this.editor.getDomNode();
326
- if (node) {
327
- const textarea = node.querySelector('textarea') as HTMLElement;
328
- textarea.focus();
329
- }
330
- }
331
-
332
- blur(): void {
333
- const node = this.editor.getDomNode();
334
- if (node) {
335
- const textarea = node.querySelector('textarea') as HTMLElement;
336
- textarea.blur();
337
- }
338
- }
339
-
340
- isFocused({ strict }: { strict: boolean } = { strict: false }): boolean {
341
- if (!this.editor.hasTextFocus()) {
342
- return false;
343
- }
344
- if (strict) {
345
- return !this.isSuggestWidgetVisible() && !this.isFindWidgetVisible() && !this.isRenameInputVisible();
346
- }
347
- return true;
348
- }
349
-
350
- get onFocusChanged(): Event<boolean> {
351
- return this.onFocusChangedEmitter.event;
352
- }
353
-
354
- get onMouseDown(): Event<EditorMouseEvent> {
355
- return this.onMouseDownEmitter.event;
356
- }
357
-
358
- /**
359
- * `true` if the suggest widget is visible in the editor. Otherwise, `false`.
360
- */
361
- isSuggestWidgetVisible(): boolean {
362
- return this.contextKeyService.match('suggestWidgetVisible', this.editor.getDomNode() || this.node);
363
- }
364
-
365
- /**
366
- * `true` if the find (and replace) widget is visible in the editor. Otherwise, `false`.
367
- */
368
- isFindWidgetVisible(): boolean {
369
- return this.contextKeyService.match('findWidgetVisible', this.editor.getDomNode() || this.node);
370
- }
371
-
372
- /**
373
- * `true` if the name rename refactoring input HTML element is visible. Otherwise, `false`.
374
- */
375
- isRenameInputVisible(): boolean {
376
- return this.contextKeyService.match('renameInputVisible', this.editor.getDomNode() || this.node);
377
- }
378
-
379
- dispose(): void {
380
- this.toDispose.dispose();
381
- }
382
-
383
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
384
- trigger(source: string, handlerId: string, payload: any): void {
385
- this.editor.trigger(source, handlerId, payload);
386
- }
387
-
388
- getControl(): monaco.editor.IStandaloneCodeEditor {
389
- return this.editor;
390
- }
391
-
392
- refresh(): void {
393
- this.autoresize();
394
- }
395
-
396
- resizeToFit(): void {
397
- this.autoresize();
398
- // eslint-disable-next-line no-null/no-null
399
- this.onResizeEmitter.fire(null);
400
- }
401
-
402
- setSize(dimension: Dimension): void {
403
- this.resize(dimension);
404
- this.onResizeEmitter.fire(dimension);
405
- }
406
-
407
- protected autoresize(): void {
408
- if (this.autoSizing) {
409
- // eslint-disable-next-line no-null/no-null
410
- this.resize(null);
411
- }
412
- }
413
-
414
- protected resize(dimension: Dimension | null): void {
415
- if (this.node) {
416
- const layoutSize = this.computeLayoutSize(this.node, dimension);
417
- this.editor.layout(layoutSize);
418
- }
419
- }
420
-
421
- protected computeLayoutSize(hostNode: HTMLElement, dimension: monaco.editor.IDimension | null): monaco.editor.IDimension {
422
- if (dimension && dimension.width >= 0 && dimension.height >= 0) {
423
- return dimension;
424
- }
425
- const boxSizing = ElementExt.boxSizing(hostNode);
426
-
427
- const width = (!dimension || dimension.width < 0) ?
428
- this.getWidth(hostNode, boxSizing) :
429
- dimension.width;
430
-
431
- const height = (!dimension || dimension.height < 0) ?
432
- this.getHeight(hostNode, boxSizing) :
433
- dimension.height;
434
-
435
- return { width, height };
436
- }
437
-
438
- protected getWidth(hostNode: HTMLElement, boxSizing: ElementExt.IBoxSizing): number {
439
- return hostNode.offsetWidth - boxSizing.horizontalSum;
440
- }
441
-
442
- protected getHeight(hostNode: HTMLElement, boxSizing: ElementExt.IBoxSizing): number {
443
- if (!this.autoSizing) {
444
- return hostNode.offsetHeight - boxSizing.verticalSum;
445
- }
446
-
447
- const lineHeight = this.editor.getOption(monaco.editor.EditorOption.lineHeight);
448
- const lineCount = this.editor.getModel()!.getLineCount();
449
- const contentHeight = lineHeight * lineCount;
450
-
451
- const horizontalScrollbarHeight = this.editor.getLayoutInfo().horizontalScrollbarHeight;
452
-
453
- const editorHeight = contentHeight + horizontalScrollbarHeight;
454
- if (this.minHeight >= 0) {
455
- const minHeight = lineHeight * this.minHeight + horizontalScrollbarHeight;
456
- if (editorHeight < minHeight) {
457
- return minHeight;
458
- }
459
- }
460
- if (this.maxHeight >= 0) {
461
- const maxHeight = lineHeight * this.maxHeight + horizontalScrollbarHeight;
462
- return Math.min(maxHeight, editorHeight);
463
- }
464
- return editorHeight;
465
- }
466
-
467
- isActionSupported(id: string): boolean {
468
- const action = this.editor.getAction(id);
469
- return !!action && action.isSupported();
470
- }
471
-
472
- async runAction(id: string): Promise<void> {
473
- const action = this.editor.getAction(id);
474
- if (action && action.isSupported()) {
475
- await action.run();
476
- }
477
- }
478
-
479
- deltaDecorations(params: DeltaDecorationParams): string[] {
480
- const oldDecorations = params.oldDecorations;
481
- const newDecorations = this.toDeltaDecorations(params);
482
- return this.editor.deltaDecorations(oldDecorations, newDecorations);
483
- }
484
-
485
- protected toDeltaDecorations(params: DeltaDecorationParams): monaco.editor.IModelDeltaDecoration[] {
486
- return params.newDecorations.map(({ options: theiaOptions, range }) => {
487
- const options: monaco.editor.IModelDecorationOptions = {
488
- ...theiaOptions,
489
- hoverMessage: this.fromStringToMarkdownString(theiaOptions.hoverMessage),
490
- glyphMarginHoverMessage: this.fromStringToMarkdownString(theiaOptions.glyphMarginHoverMessage)
491
- };
492
- return {
493
- options,
494
- range: this.p2m.asRange(range),
495
- };
496
- });
497
- }
498
-
499
- protected fromStringToMarkdownString(hoverMessage?: string | monaco.IMarkdownString | monaco.IMarkdownString[]): monaco.IMarkdownString | monaco.IMarkdownString[] | undefined {
500
- if (typeof hoverMessage === 'string') {
501
- return { value: hoverMessage };
502
- }
503
- return hoverMessage;
504
- }
505
-
506
- protected fromMarkdownToString(maybeMarkdown?: null | string | monaco.IMarkdownString | monaco.IMarkdownString[]): string | undefined {
507
- if (!maybeMarkdown) {
508
- return undefined;
509
- }
510
- if (typeof maybeMarkdown === 'string') {
511
- return maybeMarkdown;
512
- }
513
- if (Array.isArray(maybeMarkdown)) {
514
- return maybeMarkdown.map(({ value }) => value).join('\n');
515
- }
516
- return maybeMarkdown.value;
517
- }
518
-
519
- getLinesDecorations(startLineNumber: number, endLineNumber: number): (EditorDecoration & Readonly<{ id: string }>)[] {
520
- const toPosition = (line: number): monaco.Position => this.p2m.asPosition({ line, character: 0 });
521
- const start = toPosition(startLineNumber).lineNumber;
522
- const end = toPosition(endLineNumber).lineNumber;
523
- return this.editor
524
- .getModel()!
525
- .getLinesDecorations(start, end)
526
- .map(this.toEditorDecoration.bind(this));
527
- }
528
-
529
- protected toEditorDecoration(decoration: monaco.editor.IModelDecoration): EditorDecoration & Readonly<{ id: string }> {
530
- const range = this.m2p.asRange(decoration.range);
531
- const { id, options: monacoOptions } = decoration;
532
- const options: MaybeNull<EditorDecorationOptions> = {
533
- ...monacoOptions,
534
- hoverMessage: this.fromMarkdownToString(monacoOptions.hoverMessage),
535
- glyphMarginHoverMessage: this.fromMarkdownToString(monacoOptions.hoverMessage),
536
- };
537
- return {
538
- options: nullToUndefined(options),
539
- range,
540
- id
541
- };
542
- }
543
-
544
- getVisibleColumn(position: Position): number {
545
- return this.editor.getVisibleColumnFromPosition(this.p2m.asPosition(position));
546
- }
547
-
548
- async replaceText(params: ReplaceTextParams): Promise<boolean> {
549
- const edits: monaco.editor.IIdentifiedSingleEditOperation[] = params.replaceOperations.map(param => {
550
- const range = monaco.Range.fromPositions(this.p2m.asPosition(param.range.start), this.p2m.asPosition(param.range.end));
551
- return {
552
- forceMoveMarkers: true,
553
- identifier: {
554
- major: range.startLineNumber,
555
- minor: range.startColumn
556
- },
557
- range,
558
- text: param.text
559
- };
560
- });
561
- return this.editor.executeEdits(params.source, edits);
562
- }
563
-
564
- executeEdits(edits: TextEdit[]): boolean {
565
- return this.editor.executeEdits('MonacoEditor', this.p2m.asTextEdits(edits) as monaco.editor.IIdentifiedSingleEditOperation[]);
566
- }
567
-
568
- storeViewState(): object {
569
- return this.editor.saveViewState()!;
570
- }
571
-
572
- restoreViewState(state: monaco.editor.ICodeEditorViewState): void {
573
- this.editor.restoreViewState(state);
574
- }
575
-
576
- /* `true` because it is derived from an URI during the instantiation */
577
- protected _languageAutoDetected = true;
578
-
579
- get languageAutoDetected(): boolean {
580
- return this._languageAutoDetected;
581
- }
582
-
583
- async detectLanguage(): Promise<void> {
584
- const languageService = StandaloneServices.get(ILanguageService);
585
- const firstLine = this.document.textEditorModel.getLineContent(1);
586
- const model = this.getControl().getModel();
587
- const language = languageService.createByFilepathOrFirstLine(model && model.uri, firstLine);
588
- this.setLanguage(language.languageId);
589
- this._languageAutoDetected = true;
590
- }
591
-
592
- setLanguage(languageId: string): void {
593
- for (const document of this.documents) {
594
- monaco.editor.setModelLanguage(document.textEditorModel, languageId);
595
- }
596
- }
597
-
598
- protected fireLanguageChanged(languageId: string): void {
599
- this._languageAutoDetected = false;
600
- this.onLanguageChangedEmitter.fire(languageId);
601
- }
602
-
603
- getResourceUri(): URI {
604
- return this.uri;
605
- }
606
- createMoveToUri(resourceUri: URI): URI {
607
- return this.uri.withPath(resourceUri.path);
608
- }
609
-
610
- shouldDisplayDirtyDiff(): boolean {
611
- return true;
612
- }
613
- }
614
-
615
- export namespace MonacoEditor {
616
- export interface ICommonOptions {
617
- /**
618
- * Whether an editor should be auto resized on a content change.
619
- *
620
- * #### Fixme
621
- * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
622
- */
623
- autoSizing?: boolean;
624
- /**
625
- * A minimal height of an editor in lines.
626
- *
627
- * #### Fixme
628
- * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
629
- */
630
- minHeight?: number;
631
- /**
632
- * A maximal height of an editor in lines.
633
- *
634
- * #### Fixme
635
- * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
636
- */
637
- maxHeight?: number;
638
- }
639
-
640
- export interface IOptions extends ICommonOptions, monaco.editor.IStandaloneEditorConstructionOptions { }
641
-
642
- export function getAll(manager: EditorManager): MonacoEditor[] {
643
- return manager.all.map(e => get(e)).filter(e => !!e) as MonacoEditor[];
644
- }
645
-
646
- export function getCurrent(manager: EditorManager): MonacoEditor | undefined {
647
- return get(manager.currentEditor);
648
- }
649
-
650
- export function getActive(manager: EditorManager): MonacoEditor | undefined {
651
- return get(manager.activeEditor);
652
- }
653
-
654
- export function get(editorWidget: EditorWidget | undefined): MonacoEditor | undefined {
655
- if (editorWidget && editorWidget.editor instanceof MonacoEditor) {
656
- return editorWidget.editor;
657
- }
658
- return undefined;
659
- }
660
-
661
- export function findByDocument(manager: EditorManager, document: MonacoEditorModel): MonacoEditor[] {
662
- return getAll(manager).filter(candidate => candidate.documents.has(document));
663
- }
664
-
665
- export function getWidgetFor(manager: EditorManager, control: monaco.editor.ICodeEditor | ICodeEditor | undefined | null): EditorWidget | undefined {
666
- if (!control) {
667
- return undefined;
668
- }
669
- return manager.all.find(widget => {
670
- const candidate = get(widget);
671
- return candidate && candidate.getControl() === control;
672
- });
673
- }
674
-
675
- export function createReadOnlyOptions(readOnly?: boolean | MarkdownString): monaco.editor.IEditorOptions {
676
- if (typeof readOnly === 'boolean') {
677
- return { readOnly, readOnlyMessage: undefined };
678
- }
679
- if (readOnly) {
680
- return { readOnly: true, readOnlyMessage: readOnly };
681
- }
682
- return {};
683
- }
684
- }
685
-
686
- // adapted from https://github.com/microsoft/vscode/blob/0bd70d48ad8b3e2fb1922aa54f87c786ff2b4bd8/src/vs/editor/browser/widget/codeEditor/embeddedCodeEditorWidget.ts
687
- // This class reproduces the logic in EmbeddedCodeEditorWidget but extends StandaloneCodeEditor rather than CodeEditorWidget.
688
- class EmbeddedCodeEditor extends StandaloneCodeEditor {
689
-
690
- private readonly _parentEditor: ICodeEditor;
691
- private readonly _overwriteOptions: IEditorOptions;
692
-
693
- constructor(
694
- domElement: HTMLElement,
695
- options: Readonly<IStandaloneEditorConstructionOptions>,
696
- parentEditor: ICodeEditor,
697
- @IInstantiationService instantiationService: IInstantiationService,
698
- @ICodeEditorService codeEditorService: ICodeEditorService,
699
- @ICommandService commandService: ICommandService,
700
- @IContextKeyService contextKeyService: IContextKeyService,
701
- @IKeybindingService keybindingService: IKeybindingService,
702
- @IThemeService themeService: IThemeService,
703
- @INotificationService notificationService: INotificationService,
704
- @IAccessibilityService accessibilityService: IAccessibilityService,
705
- @ILanguageConfigurationService languageConfigurationService: ILanguageConfigurationService,
706
- @ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
707
- ) {
708
- super(domElement, { ...parentEditor.getRawOptions(), overflowWidgetsDomNode: parentEditor.getOverflowWidgetsDomNode() }, instantiationService, codeEditorService,
709
- commandService, contextKeyService, keybindingService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
710
-
711
- this._parentEditor = parentEditor;
712
- this._overwriteOptions = options;
713
-
714
- // Overwrite parent's options
715
- super.updateOptions(this._overwriteOptions);
716
-
717
- this._register(parentEditor.onDidChangeConfiguration((e: ConfigurationChangedEvent) => this._onParentConfigurationChanged(e)));
718
- }
719
-
720
- getParentEditor(): ICodeEditor {
721
- return this._parentEditor;
722
- }
723
-
724
- private _onParentConfigurationChanged(e: ConfigurationChangedEvent): void {
725
- super.updateOptions(this._parentEditor.getRawOptions());
726
- super.updateOptions(this._overwriteOptions);
727
- }
728
-
729
- override updateOptions(newOptions: IEditorOptions): void {
730
- objects.mixin(this._overwriteOptions, newOptions, true);
731
- super.updateOptions(this._overwriteOptions);
732
- }
733
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { injectable, inject, unmanaged } from '@theia/core/shared/inversify';
18
+ import { ElementExt } from '@theia/core/shared/@phosphor/domutils';
19
+ import URI from '@theia/core/lib/common/uri';
20
+ import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
21
+ import { DisposableCollection, Disposable, Emitter, Event, nullToUndefined, MaybeNull } from '@theia/core/lib/common';
22
+ import {
23
+ Dimension,
24
+ EditorManager,
25
+ EditorWidget,
26
+ Position,
27
+ Range,
28
+ TextDocumentContentChangeDelta,
29
+ TextDocumentChangeEvent,
30
+ TextEditor,
31
+ RevealRangeOptions,
32
+ RevealPositionOptions,
33
+ DeltaDecorationParams,
34
+ ReplaceTextParams,
35
+ EditorDecoration,
36
+ EditorMouseEvent,
37
+ EncodingMode,
38
+ EditorDecorationOptions,
39
+ MouseTargetType
40
+ } from '@theia/editor/lib/browser';
41
+ import { MonacoEditorModel } from './monaco-editor-model';
42
+ import { MonacoToProtocolConverter } from './monaco-to-protocol-converter';
43
+ import { ProtocolToMonacoConverter } from './protocol-to-monaco-converter';
44
+ import { TextEdit } from '@theia/core/shared/vscode-languageserver-protocol';
45
+ import { UTF8 } from '@theia/core/lib/common/encodings';
46
+ import * as monaco from '@theia/monaco-editor-core';
47
+ import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
48
+ import { ILanguageService } from '@theia/monaco-editor-core/esm/vs/editor/common/languages/language';
49
+ import { IInstantiationService, ServiceIdentifier } from '@theia/monaco-editor-core/esm/vs/platform/instantiation/common/instantiation';
50
+ import { ICodeEditor, IMouseTargetMargin } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser';
51
+ import { IStandaloneEditorConstructionOptions, StandaloneCodeEditor, StandaloneEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor';
52
+ import { ServiceCollection } from '@theia/monaco-editor-core/esm/vs/platform/instantiation/common/serviceCollection';
53
+ import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
54
+ import { ConfigurationChangedEvent, IEditorOptions } from '@theia/monaco-editor-core/esm/vs/editor/common/config/editorOptions';
55
+ import { ICodeEditorService } from '@theia/monaco-editor-core/esm/vs/editor/browser/services/codeEditorService';
56
+ import { ICommandService } from '@theia/monaco-editor-core/esm/vs/platform/commands/common/commands';
57
+ import { IContextKeyService } from '@theia/monaco-editor-core/esm/vs/platform/contextkey/common/contextkey';
58
+ import { IKeybindingService } from '@theia/monaco-editor-core/esm/vs/platform/keybinding/common/keybinding';
59
+ import { IThemeService } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
60
+ import { INotificationService } from '@theia/monaco-editor-core/esm/vs/platform/notification/common/notification';
61
+ import { IAccessibilityService } from '@theia/monaco-editor-core/esm/vs/platform/accessibility/common/accessibility';
62
+ import { ILanguageConfigurationService } from '@theia/monaco-editor-core/esm/vs/editor/common/languages/languageConfigurationRegistry';
63
+ import { ILanguageFeaturesService } from '@theia/monaco-editor-core/esm/vs/editor/common/services/languageFeatures';
64
+ import * as objects from '@theia/monaco-editor-core/esm/vs/base/common/objects';
65
+ import { Selection } from '@theia/editor/lib/browser/editor';
66
+
67
+ export type ServicePair<T> = [ServiceIdentifier<T>, T];
68
+
69
+ export interface EditorServiceOverrides extends Iterable<ServicePair<unknown>> { }
70
+
71
+ @injectable()
72
+ export class MonacoEditorServices {
73
+
74
+ @inject(MonacoToProtocolConverter)
75
+ protected readonly m2p: MonacoToProtocolConverter;
76
+
77
+ @inject(ProtocolToMonacoConverter)
78
+ protected readonly p2m: ProtocolToMonacoConverter;
79
+
80
+ @inject(ContextKeyService)
81
+ protected readonly contextKeyService: ContextKeyService;
82
+
83
+ constructor(@unmanaged() services: MonacoEditorServices) {
84
+ Object.assign(this, services);
85
+ }
86
+ }
87
+
88
+ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
89
+
90
+ protected readonly toDispose = new DisposableCollection();
91
+
92
+ protected readonly autoSizing: boolean;
93
+ protected readonly minHeight: number;
94
+ protected readonly maxHeight: number;
95
+ protected editor: monaco.editor.IStandaloneCodeEditor;
96
+
97
+ protected readonly onCursorPositionChangedEmitter = new Emitter<Position>();
98
+ protected readonly onSelectionChangedEmitter = new Emitter<Selection>();
99
+ protected readonly onFocusChangedEmitter = new Emitter<boolean>();
100
+ protected readonly onDocumentContentChangedEmitter = new Emitter<TextDocumentChangeEvent>();
101
+ protected readonly onMouseDownEmitter = new Emitter<EditorMouseEvent>();
102
+ readonly onDidChangeReadOnly = this.document.onDidChangeReadOnly;
103
+ protected readonly onLanguageChangedEmitter = new Emitter<string>();
104
+ readonly onLanguageChanged = this.onLanguageChangedEmitter.event;
105
+ protected readonly onScrollChangedEmitter = new Emitter<void>();
106
+ readonly onEncodingChanged = this.document.onDidChangeEncoding;
107
+ protected readonly onResizeEmitter = new Emitter<Dimension | null>();
108
+ readonly onDidResize = this.onResizeEmitter.event;
109
+
110
+ readonly documents = new Set<MonacoEditorModel>();
111
+
112
+ constructor(
113
+ readonly uri: URI,
114
+ readonly document: MonacoEditorModel,
115
+ readonly node: HTMLElement,
116
+ services: MonacoEditorServices,
117
+ options?: MonacoEditor.IOptions,
118
+ override?: EditorServiceOverrides,
119
+ readonly parentEditor?: MonacoEditor
120
+ ) {
121
+ super(services);
122
+ this.toDispose.pushAll([
123
+ this.onCursorPositionChangedEmitter,
124
+ this.onSelectionChangedEmitter,
125
+ this.onFocusChangedEmitter,
126
+ this.onDocumentContentChangedEmitter,
127
+ this.onMouseDownEmitter,
128
+ this.onLanguageChangedEmitter,
129
+ this.onScrollChangedEmitter
130
+ ]);
131
+ this.documents.add(document);
132
+ this.autoSizing = options && options.autoSizing !== undefined ? options.autoSizing : false;
133
+ this.minHeight = options && options.minHeight !== undefined ? options.minHeight : -1;
134
+ this.maxHeight = options && options.maxHeight !== undefined ? options.maxHeight : -1;
135
+ this.toDispose.push(this.create({
136
+ ...MonacoEditor.createReadOnlyOptions(document.readOnly),
137
+ ...options
138
+ }, override));
139
+ this.addHandlers(this.editor);
140
+ }
141
+
142
+ getEncoding(): string {
143
+ return this.document.getEncoding() || UTF8;
144
+ }
145
+
146
+ setEncoding(encoding: string, mode: EncodingMode): Promise<void> {
147
+ return this.document.setEncoding(encoding, mode);
148
+ }
149
+
150
+ protected create(options?: monaco.editor.IStandaloneEditorConstructionOptions | IStandaloneEditorConstructionOptions, override?: EditorServiceOverrides): Disposable {
151
+ const combinedOptions = {
152
+ ...options,
153
+ lightbulb: { enabled: true },
154
+ fixedOverflowWidgets: true,
155
+ scrollbar: {
156
+ useShadows: false,
157
+ verticalHasArrows: false,
158
+ horizontalHasArrows: false,
159
+ verticalScrollbarSize: 10,
160
+ horizontalScrollbarSize: 10,
161
+ ...options?.scrollbar,
162
+ }
163
+ } as IStandaloneEditorConstructionOptions;
164
+ const instantiator = this.getInstantiatorWithOverrides(override);
165
+ /**
166
+ * @monaco-uplift. Should be guaranteed to work.
167
+ * Incomparable enums prevent TypeScript from believing that public IStandaloneCodeEditor is satisfied by private StandaloneCodeEditor
168
+ */
169
+ return this.editor = (this.parentEditor ?
170
+ instantiator.createInstance(EmbeddedCodeEditor, this.node, combinedOptions, this.parentEditor.getControl() as unknown as ICodeEditor) :
171
+ instantiator.createInstance(StandaloneEditor, this.node, combinedOptions)) as unknown as monaco.editor.IStandaloneCodeEditor;
172
+ }
173
+
174
+ protected getInstantiatorWithOverrides(override?: EditorServiceOverrides): IInstantiationService {
175
+ const instantiator = StandaloneServices.get(IInstantiationService);
176
+ if (override) {
177
+ const overrideServices = new ServiceCollection(...override);
178
+ return instantiator.createChild(overrideServices);
179
+ }
180
+ return instantiator;
181
+ }
182
+
183
+ protected addHandlers(codeEditor: monaco.editor.IStandaloneCodeEditor): void {
184
+ this.toDispose.push(codeEditor.onDidChangeModelLanguage(e =>
185
+ this.fireLanguageChanged(e.newLanguage)
186
+ ));
187
+ this.toDispose.push(codeEditor.onDidChangeConfiguration(() => this.refresh()));
188
+ this.toDispose.push(codeEditor.onDidChangeModel(() => this.refresh()));
189
+ this.toDispose.push(codeEditor.onDidChangeModelContent(e => {
190
+ this.refresh();
191
+ this.onDocumentContentChangedEmitter.fire({ document: this.document, contentChanges: e.changes.map(this.mapModelContentChange.bind(this)) });
192
+ }));
193
+ this.toDispose.push(codeEditor.onDidChangeCursorPosition(() =>
194
+ this.onCursorPositionChangedEmitter.fire(this.cursor)
195
+ ));
196
+ this.toDispose.push(codeEditor.onDidChangeCursorSelection(event =>
197
+ this.onSelectionChangedEmitter.fire({
198
+ ...this.m2p.asRange(event.selection),
199
+ direction: event.selection.getDirection() === monaco.SelectionDirection.LTR ? 'ltr' : 'rtl'
200
+ })
201
+ ));
202
+ this.toDispose.push(codeEditor.onDidFocusEditorText(() =>
203
+ this.onFocusChangedEmitter.fire(this.isFocused())
204
+ ));
205
+ this.toDispose.push(codeEditor.onDidBlurEditorText(() =>
206
+ this.onFocusChangedEmitter.fire(this.isFocused())
207
+ ));
208
+ this.toDispose.push(codeEditor.onMouseDown(e => {
209
+ const { element, position, range } = e.target;
210
+ this.onMouseDownEmitter.fire({
211
+ target: {
212
+ type: e.target.type as unknown as MouseTargetType,
213
+ element: element || undefined,
214
+ mouseColumn: this.m2p.asPosition(undefined, e.target.mouseColumn).character,
215
+ range: range && this.m2p.asRange(range) || undefined,
216
+ position: position && this.m2p.asPosition(position.lineNumber, position.column) || undefined,
217
+ detail: (e.target as unknown as IMouseTargetMargin).detail || {},
218
+ },
219
+ event: e.event.browserEvent
220
+ });
221
+ }));
222
+ this.toDispose.push(codeEditor.onDidScrollChange(e => {
223
+ this.onScrollChangedEmitter.fire(undefined);
224
+ }));
225
+ this.toDispose.push(this.onDidChangeReadOnly(readOnly => {
226
+ codeEditor.updateOptions(MonacoEditor.createReadOnlyOptions(readOnly));
227
+ }));
228
+ }
229
+
230
+ getVisibleRanges(): Range[] {
231
+ return this.editor.getVisibleRanges().map(range => this.m2p.asRange(range));
232
+ }
233
+
234
+ protected mapModelContentChange(change: monaco.editor.IModelContentChange): TextDocumentContentChangeDelta {
235
+ return {
236
+ range: this.m2p.asRange(change.range),
237
+ rangeLength: change.rangeLength,
238
+ text: change.text
239
+ };
240
+ }
241
+
242
+ get onDispose(): Event<void> {
243
+ return this.toDispose.onDispose;
244
+ }
245
+
246
+ get onDocumentContentChanged(): Event<TextDocumentChangeEvent> {
247
+ return this.onDocumentContentChangedEmitter.event;
248
+ }
249
+
250
+ get isReadonly(): boolean | MarkdownString {
251
+ return this.document.readOnly;
252
+ }
253
+
254
+ get cursor(): Position {
255
+ const { lineNumber, column } = this.editor.getPosition()!;
256
+ return this.m2p.asPosition(lineNumber, column);
257
+ }
258
+
259
+ set cursor(cursor: Position) {
260
+ const position = this.p2m.asPosition(cursor);
261
+ this.editor.setPosition(position);
262
+ }
263
+
264
+ get onCursorPositionChanged(): Event<Position> {
265
+ return this.onCursorPositionChangedEmitter.event;
266
+ }
267
+
268
+ get selection(): Selection {
269
+ return this.m2p.asSelection(this.editor.getSelection()!);
270
+ }
271
+
272
+ set selection(selection: Selection) {
273
+ const range = this.p2m.asRange(selection);
274
+ this.editor.setSelection(range);
275
+ }
276
+
277
+ get onSelectionChanged(): Event<Selection> {
278
+ return this.onSelectionChangedEmitter.event;
279
+ }
280
+
281
+ get onScrollChanged(): Event<void> {
282
+ return this.onScrollChangedEmitter.event;
283
+ }
284
+
285
+ revealPosition(raw: Position, options: RevealPositionOptions = { vertical: 'center' }): void {
286
+ const position = this.p2m.asPosition(raw);
287
+ switch (options.vertical) {
288
+ case 'auto':
289
+ this.editor.revealPosition(position);
290
+ break;
291
+ case 'center':
292
+ this.editor.revealPositionInCenter(position);
293
+ break;
294
+ case 'centerIfOutsideViewport':
295
+ this.editor.revealPositionInCenterIfOutsideViewport(position);
296
+ break;
297
+ }
298
+ }
299
+
300
+ revealRange(raw: Range, options: RevealRangeOptions = { at: 'center' }): void {
301
+ const range = this.p2m.asRange(raw);
302
+ switch (options.at) {
303
+ case 'top':
304
+ this.editor.revealRangeAtTop(range!);
305
+ break;
306
+ case 'center':
307
+ this.editor.revealRangeInCenter(range!);
308
+ break;
309
+ case 'centerIfOutsideViewport':
310
+ this.editor.revealRangeInCenterIfOutsideViewport(range!);
311
+ break;
312
+ case 'auto':
313
+ this.editor.revealRange(range!);
314
+ break;
315
+ }
316
+ }
317
+
318
+ focus(): void {
319
+ /**
320
+ * `this.editor.focus` forcefully changes the focus editor state,
321
+ * regardless whether the textarea actually received the focus.
322
+ * It could lead to issues like https://github.com/eclipse-theia/theia/issues/7902
323
+ * Instead we focus the underlying textarea.
324
+ */
325
+ const node = this.editor.getDomNode();
326
+ if (node) {
327
+ const textarea = node.querySelector('textarea') as HTMLElement;
328
+ textarea.focus();
329
+ }
330
+ }
331
+
332
+ blur(): void {
333
+ const node = this.editor.getDomNode();
334
+ if (node) {
335
+ const textarea = node.querySelector('textarea') as HTMLElement;
336
+ textarea.blur();
337
+ }
338
+ }
339
+
340
+ isFocused({ strict }: { strict: boolean } = { strict: false }): boolean {
341
+ if (!this.editor.hasTextFocus()) {
342
+ return false;
343
+ }
344
+ if (strict) {
345
+ return !this.isSuggestWidgetVisible() && !this.isFindWidgetVisible() && !this.isRenameInputVisible();
346
+ }
347
+ return true;
348
+ }
349
+
350
+ get onFocusChanged(): Event<boolean> {
351
+ return this.onFocusChangedEmitter.event;
352
+ }
353
+
354
+ get onMouseDown(): Event<EditorMouseEvent> {
355
+ return this.onMouseDownEmitter.event;
356
+ }
357
+
358
+ /**
359
+ * `true` if the suggest widget is visible in the editor. Otherwise, `false`.
360
+ */
361
+ isSuggestWidgetVisible(): boolean {
362
+ return this.contextKeyService.match('suggestWidgetVisible', this.editor.getDomNode() || this.node);
363
+ }
364
+
365
+ /**
366
+ * `true` if the find (and replace) widget is visible in the editor. Otherwise, `false`.
367
+ */
368
+ isFindWidgetVisible(): boolean {
369
+ return this.contextKeyService.match('findWidgetVisible', this.editor.getDomNode() || this.node);
370
+ }
371
+
372
+ /**
373
+ * `true` if the name rename refactoring input HTML element is visible. Otherwise, `false`.
374
+ */
375
+ isRenameInputVisible(): boolean {
376
+ return this.contextKeyService.match('renameInputVisible', this.editor.getDomNode() || this.node);
377
+ }
378
+
379
+ dispose(): void {
380
+ this.toDispose.dispose();
381
+ }
382
+
383
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
384
+ trigger(source: string, handlerId: string, payload: any): void {
385
+ this.editor.trigger(source, handlerId, payload);
386
+ }
387
+
388
+ getControl(): monaco.editor.IStandaloneCodeEditor {
389
+ return this.editor;
390
+ }
391
+
392
+ refresh(): void {
393
+ this.autoresize();
394
+ }
395
+
396
+ resizeToFit(): void {
397
+ this.autoresize();
398
+ // eslint-disable-next-line no-null/no-null
399
+ this.onResizeEmitter.fire(null);
400
+ }
401
+
402
+ setSize(dimension: Dimension): void {
403
+ this.resize(dimension);
404
+ this.onResizeEmitter.fire(dimension);
405
+ }
406
+
407
+ protected autoresize(): void {
408
+ if (this.autoSizing) {
409
+ // eslint-disable-next-line no-null/no-null
410
+ this.resize(null);
411
+ }
412
+ }
413
+
414
+ protected resize(dimension: Dimension | null): void {
415
+ if (this.node) {
416
+ const layoutSize = this.computeLayoutSize(this.node, dimension);
417
+ this.editor.layout(layoutSize);
418
+ }
419
+ }
420
+
421
+ protected computeLayoutSize(hostNode: HTMLElement, dimension: monaco.editor.IDimension | null): monaco.editor.IDimension {
422
+ if (dimension && dimension.width >= 0 && dimension.height >= 0) {
423
+ return dimension;
424
+ }
425
+ const boxSizing = ElementExt.boxSizing(hostNode);
426
+
427
+ const width = (!dimension || dimension.width < 0) ?
428
+ this.getWidth(hostNode, boxSizing) :
429
+ dimension.width;
430
+
431
+ const height = (!dimension || dimension.height < 0) ?
432
+ this.getHeight(hostNode, boxSizing) :
433
+ dimension.height;
434
+
435
+ return { width, height };
436
+ }
437
+
438
+ protected getWidth(hostNode: HTMLElement, boxSizing: ElementExt.IBoxSizing): number {
439
+ return hostNode.offsetWidth - boxSizing.horizontalSum;
440
+ }
441
+
442
+ protected getHeight(hostNode: HTMLElement, boxSizing: ElementExt.IBoxSizing): number {
443
+ if (!this.autoSizing) {
444
+ return hostNode.offsetHeight - boxSizing.verticalSum;
445
+ }
446
+
447
+ const lineHeight = this.editor.getOption(monaco.editor.EditorOption.lineHeight);
448
+ const lineCount = this.editor.getModel()!.getLineCount();
449
+ const contentHeight = lineHeight * lineCount;
450
+
451
+ const horizontalScrollbarHeight = this.editor.getLayoutInfo().horizontalScrollbarHeight;
452
+
453
+ const editorHeight = contentHeight + horizontalScrollbarHeight;
454
+ if (this.minHeight >= 0) {
455
+ const minHeight = lineHeight * this.minHeight + horizontalScrollbarHeight;
456
+ if (editorHeight < minHeight) {
457
+ return minHeight;
458
+ }
459
+ }
460
+ if (this.maxHeight >= 0) {
461
+ const maxHeight = lineHeight * this.maxHeight + horizontalScrollbarHeight;
462
+ return Math.min(maxHeight, editorHeight);
463
+ }
464
+ return editorHeight;
465
+ }
466
+
467
+ isActionSupported(id: string): boolean {
468
+ const action = this.editor.getAction(id);
469
+ return !!action && action.isSupported();
470
+ }
471
+
472
+ async runAction(id: string): Promise<void> {
473
+ const action = this.editor.getAction(id);
474
+ if (action && action.isSupported()) {
475
+ await action.run();
476
+ }
477
+ }
478
+
479
+ deltaDecorations(params: DeltaDecorationParams): string[] {
480
+ const oldDecorations = params.oldDecorations;
481
+ const newDecorations = this.toDeltaDecorations(params);
482
+ return this.editor.deltaDecorations(oldDecorations, newDecorations);
483
+ }
484
+
485
+ protected toDeltaDecorations(params: DeltaDecorationParams): monaco.editor.IModelDeltaDecoration[] {
486
+ return params.newDecorations.map(({ options: theiaOptions, range }) => {
487
+ const options: monaco.editor.IModelDecorationOptions = {
488
+ ...theiaOptions,
489
+ hoverMessage: this.fromStringToMarkdownString(theiaOptions.hoverMessage),
490
+ glyphMarginHoverMessage: this.fromStringToMarkdownString(theiaOptions.glyphMarginHoverMessage)
491
+ };
492
+ return {
493
+ options,
494
+ range: this.p2m.asRange(range),
495
+ };
496
+ });
497
+ }
498
+
499
+ protected fromStringToMarkdownString(hoverMessage?: string | monaco.IMarkdownString | monaco.IMarkdownString[]): monaco.IMarkdownString | monaco.IMarkdownString[] | undefined {
500
+ if (typeof hoverMessage === 'string') {
501
+ return { value: hoverMessage };
502
+ }
503
+ return hoverMessage;
504
+ }
505
+
506
+ protected fromMarkdownToString(maybeMarkdown?: null | string | monaco.IMarkdownString | monaco.IMarkdownString[]): string | undefined {
507
+ if (!maybeMarkdown) {
508
+ return undefined;
509
+ }
510
+ if (typeof maybeMarkdown === 'string') {
511
+ return maybeMarkdown;
512
+ }
513
+ if (Array.isArray(maybeMarkdown)) {
514
+ return maybeMarkdown.map(({ value }) => value).join('\n');
515
+ }
516
+ return maybeMarkdown.value;
517
+ }
518
+
519
+ getLinesDecorations(startLineNumber: number, endLineNumber: number): (EditorDecoration & Readonly<{ id: string }>)[] {
520
+ const toPosition = (line: number): monaco.Position => this.p2m.asPosition({ line, character: 0 });
521
+ const start = toPosition(startLineNumber).lineNumber;
522
+ const end = toPosition(endLineNumber).lineNumber;
523
+ return this.editor
524
+ .getModel()!
525
+ .getLinesDecorations(start, end)
526
+ .map(this.toEditorDecoration.bind(this));
527
+ }
528
+
529
+ protected toEditorDecoration(decoration: monaco.editor.IModelDecoration): EditorDecoration & Readonly<{ id: string }> {
530
+ const range = this.m2p.asRange(decoration.range);
531
+ const { id, options: monacoOptions } = decoration;
532
+ const options: MaybeNull<EditorDecorationOptions> = {
533
+ ...monacoOptions,
534
+ hoverMessage: this.fromMarkdownToString(monacoOptions.hoverMessage),
535
+ glyphMarginHoverMessage: this.fromMarkdownToString(monacoOptions.hoverMessage),
536
+ };
537
+ return {
538
+ options: nullToUndefined(options),
539
+ range,
540
+ id
541
+ };
542
+ }
543
+
544
+ getVisibleColumn(position: Position): number {
545
+ return this.editor.getVisibleColumnFromPosition(this.p2m.asPosition(position));
546
+ }
547
+
548
+ async replaceText(params: ReplaceTextParams): Promise<boolean> {
549
+ const edits: monaco.editor.IIdentifiedSingleEditOperation[] = params.replaceOperations.map(param => {
550
+ const range = monaco.Range.fromPositions(this.p2m.asPosition(param.range.start), this.p2m.asPosition(param.range.end));
551
+ return {
552
+ forceMoveMarkers: true,
553
+ identifier: {
554
+ major: range.startLineNumber,
555
+ minor: range.startColumn
556
+ },
557
+ range,
558
+ text: param.text
559
+ };
560
+ });
561
+ return this.editor.executeEdits(params.source, edits);
562
+ }
563
+
564
+ executeEdits(edits: TextEdit[]): boolean {
565
+ return this.editor.executeEdits('MonacoEditor', this.p2m.asTextEdits(edits) as monaco.editor.IIdentifiedSingleEditOperation[]);
566
+ }
567
+
568
+ storeViewState(): object {
569
+ return this.editor.saveViewState()!;
570
+ }
571
+
572
+ restoreViewState(state: monaco.editor.ICodeEditorViewState): void {
573
+ this.editor.restoreViewState(state);
574
+ }
575
+
576
+ /* `true` because it is derived from an URI during the instantiation */
577
+ protected _languageAutoDetected = true;
578
+
579
+ get languageAutoDetected(): boolean {
580
+ return this._languageAutoDetected;
581
+ }
582
+
583
+ async detectLanguage(): Promise<void> {
584
+ const languageService = StandaloneServices.get(ILanguageService);
585
+ const firstLine = this.document.textEditorModel.getLineContent(1);
586
+ const model = this.getControl().getModel();
587
+ const language = languageService.createByFilepathOrFirstLine(model && model.uri, firstLine);
588
+ this.setLanguage(language.languageId);
589
+ this._languageAutoDetected = true;
590
+ }
591
+
592
+ setLanguage(languageId: string): void {
593
+ for (const document of this.documents) {
594
+ monaco.editor.setModelLanguage(document.textEditorModel, languageId);
595
+ }
596
+ }
597
+
598
+ protected fireLanguageChanged(languageId: string): void {
599
+ this._languageAutoDetected = false;
600
+ this.onLanguageChangedEmitter.fire(languageId);
601
+ }
602
+
603
+ getResourceUri(): URI {
604
+ return this.uri;
605
+ }
606
+ createMoveToUri(resourceUri: URI): URI {
607
+ return this.uri.withPath(resourceUri.path);
608
+ }
609
+
610
+ shouldDisplayDirtyDiff(): boolean {
611
+ return true;
612
+ }
613
+ }
614
+
615
+ export namespace MonacoEditor {
616
+ export interface ICommonOptions {
617
+ /**
618
+ * Whether an editor should be auto resized on a content change.
619
+ *
620
+ * #### Fixme
621
+ * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
622
+ */
623
+ autoSizing?: boolean;
624
+ /**
625
+ * A minimal height of an editor in lines.
626
+ *
627
+ * #### Fixme
628
+ * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
629
+ */
630
+ minHeight?: number;
631
+ /**
632
+ * A maximal height of an editor in lines.
633
+ *
634
+ * #### Fixme
635
+ * remove when https://github.com/Microsoft/monaco-editor/issues/103 is resolved
636
+ */
637
+ maxHeight?: number;
638
+ }
639
+
640
+ export interface IOptions extends ICommonOptions, monaco.editor.IStandaloneEditorConstructionOptions { }
641
+
642
+ export function getAll(manager: EditorManager): MonacoEditor[] {
643
+ return manager.all.map(e => get(e)).filter(e => !!e) as MonacoEditor[];
644
+ }
645
+
646
+ export function getCurrent(manager: EditorManager): MonacoEditor | undefined {
647
+ return get(manager.currentEditor);
648
+ }
649
+
650
+ export function getActive(manager: EditorManager): MonacoEditor | undefined {
651
+ return get(manager.activeEditor);
652
+ }
653
+
654
+ export function get(editorWidget: EditorWidget | undefined): MonacoEditor | undefined {
655
+ if (editorWidget && editorWidget.editor instanceof MonacoEditor) {
656
+ return editorWidget.editor;
657
+ }
658
+ return undefined;
659
+ }
660
+
661
+ export function findByDocument(manager: EditorManager, document: MonacoEditorModel): MonacoEditor[] {
662
+ return getAll(manager).filter(candidate => candidate.documents.has(document));
663
+ }
664
+
665
+ export function getWidgetFor(manager: EditorManager, control: monaco.editor.ICodeEditor | ICodeEditor | undefined | null): EditorWidget | undefined {
666
+ if (!control) {
667
+ return undefined;
668
+ }
669
+ return manager.all.find(widget => {
670
+ const candidate = get(widget);
671
+ return candidate && candidate.getControl() === control;
672
+ });
673
+ }
674
+
675
+ export function createReadOnlyOptions(readOnly?: boolean | MarkdownString): monaco.editor.IEditorOptions {
676
+ if (typeof readOnly === 'boolean') {
677
+ return { readOnly, readOnlyMessage: undefined };
678
+ }
679
+ if (readOnly) {
680
+ return { readOnly: true, readOnlyMessage: readOnly };
681
+ }
682
+ return {};
683
+ }
684
+ }
685
+
686
+ // adapted from https://github.com/microsoft/vscode/blob/0bd70d48ad8b3e2fb1922aa54f87c786ff2b4bd8/src/vs/editor/browser/widget/codeEditor/embeddedCodeEditorWidget.ts
687
+ // This class reproduces the logic in EmbeddedCodeEditorWidget but extends StandaloneCodeEditor rather than CodeEditorWidget.
688
+ class EmbeddedCodeEditor extends StandaloneCodeEditor {
689
+
690
+ private readonly _parentEditor: ICodeEditor;
691
+ private readonly _overwriteOptions: IEditorOptions;
692
+
693
+ constructor(
694
+ domElement: HTMLElement,
695
+ options: Readonly<IStandaloneEditorConstructionOptions>,
696
+ parentEditor: ICodeEditor,
697
+ @IInstantiationService instantiationService: IInstantiationService,
698
+ @ICodeEditorService codeEditorService: ICodeEditorService,
699
+ @ICommandService commandService: ICommandService,
700
+ @IContextKeyService contextKeyService: IContextKeyService,
701
+ @IKeybindingService keybindingService: IKeybindingService,
702
+ @IThemeService themeService: IThemeService,
703
+ @INotificationService notificationService: INotificationService,
704
+ @IAccessibilityService accessibilityService: IAccessibilityService,
705
+ @ILanguageConfigurationService languageConfigurationService: ILanguageConfigurationService,
706
+ @ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
707
+ ) {
708
+ super(domElement, { ...parentEditor.getRawOptions(), overflowWidgetsDomNode: parentEditor.getOverflowWidgetsDomNode() }, instantiationService, codeEditorService,
709
+ commandService, contextKeyService, keybindingService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
710
+
711
+ this._parentEditor = parentEditor;
712
+ this._overwriteOptions = options;
713
+
714
+ // Overwrite parent's options
715
+ super.updateOptions(this._overwriteOptions);
716
+
717
+ this._register(parentEditor.onDidChangeConfiguration((e: ConfigurationChangedEvent) => this._onParentConfigurationChanged(e)));
718
+ }
719
+
720
+ getParentEditor(): ICodeEditor {
721
+ return this._parentEditor;
722
+ }
723
+
724
+ private _onParentConfigurationChanged(e: ConfigurationChangedEvent): void {
725
+ super.updateOptions(this._parentEditor.getRawOptions());
726
+ super.updateOptions(this._overwriteOptions);
727
+ }
728
+
729
+ override updateOptions(newOptions: IEditorOptions): void {
730
+ objects.mixin(this._overwriteOptions, newOptions, true);
731
+ super.updateOptions(this._overwriteOptions);
732
+ }
733
+ }