Skip to content

Commit

Permalink
workspace.onDidRenameFile and workspace.onWillRenameFile implementati…
Browse files Browse the repository at this point in the history
…ons are missed #4167

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny committed Mar 11, 2019
1 parent f59ecde commit 15a3b34
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 5 deletions.
18 changes: 17 additions & 1 deletion packages/filesystem/src/browser/filesystem-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export namespace FileMoveEvent {
}
}

export interface FileWillMoveEvent {
sourceUri: URI
targetUri: URI
}

@injectable()
export class FileSystemWatcher implements Disposable {

Expand All @@ -88,6 +93,9 @@ export class FileSystemWatcher implements Disposable {
protected readonly onDidMoveEmitter = new Emitter<FileMoveEvent>();
readonly onDidMove: Event<FileMoveEvent> = this.onDidMoveEmitter.event;

protected readonly onWillMoveEmitter = new Emitter<FileWillMoveEvent>();
readonly onWillMove: Event<FileWillMoveEvent> = this.onWillMoveEmitter.event;

@inject(FileSystemWatcherServer)
protected readonly server: FileSystemWatcherServer;

Expand Down Expand Up @@ -120,7 +128,8 @@ export class FileSystemWatcher implements Disposable {

this.filesystem.setClient({
shouldOverwrite: this.shouldOverwrite.bind(this),
onDidMove: this.fireDidMove.bind(this)
onDidMove: this.fireDidMove.bind(this),
onWillMove: this.fireWillMove.bind(this)
});
}

Expand Down Expand Up @@ -185,4 +194,11 @@ export class FileSystemWatcher implements Disposable {
});
}

protected fireWillMove(sourceUri: string, targetUri: string): void {
this.onWillMoveEmitter.fire({
sourceUri: new URI(sourceUri),
targetUri: new URI(targetUri)
});
}

}
5 changes: 5 additions & 0 deletions packages/filesystem/src/common/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface FileSystemClient {

onDidMove(sourceUri: string, targetUri: string): void;

onWillMove(sourceUri: string, targetUri: string): void;
}

@injectable()
Expand All @@ -217,6 +218,10 @@ export class DispatchingFileSystemClient implements FileSystemClient {
this.clients.forEach(client => client.onDidMove(sourceUri, targetUri));
}

onWillMove(sourceUri: string, targetUri: string): void {
this.clients.forEach(client => client.onWillMove(sourceUri, targetUri));
}

}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/filesystem/src/node/node-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ export class FileSystemNode implements FileSystem {
}

