aligned rename with the java IDE.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta
2020-08-01 15:31:12 +02:00
parent 111ba7fef3
commit ada0f4c7ed
4 changed files with 74 additions and 18 deletions

View File

@@ -1,21 +1,34 @@
import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { open } from '@theia/core/lib/browser/opener-service';
import { FileStat } from '@theia/filesystem/lib/common';
import { CommandRegistry } from '@theia/core/lib/common/command';
import { CommandRegistry, CommandService } from '@theia/core/lib/common/command';
import { WorkspaceCommandContribution as TheiaWorkspaceCommandContribution, WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands';
import { Extensions } from '../../../common/protocol';
import { Sketch } from '../../../common/protocol';
import { WorkspaceInputDialog } from './workspace-input-dialog';
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
import { SaveAsSketch } from '../../contributions/save-as-sketch';
import { SingleTextInputDialog } from '@theia/core/lib/browser';
@injectable()
export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribution {
@inject(SketchesServiceClientImpl)
protected readonly sketchesServiceClient: SketchesServiceClientImpl;
@inject(CommandService)
protected readonly commandService: CommandService;
registerCommands(registry: CommandRegistry): void {
super.registerCommands(registry);
registry.unregisterCommand(WorkspaceCommands.NEW_FILE);
registry.registerCommand(WorkspaceCommands.NEW_FILE, this.newWorkspaceRootUriAwareCommandHandler({
execute: uri => this.newFile(uri)
}));
registry.unregisterCommand(WorkspaceCommands.FILE_RENAME);
registry.registerCommand(WorkspaceCommands.FILE_RENAME, this.newUriAwareCommandHandler({
execute: uri => this.renameFile(uri)
}));
}
protected async newFile(uri: URI | undefined): Promise<void> {
@@ -57,7 +70,7 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
if (!extension) {
return 'Invalid filename.'; // XXX: this should not happen as we forcefully append `.ino` if it's not there.
}
if (Extensions.ALL.indexOf(`.${extension}`) === -1) {
if (Sketch.Extensions.ALL.indexOf(`.${extension}`) === -1) {
return `.${extension} is not a valid extension.`;
}
return '';
@@ -77,4 +90,44 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
}
return name;
}
protected async renameFile(uri: URI | undefined): Promise<void> {
if (!uri) {
return;
}
const sketch = await this.sketchesServiceClient.currentSketch();
if (!sketch) {
return;
}
if (uri.toString() === sketch.mainFileUri) {
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, { execOnlyIfTemp: false, openAfterMove: true });
return;
}
const parent = await this.getParent(uri);
if (!parent) {
return;
}
const initialValue = uri.path.base;
const dialog = new SingleTextInputDialog({
title: 'New name for file',
initialValue,
initialSelectionRange: {
start: 0,
end: uri.path.name.length
},
validate: (name, mode) => {
if (initialValue === name && mode === 'preview') {
return false;
}
return this.validateFileName(name, parent, false);
}
});
const fileName = await dialog.open();
if (fileName) {
const oldUri = uri;
const newUri = uri.parent.resolve(fileName);
this.fileSystem.move(oldUri.toString(), newUri.toString());
}
}
}