|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2021 SAP SE or an SAP affiliate company and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | + * with the GNU Classpath Exception which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + ********************************************************************************/ |
| 16 | + |
| 17 | +import { injectable, inject } from 'inversify'; |
| 18 | +import { Widget } from '@theia/core/lib/browser/widgets/widget'; |
| 19 | +import { CommandRegistry } from '@theia/core/lib/common'; |
| 20 | +import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution'; |
| 21 | +import { BulkEditCommands } from './bulk-edit-commands'; |
| 22 | +import { MonacoBulkEditService } from '@theia/monaco/lib/browser/monaco-bulk-edit-service'; |
| 23 | +import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar'; |
| 24 | +import { BulkEditTreeWidget, BULK_EDIT_TREE_WIDGET_ID } from './bulk-edit-tree'; |
| 25 | +import { QuickViewService } from '@theia/core/lib/browser/quick-view-service'; |
| 26 | + |
| 27 | +export const BULK_EDIT_WIDGET_NAME = 'Refactor Preview'; |
| 28 | + |
| 29 | +@injectable() |
| 30 | +export class BulkEditContribution extends AbstractViewContribution<BulkEditTreeWidget> implements TabBarToolbarContribution { |
| 31 | + private workspaceEdit: monaco.languages.WorkspaceEdit; |
| 32 | + |
| 33 | + @inject(QuickViewService) |
| 34 | + protected readonly quickView: QuickViewService; |
| 35 | + |
| 36 | + constructor(private readonly bulkEditService: MonacoBulkEditService) { |
| 37 | + super({ |
| 38 | + widgetId: BULK_EDIT_TREE_WIDGET_ID, |
| 39 | + widgetName: BULK_EDIT_WIDGET_NAME, |
| 40 | + defaultWidgetOptions: { |
| 41 | + area: 'bottom' |
| 42 | + } |
| 43 | + }); |
| 44 | + this.bulkEditService.setPreviewHandler((edits: monaco.languages.WorkspaceEdit) => this.previewEdit(edits)); |
| 45 | + } |
| 46 | + |
| 47 | + registerCommands(registry: CommandRegistry): void { |
| 48 | + super.registerCommands(registry); |
| 49 | + this.quickView.hideItem(BULK_EDIT_WIDGET_NAME); |
| 50 | + |
| 51 | + registry.registerCommand(BulkEditCommands.APPLY, { |
| 52 | + isEnabled: widget => this.withWidget(widget, () => true), |
| 53 | + isVisible: widget => this.withWidget(widget, () => true), |
| 54 | + execute: widget => this.withWidget(widget, () => this.apply()) |
| 55 | + }); |
| 56 | + registry.registerCommand(BulkEditCommands.DISCARD, { |
| 57 | + isEnabled: widget => this.withWidget(widget, () => true), |
| 58 | + isVisible: widget => this.withWidget(widget, () => true), |
| 59 | + execute: widget => this.withWidget(widget, () => this.discard()) |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + async registerToolbarItems(toolbarRegistry: TabBarToolbarRegistry): Promise<void> { |
| 64 | + toolbarRegistry.registerItem({ |
| 65 | + id: BulkEditCommands.APPLY.id, |
| 66 | + command: BulkEditCommands.APPLY.id, |
| 67 | + tooltip: 'Apply Refactoring', |
| 68 | + priority: 0, |
| 69 | + }); |
| 70 | + toolbarRegistry.registerItem({ |
| 71 | + id: BulkEditCommands.DISCARD.id, |
| 72 | + command: BulkEditCommands.DISCARD.id, |
| 73 | + tooltip: 'Discard Refactoring', |
| 74 | + priority: 1, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), cb: (bulkEdit: BulkEditTreeWidget) => T): T | false { |
| 79 | + if (widget instanceof BulkEditTreeWidget) { |
| 80 | + return cb(widget); |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + private async previewEdit(workspaceEdit: monaco.languages.WorkspaceEdit): Promise<monaco.languages.WorkspaceEdit> { |
| 86 | + const widget = await this.openView({ activate: true }); |
| 87 | + |
| 88 | + if (widget) { |
| 89 | + this.workspaceEdit = workspaceEdit; |
| 90 | + await widget.initModel(workspaceEdit); |
| 91 | + } |
| 92 | + |
| 93 | + return workspaceEdit; |
| 94 | + } |
| 95 | + |
| 96 | + private apply(): void { |
| 97 | + if (this.workspaceEdit?.edits) { |
| 98 | + this.workspaceEdit.edits.forEach(edit => { |
| 99 | + if (edit.metadata) { |
| 100 | + edit.metadata.needsConfirmation = false; |
| 101 | + } |
| 102 | + }); |
| 103 | + this.bulkEditService.apply(this.workspaceEdit); |
| 104 | + } |
| 105 | + this.closeView(); |
| 106 | + } |
| 107 | + |
| 108 | + private discard(): void { |
| 109 | + if (this.workspaceEdit) { |
| 110 | + this.workspaceEdit.edits = []; |
| 111 | + } |
| 112 | + this.closeView(); |
| 113 | + } |
| 114 | +} |
0 commit comments