async move(sourceUri: string, targetUri: string, options?: FileMoveOptions): Promise<FileStat> {
if (this.client) {
this.client.onWillMove(sourceUri, targetUri);
}
const result = await this.doMove(sourceUri, targetUri, options);
if (this.client) {
this.client.onDidMove(sourceUri, targetUri);
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-ext/src/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export interface FileChangeEvent {
type: FileChangeEventType
}

export interface FileMoveEvent {
subscriberId: string,
oldUri: UriComponents,
newUri: UriComponents
}

export interface FileWillMoveEvent {
subscriberId: string,
oldUri: UriComponents,
newUri: UriComponents
}

export type FileChangeEventType = 'created' | 'updated' | 'deleted';

export enum CompletionTriggerKind {
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import {
Breakpoint,
ColorPresentation,
RenameLocation,
FileMoveEvent,
FileWillMoveEvent
} from './model';
import { ExtPluginApi } from '../common/plugin-ext-api-contribution';
import { KeysToAnyValues, KeysToKeysToAnyValue } from '../common/types';
Expand Down Expand Up @@ -388,6 +390,8 @@ export interface WorkspaceExt {
$onWorkspaceFoldersChanged(event: WorkspaceRootsChangeEvent): void;
$provideTextDocumentContent(uri: string): Promise<string | undefined>;
$fileChanged(event: FileChangeEvent): void;
$onFileRename(event: FileMoveEvent): void;
$onWillRename(event: FileWillMoveEvent): Promise<any>;
}

export interface DialogsMain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { interfaces } from 'inversify';
import { FileSystemWatcher, FileChangeEvent, FileChangeType, FileChange } from '@theia/filesystem/lib/browser/filesystem-watcher';
import { FileSystemWatcher, FileChangeEvent, FileChangeType, FileChange, FileMoveEvent, FileWillMoveEvent } from '@theia/filesystem/lib/browser/filesystem-watcher';
import { WorkspaceExt } from '../../api/plugin-api';
import { FileWatcherSubscriberOptions } from '../../api/model';
import { parse, ParsedPattern, IRelativePattern } from '../../common/glob';
Expand All @@ -41,6 +41,8 @@ export class InPluginFileSystemWatcherManager {

const fileSystemWatcher = container.get(FileSystemWatcher);
fileSystemWatcher.onFilesChanged(event => this.onFilesChangedEventHandler(event));
fileSystemWatcher.onDidMove(event => this.onDidMoveEventHandler(event));
fileSystemWatcher.onWillMove(event => this.onWillMoveEventHandler(event));
}

// Filter file system changes according to subscribers settings here to avoid unneeded traffic.
Expand Down Expand Up @@ -72,6 +74,28 @@ export class InPluginFileSystemWatcherManager {
}
}

// Filter file system changes according to subscribers settings here to avoid unneeded traffic.
onDidMoveEventHandler(change: FileMoveEvent): void {
for (const [id] of this.subscribers) {
this.proxy.$onFileRename({
subscriberId: id,
oldUri: theiaUritoUriComponents(change.sourceUri),
newUri: theiaUritoUriComponents(change.targetUri)
});
}
}

// Filter file system changes according to subscribers settings here to avoid unneeded traffic.
onWillMoveEventHandler(change: FileWillMoveEvent): void {
for (const [id] of this.subscribers) {
this.proxy.$onWillRename({
subscriberId: id,
oldUri: theiaUritoUriComponents(change.sourceUri),
newUri: theiaUritoUriComponents(change.targetUri)
});
}
}

private uriMatches(subscriber: FileWatcherSubscriber, fileChange: FileChange): boolean {
return subscriber.mather(fileChange.uri.path.toString());
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ export function createAPIFactory(
},
// Experimental API https://github.com/theia-ide/theia/issues/4167
onDidRenameFile(listener, thisArg?, disposables?): theia.Disposable {
return new Disposable(() => { });
return workspaceExt.onDidRenameFile(listener, thisArg, disposables);
},
onWillRenameFile(listener, thisArg?, disposables?): theia.Disposable {
return new Disposable(() => { });
return workspaceExt.onWillRenameFile(listener, thisArg, disposables);
}
};

Expand Down
28 changes: 27 additions & 1 deletion packages/plugin-ext/src/plugin/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../api/plugin-api';
import { Path } from '@theia/core/lib/common/path';
import { RPCProtocol } from '../api/rpc-protocol';
import { WorkspaceRootsChangeEvent, FileChangeEvent } from '../api/model';
import { WorkspaceRootsChangeEvent, FileChangeEvent, FileMoveEvent, FileWillMoveEvent } from '../api/model';
import { EditorsAndDocumentsExtImpl } from './editors-and-documents';
import { InPluginFileSystemWatcherProxy } from './in-plugin-filesystem-watcher-proxy';
import URI from 'vscode-uri';
Expand Down Expand Up @@ -280,4 +280,30 @@ export class WorkspaceExtImpl implements WorkspaceExt {
return normalize(result, true);
}

// Experimental API https://github.com/theia-ide/theia/issues/4167
private workspaceWillRenameFileEmitter = new Emitter<theia.FileWillRenameEvent>();
private workspaceDidRenameFileEmitter = new Emitter<theia.FileRenameEvent>();

/**
* Adds a listener for an event that is emitted when a workspace file is going to be renamed.
*/
public readonly onWillRenameFile: Event<theia.FileWillRenameEvent> = this.workspaceWillRenameFileEmitter.event;

/**
* Adds a listener for an event that is emitted when a workspace file is renamed.
*/
public readonly onDidRenameFile: Event<theia.FileRenameEvent> = this.workspaceDidRenameFileEmitter.event;

$onFileRename(event: FileMoveEvent) {
this.workspaceDidRenameFileEmitter.fire(Object.freeze({ oldUri: URI.revive(event.oldUri), newUri: URI.revive(event.newUri) }));
}

/* tslint:disable-next-line:no-any */
$onWillRename(event: FileWillMoveEvent): Promise<any> {
return this.workspaceWillRenameFileEmitter.fire({
oldUri: URI.revive(event.oldUri),
newUri: URI.revive(event.newUri),
waitUntil: (thenable: Promise<theia.WorkspaceEdit>): void => { }
});
}
}

0 comments on commit 15a3b34

Please sign in to comment.