Skip to content

Commit

Permalink
feat: support WorkspaceEditMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bk1012 committed Oct 25, 2024
1 parent d5d4dae commit 016052d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ export class MainThreadWorkspace extends WithEventBus implements IMainThreadWork
);
}

async $tryApplyWorkspaceEdit(dto: model.WorkspaceEditDto): Promise<boolean> {
async $tryApplyWorkspaceEdit(
dto: model.WorkspaceEditDto,
metadata?: model.WorkspaceEditMetadataDto,
): Promise<boolean> {
try {
const edits = ResourceEdit.convert(dto);
const { success } = (await this.bulkEditService.apply(edits)) as IBulkEditResult & { success: boolean };
const { success } = (await this.bulkEditService.apply(edits, {
respectAutoSaveConfig: metadata?.isRefactoring,
})) as IBulkEditResult & { success: boolean };
return success;
} catch (e) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions packages/extension/src/common/vscode/model.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface CustomCodeAction {
isPreferred?: boolean;
}

export interface WorkspaceEditMetadataDto {
isRefactoring?: boolean;
}

/**
* A position in the editor. This interface is suitable for serialization.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/common/vscode/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type vscode from 'vscode';

export interface IMainThreadWorkspace extends IDisposable {
$saveAll(): Promise<boolean>;
$tryApplyWorkspaceEdit(dto: model.WorkspaceEditDto): Promise<boolean>;
$tryApplyWorkspaceEdit(dto: model.WorkspaceEditDto, metadata?: model.WorkspaceEditMetadataDto): Promise<boolean>;
$updateWorkspaceFolders(
start: number,
deleteCount?: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createWorkspaceApiFactory(
console.warn(false, '[Deprecated warning]: Use the corresponding function on the `tasks` namespace instead');
return extHostTasks.registerTaskProvider(type, provider, extension);
},
applyEdit: (edit) => extHostWorkspace.applyEdit(edit),
applyEdit: (edit, metadata?: vscode.WorkspaceEditMetadata) => extHostWorkspace.applyEdit(edit, metadata),
get textDocuments() {
return extHostDocument.getAllDocument();
},
Expand Down Expand Up @@ -405,9 +405,9 @@ export class ExtHostWorkspace implements IExtHostWorkspace {
return this.folders.some((folder) => folder.uri.toString() === uri.toString());
}

applyEdit(edit: WorkspaceEdit): Promise<boolean> {
applyEdit(edit: WorkspaceEdit, metadata?: vscode.WorkspaceEditMetadata): Promise<boolean> {
const dto = TypeConverts.WorkspaceEdit.from(edit, this.extHostDoc);
return this.proxy.$tryApplyWorkspaceEdit(dto);
return this.proxy.$tryApplyWorkspaceEdit(dto, metadata);
}

saveAll(): Promise<boolean> {
Expand Down
21 changes: 16 additions & 5 deletions packages/types/sumi-worker/worker.editor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,24 +543,35 @@ declare module 'sumi-worker' {
Nine = 9,
}

/**
* Additional data about a workspace edit.
*/
export interface WorkspaceEditMetadata {
/**
* Signal to the editor that this edit is a refactoring.
*/
isRefactoring?: boolean;
}

export namespace workspace {
/**
* Make changes to one or many resources or create, delete, and rename resources as defined by the given
* [workspace edit](#WorkspaceEdit).
* {@link WorkspaceEdit workspace edit}.
*
* All changes of a workspace edit are applied in the same order in which they have been added. If
* multiple textual inserts are made at the same position, these strings appear in the resulting text
* in the order the 'inserts' were made. Invalid sequences like 'delete file a' -> 'insert text in file a'
* cause failure of the operation.
* in the order the 'inserts' were made, unless that are interleaved with resource edits. Invalid sequences
* like 'delete file a' -> 'insert text in file a' cause failure of the operation.
*
* When applying a workspace edit that consists only of text edits an 'all-or-nothing'-strategy is used.
* A workspace edit with resource creations or deletions aborts the operation, e.g. consecutive edits will
* not be attempted, when a single edit fails.
*
* @param edit A workspace edit.
* @return A thenable that resolves when the edit could be applied.
* @param metadata Optional {@link WorkspaceEditMetadata metadata} for the edit.
* @returns A thenable that resolves when the edit could be applied.
*/
export function applyEdit(edit: WorkspaceEdit): Thenable<boolean>;
export function applyEdit(edit: WorkspaceEdit, metadata?: WorkspaceEditMetadata): Thenable<boolean>;
}

export namespace window {
Expand Down
10 changes: 10 additions & 0 deletions packages/types/vscode/typings/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,16 @@ declare module 'vscode' {
iconPath?: Uri | { light: Uri; dark: Uri } | ThemeIcon;
}

/**
* Additional data about a workspace edit.
*/
export interface WorkspaceEditMetadata {
/**
* Signal to the editor that this edit is a refactoring.
*/
isRefactoring?: boolean;
}

/**
* A workspace edit is a collection of textual and files changes for
* multiple resources and documents.
Expand Down
33 changes: 17 additions & 16 deletions packages/types/vscode/typings/vscode.workspace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ declare module 'vscode' {
export function getWorkspaceFolder(uri: Uri): WorkspaceFolder | undefined;

/**
* Make changes to one or many resources or create, delete, and rename resources as defined by the given
* [workspace edit](#WorkspaceEdit).
*
* All changes of a workspace edit are applied in the same order in which they have been added. If
* multiple textual inserts are made at the same position, these strings appear in the resulting text
* in the order the 'inserts' were made. Invalid sequences like 'delete file a' -> 'insert text in file a'
* cause failure of the operation.
*
* When applying a workspace edit that consists only of text edits an 'all-or-nothing'-strategy is used.
* A workspace edit with resource creations or deletions aborts the operation, e.g. consecutive edits will
* not be attempted, when a single edit fails.
*
* @param edit A workspace edit.
* @return A thenable that resolves when the edit could be applied.
*/
export function applyEdit(edit: WorkspaceEdit): Thenable<boolean>;
* Make changes to one or many resources or create, delete, and rename resources as defined by the given
* {@link WorkspaceEdit workspace edit}.
*
* All changes of a workspace edit are applied in the same order in which they have been added. If
* multiple textual inserts are made at the same position, these strings appear in the resulting text
* in the order the 'inserts' were made, unless that are interleaved with resource edits. Invalid sequences
* like 'delete file a' -> 'insert text in file a' cause failure of the operation.
*
* When applying a workspace edit that consists only of text edits an 'all-or-nothing'-strategy is used.
* A workspace edit with resource creations or deletions aborts the operation, e.g. consecutive edits will
* not be attempted, when a single edit fails.
*
* @param edit A workspace edit.
* @param metadata Optional {@link WorkspaceEditMetadata metadata} for the edit.
* @returns A thenable that resolves when the edit could be applied.
*/
export function applyEdit(edit: WorkspaceEdit, metadata?: WorkspaceEditMetadata): Thenable<boolean>;

/**
* Get a workspace configuration object.
Expand Down

0 comments on commit 016052d

Please sign in to comment.