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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +100 -100
  2. package/data/monaco-nls.json +1379 -1379
  3. package/data/monaco-themes/vscode/dark_plus.json +201 -201
  4. package/data/monaco-themes/vscode/dark_theia.json +5 -5
  5. package/data/monaco-themes/vscode/dark_vs.json +393 -393
  6. package/data/monaco-themes/vscode/hc_black.json +457 -457
  7. package/data/monaco-themes/vscode/hc_light.json +590 -590
  8. package/data/monaco-themes/vscode/hc_theia.json +5 -5
  9. package/data/monaco-themes/vscode/hc_theia_light.json +5 -5
  10. package/data/monaco-themes/vscode/light_plus.json +202 -202
  11. package/data/monaco-themes/vscode/light_theia.json +10 -10
  12. package/data/monaco-themes/vscode/light_vs.json +421 -421
  13. package/lib/browser/monaco-editor-zone-widget.js +1 -1
  14. package/lib/browser/monaco-frontend-application-contribution.js +25 -25
  15. package/lib/browser/simple-monaco-editor.d.ts +3 -1
  16. package/lib/browser/simple-monaco-editor.d.ts.map +1 -1
  17. package/lib/browser/simple-monaco-editor.js +9 -2
  18. package/lib/browser/simple-monaco-editor.js.map +1 -1
  19. package/package.json +8 -8
  20. package/src/browser/index.ts +17 -17
  21. package/src/browser/markdown-renderer/monaco-markdown-renderer.ts +109 -109
  22. package/src/browser/monaco-bulk-edit-service.ts +64 -64
  23. package/src/browser/monaco-color-registry.ts +73 -73
  24. package/src/browser/monaco-command-registry.ts +85 -85
  25. package/src/browser/monaco-command-service.ts +90 -90
  26. package/src/browser/monaco-command.ts +303 -303
  27. package/src/browser/monaco-context-key-service.ts +144 -144
  28. package/src/browser/monaco-context-menu.ts +112 -112
  29. package/src/browser/monaco-diff-editor.ts +141 -141
  30. package/src/browser/monaco-diff-navigator-factory.ts +39 -39
  31. package/src/browser/monaco-editor-model.ts +693 -693
  32. package/src/browser/monaco-editor-peek-view-widget.ts +233 -233
  33. package/src/browser/monaco-editor-provider.ts +455 -455
  34. package/src/browser/monaco-editor-service.ts +152 -152
  35. package/src/browser/monaco-editor-zone-widget.ts +250 -250
  36. package/src/browser/monaco-editor.ts +733 -733
  37. package/src/browser/monaco-formatting-conflicts.ts +116 -116
  38. package/src/browser/monaco-frontend-application-contribution.ts +182 -182
  39. package/src/browser/monaco-frontend-module.ts +319 -319
  40. package/src/browser/monaco-gotoline-quick-access.ts +47 -47
  41. package/src/browser/monaco-gotosymbol-quick-access.ts +53 -53
  42. package/src/browser/monaco-icon-registry.ts +49 -49
  43. package/src/browser/monaco-indexed-db.ts +130 -130
  44. package/src/browser/monaco-init.ts +134 -134
  45. package/src/browser/monaco-keybinding.ts +111 -111
  46. package/src/browser/monaco-keycode-map.ts +171 -171
  47. package/src/browser/monaco-languages.ts +177 -177
  48. package/src/browser/monaco-marker-collection.ts +83 -83
  49. package/src/browser/monaco-menu.ts +147 -147
  50. package/src/browser/monaco-mime-service.ts +71 -71
  51. package/src/browser/monaco-outline-contribution.ts +404 -404
  52. package/src/browser/monaco-outline-decorator.ts +66 -66
  53. package/src/browser/monaco-quick-access-registry.ts +112 -112
  54. package/src/browser/monaco-quick-input-service.ts +701 -701
  55. package/src/browser/monaco-resolved-keybinding.ts +162 -162
  56. package/src/browser/monaco-snippet-suggest-provider.ts +306 -306
  57. package/src/browser/monaco-standalone-theme-service.ts +51 -51
  58. package/src/browser/monaco-status-bar-contribution.ts +110 -110
  59. package/src/browser/monaco-text-model-service.ts +157 -157
  60. package/src/browser/monaco-theming-service.ts +204 -204
  61. package/src/browser/monaco-to-protocol-converter.ts +82 -82
  62. package/src/browser/monaco-undo-redo-handler.ts +64 -64
  63. package/src/browser/monaco-workspace.ts +416 -416
  64. package/src/browser/protocol-to-monaco-converter.ts +158 -158
  65. package/src/browser/simple-monaco-editor.ts +228 -216
  66. package/src/browser/style/index.css +266 -266
  67. package/src/browser/textmate/index.ts +20 -20
  68. package/src/browser/textmate/monaco-textmate-frontend-bindings.ts +90 -90
  69. package/src/browser/textmate/monaco-textmate-service.ts +187 -187
  70. package/src/browser/textmate/monaco-theme-registry.ts +176 -176
  71. package/src/browser/textmate/monaco-theme-types.ts +37 -37
  72. package/src/browser/textmate/textmate-contribution.ts +29 -29
  73. package/src/browser/textmate/textmate-registry.ts +129 -129
  74. package/src/browser/textmate/textmate-snippet-completion-provider.ts +73 -73
  75. package/src/browser/textmate/textmate-tokenizer.ts +84 -84
  76. package/src/browser/workspace-symbol-command.ts +196 -196
  77. package/src/package.spec.ts +28 -28
