Skip to content

Commit

Permalink
7989-refactor-preview: Support for Refactor Preview functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Arad <dan.arad@sap.com>
  • Loading branch information
danarad05 authored and amiramw committed Feb 11, 2021
1 parent f1d4690 commit 6798167
Show file tree
Hide file tree
Showing 37 changed files with 1,240 additions and 42 deletions.
3 changes: 3 additions & 0 deletions configs/root-compilation.tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
{
"path": "../dev-packages/cli/compile.tsconfig.json"
},
{
"path": "../packages/bulk-edit/compile.tsconfig.json"
},
{
"path": "../packages/callhierarchy/compile.tsconfig.json"
},
Expand Down
1 change: 1 addition & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@theia/api-samples": "1.10.0",
"@theia/bulk-edit": "1.10.0",
"@theia/callhierarchy": "1.10.0",
"@theia/console": "1.10.0",
"@theia/core": "1.10.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/electron/compile.tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
{
"path": "../api-samples/compile.tsconfig.json"
},
{
"path": "../../packages/bulk-edit/compile.tsconfig.json"
},
{
"path": "../../packages/callhierarchy/compile.tsconfig.json"
},
Expand Down
1 change: 1 addition & 0 deletions examples/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@theia/api-samples": "1.10.0",
"@theia/bulk-edit": "1.10.0",
"@theia/callhierarchy": "1.10.0",
"@theia/console": "1.10.0",
"@theia/core": "1.10.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/bulk-edit/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: [
'../../configs/build.eslintrc.json'
],
parserOptions: {
tsconfigRootDir: __dirname,
project: 'compile.tsconfig.json'
}
};
30 changes: 30 additions & 0 deletions packages/bulk-edit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div align='center'>

<br />

<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />

<h2>ECLIPSE THEIA - BULK EDIT EXTENSION</h2>

<hr />

</div>

## Description

The `@theia/bulk-edit` extension contributes a `Refactor Preview` widget to the application that displays WorkspaceEdits to end-users.

## Additional Information

