Skip to content

Commit c1a87c1

Browse files
Offer permanent deletion if Trash deletion fails (eclipse-theia#10151)
Fixes eclipse-theia#10150
1 parent 450d257 commit c1a87c1

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

packages/workspace/src/browser/workspace-delete-handler.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { WorkspaceUtils } from './workspace-utils';
2323
import { FileService } from '@theia/filesystem/lib/browser/file-service';
2424
import { FileSystemPreferences } from '@theia/filesystem/lib/browser/filesystem-preferences';
2525
import { FileDeleteOptions, FileSystemProviderCapabilities } from '@theia/filesystem/lib/common/files';
26+
import { nls } from '@theia/core/lib/common/nls';
2627

2728
@injectable()
2829
export class WorkspaceDeleteHandler implements UriCommandHandler<URI[]> {
@@ -147,18 +148,58 @@ export class WorkspaceDeleteHandler implements UriCommandHandler<URI[]> {
147148
* Perform deletion of a given URI.
148149
*
149150
* @param uri URI of selected resource.
151+
* @param options deletion options.
150152
*/
151153
protected async delete(uri: URI, options: FileDeleteOptions): Promise<void> {
152154
try {
153155
await Promise.all([
154156
this.closeWithoutSaving(uri),
155-
this.fileService.delete(uri, options)
157+
options.useTrash ? this.moveFileToTrash(uri, options) : this.deleteFilePermanently(uri, options)
156158
]);
157159
} catch (e) {
158160
console.error(e);
159161
}
160162
}
161163

164+
protected async deleteFilePermanently(uri: URI, options: FileDeleteOptions): Promise<void> {
165+
this.fileService.delete(uri, { ...options, useTrash: false });
166+
}
167+
168+
protected async moveFileToTrash(uri: URI, options: FileDeleteOptions): Promise<void> {
169+
try {
170+
this.fileService.delete(uri, { ...options, useTrash: true });
171+
} catch (error) {
172+
console.error('Error deleting with trash:', error);
173+
if (await this.confirmDeletePermanently(uri)) {
174+
return this.deleteFilePermanently(uri, options);
175+
}
176+
}
177+
}
178+
179+
/**
180+
* Display dialog to confirm the permanent deletion of a file.
181+
*
182+
* @param uri URI of selected resource.
183+
*/
184+
protected async confirmDeletePermanently(uri: URI): Promise<boolean> {
185+
const title = nls.localize('theia/workspace/confirmDeletePermanently.title', 'Error deleting file');
186+
187+
const msg = document.createElement('div');
188+
189+
const question = document.createElement('p');
190+
question.textContent = nls.localize('theia/workspace/confirmDeletePermanently.description',
191+
'Failed to delete "{0}" using the Trash. Do you want to permanently delete instead?',
192+
uri.path.base);
193+
msg.append(question);
194+
195+
const info = document.createElement('p');
196+
info.textContent = nls.localize('theia/workspace/confirmDeletePermanently.solution', 'You can disable the use of Trash in the preferences.');
197+
msg.append(info);
198+
199+
const response = await new ConfirmDialog({ title, msg }).open();
200+
return response || false;
201+
}
202+
162203
/**
163204
* Close widget without saving changes.
164205
*

0 commit comments

Comments
 (0)