Skip to content

Commit

Permalink
[WIP] [editor] Renaming a file should rename a corresponding editor
Browse files Browse the repository at this point in the history
Signed-off-by: Guy Perron <[email protected]>
  • Loading branch information
lmcgupe committed Oct 2, 2017
1 parent 825848e commit 9f174dd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
22 changes: 22 additions & 0 deletions packages/core/src/browser/widget-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ export class WidgetManager {
return widget as T;
}

/*
* check if a widget exists already and rename it
*/
renameWidget(factoryId: string, options: any, newOptions: any, newLabel: any): void {
let key = this.toKey({ factoryId, options });
let existingWidget = this.widgets.get(key);
if (existingWidget) {
existingWidget.title.label = newLabel;
this.updateWidgetKey(key, existingWidget, factoryId, options, newOptions);
}
}

/*
* update a widget key following a rename
*/
private updateWidgetKey(key: any, widget: Widget, factoryId: string, options: any, newOptions: any): void {
this.widgets.delete(key);
options = newOptions;
key = this.toKey({ factoryId, options });
this.widgets.set(key, widget);
}

/*
* returns the construction description for the given widget, or undefined if the widget was not created through this manager.
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/editor/src/browser/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export interface EditorManager extends OpenHandler {
* Reject if the given input is not an editor input or an editor cannot be opened.
*/
open(uri: URI, input?: EditorInput): Promise<EditorWidget>;

renameWidget(uri: URI, newUri: URI, newLabel: string, input?: EditorInput): void;

/**
* The most recently focused editor.
*/
Expand Down Expand Up @@ -105,6 +108,10 @@ export class EditorManagerImpl implements EditorManager, WidgetFactory {
});
}

renameWidget(uri: URI, newUri: URI, newLabel: string, input?: EditorInput): void {
this.widgetManager.renameWidget(this.id, uri.toString(), newUri.toString(), newLabel);
}

// don't call directly, but use WidgetManager
createWidget(uriAsString: string): Promise<Widget> {
const uri = new URI(uriAsString);
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Theia - Workspace Extension",
"dependencies": {
"@theia/core": "^0.1.1",
"@theia/editor": "^0.1.1",
"@theia/filesystem": "^0.1.1"
},
"publishConfig": {
Expand Down
13 changes: 9 additions & 4 deletions packages/workspace/src/browser/workspace-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { UriSelection } from '@theia/filesystem/lib/common/filesystem-selection'
import { SingleTextInputDialog, ConfirmDialog } from "@theia/core/lib/browser/dialogs";
import { OpenerService, OpenHandler, open } from "@theia/core/lib/browser";
import { WorkspaceServer } from '../common/workspace-protocol';
import { EditorManager } from "@theia/editor/lib/browser";

export namespace WorkspaceCommands {
export const NEW_FILE = 'file:newFile';
Expand Down Expand Up @@ -61,7 +62,9 @@ export class WorkspaceCommandContribution implements CommandContribution {
@inject(FileSystem) protected readonly fileSystem: FileSystem,
@inject(WorkspaceServer) protected readonly workspaceServer: WorkspaceServer,
@inject(SelectionService) protected readonly selectionService: SelectionService,
@inject(OpenerService) protected readonly openerService: OpenerService
@inject(OpenerService) protected readonly openerService: OpenerService,
@inject(EditorManager) private editorService: EditorManager

) { }

registerCommands(registry: CommandRegistry): void {
Expand Down Expand Up @@ -104,9 +107,11 @@ export class WorkspaceCommandContribution implements CommandContribution {
initialValue: uri.path.base,
validate: name => this.validateFileName(name, parent)
});
dialog.open().then(name =>
this.fileSystem.move(uri.toString(), uri.parent.resolve(name).toString())
);
dialog.open().then(name => {
this.fileSystem.move(uri.toString(), uri.parent.resolve(name).toString()).then(() => {
this.editorService.renameWidget(uri.parent.resolve(uri.path.base), uri.parent.resolve(name), name);
});
});
})
}));

Expand Down

0 comments on commit 9f174dd

Please sign in to comment.