@@ -1,416 +1,416 @@
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
- /* eslint-disable no-null/no-null */
18
-
19
- import { URI as Uri } from '@theia/core/shared/vscode-uri';
20
- import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
21
- import URI from '@theia/core/lib/common/uri';
22
- import { Emitter } from '@theia/core/lib/common/event';
23
- import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
24
- import { EditorManager, EditorPreferences } from '@theia/editor/lib/browser';
25
- import { MonacoTextModelService } from './monaco-text-model-service';
26
- import { WillSaveMonacoModelEvent, MonacoEditorModel, MonacoModelContentChangedEvent } from './monaco-editor-model';
27
- import { MonacoEditor } from './monaco-editor';
28
- import { ProblemManager } from '@theia/markers/lib/browser';
29
- import { ArrayUtils } from '@theia/core/lib/common/types';
30
- import { FileService } from '@theia/filesystem/lib/browser/file-service';
31
- import { FileSystemProviderCapabilities } from '@theia/filesystem/lib/common/files';
32
- import * as monaco from '@theia/monaco-editor-core';
33
- import {
34
- IBulkEditOptions,
35
- IBulkEditResult, ResourceEdit, ResourceFileEdit as MonacoResourceFileEdit,
36
- ResourceTextEdit as MonacoResourceTextEdit
37
- } from '@theia/monaco-editor-core/esm/vs/editor/browser/services/bulkEditService';
38
- import { IEditorWorkerService } from '@theia/monaco-editor-core/esm/vs/editor/common/services/editorWorker';
39
- import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
40
- import { EndOfLineSequence } from '@theia/monaco-editor-core/esm/vs/editor/common/model';
41
- import { SnippetParser } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetParser';
42
- import { TextEdit } from '@theia/monaco-editor-core/esm/vs/editor/common/languages';
43
- import { SnippetController2 } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetController2';
44
- import { isObject, MaybePromise, nls } from '@theia/core/lib/common';
45
- import { SaveableService } from '@theia/core/lib/browser';
46
-
47
- export namespace WorkspaceFileEdit {
48
- export function is(arg: Edit): arg is monaco.languages.IWorkspaceFileEdit {
49
- return ('oldResource' in arg && monaco.Uri.isUri(arg.oldResource)) ||
50
- ('newResource' in arg && monaco.Uri.isUri(arg.newResource));
51
- }
52
- }
53
-
54
- export namespace WorkspaceTextEdit {
55
- export function is(arg: Edit): arg is monaco.languages.IWorkspaceTextEdit {
56
- return isObject<monaco.languages.IWorkspaceTextEdit>(arg)
57
- && monaco.Uri.isUri(arg.resource)
58
- && isObject(arg.textEdit);
59
- }
60
- }
61
-
62
- export type Edit = monaco.languages.IWorkspaceFileEdit | monaco.languages.IWorkspaceTextEdit;
63
-
64
- export namespace ResourceFileEdit {
65
- export function is(arg: ResourceEdit): arg is MonacoResourceFileEdit {
66
- return isObject<MonacoResourceFileEdit>(arg) && (monaco.Uri.isUri(arg.oldResource) || monaco.Uri.isUri(arg.newResource));
67
- }
68
- }
69
-
70
- export namespace ResourceTextEdit {
71
- export function is(arg: ResourceEdit): arg is MonacoResourceTextEdit {
72
- return ('resource' in arg && monaco.Uri.isUri((arg as MonacoResourceTextEdit).resource));
73
- }
74
- }
75
-
76
- export interface WorkspaceFoldersChangeEvent {
77
- readonly added: WorkspaceFolder[];
78
- readonly removed: WorkspaceFolder[];
79
- }
80
-
81
- export interface WorkspaceFolder {
82
- readonly uri: Uri;
83
- readonly name: string;
84
- readonly index: number;
85
- }
86
-
87
- @injectable()
88
- export class MonacoWorkspace {
89
-
90
- protected resolveReady: () => void;
91
- readonly ready = new Promise<void>(resolve => {
92
- this.resolveReady = resolve;
93
- });
94
-
95
- protected readonly onDidOpenTextDocumentEmitter = new Emitter<MonacoEditorModel>();
96
- readonly onDidOpenTextDocument = this.onDidOpenTextDocumentEmitter.event;
97
-
98
- protected readonly onDidCloseTextDocumentEmitter = new Emitter<MonacoEditorModel>();
99
- readonly onDidCloseTextDocument = this.onDidCloseTextDocumentEmitter.event;
100
-
101
- protected readonly onDidChangeTextDocumentEmitter = new Emitter<MonacoModelContentChangedEvent>();
102
- readonly onDidChangeTextDocument = this.onDidChangeTextDocumentEmitter.event;
103
-
104
- protected readonly onWillSaveTextDocumentEmitter = new Emitter<WillSaveMonacoModelEvent>();
105
- readonly onWillSaveTextDocument = this.onWillSaveTextDocumentEmitter.event;
106
-
107
- protected readonly onDidSaveTextDocumentEmitter = new Emitter<MonacoEditorModel>();
108
- readonly onDidSaveTextDocument = this.onDidSaveTextDocumentEmitter.event;
109
-
110
- @inject(FileService)
111
- protected readonly fileService: FileService;
112
-
113
- @inject(FileSystemPreferences)
114
- protected readonly filePreferences: FileSystemPreferences;
115
-
116
- @inject(EditorPreferences)
117
- protected readonly editorPreferences: EditorPreferences;
118
-
119
- @inject(MonacoTextModelService)
120
- protected readonly textModelService: MonacoTextModelService;
121
-
122
- @inject(EditorManager)
123
- protected readonly editorManager: EditorManager;
124
-
125
- @inject(ProblemManager)
126
- protected readonly problems: ProblemManager;
127
-
128
- @inject(SaveableService)
129
- protected readonly saveService: SaveableService;
130
-
131
- @postConstruct()
132
- protected init(): void {
133
- this.resolveReady();
134
-
135
- for (const model of this.textModelService.models) {
136
- this.fireDidOpen(model);
137
- }
138
- this.textModelService.onDidCreate(model => this.fireDidOpen(model));
139
- }
140
-
141
- get textDocuments(): MonacoEditorModel[] {
142
- return this.textModelService.models;
143
- }
144
-
145
- getTextDocument(uri: string): MonacoEditorModel | undefined {
146
- return this.textModelService.get(uri);
147
- }
148
-
149
- protected fireDidOpen(model: MonacoEditorModel): void {
150
- this.doFireDidOpen(model);
151
- model.textEditorModel.onDidChangeLanguage(e => {
152
- this.problems.cleanAllMarkers(new URI(model.uri));
153
- model.setLanguageId(e.oldLanguage);
154
- try {
155
- this.fireDidClose(model);
156
- } finally {
157
- model.setLanguageId(undefined);
158
- }
159
- this.doFireDidOpen(model);
160
- });
161
- model.onDidChangeContent(event => this.fireDidChangeContent(event));
162
- model.onDidSaveModel(() => this.fireDidSave(model));
163
- model.onWillSaveModel(event => this.fireWillSave(event));
164
- model.onDirtyChanged(() => this.openEditorIfDirty(model));
165
- model.onDispose(() => this.fireDidClose(model));
166
- }
167
-
168
- protected doFireDidOpen(model: MonacoEditorModel): void {
169
- this.onDidOpenTextDocumentEmitter.fire(model);
170
- }
171
-
172
- protected fireDidClose(model: MonacoEditorModel): void {
173
- this.onDidCloseTextDocumentEmitter.fire(model);
174
- }
175
-
176
- protected fireDidChangeContent(event: MonacoModelContentChangedEvent): void {
177
- this.onDidChangeTextDocumentEmitter.fire(event);
178
- }
179
-
180
- protected fireWillSave(event: WillSaveMonacoModelEvent): void {
181
- this.onWillSaveTextDocumentEmitter.fire(event);
182
- }
183
-
184
- protected fireDidSave(model: MonacoEditorModel): void {
185
- this.onDidSaveTextDocumentEmitter.fire(model);
186
- }
187
-
188
- protected readonly suppressedOpenIfDirty: MonacoEditorModel[] = [];
189
-
190
- protected openEditorIfDirty(model: MonacoEditorModel): void {
191
- if (model.suppressOpenEditorWhenDirty || this.suppressedOpenIfDirty.indexOf(model) !== -1) {
192
- return;
193
- }
194
- if (model.dirty && MonacoEditor.findByDocument(this.editorManager, model).length === 0) {
195
- // create a new reference to make sure the model is not disposed before it is
196
- // acquired by the editor, thus losing the changes that made it dirty.
197
- this.textModelService.createModelReference(model.textEditorModel.uri).then(ref => {
198
- (
199
- this.saveService.autoSave !== 'off' ? new Promise(resolve => model.onDidSaveModel(resolve)) :
200
- this.editorManager.open(new URI(model.uri), { mode: 'open' })
201
- ).then(
202
- () => ref.dispose()
203
- );
204
- });
205
- }
206
- }
207
-
208
- protected async suppressOpenIfDirty(model: MonacoEditorModel, cb: () => MaybePromise<void>): Promise<void> {
209
- this.suppressedOpenIfDirty.push(model);
210
- try {
211
- await cb();
212
- } finally {
213
- const i = this.suppressedOpenIfDirty.indexOf(model);
214
- if (i !== -1) {
215
- this.suppressedOpenIfDirty.splice(i, 1);
216
- }
217
- }
218
- }
219
-
220
- /**
221
- * Applies given edits to the given model.
222
- * The model is saved if no editors is opened for it.
223
- */
224
- applyBackgroundEdit(model: MonacoEditorModel, editOperations: monaco.editor.IIdentifiedSingleEditOperation[], shouldSave = true): Promise<void> {
225
- return this.suppressOpenIfDirty(model, async () => {
226
- const editor = MonacoEditor.findByDocument(this.editorManager, model)[0];
227
- const cursorState = editor && editor.getControl().getSelections() || [];
228
- model.textEditorModel.pushStackElement();
229
- model.textEditorModel.pushEditOperations(cursorState, editOperations, () => cursorState);
230
- model.textEditorModel.pushStackElement();
231
- if (!editor && shouldSave) {
232
- await model.save();
233
- }
234
- });
235
- }
236
-
237
- async applyBulkEdit(edits: ResourceEdit[], options?: IBulkEditOptions): Promise<IBulkEditResult> {
238
- try {
239
- let totalEdits = 0;
240
- let totalFiles = 0;
241
- const fileEdits = edits.filter(edit => edit instanceof MonacoResourceFileEdit);
242
- const [snippetEdits, textEdits] = ArrayUtils.partition(edits.filter(edit => edit instanceof MonacoResourceTextEdit) as MonacoResourceTextEdit[],
243
- edit => edit.textEdit.insertAsSnippet && (edit.resource.toString() === this.editorManager.activeEditor?.getResourceUri()?.toString()));
244
-
245
- if (fileEdits.length > 0) {
246
- await this.performFileEdits(<MonacoResourceFileEdit[]>fileEdits);
247
- }
248
-
249
- if (textEdits.length > 0) {
250
- const result = await this.performTextEdits(<MonacoResourceTextEdit[]>textEdits);
251
- totalEdits += result.totalEdits;
252
- totalFiles += result.totalFiles;
253
- }
254
-
255
- if (snippetEdits.length > 0) {
256
- await this.performSnippetEdits(<MonacoResourceTextEdit[]>snippetEdits);
257
- }
258
-
259
- // when enabled (option AND setting) loop over all dirty working copies and trigger save
260
- // for those that were involved in this bulk edit operation.
261
- const resources = new Set<string>(
262
- edits
263
- .filter((edit): edit is MonacoResourceTextEdit => edit instanceof MonacoResourceTextEdit)
264
- .map(edit => edit.resource.toString())
265
- );
266
- if (resources.size > 0 && options?.respectAutoSaveConfig && this.editorPreferences.get('files.refactoring.autoSave') === true) {
267
- await this.saveAll(resources);
268
- }
269
-
270
- const ariaSummary = this.getAriaSummary(totalEdits, totalFiles);
271
- return { ariaSummary, isApplied: true };
272
- } catch (e) {
273
- console.error('Failed to apply Resource edits:', e);
274
- return {
275
- ariaSummary: `Error applying Resource edits: ${e.toString()}`,
276
- isApplied: false
277
- };
278
- }
279
- }
280
-
281
- protected async saveAll(resources: Set<string>): Promise<void> {
282
- await Promise.all(Array.from(resources.values()).map(uri => this.textModelService.get(uri)?.save()));
283
- }
284
-
285
- protected getAriaSummary(totalEdits: number, totalFiles: number): string {
286
- if (totalEdits === 0) {
287
- return nls.localizeByDefault('Made no edits');
288
- }
289
- if (totalEdits > 1 && totalFiles > 1) {
290
- return nls.localizeByDefault('Made {0} text edits in {1} files', totalEdits, totalFiles);
291
- }
292
- return nls.localizeByDefault('Made {0} text edits in one file', totalEdits);
293
- }
294
-
295
- protected async performTextEdits(edits: MonacoResourceTextEdit[]): Promise<{
296
- totalEdits: number,
297
- totalFiles: number
298
- }> {
299
- let totalEdits = 0;
300
- let totalFiles = 0;
301
- const resourceEdits = new Map<string, MonacoResourceTextEdit[]>();
302
- for (const edit of edits) {
303
- if (typeof edit.versionId === 'number') {
304
- const model = this.textModelService.get(edit.resource.toString());
305
- if (model && model.textEditorModel.getVersionId() !== edit.versionId) {
306
- throw new Error(`${model.uri} has changed in the meantime`);
307
- }
308
- }
309
- const key = edit.resource.toString();
310
- let array = resourceEdits.get(key);
311
- if (!array) {
312
- array = [];
313
- resourceEdits.set(key, array);
314
- }
315
- array.push(edit);
316
- }
317
- const pending: Promise<void>[] = [];
318
- for (const [key, value] of resourceEdits) {
319
- pending.push((async () => {
320
- const uri = monaco.Uri.parse(key);
321
- let eol: EndOfLineSequence | undefined;
322
- const editOperations: monaco.editor.IIdentifiedSingleEditOperation[] = [];
323
- const minimalEdits = await StandaloneServices.get(IEditorWorkerService)
324
- .computeMoreMinimalEdits(uri, value.map(edit => this.transformSnippetStringToInsertText(edit)));
325
- if (minimalEdits) {
326
- for (const textEdit of minimalEdits) {
327
- if (typeof textEdit.eol === 'number') {
328
- eol = textEdit.eol;
329
- }
330
- if (monaco.Range.isEmpty(textEdit.range) && !textEdit.text) {
331
- // skip no-op
332
- continue;
333
- }
334
- editOperations.push({
335
- forceMoveMarkers: false,
336
- range: monaco.Range.lift(textEdit.range),
337
- text: textEdit.text
338
- });
339
- }
340
- }
341
- if (!editOperations.length && eol === undefined) {
342
- return;
343
- }
344
- const reference = await this.textModelService.createModelReference(uri);
345
- try {
346
- const document = reference.object as MonacoEditorModel;
347
- const model = document.textEditorModel;
348
- const editor = MonacoEditor.findByDocument(this.editorManager, document)[0];
349
- const cursorState = editor?.getControl().getSelections() ?? [];
350
- // start a fresh operation
351
- model.pushStackElement();
352
- if (editOperations.length) {
353
- model.pushEditOperations(cursorState, editOperations, () => cursorState);
354
- }
355
- if (eol !== undefined) {
356
- model.pushEOL(eol);
357
- }
358
- // push again to make this change an undoable operation
359
- model.pushStackElement();
360
- totalFiles += 1;
361
- totalEdits += editOperations.length;
362
- } finally {
363
- reference.dispose();
364
- }
365
- })());
366
- }
367
- await Promise.all(pending);
368
- return { totalEdits, totalFiles };
369
- }
370
-
371
- protected async performFileEdits(edits: MonacoResourceFileEdit[]): Promise<void> {
372
- for (const edit of edits) {
373
- const options = edit.options || {};
374
- if (edit.newResource && edit.oldResource) {
375
- // rename
376
- if (options.overwrite === undefined && options.ignoreIfExists && await this.fileService.exists(URI.fromComponents(edit.newResource))) {
377
- return; // not overwriting, but ignoring, and the target file exists
378
- }
379
- await this.fileService.move(URI.fromComponents(edit.oldResource), URI.fromComponents(edit.newResource), { overwrite: options.overwrite });
380
- } else if (!edit.newResource && edit.oldResource) {
381
- // delete file
382
- if (await this.fileService.exists(URI.fromComponents(edit.oldResource))) {
383
- let useTrash = this.filePreferences['files.enableTrash'];
384
- if (useTrash && !(this.fileService.hasCapability(URI.fromComponents(edit.oldResource), FileSystemProviderCapabilities.Trash))) {
385
- useTrash = false; // not supported by provider
386
- }
387
- await this.fileService.delete(URI.fromComponents(edit.oldResource), { useTrash, recursive: options.recursive });
388
- } else if (!options.ignoreIfNotExists) {
389
- throw new Error(`${edit.oldResource} does not exist and can not be deleted`);
390
- }
391
- } else if (edit.newResource && !edit.oldResource) {
392
- // create file
393
- if (options.overwrite === undefined && options.ignoreIfExists && await this.fileService.exists(URI.fromComponents(edit.newResource))) {
394
- return; // not overwriting, but ignoring, and the target file exists
395
- }
396
- await this.fileService.create(URI.fromComponents(edit.newResource), undefined, { overwrite: options.overwrite });
397
- }
398
- }
399
- }
400
-
401
- protected async performSnippetEdits(edits: MonacoResourceTextEdit[]): Promise<void> {
402
- const activeEditor = MonacoEditor.getActive(this.editorManager)?.getControl();
403
- if (activeEditor) {
404
- const snippetController: SnippetController2 = activeEditor.getContribution('snippetController2')!;
405
- snippetController.apply(edits.map(edit => ({ range: monaco.Range.lift(edit.textEdit.range), template: edit.textEdit.text })));
406
- }
407
- }
408
-
409
- protected transformSnippetStringToInsertText(resourceEdit: MonacoResourceTextEdit): TextEdit & { insertAsSnippet?: boolean } {
410
- if (resourceEdit.textEdit.insertAsSnippet) {
411
- return { ...resourceEdit.textEdit, insertAsSnippet: false, text: SnippetParser.asInsertText(resourceEdit.textEdit.text) };
412
- } else {
413
- return resourceEdit.textEdit;
414
- }
415
- }
416
- }
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
+ /* eslint-disable no-null/no-null */
18
+
19
+ import { URI as Uri } from '@theia/core/shared/vscode-uri';
20
+ import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
21
+ import URI from '@theia/core/lib/common/uri';
22
+ import { Emitter } from '@theia/core/lib/common/event';
23
+ import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
24
+ import { EditorManager, EditorPreferences } from '@theia/editor/lib/browser';
25
+ import { MonacoTextModelService } from './monaco-text-model-service';
26
+ import { WillSaveMonacoModelEvent, MonacoEditorModel, MonacoModelContentChangedEvent } from './monaco-editor-model';
27
+ import { MonacoEditor } from './monaco-editor';
28
+ import { ProblemManager } from '@theia/markers/lib/browser';
29
+ import { ArrayUtils } from '@theia/core/lib/common/types';
30
+ import { FileService } from '@theia/filesystem/lib/browser/file-service';
31
+ import { FileSystemProviderCapabilities } from '@theia/filesystem/lib/common/files';
32
+ import * as monaco from '@theia/monaco-editor-core';
33
+ import {
34
+ IBulkEditOptions,
35
+ IBulkEditResult, ResourceEdit, ResourceFileEdit as MonacoResourceFileEdit,
36
+ ResourceTextEdit as MonacoResourceTextEdit
37
+ } from '@theia/monaco-editor-core/esm/vs/editor/browser/services/bulkEditService';
38
+ import { IEditorWorkerService } from '@theia/monaco-editor-core/esm/vs/editor/common/services/editorWorker';
39
+ import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
40
+ import { EndOfLineSequence } from '@theia/monaco-editor-core/esm/vs/editor/common/model';
41
+ import { SnippetParser } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetParser';
42
+ import { TextEdit } from '@theia/monaco-editor-core/esm/vs/editor/common/languages';
43
+ import { SnippetController2 } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetController2';
44
+ import { isObject, MaybePromise, nls } from '@theia/core/lib/common';
45
+ import { SaveableService } from '@theia/core/lib/browser';
46
+
47
+ export namespace WorkspaceFileEdit {
48
+ export function is(arg: Edit): arg is monaco.languages.IWorkspaceFileEdit {
49
+ return ('oldResource' in arg && monaco.Uri.isUri(arg.oldResource)) ||
50
+ ('newResource' in arg && monaco.Uri.isUri(arg.newResource));
51
+ }
52
+ }
53
+
54
+ export namespace WorkspaceTextEdit {
55
+ export function is(arg: Edit): arg is monaco.languages.IWorkspaceTextEdit {
56
+ return isObject<monaco.languages.IWorkspaceTextEdit>(arg)
57
+ && monaco.Uri.isUri(arg.resource)
58
+ && isObject(arg.textEdit);
59
+ }
60
+ }
61
+
62
+ export type Edit = monaco.languages.IWorkspaceFileEdit | monaco.languages.IWorkspaceTextEdit;
63
+
64
+ export namespace ResourceFileEdit {
65
+ export function is(arg: ResourceEdit): arg is MonacoResourceFileEdit {
66
+ return isObject<MonacoResourceFileEdit>(arg) && (monaco.Uri.isUri(arg.oldResource) || monaco.Uri.isUri(arg.newResource));
67
+ }
68
+ }
69
+
70
+ export namespace ResourceTextEdit {
71
+ export function is(arg: ResourceEdit): arg is MonacoResourceTextEdit {
72
+ return ('resource' in arg && monaco.Uri.isUri((arg as MonacoResourceTextEdit).resource));
73
+ }
74
+ }
75
+
76
+ export interface WorkspaceFoldersChangeEvent {
77
+ readonly added: WorkspaceFolder[];
78
+ readonly removed: WorkspaceFolder[];
79
+ }
80
+
81
+ export interface WorkspaceFolder {
82
+ readonly uri: Uri;
83
+ readonly name: string;
84
+ readonly index: number;
85
+ }
86
+
87
+ @injectable()
88
+ export class MonacoWorkspace {
89
+
90
+ protected resolveReady: () => void;
91
+ readonly ready = new Promise<void>(resolve => {
92
+ this.resolveReady = resolve;
93
+ });
94
+
95
+ protected readonly onDidOpenTextDocumentEmitter = new Emitter<MonacoEditorModel>();
96
+ readonly onDidOpenTextDocument = this.onDidOpenTextDocumentEmitter.event;
97
+
98
+ protected readonly onDidCloseTextDocumentEmitter = new Emitter<MonacoEditorModel>();
99
+ readonly onDidCloseTextDocument = this.onDidCloseTextDocumentEmitter.event;
100
+
101
+ protected readonly onDidChangeTextDocumentEmitter = new Emitter<MonacoModelContentChangedEvent>();
102
+ readonly onDidChangeTextDocument = this.onDidChangeTextDocumentEmitter.event;
103
+
104
+ protected readonly onWillSaveTextDocumentEmitter = new Emitter<WillSaveMonacoModelEvent>();
105
+ readonly onWillSaveTextDocument = this.onWillSaveTextDocumentEmitter.event;
106
+
107
+ protected readonly onDidSaveTextDocumentEmitter = new Emitter<MonacoEditorModel>();
108
+ readonly onDidSaveTextDocument = this.onDidSaveTextDocumentEmitter.event;
109
+
110
+ @inject(FileService)
111
+ protected readonly fileService: FileService;
112
+
113
+ @inject(FileSystemPreferences)
114
+ protected readonly filePreferences: FileSystemPreferences;
115
+
116
+ @inject(EditorPreferences)
117
+ protected readonly editorPreferences: EditorPreferences;
118
+
119
+ @inject(MonacoTextModelService)
120
+ protected readonly textModelService: MonacoTextModelService;
121
+
122
+ @inject(EditorManager)
123
+ protected readonly editorManager: EditorManager;
124
+
125
+ @inject(ProblemManager)
126
+ protected readonly problems: ProblemManager;
127
+
128
+ @inject(SaveableService)
129
+ protected readonly saveService: SaveableService;
130
+
131
+ @postConstruct()
132
+ protected init(): void {
133
+ this.resolveReady();
134
+
135
+ for (const model of this.textModelService.models) {
136
+ this.fireDidOpen(model);
137
+ }
138
+ this.textModelService.onDidCreate(model => this.fireDidOpen(model));
139
+ }
140
+
141
+ get textDocuments(): MonacoEditorModel[] {
142
+ return this.textModelService.models;
143
+ }
144
+
145
+ getTextDocument(uri: string): MonacoEditorModel | undefined {
146
+ return this.textModelService.get(uri);
147
+ }
148
+
149
+ protected fireDidOpen(model: MonacoEditorModel): void {
150
+ this.doFireDidOpen(model);
151
+ model.textEditorModel.onDidChangeLanguage(e => {
152
+ this.problems.cleanAllMarkers(new URI(model.uri));
153
+ model.setLanguageId(e.oldLanguage);
154
+ try {
155
+ this.fireDidClose(model);
156
+ } finally {
157
+ model.setLanguageId(undefined);
158
+ }
159
+ this.doFireDidOpen(model);
160
+ });
161
+ model.onDidChangeContent(event => this.fireDidChangeContent(event));
162
+ model.onDidSaveModel(() => this.fireDidSave(model));
163
+ model.onWillSaveModel(event => this.fireWillSave(event));
164
+ model.onDirtyChanged(() => this.openEditorIfDirty(model));
165
+ model.onDispose(() => this.fireDidClose(model));
166
+ }
167
+
168
+ protected doFireDidOpen(model: MonacoEditorModel): void {
169
+ this.onDidOpenTextDocumentEmitter.fire(model);
170
+ }
171
+
172
+ protected fireDidClose(model: MonacoEditorModel): void {
173
+ this.onDidCloseTextDocumentEmitter.fire(model);
174
+ }
175
+
176
+ protected fireDidChangeContent(event: MonacoModelContentChangedEvent): void {
177
+ this.onDidChangeTextDocumentEmitter.fire(event);
178
+ }
179
+
180
+ protected fireWillSave(event: WillSaveMonacoModelEvent): void {
181
+ this.onWillSaveTextDocumentEmitter.fire(event);
182
+ }
183
+
184
+ protected fireDidSave(model: MonacoEditorModel): void {
185
+ this.onDidSaveTextDocumentEmitter.fire(model);
186
+ }
187
+
188
+ protected readonly suppressedOpenIfDirty: MonacoEditorModel[] = [];
189
+
190
+ protected openEditorIfDirty(model: MonacoEditorModel): void {
191
+ if (model.suppressOpenEditorWhenDirty || this.suppressedOpenIfDirty.indexOf(model) !== -1) {
192
+ return;
193
+ }
194
+ if (model.dirty && MonacoEditor.findByDocument(this.editorManager, model).length === 0) {
195
+ // create a new reference to make sure the model is not disposed before it is
196
+ // acquired by the editor, thus losing the changes that made it dirty.
197
+ this.textModelService.createModelReference(model.textEditorModel.uri).then(ref => {
198
+ (
199
+ this.saveService.autoSave !== 'off' ? new Promise(resolve => model.onDidSaveModel(resolve)) :
200
+ this.editorManager.open(new URI(model.uri), { mode: 'open' })
201
+ ).then(
202
+ () => ref.dispose()
203
+ );
204
+ });
205
+ }
206
+ }
207
+
208
+ protected async suppressOpenIfDirty(model: MonacoEditorModel, cb: () => MaybePromise<void>): Promise<void> {
209
+ this.suppressedOpenIfDirty.push(model);
210
+ try {
211
+ await cb();
212
+ } finally {
213
+ const i = this.suppressedOpenIfDirty.indexOf(model);
214
+ if (i !== -1) {
215
+ this.suppressedOpenIfDirty.splice(i, 1);
216
+ }
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Applies given edits to the given model.
222
+ * The model is saved if no editors is opened for it.
223
+ */
224
+ applyBackgroundEdit(model: MonacoEditorModel, editOperations: monaco.editor.IIdentifiedSingleEditOperation[], shouldSave = true): Promise<void> {
225
+ return this.suppressOpenIfDirty(model, async () => {
226
+ const editor = MonacoEditor.findByDocument(this.editorManager, model)[0];
227
+ const cursorState = editor && editor.getControl().getSelections() || [];
228
+ model.textEditorModel.pushStackElement();
229
+ model.textEditorModel.pushEditOperations(cursorState, editOperations, () => cursorState);
230
+ model.textEditorModel.pushStackElement();
231
+ if (!editor && shouldSave) {
232
+ await model.save();
233
+ }
234
+ });
235
+ }
236
+
237
+ async applyBulkEdit(edits: ResourceEdit[], options?: IBulkEditOptions): Promise<IBulkEditResult> {
238
+ try {
239
+ let totalEdits = 0;
240
+ let totalFiles = 0;
241
+ const fileEdits = edits.filter(edit => edit instanceof MonacoResourceFileEdit);
242
+ const [snippetEdits, textEdits] = ArrayUtils.partition(edits.filter(edit => edit instanceof MonacoResourceTextEdit) as MonacoResourceTextEdit[],
243
+ edit => edit.textEdit.insertAsSnippet && (edit.resource.toString() === this.editorManager.activeEditor?.getResourceUri()?.toString()));
244
+
245
+ if (fileEdits.length > 0) {
246
+ await this.performFileEdits(<MonacoResourceFileEdit[]>fileEdits);
247
+ }
248
+
249
+ if (textEdits.length > 0) {
250
+ const result = await this.performTextEdits(<MonacoResourceTextEdit[]>textEdits);
251
+ totalEdits += result.totalEdits;
252
+ totalFiles += result.totalFiles;
253
+ }
254
+
255
+ if (snippetEdits.length > 0) {
256
+ await this.performSnippetEdits(<MonacoResourceTextEdit[]>snippetEdits);
257
+ }
258
+
259
+ // when enabled (option AND setting) loop over all dirty working copies and trigger save
260
+ // for those that were involved in this bulk edit operation.
261
+ const resources = new Set<string>(
262
+ edits
263
+ .filter((edit): edit is MonacoResourceTextEdit => edit instanceof MonacoResourceTextEdit)
264
+ .map(edit => edit.resource.toString())
265
+ );
266
+ if (resources.size > 0 && options?.respectAutoSaveConfig && this.editorPreferences.get('files.refactoring.autoSave') === true) {
267
+ await this.saveAll(resources);
268
+ }
269
+
270
+ const ariaSummary = this.getAriaSummary(totalEdits, totalFiles);
271
+ return { ariaSummary, isApplied: true };
272
+ } catch (e) {
273
+ console.error('Failed to apply Resource edits:', e);
274
+ return {
275
+ ariaSummary: `Error applying Resource edits: ${e.toString()}`,
276
+ isApplied: false
277
+ };
278
+ }
279
+ }
280
+
281
+ protected async saveAll(resources: Set<string>): Promise<void> {
282
+ await Promise.all(Array.from(resources.values()).map(uri => this.textModelService.get(uri)?.save()));
283
+ }
284
+
285
+ protected getAriaSummary(totalEdits: number, totalFiles: number): string {
286
+ if (totalEdits === 0) {
287
+ return nls.localizeByDefault('Made no edits');
288
+ }
289
+ if (totalEdits > 1 && totalFiles > 1) {
290
+ return nls.localizeByDefault('Made {0} text edits in {1} files', totalEdits, totalFiles);
291
+ }
292
+ return nls.localizeByDefault('Made {0} text edits in one file', totalEdits);
293
+ }
294
+
295
+ protected async performTextEdits(edits: MonacoResourceTextEdit[]): Promise<{
296
+ totalEdits: number,
297
+ totalFiles: number
298
+ }> {
299
+ let totalEdits = 0;
300
+ let totalFiles = 0;
301
+ const resourceEdits = new Map<string, MonacoResourceTextEdit[]>();
302
+ for (const edit of edits) {
303
+ if (typeof edit.versionId === 'number') {
304
+ const model = this.textModelService.get(edit.resource.toString());
305
+ if (model && model.textEditorModel.getVersionId() !== edit.versionId) {
306
+ throw new Error(`${model.uri} has changed in the meantime`);
307
+ }
308
+ }
309
+ const key = edit.resource.toString();
310
+ let array = resourceEdits.get(key);
311
+ if (!array) {
312
+ array = [];
313
+ resourceEdits.set(key, array);
314
+ }
315
+ array.push(edit);
316
+ }
317
+ const pending: Promise<void>[] = [];
318
+ for (const [key, value] of resourceEdits) {
319
+ pending.push((async () => {
320
+ const uri = monaco.Uri.parse(key);
321
+ let eol: EndOfLineSequence | undefined;
322
+ const editOperations: monaco.editor.IIdentifiedSingleEditOperation[] = [];
323
+ const minimalEdits = await StandaloneServices.get(IEditorWorkerService)
324
+ .computeMoreMinimalEdits(uri, value.map(edit => this.transformSnippetStringToInsertText(edit)));
325
+ if (minimalEdits) {
326
+ for (const textEdit of minimalEdits) {
327
+ if (typeof textEdit.eol === 'number') {
328
+ eol = textEdit.eol;
329
+ }
330
+ if (monaco.Range.isEmpty(textEdit.range) && !textEdit.text) {
331
+ // skip no-op
332
+ continue;
333
+ }
334
+ editOperations.push({
335
+ forceMoveMarkers: false,
336
+ range: monaco.Range.lift(textEdit.range),
337
+ text: textEdit.text
338
+ });
339
+ }
340
+ }
341
+ if (!editOperations.length && eol === undefined) {
342
+ return;
343
+ }
344
+ const reference = await this.textModelService.createModelReference(uri);
345
+ try {
346
+ const document = reference.object as MonacoEditorModel;
347
+ const model = document.textEditorModel;
348
+ const editor = MonacoEditor.findByDocument(this.editorManager, document)[0];
349
+ const cursorState = editor?.getControl().getSelections() ?? [];
350
+ // start a fresh operation
351
+ model.pushStackElement();
352
+ if (editOperations.length) {
353
+ model.pushEditOperations(cursorState, editOperations, () => cursorState);
354
+ }
355
+ if (eol !== undefined) {
356
+ model.pushEOL(eol);
357
+ }
358
+ // push again to make this change an undoable operation
359
+ model.pushStackElement();
360
+ totalFiles += 1;
361
+ totalEdits += editOperations.length;
362
+ } finally {
363
+ reference.dispose();
364
+ }
365
+ })());
366
+ }
367
+ await Promise.all(pending);
368
+ return { totalEdits, totalFiles };
369
+ }
370
+
371
+ protected async performFileEdits(edits: MonacoResourceFileEdit[]): Promise<void> {
372
+ for (const edit of edits) {
373
+ const options = edit.options || {};
374
+ if (edit.newResource && edit.oldResource) {
375
+ // rename
376
+ if (options.overwrite === undefined && options.ignoreIfExists && await this.fileService.exists(URI.fromComponents(edit.newResource))) {
377
+ return; // not overwriting, but ignoring, and the target file exists
378
+ }
379
+ await this.fileService.move(URI.fromComponents(edit.oldResource), URI.fromComponents(edit.newResource), { overwrite: options.overwrite });
380
+ } else if (!edit.newResource && edit.oldResource) {
381
+ // delete file
382
+ if (await this.fileService.exists(URI.fromComponents(edit.oldResource))) {
383
+ let useTrash = this.filePreferences['files.enableTrash'];
384
+ if (useTrash && !(this.fileService.hasCapability(URI.fromComponents(edit.oldResource), FileSystemProviderCapabilities.Trash))) {
385
+ useTrash = false; // not supported by provider
386
+ }
387
+ await this.fileService.delete(URI.fromComponents(edit.oldResource), { useTrash, recursive: options.recursive });
388
+ } else if (!options.ignoreIfNotExists) {
389
+ throw new Error(`${edit.oldResource} does not exist and can not be deleted`);
390
+ }
391
+ } else if (edit.newResource && !edit.oldResource) {
392
+ // create file
393
+ if (options.overwrite === undefined && options.ignoreIfExists && await this.fileService.exists(URI.fromComponents(edit.newResource))) {
394
+ return; // not overwriting, but ignoring, and the target file exists
395
+ }
396
+ await this.fileService.create(URI.fromComponents(edit.newResource), undefined, { overwrite: options.overwrite });
397
+ }
398
+ }
399
+ }
400
+
401
+ protected async performSnippetEdits(edits: MonacoResourceTextEdit[]): Promise<void> {
402
+ const activeEditor = MonacoEditor.getActive(this.editorManager)?.getControl();
403
+ if (activeEditor) {
404
+ const snippetController: SnippetController2 = activeEditor.getContribution('snippetController2')!;
405
+ snippetController.apply(edits.map(edit => ({ range: monaco.Range.lift(edit.textEdit.range), template: edit.textEdit.text })));
406
+ }
407
+ }
408
+
409
+ protected transformSnippetStringToInsertText(resourceEdit: MonacoResourceTextEdit): TextEdit & { insertAsSnippet?: boolean } {
410
+ if (resourceEdit.textEdit.insertAsSnippet) {
411
+ return { ...resourceEdit.textEdit, insertAsSnippet: false, text: SnippetParser.asInsertText(resourceEdit.textEdit.text) };
412
+ } else {
413
+ return resourceEdit.textEdit;
414
+ }
415
+ }
416
+ }