@theia/monaco 1.34.3 → 1.34.4

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,556 +1,556 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2017 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.MonacoEditorModel = exports.TextDocumentSaveReason = void 0;
19
- const vscode_languageserver_protocol_1 = require("@theia/core/shared/vscode-languageserver-protocol");
20
- Object.defineProperty(exports, "TextDocumentSaveReason", { enumerable: true, get: function () { return vscode_languageserver_protocol_1.TextDocumentSaveReason; } });
21
- const disposable_1 = require("@theia/core/lib/common/disposable");
22
- const event_1 = require("@theia/core/lib/common/event");
23
- const cancellation_1 = require("@theia/core/lib/common/cancellation");
24
- const resource_1 = require("@theia/core/lib/common/resource");
25
- const monaco = require("@theia/monaco-editor-core");
26
- const standaloneServices_1 = require("@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices");
27
- const language_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/languages/language");
28
- const model_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/services/model");
29
- const textModel_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/model/textModel");
30
- const editor_generated_preference_schema_1 = require("@theia/editor/lib/browser/editor-generated-preference-schema");
31
- class MonacoEditorModel {
32
- constructor(resource, m2p, p2m, logger, editorPreferences) {
33
- this.resource = resource;
34
- this.m2p = m2p;
35
- this.p2m = p2m;
36
- this.logger = logger;
37
- this.editorPreferences = editorPreferences;
38
- this.autoSave = 'afterDelay';
39
- this.autoSaveDelay = 500;
40
- this.suppressOpenEditorWhenDirty = false;
41
- this.lineNumbersMinChars = 3;
42
- /* @deprecated there is no general save timeout, each participant should introduce a sensible timeout */
43
- this.onWillSaveLoopTimeOut = 1500;
44
- this.toDispose = new disposable_1.DisposableCollection();
45
- this.toDisposeOnAutoSave = new disposable_1.DisposableCollection();
46
- this.onDidChangeContentEmitter = new event_1.Emitter();
47
- this.onDidChangeContent = this.onDidChangeContentEmitter.event;
48
- this.onDidSaveModelEmitter = new event_1.Emitter();
49
- this.onDidSaveModel = this.onDidSaveModelEmitter.event;
50
- this.onWillSaveModelEmitter = new event_1.Emitter();
51
- this.onWillSaveModel = this.onWillSaveModelEmitter.event;
52
- this.onDidChangeValidEmitter = new event_1.Emitter();
53
- this.onDidChangeValid = this.onDidChangeValidEmitter.event;
54
- this.onDidChangeEncodingEmitter = new event_1.Emitter();
55
- this.onDidChangeEncoding = this.onDidChangeEncodingEmitter.event;
56
- /**
57
- * Use `valid` to access it.
58
- * Use `setValid` to mutate it.
59
- */
60
- this._valid = false;
61
- this._dirty = false;
62
- this.onDirtyChangedEmitter = new event_1.Emitter();
63
- this.pendingOperation = Promise.resolve();
64
- this.syncCancellationTokenSource = new cancellation_1.CancellationTokenSource();
65
- this.ignoreDirtyEdits = false;
66
- this.saveCancellationTokenSource = new cancellation_1.CancellationTokenSource();
67
- this.ignoreContentChanges = false;
68
- this.contentChanges = [];
69
- this.toDispose.push(resource);
70
- this.toDispose.push(this.toDisposeOnAutoSave);
71
- this.toDispose.push(this.onDidChangeContentEmitter);
72
- this.toDispose.push(this.onDidSaveModelEmitter);
73
- this.toDispose.push(this.onWillSaveModelEmitter);
74
- this.toDispose.push(this.onDirtyChangedEmitter);
75
- this.toDispose.push(this.onDidChangeValidEmitter);
76
- this.toDispose.push(disposable_1.Disposable.create(() => this.cancelSave()));
77
- this.toDispose.push(disposable_1.Disposable.create(() => this.cancelSync()));
78
- this.resolveModel = this.readContents().then(content => this.initialize(content || ''));
79
- }
80
- dispose() {
81
- this.toDispose.dispose();
82
- }
83
- isDisposed() {
84
- return this.toDispose.disposed;
85
- }
86
- resolve() {
87
- return this.resolveModel;
88
- }
89
- isResolved() {
90
- return Boolean(this.model);
91
- }
92
- setEncoding(encoding, mode) {
93
- if (mode === 1 /* Decode */ && this.dirty) {
94
- return Promise.resolve();
95
- }
96
- if (!this.setPreferredEncoding(encoding)) {
97
- return Promise.resolve();
98
- }
99
- if (mode === 1 /* Decode */) {
100
- return this.sync();
101
- }
102
- return this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual, this.cancelSave(), true);
103
- }
104
- getEncoding() {
105
- return this.preferredEncoding || this.contentEncoding;
106
- }
107
- setPreferredEncoding(encoding) {
108
- if (encoding === this.preferredEncoding || (!this.preferredEncoding && encoding === this.contentEncoding)) {
109
- return false;
110
- }
111
- this.preferredEncoding = encoding;
112
- this.onDidChangeEncodingEmitter.fire(encoding);
113
- return true;
114
- }
115
- updateContentEncoding() {
116
- const contentEncoding = this.resource.encoding;
117
- if (!contentEncoding || this.contentEncoding === contentEncoding) {
118
- return;
119
- }
120
- this.contentEncoding = contentEncoding;
121
- if (!this.preferredEncoding) {
122
- this.onDidChangeEncodingEmitter.fire(contentEncoding);
123
- }
124
- }
125
- /**
126
- * #### Important
127
- * Only this method can create an instance of `monaco.editor.IModel`,
128
- * there should not be other calls to `monaco.editor.createModel`.
129
- */
130
- initialize(value) {
131
- if (!this.toDispose.disposed) {
132
- const uri = monaco.Uri.parse(this.resource.uri.toString());
133
- let firstLine;
134
- if (typeof value === 'string') {
135
- firstLine = value;
136
- const firstLF = value.indexOf('\n');
137
- if (firstLF !== -1) {
138
- firstLine = value.substring(0, firstLF);
139
- }
140
- }
141
- else {
142
- firstLine = value.getFirstLineText(1000);
143
- }
144
- const languageSelection = standaloneServices_1.StandaloneServices.get(language_1.ILanguageService).createByFilepathOrFirstLine(uri, firstLine);
145
- this.model = standaloneServices_1.StandaloneServices.get(model_1.IModelService).createModel(value, languageSelection, uri);
146
- this.resourceVersion = this.resource.version;
147
- this.setDirty(this._dirty || (this.resource.uri.scheme === resource_1.UNTITLED_SCHEME && this.model.getValueLength() > 0));
148
- this.updateSavedVersionId();
149
- this.toDispose.push(this.model);
150
- this.toDispose.push(this.model.onDidChangeContent(event => this.fireDidChangeContent(event)));
151
- if (this.resource.onDidChangeContents) {
152
- this.toDispose.push(this.resource.onDidChangeContents(() => this.sync()));
153
- }
154
- }
155
- }
156
- /**
157
- * Whether it is possible to load content from the underlying resource.
158
- */
159
- get valid() {
160
- return this._valid;
161
- }
162
- setValid(valid) {
163
- if (valid === this._valid) {
164
- return;
165
- }
166
- this._valid = valid;
167
- this.onDidChangeValidEmitter.fire(undefined);
168
- }
169
- get dirty() {
170
- return this._dirty;
171
- }
172
- setDirty(dirty) {
173
- if (dirty === this._dirty) {
174
- return;
175
- }
176
- this._dirty = dirty;
177
- if (dirty === false) {
178
- this.updateSavedVersionId();
179
- }
180
- this.onDirtyChangedEmitter.fire(undefined);
181
- }
182
- updateSavedVersionId() {
183
- this.bufferSavedVersionId = this.model.getAlternativeVersionId();
184
- }
185
- get onDirtyChanged() {
186
- return this.onDirtyChangedEmitter.event;
187
- }
188
- get uri() {
189
- return this.resource.uri.toString();
190
- }
191
- get languageId() {
192
- return this._languageId !== undefined ? this._languageId : this.model.getLanguageId();
193
- }
194
- getLanguageId() {
195
- return this.languageId;
196
- }
197
- /**
198
- * It's a hack to dispatch close notification with an old language id; don't use it.
199
- */
200
- setLanguageId(languageId) {
201
- this._languageId = languageId;
202
- }
203
- get version() {
204
- return this.model.getVersionId();
205
- }
206
- /**
207
- * Return selected text by Range or all text by default
208
- */
209
- getText(range) {
210
- if (!range) {
211
- return this.model.getValue();
212
- }
213
- else {
214
- return this.model.getValueInRange(this.p2m.asRange(range));
215
- }
216
- }
217
- positionAt(offset) {
218
- const { lineNumber, column } = this.model.getPositionAt(offset);
219
- return this.m2p.asPosition(lineNumber, column);
220
- }
221
- offsetAt(position) {
222
- return this.model.getOffsetAt(this.p2m.asPosition(position));
223
- }
224
- get lineCount() {
225
- return this.model.getLineCount();
226
- }
227
- /**
228
- * Retrieves a line in a text document expressed as a one-based position.
229
- */
230
- getLineContent(lineNumber) {
231
- return this.model.getLineContent(lineNumber);
232
- }
233
- getLineMaxColumn(lineNumber) {
234
- return this.model.getLineMaxColumn(lineNumber);
235
- }
236
- toValidPosition(position) {
237
- const { lineNumber, column } = this.model.validatePosition(this.p2m.asPosition(position));
238
- return this.m2p.asPosition(lineNumber, column);
239
- }
240
- toValidRange(range) {
241
- return this.m2p.asRange(this.model.validateRange(this.p2m.asRange(range)));
242
- }
243
- get readOnly() {
244
- return this.resource.saveContents === undefined;
245
- }
246
- isReadonly() {
247
- return this.readOnly;
248
- }
249
- get onDispose() {
250
- return this.toDispose.onDispose;
251
- }
252
- get onWillDispose() {
253
- return this.toDispose.onDispose;
254
- }
255
- // We have a TypeScript problem here. There is a const enum `DefaultEndOfLine` used for ITextModel and a non-const redeclaration of that enum in the public API in
256
- // Monaco.editor. The values will be the same, but TS won't accept that the two enums are equivalent, so it says these types are irreconcilable.
257
- get textEditorModel() {
258
- // @ts-expect-error ts(2322)
259
- return this.model;
260
- }
261
- /**
262
- * Find all matches in an editor for the given options.
263
- * @param options the options for finding matches.
264
- *
265
- * @returns the list of matches.
266
- */
267
- findMatches(options) {
268
- var _a, _b;
269
- const wordSeparators = (_b = (_a = this.editorPreferences) === null || _a === void 0 ? void 0 : _a['editor.wordSeparators']) !== null && _b !== void 0 ? _b : editor_generated_preference_schema_1.editorGeneratedPreferenceProperties['editor.wordSeparators'].default;
270
- const results = this.model.findMatches(options.searchString, false, options.isRegex, options.matchCase,
271
- // eslint-disable-next-line no-null/no-null
272
- options.matchWholeWord ? wordSeparators : null, true, options.limitResultCount);
273
- const extractedMatches = [];
274
- results.forEach(r => {
275
- if (r.matches) {
276
- extractedMatches.push({
277
- matches: r.matches,
278
- range: vscode_languageserver_protocol_1.Range.create(r.range.startLineNumber, r.range.startColumn, r.range.endLineNumber, r.range.endColumn)
279
- });
280
- }
281
- });
282
- return extractedMatches;
283
- }
284
- async load() {
285
- await this.resolveModel;
286
- return this;
287
- }
288
- save(options) {
289
- return this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual, undefined, undefined, options);
290
- }
291
- async run(operation) {
292
- if (this.toDispose.disposed) {
293
- return;
294
- }
295
- return this.pendingOperation = this.pendingOperation.then(async () => {
296
- try {
297
- await operation();
298
- }
299
- catch (e) {
300
- console.error(e);
301
- }
302
- });
303
- }
304
- cancelSync() {
305
- this.trace(log => log('MonacoEditorModel.cancelSync'));
306
- this.syncCancellationTokenSource.cancel();
307
- this.syncCancellationTokenSource = new cancellation_1.CancellationTokenSource();
308
- return this.syncCancellationTokenSource.token;
309
- }
310
- async sync() {
311
- const token = this.cancelSync();
312
- return this.run(() => this.doSync(token));
313
- }
314
- async doSync(token) {
315
- this.trace(log => log('MonacoEditorModel.doSync - enter'));
316
- if (token.isCancellationRequested) {
317
- this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled'));
318
- return;
319
- }
320
- const value = await this.readContents();
321
- if (value === undefined) {
322
- this.trace(log => log('MonacoEditorModel.doSync - exit - resource not found'));
323
- return;
324
- }
325
- if (token.isCancellationRequested) {
326
- this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled while looking for a resource'));
327
- return;
328
- }
329
- if (this._dirty) {
330
- this.trace(log => log('MonacoEditorModel.doSync - exit - pending dirty changes'));
331
- return;
332
- }
333
- this.resourceVersion = this.resource.version;
334
- this.updateModel(() => standaloneServices_1.StandaloneServices.get(model_1.IModelService).updateModel(this.model, value), {
335
- ignoreDirty: true,
336
- ignoreContentChanges: true
337
- });
338
- this.trace(log => log('MonacoEditorModel.doSync - exit'));
339
- }
340
- async readContents() {
341
- try {
342
- const options = { encoding: this.getEncoding() };
343
- const content = await (this.resource.readStream ? this.resource.readStream(options) : this.resource.readContents(options));
344
- let value;
345
- if (typeof content === 'string') {
346
- value = content;
347
- }
348
- else {
349
- value = (0, textModel_1.createTextBufferFactoryFromStream)(content);
350
- }
351
- this.updateContentEncoding();
352
- this.setValid(true);
353
- return value;
354
- }
355
- catch (e) {
356
- this.setValid(false);
357
- if (resource_1.ResourceError.NotFound.is(e)) {
358
- return undefined;
359
- }
360
- throw e;
361
- }
362
- }
363
- markAsDirty() {
364
- this.trace(log => log('MonacoEditorModel.markAsDirty - enter'));
365
- if (this.ignoreDirtyEdits) {
366
- this.trace(log => log('MonacoEditorModel.markAsDirty - exit - ignoring dirty changes enabled'));
367
- return;
368
- }
369
- this.cancelSync();
370
- this.setDirty(true);
371
- this.doAutoSave();
372
- this.trace(log => log('MonacoEditorModel.markAsDirty - exit'));
373
- }
374
- doAutoSave() {
375
- if (this.autoSave !== 'off' && this.resource.uri.scheme !== resource_1.UNTITLED_SCHEME) {
376
- const token = this.cancelSave();
377
- this.toDisposeOnAutoSave.dispose();
378
- const handle = window.setTimeout(() => {
379
- this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.AfterDelay, token);
380
- }, this.autoSaveDelay);
381
- this.toDisposeOnAutoSave.push(disposable_1.Disposable.create(() => window.clearTimeout(handle)));
382
- }
383
- }
384
- cancelSave() {
385
- this.trace(log => log('MonacoEditorModel.cancelSave'));
386
- this.saveCancellationTokenSource.cancel();
387
- this.saveCancellationTokenSource = new cancellation_1.CancellationTokenSource();
388
- return this.saveCancellationTokenSource.token;
389
- }
390
- scheduleSave(reason, token = this.cancelSave(), overwriteEncoding, options) {
391
- return this.run(() => this.doSave(reason, token, overwriteEncoding, options));
392
- }
393
- pushContentChanges(contentChanges) {
394
- if (!this.ignoreContentChanges) {
395
- this.contentChanges.push(...contentChanges);
396
- }
397
- }
398
- fireDidChangeContent(event) {
399
- this.trace(log => log(`MonacoEditorModel.fireDidChangeContent - enter - ${JSON.stringify(event, undefined, 2)}`));
400
- if (this.model.getAlternativeVersionId() === this.bufferSavedVersionId) {
401
- this.setDirty(false);
402
- }
403
- else {
404
- this.markAsDirty();
405
- }
406
- const changeContentEvent = this.asContentChangedEvent(event);
407
- this.onDidChangeContentEmitter.fire(changeContentEvent);
408
- this.pushContentChanges(changeContentEvent.contentChanges);
409
- this.trace(log => log('MonacoEditorModel.fireDidChangeContent - exit'));
410
- }
411
- asContentChangedEvent(event) {
412
- const contentChanges = event.changes.map(change => this.asTextDocumentContentChangeEvent(change));
413
- return { model: this, contentChanges };
414
- }
415
- asTextDocumentContentChangeEvent(change) {
416
- const range = this.m2p.asRange(change.range);
417
- const rangeLength = change.rangeLength;
418
- const text = change.text;
419
- return { range, rangeLength, text };
420
- }
421
- applyEdits(operations, options) {
422
- return this.updateModel(() => this.model.applyEdits(operations), options);
423
- }
424
- updateModel(doUpdate, options) {
425
- const resolvedOptions = Object.assign({ ignoreDirty: false, ignoreContentChanges: false }, options);
426
- const { ignoreDirtyEdits, ignoreContentChanges } = this;
427
- this.ignoreDirtyEdits = resolvedOptions.ignoreDirty;
428
- this.ignoreContentChanges = resolvedOptions.ignoreContentChanges;
429
- try {
430
- return doUpdate();
431
- }
432
- finally {
433
- this.ignoreDirtyEdits = ignoreDirtyEdits;
434
- this.ignoreContentChanges = ignoreContentChanges;
435
- }
436
- }
437
- async doSave(reason, token, overwriteEncoding, options) {
438
- if (token.isCancellationRequested || !this.resource.saveContents) {
439
- return;
440
- }
441
- await this.fireWillSaveModel(reason, token, options);
442
- if (token.isCancellationRequested) {
443
- return;
444
- }
445
- const changes = [...this.contentChanges];
446
- if (changes.length === 0 && !overwriteEncoding && reason !== vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual) {
447
- return;
448
- }
449
- const contentLength = this.model.getValueLength();
450
- const content = this.model.createSnapshot() || this.model.getValue();
451
- try {
452
- const encoding = this.getEncoding();
453
- const version = this.resourceVersion;
454
- await resource_1.Resource.save(this.resource, { changes, content, contentLength, options: { encoding, overwriteEncoding, version } }, token);
455
- this.contentChanges.splice(0, changes.length);
456
- this.resourceVersion = this.resource.version;
457
- this.updateContentEncoding();
458
- this.setValid(true);
459
- if (token.isCancellationRequested) {
460
- return;
461
- }
462
- this.setDirty(false);
463
- this.fireDidSaveModel();
464
- }
465
- catch (e) {
466
- if (!resource_1.ResourceError.OutOfSync.is(e)) {
467
- throw e;
468
- }
469
- }
470
- }
471
- async fireWillSaveModel(reason, token, options) {
472
- const firing = this.onWillSaveModelEmitter.sequence(async (listener) => {
473
- if (token.isCancellationRequested) {
474
- return false;
475
- }
476
- const waitables = [];
477
- const { version } = this;
478
- const event = {
479
- model: this, reason, options,
480
- waitUntil: (thenable) => {
481
- if (Object.isFrozen(waitables)) {
482
- throw new Error('waitUntil cannot be called asynchronously.');
483
- }
484
- waitables.push(thenable);
485
- }
486
- };
487
- // Fire.
488
- try {
489
- listener(event);
490
- }
491
- catch (err) {
492
- console.error(err);
493
- return true;
494
- }
495
- // Asynchronous calls to `waitUntil` should fail.
496
- Object.freeze(waitables);
497
- // Wait for all promises.
498
- const edits = await Promise.all(waitables).then(allOperations => [].concat(...allOperations));
499
- if (token.isCancellationRequested) {
500
- return false;
501
- }
502
- // In a perfect world, we should only apply edits if document is clean.
503
- if (version !== this.version) {
504
- console.error('onWillSave listeners should provide edits, not directly alter the document.');
505
- }
506
- // Finally apply edits provided by this listener before firing the next.
507
- if (edits && edits.length > 0) {
508
- this.applyEdits(edits, {
509
- ignoreDirty: true,
510
- });
511
- }
512
- return true;
513
- });
514
- try {
515
- await firing;
516
- }
517
- catch (e) {
518
- console.error(e);
519
- }
520
- }
521
- fireDidSaveModel() {
522
- this.onDidSaveModelEmitter.fire(this.model);
523
- }
524
- async revert(options) {
525
- this.trace(log => log('MonacoEditorModel.revert - enter'));
526
- this.cancelSave();
527
- const soft = options && options.soft;
528
- if (soft !== true) {
529
- const dirty = this._dirty;
530
- this._dirty = false;
531
- try {
532
- await this.sync();
533
- }
534
- finally {
535
- this._dirty = dirty;
536
- }
537
- }
538
- this.setDirty(false);
539
- this.trace(log => log('MonacoEditorModel.revert - exit'));
540
- }
541
- createSnapshot(preserveBOM) {
542
- return this.model.createSnapshot(preserveBOM);
543
- }
544
- applySnapshot(snapshot) {
545
- var _a;
546
- const value = 'value' in snapshot ? snapshot.value : (_a = snapshot.read()) !== null && _a !== void 0 ? _a : '';
547
- this.model.setValue(value);
548
- }
549
- trace(loggable) {
550
- if (this.logger) {
551
- this.logger.debug((log) => loggable((message, ...params) => log(message, ...params, this.resource.uri.toString(true))));
552
- }
553
- }
554
- }
555
- exports.MonacoEditorModel = MonacoEditorModel;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2017 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.MonacoEditorModel = exports.TextDocumentSaveReason = void 0;
19
+ const vscode_languageserver_protocol_1 = require("@theia/core/shared/vscode-languageserver-protocol");
20
+ Object.defineProperty(exports, "TextDocumentSaveReason", { enumerable: true, get: function () { return vscode_languageserver_protocol_1.TextDocumentSaveReason; } });
21
+ const disposable_1 = require("@theia/core/lib/common/disposable");
22
+ const event_1 = require("@theia/core/lib/common/event");
23
+ const cancellation_1 = require("@theia/core/lib/common/cancellation");
24
+ const resource_1 = require("@theia/core/lib/common/resource");
25
+ const monaco = require("@theia/monaco-editor-core");
26
+ const standaloneServices_1 = require("@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices");
27
+ const language_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/languages/language");
28
+ const model_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/services/model");
29
+ const textModel_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/model/textModel");
30
+ const editor_generated_preference_schema_1 = require("@theia/editor/lib/browser/editor-generated-preference-schema");
31
+ class MonacoEditorModel {
32
+ constructor(resource, m2p, p2m, logger, editorPreferences) {
33
+ this.resource = resource;
34
+ this.m2p = m2p;
35
+ this.p2m = p2m;
36
+ this.logger = logger;
37
+ this.editorPreferences = editorPreferences;
38
+ this.autoSave = 'afterDelay';
39
+ this.autoSaveDelay = 500;
40
+ this.suppressOpenEditorWhenDirty = false;
41
+ this.lineNumbersMinChars = 3;
42
+ /* @deprecated there is no general save timeout, each participant should introduce a sensible timeout */
43
+ this.onWillSaveLoopTimeOut = 1500;
44
+ this.toDispose = new disposable_1.DisposableCollection();
45
+ this.toDisposeOnAutoSave = new disposable_1.DisposableCollection();
46
+ this.onDidChangeContentEmitter = new event_1.Emitter();
47
+ this.onDidChangeContent = this.onDidChangeContentEmitter.event;
48
+ this.onDidSaveModelEmitter = new event_1.Emitter();
49
+ this.onDidSaveModel = this.onDidSaveModelEmitter.event;
50
+ this.onWillSaveModelEmitter = new event_1.Emitter();
51
+ this.onWillSaveModel = this.onWillSaveModelEmitter.event;
52
+ this.onDidChangeValidEmitter = new event_1.Emitter();
53
+ this.onDidChangeValid = this.onDidChangeValidEmitter.event;
54
+ this.onDidChangeEncodingEmitter = new event_1.Emitter();
55
+ this.onDidChangeEncoding = this.onDidChangeEncodingEmitter.event;
56
+ /**
57
+ * Use `valid` to access it.
58
+ * Use `setValid` to mutate it.
59
+ */
60
+ this._valid = false;
61
+ this._dirty = false;
62
+ this.onDirtyChangedEmitter = new event_1.Emitter();
63
+ this.pendingOperation = Promise.resolve();
64
+ this.syncCancellationTokenSource = new cancellation_1.CancellationTokenSource();
65
+ this.ignoreDirtyEdits = false;
66
+ this.saveCancellationTokenSource = new cancellation_1.CancellationTokenSource();
67
+ this.ignoreContentChanges = false;
68
+ this.contentChanges = [];
69
+ this.toDispose.push(resource);
70
+ this.toDispose.push(this.toDisposeOnAutoSave);
71
+ this.toDispose.push(this.onDidChangeContentEmitter);
72
+ this.toDispose.push(this.onDidSaveModelEmitter);
73
+ this.toDispose.push(this.onWillSaveModelEmitter);
74
+ this.toDispose.push(this.onDirtyChangedEmitter);
75
+ this.toDispose.push(this.onDidChangeValidEmitter);
76
+ this.toDispose.push(disposable_1.Disposable.create(() => this.cancelSave()));
77
+ this.toDispose.push(disposable_1.Disposable.create(() => this.cancelSync()));
78
+ this.resolveModel = this.readContents().then(content => this.initialize(content || ''));
79
+ }
80
+ dispose() {
81
+ this.toDispose.dispose();
82
+ }
83
+ isDisposed() {
84
+ return this.toDispose.disposed;
85
+ }
86
+ resolve() {
87
+ return this.resolveModel;
88
+ }
89
+ isResolved() {
90
+ return Boolean(this.model);
91
+ }
92
+ setEncoding(encoding, mode) {
93
+ if (mode === 1 /* Decode */ && this.dirty) {
94
+ return Promise.resolve();
95
+ }
96
+ if (!this.setPreferredEncoding(encoding)) {
97
+ return Promise.resolve();
98
+ }
99
+ if (mode === 1 /* Decode */) {
100
+ return this.sync();
101
+ }
102
+ return this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual, this.cancelSave(), true);
103
+ }
104
+ getEncoding() {
105
+ return this.preferredEncoding || this.contentEncoding;
106
+ }
107
+ setPreferredEncoding(encoding) {
108
+ if (encoding === this.preferredEncoding || (!this.preferredEncoding && encoding === this.contentEncoding)) {
109
+ return false;
110
+ }
111
+ this.preferredEncoding = encoding;
112
+ this.onDidChangeEncodingEmitter.fire(encoding);
113
+ return true;
114
+ }
115
+ updateContentEncoding() {
116
+ const contentEncoding = this.resource.encoding;
117
+ if (!contentEncoding || this.contentEncoding === contentEncoding) {
118
+ return;
119
+ }
120
+ this.contentEncoding = contentEncoding;
121
+ if (!this.preferredEncoding) {
122
+ this.onDidChangeEncodingEmitter.fire(contentEncoding);
123
+ }
124
+ }
125
+ /**
126
+ * #### Important
127
+ * Only this method can create an instance of `monaco.editor.IModel`,
128
+ * there should not be other calls to `monaco.editor.createModel`.
129
+ */
130
+ initialize(value) {
131
+ if (!this.toDispose.disposed) {
132
+ const uri = monaco.Uri.parse(this.resource.uri.toString());
133
+ let firstLine;
134
+ if (typeof value === 'string') {
135
+ firstLine = value;
136
+ const firstLF = value.indexOf('\n');
137
+ if (firstLF !== -1) {
138
+ firstLine = value.substring(0, firstLF);
139
+ }
140
+ }
141
+ else {
142
+ firstLine = value.getFirstLineText(1000);
143
+ }
144
+ const languageSelection = standaloneServices_1.StandaloneServices.get(language_1.ILanguageService).createByFilepathOrFirstLine(uri, firstLine);
145
+ this.model = standaloneServices_1.StandaloneServices.get(model_1.IModelService).createModel(value, languageSelection, uri);
146
+ this.resourceVersion = this.resource.version;
147
+ this.setDirty(this._dirty || (this.resource.uri.scheme === resource_1.UNTITLED_SCHEME && this.model.getValueLength() > 0));
148
+ this.updateSavedVersionId();
149
+ this.toDispose.push(this.model);
150
+ this.toDispose.push(this.model.onDidChangeContent(event => this.fireDidChangeContent(event)));
151
+ if (this.resource.onDidChangeContents) {
152
+ this.toDispose.push(this.resource.onDidChangeContents(() => this.sync()));
153
+ }
154
+ }
155
+ }
156
+ /**
157
+ * Whether it is possible to load content from the underlying resource.
158
+ */
159
+ get valid() {
160
+ return this._valid;
161
+ }
162
+ setValid(valid) {
163
+ if (valid === this._valid) {
164
+ return;
165
+ }
166
+ this._valid = valid;
167
+ this.onDidChangeValidEmitter.fire(undefined);
168
+ }
169
+ get dirty() {
170
+ return this._dirty;
171
+ }
172
+ setDirty(dirty) {
173
+ if (dirty === this._dirty) {
174
+ return;
175
+ }
176
+ this._dirty = dirty;
177
+ if (dirty === false) {
178
+ this.updateSavedVersionId();
179
+ }
180
+ this.onDirtyChangedEmitter.fire(undefined);
181
+ }
182
+ updateSavedVersionId() {
183
+ this.bufferSavedVersionId = this.model.getAlternativeVersionId();
184
+ }
185
+ get onDirtyChanged() {
186
+ return this.onDirtyChangedEmitter.event;
187
+ }
188
+ get uri() {
189
+ return this.resource.uri.toString();
190
+ }
191
+ get languageId() {
192
+ return this._languageId !== undefined ? this._languageId : this.model.getLanguageId();
193
+ }
194
+ getLanguageId() {
195
+ return this.languageId;
196
+ }
197
+ /**
198
+ * It's a hack to dispatch close notification with an old language id; don't use it.
199
+ */
200
+ setLanguageId(languageId) {
201
+ this._languageId = languageId;
202
+ }
203
+ get version() {
204
+ return this.model.getVersionId();
205
+ }
206
+ /**
207
+ * Return selected text by Range or all text by default
208
+ */
209
+ getText(range) {
210
+ if (!range) {
211
+ return this.model.getValue();
212
+ }
213
+ else {
214
+ return this.model.getValueInRange(this.p2m.asRange(range));
215
+ }
216
+ }
217
+ positionAt(offset) {
218
+ const { lineNumber, column } = this.model.getPositionAt(offset);
219
+ return this.m2p.asPosition(lineNumber, column);
220
+ }
221
+ offsetAt(position) {
222
+ return this.model.getOffsetAt(this.p2m.asPosition(position));
223
+ }
224
+ get lineCount() {
225
+ return this.model.getLineCount();
226
+ }
227
+ /**
228
+ * Retrieves a line in a text document expressed as a one-based position.
229
+ */
230
+ getLineContent(lineNumber) {
231
+ return this.model.getLineContent(lineNumber);
232
+ }
233
+ getLineMaxColumn(lineNumber) {
234
+ return this.model.getLineMaxColumn(lineNumber);
235
+ }
236
+ toValidPosition(position) {
237
+ const { lineNumber, column } = this.model.validatePosition(this.p2m.asPosition(position));
238
+ return this.m2p.asPosition(lineNumber, column);
239
+ }
240
+ toValidRange(range) {
241
+ return this.m2p.asRange(this.model.validateRange(this.p2m.asRange(range)));
242
+ }
243
+ get readOnly() {
244
+ return this.resource.saveContents === undefined;
245
+ }
246
+ isReadonly() {
247
+ return this.readOnly;
248
+ }
249
+ get onDispose() {
250
+ return this.toDispose.onDispose;
251
+ }
252
+ get onWillDispose() {
253
+ return this.toDispose.onDispose;
254
+ }
255
+ // We have a TypeScript problem here. There is a const enum `DefaultEndOfLine` used for ITextModel and a non-const redeclaration of that enum in the public API in
256
+ // Monaco.editor. The values will be the same, but TS won't accept that the two enums are equivalent, so it says these types are irreconcilable.
257
+ get textEditorModel() {
258
+ // @ts-expect-error ts(2322)
259
+ return this.model;
260
+ }
261
+ /**
262
+ * Find all matches in an editor for the given options.
263
+ * @param options the options for finding matches.
264
+ *
265
+ * @returns the list of matches.
266
+ */
267
+ findMatches(options) {
268
+ var _a, _b;
269
+ const wordSeparators = (_b = (_a = this.editorPreferences) === null || _a === void 0 ? void 0 : _a['editor.wordSeparators']) !== null && _b !== void 0 ? _b : editor_generated_preference_schema_1.editorGeneratedPreferenceProperties['editor.wordSeparators'].default;
270
+ const results = this.model.findMatches(options.searchString, false, options.isRegex, options.matchCase,
271
+ // eslint-disable-next-line no-null/no-null
272
+ options.matchWholeWord ? wordSeparators : null, true, options.limitResultCount);
273
+ const extractedMatches = [];
274
+ results.forEach(r => {
275
+ if (r.matches) {
276
+ extractedMatches.push({
277
+ matches: r.matches,
278
+ range: vscode_languageserver_protocol_1.Range.create(r.range.startLineNumber, r.range.startColumn, r.range.endLineNumber, r.range.endColumn)
279
+ });
280
+ }
281
+ });
282
+ return extractedMatches;
283
+ }
284
+ async load() {
285
+ await this.resolveModel;
286
+ return this;
287
+ }
288
+ save(options) {
289
+ return this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual, undefined, undefined, options);
290
+ }
291
+ async run(operation) {
292
+ if (this.toDispose.disposed) {
293
+ return;
294
+ }
295
+ return this.pendingOperation = this.pendingOperation.then(async () => {
296
+ try {
297
+ await operation();
298
+ }
299
+ catch (e) {
300
+ console.error(e);
301
+ }
302
+ });
303
+ }
304
+ cancelSync() {
305
+ this.trace(log => log('MonacoEditorModel.cancelSync'));
306
+ this.syncCancellationTokenSource.cancel();
307
+ this.syncCancellationTokenSource = new cancellation_1.CancellationTokenSource();
308
+ return this.syncCancellationTokenSource.token;
309
+ }
310
+ async sync() {
311
+ const token = this.cancelSync();
312
+ return this.run(() => this.doSync(token));
313
+ }
314
+ async doSync(token) {
315
+ this.trace(log => log('MonacoEditorModel.doSync - enter'));
316
+ if (token.isCancellationRequested) {
317
+ this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled'));
318
+ return;
319
+ }
320
+ const value = await this.readContents();
321
+ if (value === undefined) {
322
+ this.trace(log => log('MonacoEditorModel.doSync - exit - resource not found'));
323
+ return;
324
+ }
325
+ if (token.isCancellationRequested) {
326
+ this.trace(log => log('MonacoEditorModel.doSync - exit - cancelled while looking for a resource'));
327
+ return;
328
+ }
329
+ if (this._dirty) {
330
+ this.trace(log => log('MonacoEditorModel.doSync - exit - pending dirty changes'));
331
+ return;
332
+ }
333
+ this.resourceVersion = this.resource.version;
334
+ this.updateModel(() => standaloneServices_1.StandaloneServices.get(model_1.IModelService).updateModel(this.model, value), {
335
+ ignoreDirty: true,
336
+ ignoreContentChanges: true
337
+ });
338
+ this.trace(log => log('MonacoEditorModel.doSync - exit'));
339
+ }
340
+ async readContents() {
341
+ try {
342
+ const options = { encoding: this.getEncoding() };
343
+ const content = await (this.resource.readStream ? this.resource.readStream(options) : this.resource.readContents(options));
344
+ let value;
345
+ if (typeof content === 'string') {
346
+ value = content;
347
+ }
348
+ else {
349
+ value = (0, textModel_1.createTextBufferFactoryFromStream)(content);
350
+ }
351
+ this.updateContentEncoding();
352
+ this.setValid(true);
353
+ return value;
354
+ }
355
+ catch (e) {
356
+ this.setValid(false);
357
+ if (resource_1.ResourceError.NotFound.is(e)) {
358
+ return undefined;
359
+ }
360
+ throw e;
361
+ }
362
+ }
363
+ markAsDirty() {
364
+ this.trace(log => log('MonacoEditorModel.markAsDirty - enter'));
365
+ if (this.ignoreDirtyEdits) {
366
+ this.trace(log => log('MonacoEditorModel.markAsDirty - exit - ignoring dirty changes enabled'));
367
+ return;
368
+ }
369
+ this.cancelSync();
370
+ this.setDirty(true);
371
+ this.doAutoSave();
372
+ this.trace(log => log('MonacoEditorModel.markAsDirty - exit'));
373
+ }
374
+ doAutoSave() {
375
+ if (this.autoSave !== 'off' && this.resource.uri.scheme !== resource_1.UNTITLED_SCHEME) {
376
+ const token = this.cancelSave();
377
+ this.toDisposeOnAutoSave.dispose();
378
+ const handle = window.setTimeout(() => {
379
+ this.scheduleSave(vscode_languageserver_protocol_1.TextDocumentSaveReason.AfterDelay, token);
380
+ }, this.autoSaveDelay);
381
+ this.toDisposeOnAutoSave.push(disposable_1.Disposable.create(() => window.clearTimeout(handle)));
382
+ }
383
+ }
384
+ cancelSave() {
385
+ this.trace(log => log('MonacoEditorModel.cancelSave'));
386
+ this.saveCancellationTokenSource.cancel();
387
+ this.saveCancellationTokenSource = new cancellation_1.CancellationTokenSource();
388
+ return this.saveCancellationTokenSource.token;
389
+ }
390
+ scheduleSave(reason, token = this.cancelSave(), overwriteEncoding, options) {
391
+ return this.run(() => this.doSave(reason, token, overwriteEncoding, options));
392
+ }
393
+ pushContentChanges(contentChanges) {
394
+ if (!this.ignoreContentChanges) {
395
+ this.contentChanges.push(...contentChanges);
396
+ }
397
+ }
398
+ fireDidChangeContent(event) {
399
+ this.trace(log => log(`MonacoEditorModel.fireDidChangeContent - enter - ${JSON.stringify(event, undefined, 2)}`));
400
+ if (this.model.getAlternativeVersionId() === this.bufferSavedVersionId) {
401
+ this.setDirty(false);
402
+ }
403
+ else {
404
+ this.markAsDirty();
405
+ }
406
+ const changeContentEvent = this.asContentChangedEvent(event);
407
+ this.onDidChangeContentEmitter.fire(changeContentEvent);
408
+ this.pushContentChanges(changeContentEvent.contentChanges);
409
+ this.trace(log => log('MonacoEditorModel.fireDidChangeContent - exit'));
410
+ }
411
+ asContentChangedEvent(event) {
412
+ const contentChanges = event.changes.map(change => this.asTextDocumentContentChangeEvent(change));
413
+ return { model: this, contentChanges };
414
+ }
415
+ asTextDocumentContentChangeEvent(change) {
416
+ const range = this.m2p.asRange(change.range);
417
+ const rangeLength = change.rangeLength;
418
+ const text = change.text;
419
+ return { range, rangeLength, text };
420
+ }
421
+ applyEdits(operations, options) {
422
+ return this.updateModel(() => this.model.applyEdits(operations), options);
423
+ }
424
+ updateModel(doUpdate, options) {
425
+ const resolvedOptions = Object.assign({ ignoreDirty: false, ignoreContentChanges: false }, options);
426
+ const { ignoreDirtyEdits, ignoreContentChanges } = this;
427
+ this.ignoreDirtyEdits = resolvedOptions.ignoreDirty;
428
+ this.ignoreContentChanges = resolvedOptions.ignoreContentChanges;
429
+ try {
430
+ return doUpdate();
431
+ }
432
+ finally {
433
+ this.ignoreDirtyEdits = ignoreDirtyEdits;
434
+ this.ignoreContentChanges = ignoreContentChanges;
435
+ }
436
+ }
437
+ async doSave(reason, token, overwriteEncoding, options) {
438
+ if (token.isCancellationRequested || !this.resource.saveContents) {
439
+ return;
440
+ }
441
+ await this.fireWillSaveModel(reason, token, options);
442
+ if (token.isCancellationRequested) {
443
+ return;
444
+ }
445
+ const changes = [...this.contentChanges];
446
+ if (changes.length === 0 && !overwriteEncoding && reason !== vscode_languageserver_protocol_1.TextDocumentSaveReason.Manual) {
447
+ return;
448
+ }
449
+ const contentLength = this.model.getValueLength();
450
+ const content = this.model.createSnapshot() || this.model.getValue();
451
+ try {
452
+ const encoding = this.getEncoding();
453
+ const version = this.resourceVersion;
454
+ await resource_1.Resource.save(this.resource, { changes, content, contentLength, options: { encoding, overwriteEncoding, version } }, token);
455
+ this.contentChanges.splice(0, changes.length);
456
+ this.resourceVersion = this.resource.version;
457
+ this.updateContentEncoding();
458
+ this.setValid(true);
459
+ if (token.isCancellationRequested) {
460
+ return;
461
+ }
462
+ this.setDirty(false);
463
+ this.fireDidSaveModel();
464
+ }
465
+ catch (e) {
466
+ if (!resource_1.ResourceError.OutOfSync.is(e)) {
467
+ throw e;
468
+ }
469
+ }
470
+ }
471
+ async fireWillSaveModel(reason, token, options) {
472
+ const firing = this.onWillSaveModelEmitter.sequence(async (listener) => {
473
+ if (token.isCancellationRequested) {
474
+ return false;
475
+ }
476
+ const waitables = [];
477
+ const { version } = this;
478
+ const event = {
479
+ model: this, reason, options,
480
+ waitUntil: (thenable) => {
481
+ if (Object.isFrozen(waitables)) {
482
+ throw new Error('waitUntil cannot be called asynchronously.');
483
+ }
484
+ waitables.push(thenable);
485
+ }
486
+ };
487
+ // Fire.
488
+ try {
489
+ listener(event);
490
+ }
491
+ catch (err) {
492
+ console.error(err);
493
+ return true;
494
+ }
495
+ // Asynchronous calls to `waitUntil` should fail.
496
+ Object.freeze(waitables);
497
+ // Wait for all promises.
498
+ const edits = await Promise.all(waitables).then(allOperations => [].concat(...allOperations));
499
+ if (token.isCancellationRequested) {
500
+ return false;
501
+ }
502
+ // In a perfect world, we should only apply edits if document is clean.
503
+ if (version !== this.version) {
504
+ console.error('onWillSave listeners should provide edits, not directly alter the document.');
505
+ }
506
+ // Finally apply edits provided by this listener before firing the next.
507
+ if (edits && edits.length > 0) {
508
+ this.applyEdits(edits, {
509
+ ignoreDirty: true,
510
+ });
511
+ }
512
+ return true;
513
+ });
514
+ try {
515
+ await firing;
516
+ }
517
+ catch (e) {
518
+ console.error(e);
519
+ }
520
+ }
521
+ fireDidSaveModel() {
522
+ this.onDidSaveModelEmitter.fire(this.model);
523
+ }
524
+ async revert(options) {
525
+ this.trace(log => log('MonacoEditorModel.revert - enter'));
526
+ this.cancelSave();
527
+ const soft = options && options.soft;
528
+ if (soft !== true) {
529
+ const dirty = this._dirty;
530
+ this._dirty = false;
531
+ try {
532
+ await this.sync();
533
+ }
534
+ finally {
535
+ this._dirty = dirty;
536
+ }
537
+ }
538
+ this.setDirty(false);
539
+ this.trace(log => log('MonacoEditorModel.revert - exit'));
540
+ }
541
+ createSnapshot(preserveBOM) {
542
+ return this.model.createSnapshot(preserveBOM);
543
+ }
544
+ applySnapshot(snapshot) {
545
+ var _a;
546
+ const value = 'value' in snapshot ? snapshot.value : (_a = snapshot.read()) !== null && _a !== void 0 ? _a : '';
547
+ this.model.setValue(value);
548
+ }
549
+ trace(loggable) {
550
+ if (this.logger) {
551
+ this.logger.debug((log) => loggable((message, ...params) => log(message, ...params, this.resource.uri.toString(true))));
552
+ }
553
+ }
554
+ }
555
+ exports.MonacoEditorModel = MonacoEditorModel;
556
556
  //# sourceMappingURL=monaco-editor-model.js.map