Skip to content

Commit 8a25588

Browse files
committed
Expand custom editor delegate docs
For microsoft#93963 For microsoft#77131
1 parent cade368 commit 8a25588

1 file changed

Lines changed: 64 additions & 17 deletions

File tree

src/vs/vscode.d.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6866,41 +6866,70 @@ declare module 'vscode' {
68666866
}
68676867

68686868
/**
6869-
* Defines the editing capability of a custom editor. This allows the custom editor to hook into standard
6870-
* editor events such as `undo` or `save`.
6869+
* Implements the editing functionality of a custom editor.
6870+
*
6871+
* This delegate is how custom editors hook into standard VS Code operations such as save and undo. The delegate
6872+
* is also how custom editors notify VS Code that an edit has taken place.
68716873
*
68726874
* @param EditType Type of edits used for the documents this delegate handles.
68736875
*/
68746876
interface CustomEditorEditingDelegate<EditType = unknown> {
68756877
/**
6876-
* Save the resource.
6878+
* Save the resource for a custom editor.
6879+
*
6880+
* This method is invoked by VS Code when the user saves a custom editor. This can happen when the user
6881+
* triggers save while the custom editor is active, by commands such as `save all`, or by auto save if enabled.
6882+
*
6883+
* To implement `save`, the delegate must persist the custom editor. This usually means writing the
6884+
* file data for the custom document to disk. After `save` completes, any associated editor instances will
6885+
* no longer be marked as dirty.
68776886
*
68786887
* @param document Document to save.
68796888
* @param cancellation Token that signals the save is no longer required (for example, if another save was triggered).
68806889
*
6881-
* @return Thenable signaling that the save has completed.
6890+
* @return Thenable signaling that saving has completed.
68826891
*/
68836892
save(document: CustomDocument<EditType>, cancellation: CancellationToken): Thenable<void>;
68846893

68856894
/**
6886-
* Save the existing resource at a new path.
6895+
* Save the resource for a custom editor to a different location.
6896+
*
6897+
* This method is invoked by VS Code when the user triggers `save as` on a custom editor.
6898+
*
6899+
* To implement `saveAs`, the delegate must persist the custom editor to `targetResource`. The
6900+
* existing editor will remain open after `saveAs` completes.
68876901
*
68886902
* @param document Document to save.
68896903
* @param targetResource Location to save to.
68906904
*
6891-
* @return Thenable signaling that the save has completed.
6905+
* @return Thenable signaling that saving has completed.
68926906
*/
68936907
saveAs(document: CustomDocument<EditType>, targetResource: Uri): Thenable<void>;
68946908

68956909
/**
6896-
* Event triggered by extensions to signal to VS Code that an edit has occurred.
6910+
* Signal that an edit has occurred inside a custom editor.
6911+
*
6912+
* This event must be fired by your extension whenever an edit happens in a custom editor. An edit can be
6913+
* anything from changing some text, to cropping an image, to reordering a list. Your extension is free to
6914+
* define what an edit is and what data is stored on each edit.
6915+
*
6916+
* VS Code uses edits to determine if a custom editor is dirty or not. VS Code also passes the edit objects back
6917+
* to your extension when triggers undo, redo, or revert (using the `undoEdits`, `applyEdits`, and `revert`
6918+
* methods of `CustomEditorEditingDelegate`)
68976919
*/
68986920
readonly onDidEdit: Event<CustomDocumentEditEvent<EditType>>;
68996921

69006922
/**
6901-
* Apply a set of edits.
6923+
* Apply a list of edits to a custom editor.
69026924
*
6903-
* Note that is not invoked when `onDidEdit` is called because `onDidEdit` implies also updating the view to reflect the edit.
6925+
* This method is invoked by VS Code when the user triggers `redo` in a custom editor.
6926+
*
6927+
* To implement `applyEdits`, the delegate must make sure all editor instances (webviews) for `document`
6928+
* are updated to render the document's new state (that is, every webview must be updated to show the document
6929+
* after applying `edits` to it).
6930+
*
6931+
* Note that `applyEdits` not invoked when `onDidEdit` is called because `onDidEdit` implies that your extension
6932+
* has also updated its editor instances to reflect the edit that just occurred.
69046933
*
69056934
* @param document Document to apply edits to.
69066935
* @param edit Array of edits. Sorted from oldest to most recent.
@@ -6910,9 +6939,13 @@ declare module 'vscode' {
69106939
applyEdits(document: CustomDocument<EditType>, edits: ReadonlyArray<EditType>): Thenable<void>;
69116940

69126941
/**
6913-
* Undo a set of edits.
6942+
* Undo a list of edits to a custom editor.
69146943
*
6915-
* This is triggered when a user undoes an edit.
6944+
* This method is invoked by VS Code when the user triggers `undo` in a custom editor.
6945+
*
6946+
* To implement `undoEdits`, the delegate must make sure all editor instances (webviews) for `document`
6947+
* are updated to render the document's new state (that is, every webview must be updated to show the document
6948+
* after undoing `edits` from it).
69166949
*
69176950
* @param document Document to undo edits from.
69186951
* @param edit Array of edits. Sorted from most recent to oldest.
@@ -6922,10 +6955,20 @@ declare module 'vscode' {
69226955
undoEdits(document: CustomDocument<EditType>, edits: ReadonlyArray<EditType>): Thenable<void>;
69236956

69246957
/**
6925-
* Revert the file to its last saved state.
6958+
* Revert a custom editor to its last saved state.
6959+
*
6960+
* This method is invoked by VS Code when the user triggers `File: Revert File` in a custom editor. (Note that
6961+
* this is only used using VS Code's `File: Revert File` command and not on a `git revert` of the file).
6962+
*
6963+
* To implement `revert`, the delegate must make sure all editor instances (webviews) for `document`
6964+
* are displaying the document in the same state is saved in. This usually means reloading the file from the
6965+
* workspace.
6966+
*
6967+
* During `revert`, your extension should also clear any backups for the custom editor. Backups are only needed
6968+
* when there is a difference between an editor's state in VS Code and its save state on disk.
69266969
*
69276970
* @param document Document to revert.
6928-
* @param edits Added or applied edits.
6971+
* @param edits Added or removed edits to get back to the saved state.
69296972
*
69306973
* @return Thenable signaling that the change has completed.
69316974
*/
@@ -6967,8 +7010,13 @@ declare module 'vscode' {
69677010
/**
69687011
* Object that describes the edit.
69697012
*
7013+
* Edit objects are controlled entirely by your extension. Your extension should store whatever information it
7014+
* needs to on the edit to understand what type of edit was made, how to render that edit, and how to save that
7015+
* edit to disk.
7016+
*
69707017
* Edit objects are passed back to your extension in `CustomEditorEditingDelegate.undoEdits`,
6971-
* `CustomEditorEditingDelegate.applyEdits`, and `CustomEditorEditingDelegate.revert`.
7018+
* `CustomEditorEditingDelegate.applyEdits`, and `CustomEditorEditingDelegate.revert`. They can also be accessed
7019+
* using [`CustomDocument.appliedEdits`](#CustomDocument.appliedEdits) and [`CustomDocument.savedEdits`](#CustomDocument.savedEdits).
69727020
*/
69737021
readonly edit: EditType;
69747022

@@ -6998,9 +7046,8 @@ declare module 'vscode' {
69987046
/**
69997047
* Represents a custom document used by a [`CustomEditorProvider`](#CustomEditorProvider).
70007048
*
7001-
* All custom documents must subclass `CustomDocument`. Custom documents are only used within a given
7002-
* `CustomEditorProvider`. The lifecycle of a `CustomDocument` is managed by VS Code. When no more references
7003-
* remain to a `CustomDocument`, it is disposed of.
7049+
* Custom documents are only used within a given `CustomEditorProvider`. The lifecycle of a `CustomDocument` is
7050+
* managed by VS Code. When no more references remain to a `CustomDocument`, it is disposed of.
70047051
*
70057052
* @param EditType Type of edits used in this document.
70067053
*/

0 commit comments

Comments
 (0)