@theia/monaco 1.34.2 → 1.34.3

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