@theia/monaco 1.45.1 → 1.46.0-next.72

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