- [API documentation for `@theia/bulk-edit`](https://eclipse-theia.github.io/theia/docs/next/modules/bulk-edit.html)
- [Theia - GitHub](https://github.com/eclipse-theia/theia)
- [Theia - Website](https://theia-ide.org/)

## License

- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)

## Trademark
"Theia" is a trademark of the Eclipse Foundation
https://www.eclipse.org/theia
28 changes: 28 additions & 0 deletions packages/bulk-edit/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
],
"references": [
{
"path": "../core/compile.tsconfig.json"
},
{
"path": "../editor/compile.tsconfig.json"
},
{
"path": "../filesystem/compile.tsconfig.json"
},
{
"path": "../monaco/compile.tsconfig.json"
},
{
"path": "../workspace/compile.tsconfig.json"
}
]
}
49 changes: 49 additions & 0 deletions packages/bulk-edit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@theia/bulk-edit",
"version": "1.10.0",
"description": "Theia - Bulk Edit Extension",
"dependencies": {
"@theia/core": "^1.10.0",
"@theia/editor": "^1.10.0",
"@theia/filesystem": "^1.10.0",
"@theia/monaco": "^1.10.0",
"@theia/workspace": "^1.10.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"frontend": "lib/browser/bulk-edit-frontend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/eclipse-theia/theia.git"
},
"bugs": {
"url": "https://github.com/eclipse-theia/theia/issues"
},
"homepage": "https://github.com/eclipse-theia/theia",
"files": [
"lib",
"src"
],
"scripts": {
"lint": "theiaext lint",
"build": "theiaext build",
"watch": "theiaext watch",
"clean": "theiaext clean",
"test": "theiaext test"
},
"devDependencies": {
"@theia/ext-scripts": "^1.10.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
33 changes: 33 additions & 0 deletions packages/bulk-edit/src/browser/bulk-edit-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/********************************************************************************
* Copyright (c) 2021 SAP SE or an SAP affiliate company and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Command } from '@theia/core/lib/common';

export namespace BulkEditCommands {
export const TOGGLE_VIEW: Command = {
id: 'bulk-edit:toggleView'
};

export const APPLY: Command = {
id: 'bulk-edit:apply',
iconClass: 'codicon codicon-check'
};

export const DISCARD: Command = {
id: 'bulk-edit:discard',
iconClass: 'clear-all'
};
}
114 changes: 114 additions & 0 deletions packages/bulk-edit/src/browser/bulk-edit-contribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/********************************************************************************
* Copyright (c) 2021 SAP SE or an SAP affiliate company and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable, inject } from 'inversify';
import { Widget } from '@theia/core/lib/browser/widgets/widget';
import { CommandRegistry } from '@theia/core/lib/common';
import { AbstractViewContribution } from '@theia/core/lib/browser/shell/view-contribution';
import { BulkEditCommands } from './bulk-edit-commands';
import { MonacoBulkEditService } from '@theia/monaco/lib/browser/monaco-bulk-edit-service';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { BulkEditTreeWidget, BULK_EDIT_TREE_WIDGET_ID } from './bulk-edit-tree';
import { QuickViewService } from '@theia/core/lib/browser/quick-view-service';

export const BULK_EDIT_WIDGET_NAME = 'Refactor Preview';

@injectable()
export class BulkEditContribution extends AbstractViewContribution<BulkEditTreeWidget> implements TabBarToolbarContribution {
private workspaceEdit: monaco.languages.WorkspaceEdit;

@inject(QuickViewService)
protected readonly quickView: QuickViewService;

constructor(private readonly bulkEditService: MonacoBulkEditService) {
super({
widgetId: BULK_EDIT_TREE_WIDGET_ID,
widgetName: BULK_EDIT_WIDGET_NAME,
defaultWidgetOptions: {
area: 'bottom'
}
});
this.bulkEditService.setPreviewHandler((edits: monaco.languages.WorkspaceEdit) => this.previewEdit(edits));
}

registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
this.quickView.hideItem(BULK_EDIT_WIDGET_NAME);

registry.registerCommand(BulkEditCommands.APPLY, {
isEnabled: widget => this.withWidget(widget, () => true),
isVisible: widget => this.withWidget(widget, () => true),
execute: widget => this.withWidget(widget, () => this.apply())
});
registry.registerCommand(BulkEditCommands.DISCARD, {
isEnabled: widget => this.withWidget(widget, () => true),
isVisible: widget => this.withWidget(widget, () => true),
execute: widget => this.withWidget(widget, () => this.discard())
});
}

async registerToolbarItems(toolbarRegistry: TabBarToolbarRegistry): Promise<void> {
toolbarRegistry.registerItem({
id: BulkEditCommands.APPLY.id,
command: BulkEditCommands.APPLY.id,
tooltip: 'Apply Refactoring',
priority: 0,
});
toolbarRegistry.registerItem({
id: BulkEditCommands.DISCARD.id,
command: BulkEditCommands.DISCARD.id,
tooltip: 'Discard Refactoring',
priority: 1,
});
}

protected withWidget<T>(widget: Widget | undefined = this.tryGetWidget(), cb: (bulkEdit: BulkEditTreeWidget) => T): T | false {
if (widget instanceof BulkEditTreeWidget) {
return cb(widget);
}
return false;
}

private async previewEdit(workspaceEdit: monaco.languages.WorkspaceEdit): Promise<monaco.languages.WorkspaceEdit> {
const widget = await this.openView({ activate: true });

if (widget) {
this.workspaceEdit = workspaceEdit;
await widget.initModel(workspaceEdit);
}

return workspaceEdit;
}

private apply(): void {
if (this.workspaceEdit?.edits) {
this.workspaceEdit.edits.forEach(edit => {
if (edit.metadata) {
edit.metadata.needsConfirmation = false;
}
});
this.bulkEditService.apply(this.workspaceEdit);
}
this.closeView();
}

private discard(): void {
if (this.workspaceEdit) {
this.workspaceEdit.edits = [];
}
this.closeView();
}
}
44 changes: 44 additions & 0 deletions packages/bulk-edit/src/browser/bulk-edit-frontend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/********************************************************************************
* Copyright (c) 2021 SAP SE or an SAP affiliate company and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { ContainerModule } from 'inversify';
import { WidgetFactory } from '@theia/core/lib/browser/widget-manager';
import { BulkEditTreeWidget, BULK_EDIT_TREE_WIDGET_ID, createBulkEditTreeWidget, InMemoryTextResourceResolver } from './bulk-edit-tree';
import { FrontendApplicationContribution, LabelProviderContribution } from '@theia/core/lib/browser';
import { bindViewContribution } from '@theia/core/lib/browser';
import { BulkEditContribution } from './bulk-edit-contribution';
import { TabBarToolbarContribution } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { BulkEditTreeLabelProvider } from './bulk-edit-tree-label-provider';
import { ResourceResolver } from '@theia/core/lib/common';
import '../../src/browser/style/bulk-edit.css';

export default new ContainerModule(bind => {
bind(BulkEditTreeWidget).toDynamicValue(ctx =>
createBulkEditTreeWidget(ctx.container)
);
bind(WidgetFactory).toDynamicValue(context => ({
id: BULK_EDIT_TREE_WIDGET_ID,
createWidget: () => context.container.get(BulkEditTreeWidget)
}));
bindViewContribution(bind, BulkEditContribution);
bind(FrontendApplicationContribution).toService(BulkEditContribution);
bind(TabBarToolbarContribution).toService(BulkEditContribution);

bind(BulkEditTreeLabelProvider).toSelf().inSingletonScope();
bind(LabelProviderContribution).toService(BulkEditTreeLabelProvider);

bind(InMemoryTextResourceResolver).toSelf().inSingletonScope();
bind(ResourceResolver).toService(InMemoryTextResourceResolver);
});
Loading

0 comments on commit 6798167

Please sign in to comment.