未验证 提交 04b6bd52 编写于 作者: B Benjamin Pasero 提交者: GitHub

Send `vscode.workspace.onDidCreateFiles` events for folders #109088 (#109294)

上级 d175b2de
......@@ -874,6 +874,7 @@ async function openExplorerAndCreate(accessor: ServicesAccessor, isFolder: boole
const editorService = accessor.get(IEditorService);
const viewsService = accessor.get(IViewsService);
const notificationService = accessor.get(INotificationService);
const workingCopyFileService = accessor.get(IWorkingCopyFileService);
await viewsService.openView(VIEW_ID, true);
......@@ -895,7 +896,7 @@ async function openExplorerAndCreate(accessor: ServicesAccessor, isFolder: boole
const onSuccess = async (value: string): Promise<void> => {
try {
const created = isFolder ? await fileService.createFolder(resources.joinPath(folder.resource, value)) : await textFileService.create(resources.joinPath(folder.resource, value));
const created = isFolder ? await workingCopyFileService.createFolder(resources.joinPath(folder.resource, value)) : await textFileService.create(resources.joinPath(folder.resource, value));
await refreshIfSeparator(value, explorerService);
isFolder ?
......
......@@ -132,6 +132,17 @@ export interface IWorkingCopyFileService {
*/
create(resource: URI, contents?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata>;
/**
* Will create a folder and any parent folder that needs to be created.
*
* Working copy owners can listen to the `onWillRunWorkingCopyFileOperation` and
* `onDidRunWorkingCopyFileOperation` events to participate.
*
* Note: events will only be emitted for the provided resource, but not any
* parent folders that are being created as part of the operation.
*/
createFolder(resource: URI): Promise<IFileStatWithMetadata>;
/**
* Will move working copies matching the provided resources and corresponding children
* to the target resources using the associated file service for those resources.
......@@ -226,12 +237,22 @@ export class WorkingCopyFileService extends Disposable implements IWorkingCopyFi
//#region File operations
async create(resource: URI, contents?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata> {
create(resource: URI, contents?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata> {
return this.doCreateFileOrFolder(resource, true, contents, options);
}
createFolder(resource: URI, options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata> {
return this.doCreateFileOrFolder(resource, false);
}
async doCreateFileOrFolder(resource: URI, isFile: boolean, contents?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata> {
// validate create operation before starting
const validateCreate = await this.fileService.canCreateFile(resource, options);
if (validateCreate instanceof Error) {
throw validateCreate;
if (isFile) {
const validateCreate = await this.fileService.canCreateFile(resource, options);
if (validateCreate instanceof Error) {
throw validateCreate;
}
}
// file operation participant
......@@ -244,7 +265,11 @@ export class WorkingCopyFileService extends Disposable implements IWorkingCopyFi
// now actually create on disk
let stat: IFileStatWithMetadata;
try {
stat = await this.fileService.createFile(resource, contents, { overwrite: options?.overwrite });
if (isFile) {
stat = await this.fileService.createFile(resource, contents, { overwrite: options?.overwrite });
} else {
stat = await this.fileService.createFolder(resource);
}
} catch (error) {
// error event
......
......@@ -189,6 +189,49 @@ suite('WorkingCopyFileService', () => {
model1.dispose();
});
test('createFolder', async function () {
let eventCounter = 0;
let correlationId: number | undefined = undefined;
const resource = toResource.call(this, '/path/folder');
const participant = accessor.workingCopyFileService.addFileOperationParticipant({
participate: async (files, operation) => {
assert.equal(files.length, 1);
const file = files[0];
assert.equal(file.target.toString(), resource.toString());
assert.equal(operation, FileOperation.CREATE);
eventCounter++;
}
});
const listener1 = accessor.workingCopyFileService.onWillRunWorkingCopyFileOperation(e => {
assert.equal(e.files.length, 1);
const file = e.files[0];
assert.equal(file.target.toString(), resource.toString());
assert.equal(e.operation, FileOperation.CREATE);
correlationId = e.correlationId;
eventCounter++;
});
const listener2 = accessor.workingCopyFileService.onDidRunWorkingCopyFileOperation(e => {
assert.equal(e.files.length, 1);
const file = e.files[0];
assert.equal(file.target.toString(), resource.toString());
assert.equal(e.operation, FileOperation.CREATE);
assert.equal(e.correlationId, correlationId);
eventCounter++;
});
await accessor.workingCopyFileService.createFolder(resource);
assert.equal(eventCounter, 3);
participant.dispose();
listener1.dispose();
listener2.dispose();
});
async function testEventsMoveOrCopy(files: { source: URI, target: URI }[], move?: boolean): Promise<number> {
let eventCounter = 0;
......
......@@ -839,7 +839,7 @@ export class TestFileService implements IFileService {
move(_source: URI, _target: URI, _overwrite?: boolean): Promise<IFileStatWithMetadata> { return Promise.resolve(null!); }
copy(_source: URI, _target: URI, _overwrite?: boolean): Promise<IFileStatWithMetadata> { return Promise.resolve(null!); }
createFile(_resource: URI, _content?: VSBuffer | VSBufferReadable, _options?: ICreateFileOptions): Promise<IFileStatWithMetadata> { return Promise.resolve(null!); }
createFolder(_resource: URI): Promise<IFileStatWithMetadata> { throw new Error('not implemented'); }
createFolder(_resource: URI): Promise<IFileStatWithMetadata> { return Promise.resolve(null!); }
onDidChangeFileSystemProviderRegistrations = Event.None;
......
......@@ -201,6 +201,7 @@ export class TestWorkingCopyFileService implements IWorkingCopyFileService {
getDirty(resource: URI): IWorkingCopy[] { return []; }
create(resource: URI, contents?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: { overwrite?: boolean | undefined; } | undefined): Promise<IFileStatWithMetadata> { throw new Error('Method not implemented.'); }
createFolder(resource: URI): Promise<IFileStatWithMetadata> { throw new Error('Method not implemented.'); }
move(files: { source: URI; target: URI; }[], options?: { overwrite?: boolean }): Promise<IFileStatWithMetadata[]> { throw new Error('Method not implemented.'); }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册