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

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