|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { VSBuffer } from 'vs/base/common/buffer'; |
| 7 | +import { Codicon } from 'vs/base/common/codicons'; |
| 8 | +import { ITextModelService } from 'vs/editor/common/services/resolverService'; |
| 9 | +import { localize } from 'vs/nls'; |
| 10 | +import { Action2 } from 'vs/platform/actions/common/actions'; |
| 11 | +import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService'; |
| 12 | +import { InMemoryFileSystemProvider } from 'vs/platform/files/common/inMemoryFilesystemProvider'; |
| 13 | +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; |
| 14 | +import { INotificationService } from 'vs/platform/notification/common/notification'; |
| 15 | +import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput'; |
| 16 | +import { MergeEditor } from 'vs/workbench/contrib/mergeEditor/browser/view/mergeEditor'; |
| 17 | +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; |
| 18 | +import { IWorkbenchFileService } from 'vs/workbench/services/files/common/files'; |
| 19 | +import { URI } from 'vs/base/common/uri'; |
| 20 | +import { MergeEditorInput } from 'vs/workbench/contrib/mergeEditor/browser/mergeEditorInput'; |
| 21 | + |
| 22 | +interface MergeEditorContents { |
| 23 | + languageId: string; |
| 24 | + base: string; |
| 25 | + input1: string; |
| 26 | + input2: string; |
| 27 | + result: string; |
| 28 | +} |
| 29 | + |
| 30 | +export class MergeEditorCopyContentsToJSON extends Action2 { |
| 31 | + |
| 32 | + constructor() { |
| 33 | + super({ |
| 34 | + id: 'merge.dev.copyContents', |
| 35 | + title: localize('merge.dev.copyContents', "Developer Merge Editor: Copy Contents of Inputs, Base and Result as JSON"), |
| 36 | + icon: Codicon.layoutCentered, |
| 37 | + f1: true, |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + run(accessor: ServicesAccessor): void { |
| 42 | + const { activeEditorPane } = accessor.get(IEditorService); |
| 43 | + const clipboardService = accessor.get(IClipboardService); |
| 44 | + const notificationService = accessor.get(INotificationService); |
| 45 | + |
| 46 | + if (!(activeEditorPane instanceof MergeEditor)) { |
| 47 | + notificationService.info({ |
| 48 | + name: localize('mergeEditor.name', 'Merge Editor'), |
| 49 | + message: localize('mergeEditor.noActiveMergeEditor', "No active merge editor") |
| 50 | + }); |
| 51 | + return; |
| 52 | + } |
| 53 | + const model = activeEditorPane.model; |
| 54 | + if (!model) { |
| 55 | + return; |
| 56 | + } |
| 57 | + const contents: MergeEditorContents = { |
| 58 | + languageId: model.result.getLanguageId(), |
| 59 | + base: model.base.getValue(), |
| 60 | + input1: model.input1.getValue(), |
| 61 | + input2: model.input2.getValue(), |
| 62 | + result: model.result.getValue(), |
| 63 | + }; |
| 64 | + const jsonStr = JSON.stringify(contents, undefined, 4); |
| 65 | + clipboardService.writeText(jsonStr); |
| 66 | + |
| 67 | + notificationService.info({ |
| 68 | + name: localize('mergeEditor.name', 'Merge Editor'), |
| 69 | + message: localize('mergeEditor.successfullyCopiedMergeEditorContents', "Successfully copied merge editor contents"), |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export class MergeEditorOpenContents extends Action2 { |
| 75 | + |
| 76 | + constructor() { |
| 77 | + super({ |
| 78 | + id: 'merge.dev.openContents', |
| 79 | + title: localize('merge.dev.openContents', "Developer Merge Editor: Open Contents of Inputs, Base and Result from JSON"), |
| 80 | + icon: Codicon.layoutCentered, |
| 81 | + f1: true, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + async run(accessor: ServicesAccessor): Promise<void> { |
| 86 | + const service = accessor.get(IWorkbenchFileService); |
| 87 | + const instaService = accessor.get(IInstantiationService); |
| 88 | + const editorService = accessor.get(IEditorService); |
| 89 | + const inputService = accessor.get(IQuickInputService); |
| 90 | + const clipboardService = accessor.get(IClipboardService); |
| 91 | + const textModelService = accessor.get(ITextModelService); |
| 92 | + |
| 93 | + const result = await inputService.input({ |
| 94 | + prompt: localize('mergeEditor.enterJSON', 'Enter JSON'), |
| 95 | + value: await clipboardService.readText(), |
| 96 | + }); |
| 97 | + if (!result) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const content: MergeEditorContents = JSON.parse(result); |
| 102 | + |
| 103 | + const scheme = 'merge-editor-dev'; |
| 104 | + |
| 105 | + let provider = service.getProvider(scheme) as InMemoryFileSystemProvider | undefined; |
| 106 | + if (!provider) { |
| 107 | + provider = new InMemoryFileSystemProvider(); |
| 108 | + service.registerProvider(scheme, provider); |
| 109 | + } |
| 110 | + |
| 111 | + const baseUri = URI.from({ scheme, path: '/ancestor' }); |
| 112 | + const input1Uri = URI.from({ scheme, path: '/input1' }); |
| 113 | + const input2Uri = URI.from({ scheme, path: '/input2' }); |
| 114 | + const resultUri = URI.from({ scheme, path: '/result' }); |
| 115 | + |
| 116 | + function writeFile(uri: URI, content: string): Promise<void> { |
| 117 | + return provider!.writeFile(uri, VSBuffer.fromString(content).buffer, { create: true, overwrite: true, unlock: true }); |
| 118 | + } |
| 119 | + |
| 120 | + await Promise.all([ |
| 121 | + writeFile(baseUri, content.base), |
| 122 | + writeFile(input1Uri, content.input1), |
| 123 | + writeFile(input2Uri, content.input2), |
| 124 | + writeFile(resultUri, content.result), |
| 125 | + ]); |
| 126 | + |
| 127 | + async function setLanguageId(uri: URI, languageId: string): Promise<void> { |
| 128 | + const ref = await textModelService.createModelReference(uri); |
| 129 | + ref.object.textEditorModel.setMode(languageId); |
| 130 | + ref.dispose(); |
| 131 | + } |
| 132 | + |
| 133 | + await Promise.all([ |
| 134 | + setLanguageId(baseUri, content.languageId), |
| 135 | + setLanguageId(input1Uri, content.languageId), |
| 136 | + setLanguageId(input2Uri, content.languageId), |
| 137 | + setLanguageId(resultUri, content.languageId), |
| 138 | + ]); |
| 139 | + |
| 140 | + const input = instaService.createInstance( |
| 141 | + MergeEditorInput, |
| 142 | + baseUri, |
| 143 | + { uri: input1Uri, description: 'Input 1', detail: '(from JSON)' }, |
| 144 | + { uri: input2Uri, description: 'Input 2', detail: '(from JSON)' }, |
| 145 | + resultUri, |
| 146 | + ); |
| 147 | + editorService.openEditor(input); |
| 148 | + } |
| 149 | +} |
0 commit comments