Skip to content

Commit 6798167

Browse files
danarad05amiramw
authored andcommitted
7989-refactor-preview: Support for Refactor Preview functionality
Signed-off-by: Dan Arad <dan.arad@sap.com>
1 parent f1d4690 commit 6798167

37 files changed

+1240
-42
lines changed

configs/root-compilation.tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
{
1717
"path": "../dev-packages/cli/compile.tsconfig.json"
1818
},
19+
{
20+
"path": "../packages/bulk-edit/compile.tsconfig.json"
21+
},
1922
{
2023
"path": "../packages/callhierarchy/compile.tsconfig.json"
2124
},

examples/browser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@theia/api-samples": "1.10.0",
18+
"@theia/bulk-edit": "1.10.0",
1819
"@theia/callhierarchy": "1.10.0",
1920
"@theia/console": "1.10.0",
2021
"@theia/core": "1.10.0",

examples/electron/compile.tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
{
1515
"path": "../api-samples/compile.tsconfig.json"
1616
},
17+
{
18+
"path": "../../packages/bulk-edit/compile.tsconfig.json"
19+
},
1720
{
1821
"path": "../../packages/callhierarchy/compile.tsconfig.json"
1922
},

examples/electron/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@theia/api-samples": "1.10.0",
18+
"@theia/bulk-edit": "1.10.0",
1819
"@theia/callhierarchy": "1.10.0",
1920
"@theia/console": "1.10.0",
2021
"@theia/core": "1.10.0",

packages/bulk-edit/.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @type {import('eslint').Linter.Config} */
2+
module.exports = {
3+
extends: [
4+
'../../configs/build.eslintrc.json'
5+
],
6+
parserOptions: {
7+
tsconfigRootDir: __dirname,
8+
project: 'compile.tsconfig.json'
9+
}
10+
};

packages/bulk-edit/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<div align='center'>
2+
3+
<br />
4+
5+
<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6+
7+
<h2>ECLIPSE THEIA - BULK EDIT EXTENSION</h2>
8+
9+
<hr />
10+
11+
</div>
12+
13+
## Description
14+
15+
The `@theia/bulk-edit` extension contributes a `Refactor Preview` widget to the application that displays WorkspaceEdits to end-users.
16+
17+
## Additional Information
18+
19+
- [API documentation for `@theia/bulk-edit`](https://eclipse-theia.github.io/theia/docs/next/modules/bulk-edit.html)
20+
- [Theia - GitHub](https://github.com/eclipse-theia/theia)
21+
- [Theia - Website](https://theia-ide.org/)
22+
23+
## License
24+
25+
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
26+
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
27+
28+
## Trademark
29+
"Theia" is a trademark of the Eclipse Foundation
30+
https://www.eclipse.org/theia
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"extends": "../../configs/base.tsconfig",
3+
"compilerOptions": {
4+
"composite": true,
5+
"rootDir": "src",
6+
"outDir": "lib"
7+
},
8+
"include": [
9+
"src"
10+
],
11+
"references": [
12+
{
13+
"path": "../core/compile.tsconfig.json"
14+
},
15+
{
16+
"path": "../editor/compile.tsconfig.json"
17+
},
18+
{
19+
"path": "../filesystem/compile.tsconfig.json"
20+
},
21+
{
22+
"path": "../monaco/compile.tsconfig.json"
23+
},
24+
{
25+
"path": "../workspace/compile.tsconfig.json"
26+
}
27+
]
28+
}

packages/bulk-edit/package.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@theia/bulk-edit",
3+
"version": "1.10.0",
4+
"description": "Theia - Bulk Edit Extension",
5+
"dependencies": {
6+
"@theia/core": "^1.10.0",
7+
"@theia/editor": "^1.10.0",
8+
"@theia/filesystem": "^1.10.0",
9+
"@theia/monaco": "^1.10.0",
10+
"@theia/workspace": "^1.10.0"
11+
},
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"theiaExtensions": [
16+
{
17+
"frontend": "lib/browser/bulk-edit-frontend-module"
18+
}
19+
],
20+
"keywords": [
21+
"theia-extension"
22+
],
23+
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
24+
"repository": {
25+
"type": "git",
26+
"url": "https://github.com/eclipse-theia/theia.git"
27+
},
28+
"bugs": {
29+
"url": "https://github.com/eclipse-theia/theia/issues"
30+
},
31+
"homepage": "https://github.com/eclipse-theia/theia",
32+
"files": [
33+
"lib",
34+
"src"
35+
],
36+
"scripts": {
37+
"lint": "theiaext lint",
38+
"build": "theiaext build",
39+
"watch": "theiaext watch",
40+
"clean": "theiaext clean",
41+
"test": "theiaext test"
42+
},
43+
"devDependencies": {
44+
"@theia/ext-scripts": "^1.10.0"
45+
},
46+
"nyc": {
47+
"extends": "../../configs/nyc.json"
48+
}
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 { Command } from '@theia/core/lib/common';
18+
19+
export namespace BulkEditCommands {
20+
export const TOGGLE_VIEW: Command = {
21+
id: 'bulk-edit:toggleView'
22+
};
23+
24+
export const APPLY: Command = {
25+
id: 'bulk-edit:apply',
26+
iconClass: 'codicon codicon-check'
27+
};
28+
29+
export const DISCARD: Command = {
30+
id: 'bulk-edit:discard',
31+
iconClass: 'clear-all'
32+
};
33+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)